开发者

How to create MUC and send messages to existing MUC using Python and XMPP

开发者 https://www.devze.com 2023-01-12 03:42 出处:网络
I was wondering if anyone here can provide some code samples on the following scenarios. I\'m particularly interested in using xmpppy to do this as I\'m already using the library for my app, but other

I was wondering if anyone here can provide some code samples on the following scenarios. I'm particularly interested in using xmpppy to do this as I'm already using the library for my app, but other libraries ok too. It is unfortunate that the xmpppy project website doesn't have any samples on this. Browsing the expert/advanced API docs, I couldn't figure out how to do it, or is multi-user chat (MUC) not supported with xmpppy?

  • create a MUC by inviting specific users (say 2 or 3)

  • send message to an existing MUC (assuming you know it's MUC JID handle or nickname)

  • look up existing MUCs on the XMPP server, getting the JID or nickname, etc. If this is done by getting roster, we want to only look for MUCs, ignoring users.

I found sort of an answer here, but then I'd probably have to learn new library API calls and figure out how to do my above mentioned scenarios as this sample doesn't cover all of them:

pyxmpp: quick tutorial for creating a muc client?

I'm really looking to do a load generator that pumps messages to MUCs and creating large MUCs with many participants. 开发者_如何转开发I've already got the part in place for pumping messages to user recipients.


While I dont know about specific MUC interface there, xmpppy supports custom messages, so it supports whole XMPP.

To join chat, you need to send presence stranza, conn.send(xmpp.Presence(to='{0}/{1}'.format(room, nick)))

To send a message to chat:

    stranza = "<message to='{0}' type='groupchat'><body>{1}</body></message>".format(room, text)
    conn.send(stranza)

As for creating new chat or looking it up in roster, I don't have ready code at hand, but it is easy to write in the same fashion, just look up needed stranzas in XEPs:

http://xmpp.org/extensions/xep-0045.html#createroom

http://xmpp.org/extensions/xep-0045.html#disco-rooms

http://xmpp.org/extensions/xep-0045.html#invite


Xmpppy does support using MUC services, but the support is very basic and some extra code will be needed to manage multiple rooms.

To "join" a MUC room you need to broadcast your presence to the JID.

conn.send(xmpp.Presence(to="%s/%s" % (room, nickname)))

Then to send messages, you send them using the 'groupchat' message type to the room JID.

msg = xmpp.protocol.Message(body=text)
msg.setTo(room)
msg.setType('groupchat')
conn.send(msg)

As for your question regarding finding MUC rooms, that'll be done via service discovery.

0

精彩评论

暂无评论...
验证码 换一张
取 消