Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/330.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用xmppy向聊天室发送消息?_Python_Xmpp_Chatroom_Xmpppy - Fatal编程技术网

Python 如何使用xmppy向聊天室发送消息?

Python 如何使用xmppy向聊天室发送消息?,python,xmpp,chatroom,xmpppy,Python,Xmpp,Chatroom,Xmpppy,我已成功地向单个用户发送消息。如何向房间发送信息?我正在尝试以下代码: cl.send(xmpp.Message('99999_myroom@chat.hipchat.com', 'test message', typ='groupchat')) 另外,我在不发送状态信息的情况下发送此消息。一些较旧的XMPP服务器需要初始状态通知。在发送cl.send之前,请尝试以下操作: cl.SendInitPresence(requestRoster=0) 另请参见要向房间发送消息,您必须先加入房间。

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

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

另外,我在不发送状态信息的情况下发送此消息。

一些较旧的XMPP服务器需要初始状态通知。在发送
cl.send
之前,请尝试以下操作:

cl.SendInitPresence(requestRoster=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()

谢谢对于有相同问题的任何人,首先定义名称空间
NS\u MUC=http://jabber.org/protocol/muc“
,然后定义状态,
presence=xmpp.presence(to=ROOM\u JID)
,最后,像这样设置x标记
presence.setTag('x',namespace=NS\u MUC).setTagData('password','password')
。现在,您可以通过客户端发送状态信息
client.send(presence)
#!/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()