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

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.

7
задан Community 23 May 2017 в 12:07
поделиться

2 ответа

Хотя я не знаю о конкретном интерфейсе MUC, xmpppy поддерживает пользовательские сообщения, так что он поддерживает весь XMPP.

Чтобы присоединиться к чату, вам нужно отправить presence stranza, conn.send(xmpp.Presence(to='{0}/{1}'.format(room, nick))))

Чтобы отправить сообщение в чат:

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

Что касается создания нового чата или поиска его в реестре, у меня нет готового кода под рукой, но его легко написать аналогичным образом, просто найдите нужные строфы в 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

5
ответ дан 7 December 2019 в 01:14
поделиться

Xmpppy действительно поддерживает использование сервисов MUC, но поддержка очень проста, и для управления несколькими комнатами потребуется некоторый дополнительный код.

Чтобы «присоединиться» к комнате MUC, вам необходимо передать свое присутствие JID.

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

Затем, чтобы отправлять сообщения, вы отправляете их с помощью типа сообщения «групповой чат» на JID комнаты.

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

Что касается вашего вопроса о поиске комнат MUC, это будет сделано через обнаружение сервисов.

5
ответ дан 7 December 2019 в 01:14
поделиться
Другие вопросы по тегам:

Похожие вопросы: