基于XMPP的Gtalk机器人

  • View
    7.921

  • Download
    0

Embed Size (px)

DESCRIPTION

 

Text of 基于XMPP的Gtalk机器人

  • XMPP Gtalk [email_address] 2010.9.4
  • 1, SVN 2, BlackBerry 3, ;)
  • Gtalk ^_&
  • HOW? Python + XMPP
  • Python XMPP ~~
  • XMPP Jabber eXtensible Messaging Presence Protocol Gtalk XMPP Google Wave Orz http://knb.im/67y
  • XMPP Client-Server Server-Server $%^*#$
  • Bot Z.Q Python ~~~
  • JID XMPP client server XMPP JID node@domain/resource zhoo.xuan@ngosoft.com/xxxx -_-
  • xmpp 1, Message
  • xmpp 2, Presence
  • xmpp 3, / Iq XMPP
  • xmpp stanza xmpp xml stanza from: xmpp JID to: xmpp JID id: type: stanza
  • XMPP XML Fuck~ python xmpppy
  • import xmpp jid = xmpp.JID('bot@gmail.com/python') message = xmpp.Message('say something') presence = xmpp.Presence('I'm at NGOsoft.com ') iq = xmpp.Iq() ~
  • bot jid = 'bot@gmail.com' pwd = 'password' jid_instance = xmpp.JID(jid) cl = xmpp.client(jid_instance.getDomain(), debug[] ) cl.connect() cl.auth(jid_instance.getNode(), pwd)
  • bot status = xmpp.Presence(status=' ') cl.sendInitPresence() cl.send(status) while 1: cl.Process(1) send
  • bot handler cl.RegisterHandler('message', message_handler) cl.RegisterHandler('presence', presence_handler) cl.RegisterHandler('iq', iq_handler) bot handler
  • bot presence cl.send(xmpp.Presence(to='xx@xx.xx', typ=' subscribe ')) cl.send(xmpp.Presence(to='xx@xx.xx', typ=' subscribed '))
  • bot bot ===> def message_handler(self, conn, mess): who = mess.getFrom() content = mess.getBody() content cmd arguments if cmd in commands: cmd_xxx(arguments) #commands #cmd_xxx
  • client ==> (message)==>message_handler==>cmd_xxx
  • def cmd_xxx(self, args): # cl.send(xmpp.Message(who, reply))
  • cmd_xxx() xxx commands[xxx]() commands={'xxx':func, ...} commands{ }
  • register_commands() __init__() class cmd_xxx commands def register_commands(self): self.commands={} for name, value in inspect.getmembers(self): if inspect.ismethod(value) and getattr(value, '_is_method', Falsa): self.commands[name] = value
  • cmd_ "_is_command" @ cmd_ def botcommands(*args): func = args[0] setattr(func, '_is_command', True) return func @botcommands def cmd_xxx():
  • def bootcommands(*args): func = args[0] setattr(func, '_is_command', True) return func commands system
  • bot log handler
  • bot client-->connect-->auth-->register_handler verify_request-->presence_handler-->log message-->message_handler-->cmd_xx-->log cmd @botcommands() def cmd_xx(): Perfect ;)
  • 1,log 2, bot daemon bot screen -_&!
  • bot ~~ cmd_ cmd_help cmd_server xmpp cmd_work server server cmd_eat cmd_joy Orz
  • & http://xmpppy.sourceforge.net http://code.google.com/p/pygtalkrobot/ http://www.ibm.com/developerworks/cn/xml/tutorials/x-realtimeXMPPtut/index.html
  • END Thanks ;) @roamin9 [email_address]