如何使用python xmpppy在gtalk中创建MUC



谁能帮我修复这段代码,我真的需要它,但不知道下一步该怎么做。我需要创建一个群聊并向受邀人员发送消息,现在它 example2@gmail.com,但它没有......
有错误吗?

#!/usr/bin/python
import sys,os,xmpp,time                                          
jid = 'example1@gmail.com'
psw = 'psw'
jid=xmpp.protocol.JID(jid)
cl=xmpp.Client(jid.getDomain(),debug=[])
cl.connect()
cl.auth(jid.getNode(),psw)
node = jid.getNode()
domain = 'talk.google.com'
room = node + '@' + domain
nroom = room + '/' + 'Maria'
mes = xmpp.Presence(to=nroom) 
cl.sendInitPresence()
cl.send(mes)

NS_MUCUSER = 'http://jabber.org/protocol/muc#user'
invite = xmpp.simplexml.Node('invite')
invite.setAttr('to', 'example2@gmail.com')
invite.setTagData('reason', 'I really need it!') 
mess = xmpp.Message(to=room)
mess.setTag('x', namespace=NS_MUCUSER).addChild(node=invite)
cl.send(mess)

msg = xmpp.protocol.Message(body="Hello there!")
msg.setTo(room)
msg.setType('groupchat')
cl.send(msg)
time.sleep(1)   # some older servers will not send the message if you disconnect immediately after sending
cl.disconnect()
print "Done"

根据规格 - http://xmpp.org/extensions/xep-0045.html#createroom - 发送加入不存在的房间的请求应该创建该房间(或 MUC)

创建和配置此类聊天室的工作流如下:

The user sends presence to <room@service/nick> and signal his or her support
for the Multi-User Chat protocol by including extended presence information 
in an empty <x/> child element qualified by the 'http://jabber.org/protocol/muc' 
namespace (note the lack of an '#owner' or '#user' fragment).
If this user is allowed to create a room and the room does not yet exist, the
service MUST create the room according to some default configuration, assign the
requesting user as the initial room owner, and add the owner to the room but not 
allow anyone else to enter the room (effectively "locking" the room). The initial
presence stanza received by the owner from the room MUST include extended 
presence information indicating the user's status as an owner and acknowledging
that the room has been created (via status code 201) and is awaiting 
configuration.

因此,根据文档,这样的东西应该可以工作。

jid=xmpp.protocol.JID('example@gmail.com')
cl=xmpp.Client(jid.getDomain(),debug=[])
jid = xmpp.protocol.JID('example@gmail.com')
client = xmpp.Client(jid.getDomain(), debug=[])
client.connect()
client.auth(jid.getNode(), 'my secret password')
client.send(xmpp.Presence(to='room@talk.google.com/ANick')

我发现我的错误。问题是我没有等待足够的时间来获得服务器的答案,并且在服务器能够创建聊天室之前我邀请了人们。现在我等到我从服务器得到答案,然后发送邀请消息。

最新更新