2

I've successfully sent message to individual user. How can I send message to a room? I'm trying the following code:

cl.send(xmpp.Message('99999_myroom@chat.hipchat.com', 'test message', typ='groupchat'))

Also, I'm sending this message without sending presence.

wasimbhalli
  • 5,122
  • 8
  • 45
  • 63

3 Answers3

2

Here is the basic implementation for sending a message to a room chat. You need to send your presence to the group, and also set the message type to 'groupchat'.

Tested with Openfire server

#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys,time,xmpp

def sendMessageToGroup(server, user, password, room, message):

    jid = xmpp.protocol.JID(user)
    user = jid.getNode()
    client = xmpp.Client(server)
    connection = client.connect(secure=False)
    if not connection:
        print 'connection failed'
        sys.exit(1)

    auth = client.auth(user, password)
    if not auth:
        print 'authentication failed'
        sys.exit(1)

    # Join a room by sending your presence to the room
    client.send(xmpp.Presence(to="%s/%s" % (room, user)))

    msgObj = xmpp.protocol.Message(room, message)
    #Set message type to 'groupchat' for conference messages
    msgObj.setType('groupchat')

    client.send(msgObj)

    # some older servers will not send the message if you disconnect immediately after sending
    time.sleep(1)   

    client.disconnect()
Denis.Kipchakbaev
  • 970
  • 15
  • 24
  • Be aware to use the correct _user_ and _room_ IDs. User-JID might look like `username@domain.tld` and room-JID might look like `room@conference.domain.tld`. – stackprotector May 31 '22 at 12:50
0

Some older XMPP servers require an initial presence notification. Try this before your cl.send:

cl.SendInitPresence(requestRoster=0)

See also http://xmpppy.sourceforge.net/examples/xsend.py

Ilmo Euro
  • 4,925
  • 1
  • 27
  • 29
0

To send a message to a room, you must join the room first. From XEP-0045, section 7.2.2:

<presence to='99999_myroom@chat.hipchat.com/my_nickname'>
  <x xmlns='http://jabber.org/protocol/muc'/>
</presence>

Then your message should work.

Joe Hildebrand
  • 10,354
  • 2
  • 38
  • 48
  • Thanks. For anyone having the same problem first define the namespace `NS_MUC = 'http://jabber.org/protocol/muc'`, then define presence, `presence = xmpp.Presence(to=ROOM_JID)`, and finally, set the x tag like this `presence.setTag('x', namespace=NS_MUC).setTagData('password', 'PASSWORD')`. Now, you can send presence with your client `client.send(presence)` – wasimbhalli Jun 28 '12 at 10:35