pyxmpp: quick tutorial for creating a muc client?

删除回忆录丶 提交于 2019-12-03 14:15:50
Richy

Hey I stumbled over your question a few times, while trying the same thing. Here is my answer:

Using http://pyxmpp.jajcus.net/svn/pyxmpp/trunk/examples/echobot.py as a quickstart, all you have to do is import the MUC-Stuff

from pyxmpp.jabber.muc import MucRoomState, MucRoomManager

And once your Client is connected, you can connect to your room:

def session_started(self): 
    """Handle session started event. May be overriden in derived classes. 
    This one requests the user's roster and sends the initial presence.""" 
    print u'SESSION STARTED'
    self.request_roster() 
    p=Presence() 
    self.stream.send(p) 
    print u'ConnectToParty'
    self.connectToMUC()

def connectToMUC(self):
    self.roomManager = MucRoomManager(self.stream);
    self.roomHandler = MucRoomHandler()
    self.roomState = self.roomManager.join(
        room=JID('room@conference.server.domain'),
        nick='PartyBot',
        handler=self.roomHandler, 
        history_maxchars=0,
        password = None)
    self.roomManager.set_handlers()

To send a message, all you have to do is call self.roomState.send_message("Sending this Message")

To do stuff, inherit from MucRoomHandler and react on events. Notice the "set_handlers()" to roomManager though, it's is important, otherwise callbacks will not be called..

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!