如何使用xmpppy将消息发送到聊天室?

时间:2012-06-26 08:59:22

标签: python xmpp chatroom xmpppy

我已成功向个人用户发送消息。如何向房间发送消息?我正在尝试以下代码:

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

另外,我发送此邮件时没有发送。

3 个答案:

答案 0 :(得分:0)

某些较旧的XMPP服务器需要初始状态通知。在cl.send

之前尝试此操作
cl.SendInitPresence(requestRoster=0)

另见http://xmpppy.sourceforge.net/examples/xsend.py

答案 1 :(得分:0)

要向房间发送消息,您必须先加入房间。来自XEP-0045, section 7.2.2

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

然后你的信息应该有效。

答案 2 :(得分:0)

这是将消息发送到聊天室的基本实现。您需要将您的状态发送给该网上论坛,并将消息类型设置为“ groupchat”。

通过Openfire服务器测试

#!/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()