玩蛇网提供最新Python编程技术信息以及Python资源下载!

Python Google talk聊天机器人源码

python 培训

Python玩蛇网本文是关于依据xmpp封装的Jabber聊天机器人类。 并提供了Python Google talk聊天机器人源码供大家参考。这个python gtalk机器人是可以通过继承,并能重载部分函数来自定义功能。

Python gatalk机器人相关文章推荐:Python WebSocket服务端编程代码完成gtalk机器人

python gtalk机器人源码部分如下:

# coding: utf-8

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))

# www.iplaypython.com

    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

玩蛇网原创,转载请注明文章出处和来源网址:http://www.iplaypython.com/code/other/o2618.html



微信公众号搜索"玩蛇网Python之家"加关注,每日最新的Python资讯、图文视频教程可以让你一手全掌握。强烈推荐关注!

微信扫描下图可直接关注

玩蛇网Python新手QQ群,欢迎加入: ① 240764603 玩蛇网Python新手群
文章发布日期:2016-01-06 10:01 玩蛇网 www.iplaypython.com

评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
相关文章推荐
别人正在看
特别推荐
去顶部去底部