【问题标题】:How to send message to a chatroom using xmpppy?如何使用 xmpppy 向聊天室发送消息?
【发布时间】:2012-06-26 08:59:21
【问题描述】:

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

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

另外,我发送此消息时未发送出席信息。

【问题讨论】:

    标签: python xmpp chatroom xmpppy


    【解决方案1】:

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

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

    那么你的消息应该有效。

    【讨论】:

    • 谢谢。对于遇到相同问题的任何人,首先定义命名空间NS_MUC = 'http://jabber.org/protocol/muc',然后定义存在presence = xmpp.Presence(to=ROOM_JID),最后,像presence.setTag('x', namespace=NS_MUC).setTagData('password', 'PASSWORD') 一样设置x 标签。现在,您可以向您的客户发送出席信息client.send(presence)
    【解决方案2】:

    一些较旧的 XMPP 服务器需要初始状态通知。在cl.send 之前试试这个:

    cl.SendInitPresence(requestRoster=0)
    

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

    【讨论】:

      【解决方案3】:

      这是向聊天室发送消息的基本实现。您需要将您的出席信息发送到群组,并将消息类型设置为“群聊”。

      用 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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-24
        • 1970-01-01
        • 2014-10-31
        • 1970-01-01
        • 2019-11-06
        • 1970-01-01
        • 1970-01-01
        • 2021-04-14
        相关资源
        最近更新 更多