• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

python gtalk机器人

python 水墨上仙 2357次浏览

python gtalk机器人


Python语言: python gtalk机器人
# coding: utf-8
#
# 这是根据xmpp封装的Jabber聊天机器人类, 可以通过继承,重载部分函数来自定义功能.
# Jabber ID(JID): 比如gamcat@gmail.com

import xmpp

class Bot:
    """ Jabber Bot Base Class """
    JID = ''
    PASSWORD = ''

    client = None   

    def __init__ (self, jid, password):
        self.JID = xmpp.JID(jid)
        self.PASSWORD = password

        self.login()

    def login (self):         
        self.client = xmpp.Client(self.JID.getDomain(), debug=[])
        if self.client.connect() == '':
            raise 'JabberBot not connected.'
        if self.client.auth(self.JID.getNode(), self.PASSWORD) == None:
            raise 'JabberBot authentication failed.'
        
        self.client.RegisterHandler('message', self.message_callback)
        self.client.RegisterHandler('presence', self.presence_callback)
        self.client.sendInitPresence()

    def message_callback (self, client, message):
        """ 默认消息回调(可通过继承自定义) """

    def presence_callback (self, client, message):
        """ 默认事件回调,包括下面几个(可通过继承自定义) """
        type = message.getType()
        who = message.getFrom().getStripped()

        if type == 'subscribe':
            self.subscribe(who)
        elif type == 'unsubscribe':
            self.unsubscribe(who)
        elif type == 'subscribed':
            self.subscribed(who)
        elif type == 'unsubscribed':
            self.unsubscribed(who)
        elif type == 'available' or type == None:
            self.available(message)
        elif type == 'unavailable':
            self.unavailable(who)

    def subscribe (self, jid):
        """ 加好友 """
        self.client.send(xmpp.Presence(to=jid, typ='subscribed'))
        self.client.send(xmpp.Presence(to=jid, typ='subscribe'))

    def unsubscribe (self, jid):
        """ 取消好友 """
        self.client.send(xmpp.Presence(to=jid, typ='unsubscribe'))
        self.client.send(xmpp.Presence(to=jid, typ='unsubscribed'))

    def subscribed (self, jid):
        """ 已加 """

    def unsubscribed (self, jid):
        """ 已退 """
        
    def available (self, message):
        """ 上线 """

    def unavailable (self, jid):
        """ 下线 """

    def send (self, jid, message):
        """ 发消息给某人"""
        self.client.send(xmpp.protocol.Message(jid, message))

    def step (self):
        """ 用在循环中 """
        try:
            self.client.Process(1)
        except KeyboardInterrupt:   # Ctrl+C停止
            return False
        return True


#===========================
# 测试
#===========================
class Bot(Bot):
    def message_callback (self, cl, msg):
        fromid = msg.getFrom().getStripped()
        cont = msg.getBody()
        self.send2admin(msg)

    def send2admin (self, message):
        self.send('admin@gmail.com', message)

if __name__ == '__main__':
    gb = Bot ('yourbot@gmail.com', '******')
    gb.send2admin ('Bot Started')

    # 开始运行
    while (gb.step()): pass


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明python gtalk机器人
喜欢 (0)
加载中……