Jabber bot-如何获取联系人的可用性? [英] Jabber bot - how to get the availability of contacts?
问题描述
我需要使用python设置一个jabber机器人,该机器人将根据多个联系人的在线/离线可用性发送消息.
I need to set up a jabber bot, using python, that will send messages based on the online/offline availability of several contacts.
我一直在研究pyxmpp和xmpppy,但找不到任何方法(至少没有直接方法)来检查给定联系人的状态.
I've been looking into pyxmpp and xmpppy, but couldn't find any way (at least nothing straightforward) to check the status of a given contact.
任何有关如何实现这一目标的指针?
Any pointers on how to achieve this?
理想情况下,我想要类似bot.status_of("contact1@gmail.com")
返回"online"
Ideally I would like something like e.g. bot.status_of("contact1@gmail.com")
returning "online"
推荐答案
我认为不可能以您想要的方式实现,因为联系人的存在(包含有关其可用性的信息)是由异步接收的机器人.
I don't think it is possible in the way you want it because the presence of contacts (which contains the information about their availability) is received asynchronously by the bot.
您将必须编写一个状态处理程序函数并将其注册到连接中.只要从联系人那里收到在线状态,就会调用此函数.呼叫参数将告诉您联系人是否在线.您可以根据需要将消息发送给联系人.
You will have to write a presence handler function and registered it with the connection. This function will get called whenever a presence is received from a contact. The parameter of the call will tell you if the contact is online or not. Depending upon it you can send the message to the contact.
使用xmpppy
,您可以执行以下操作:
Using xmpppy
you do it something like this:
def connect(jid, password, res, server, proxy, use_srv):
conn = xmpp.Client(jid.getDomain())
if not conn.connect(server=server, proxy=proxy, use_srv=use_srv):
log( 'unable to connect to server.')
return None
if not conn.auth(jid.getNode(), password, res):
log( 'unable to authorize with server.')
return None
conn.RegisterHandler( 'presence', callback_presence)
return conn
conn = connect(...)
def callback_presence(sess, pres):
if pres.getStatus() == "online":
msg = xmpp.Message(pres.getFrom(), "Hi!")
conn.send(msg)
PS:我尚未测试代码,但是应该与此非常相似.
PS: I have not tested the code but it should be something very similar to this.
这篇关于Jabber bot-如何获取联系人的可用性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!