gpt4 book ai didi

python - Jabber bot - 如何获得联系人的可用性?

转载 作者:太空宇宙 更新时间:2023-11-03 11:35:45 26 4
gpt4 key购买 nike

我需要使用 python 设置一个 jabber bot,它将根据多个联系人的在线/离线可用性发送消息。

我一直在研究 pyxmpp 和 xmpppy,但找不到任何方法(至少没有直接的方法)来检查给定联系人的状态。

关于如何实现这一目标的任何指示?

理想情况下,我想要像这样的东西bot.status_of("contact1@gmail.com") 返回 "online"

最佳答案

我认为这不可能以您想要的方式实现,因为机器人会异步接收联系人的存在(其中包含有关其可用性的信息)。

您将必须编写一个存在处理程序函数并将其注册到连接中。每当收到联系人的在线状态时,都会调用此函数。调用的参数将告诉您联系人是否在线。根据它,您可以将消息发送给联系人。

使用 xmpppy 你可以这样做:

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:我没有测试过代码,但它应该与此非常相似。

关于python - Jabber bot - 如何获得联系人的可用性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3890726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com