如何使用 python-xmpp 设置 Jabber 状态?

发布于 2024-08-25 16:14:54 字数 397 浏览 5 评论 0原文

如何通过 python 设置 GChat 或 jabber 状态?现在我得到了这个:

import xmpp     

new_status = "blah blah blah"
login = 'email' 
pwd   = 'password'

cnx = xmpp.Client('gmail.com')
cnx.connect( server=('talk.google.com',5223) )

cnx.auth(login, pwd, 'botty')

pres = xmpp.Presence()
pres.setStatus(new_status)
cnx.send(pres)

它执行了,但状态没有更新。我知道我已成功连接到服务器,因为我可以向其他人发送聊天消息。我在这里做错了什么?

How do I set a GChat or jabber status via python? Right now I've got this:

import xmpp     

new_status = "blah blah blah"
login = 'email' 
pwd   = 'password'

cnx = xmpp.Client('gmail.com')
cnx.connect( server=('talk.google.com',5223) )

cnx.auth(login, pwd, 'botty')

pres = xmpp.Presence()
pres.setStatus(new_status)
cnx.send(pres)

It executes, but the status is not updated. I know I'm connecting to the server successfully, as I can send chat messages to others. What am I doing wrong here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我们只是彼此的过ke 2024-09-01 16:14:54

您可能想查看以下文件:

http://steliosm.net/projects/picaxejabber /picaxe_xmpp.py

编辑:
我不好,第一个答案断章取义,我误读了你的代码。

cnx.sendInitPresence()

我猜你还没有发送你的初始状态......

You might want to take a look at this file:

http://steliosm.net/projects/picaxejabber/picaxe_xmpp.py

Edit:
My bad, first answer was out of context, I've misread your code.

cnx.sendInitPresence()

You haven't send your initial state I guess ...

假装不在乎 2024-09-01 16:14:54

注意:想向那些想要执行此线程中提到的操作的人提及这一点。如果不熟悉 XMPP 协议和节,可能会错过一些设置正确状态所需的信息。 xmpppy 模块文档似乎没有明确阐明设置存在的步骤。

设置初始存在是最简单的,如本线程之前的帖子所示。它设置可用用户的默认状态(类型)。不确定默认的“状态”和“显示”状态是什么,也假设为空白或“可用”。

但是,当通过定义新的状态对象来发送状态来设置新状态时,如果您使用默认值(无参数)初始化对象,如此处的原始帖子所示,则要发送的状态对象(或节)是不完整的,因为它不完整t 定义适当的存在“类型”。因此,根据您使用的 XMPP 服务器,它可能会也可能不会正确地接受设置。

初始化新的在线状态对象的正确方法如下:

offPres = xmpp.Presence(typ='unavailable',show='unavailable',status='unavailable')

或者简单地如下,如果在“可用/在线”和“不可用/离线”之间切换而不登录和关闭 XMPP IM 会话,我们不这样做关心状态/显示状态显示的内容(即您看到的与状态相关的标签,例如“离线 - 离开”与“离线”)。

offPres = xmpp.Presence(typ='unavailable')

对于诸如“请勿打扰”、“离开”、“出去吃午饭”等自定义状态,这会变得有点棘手。我自己对 XMPP 并不熟悉,但假设您会指定状态并显示状态值(例如 DND、离开),同时将状态类型设置为“可用”或“不可用”,具体取决于您是否希望以这种方式显示或不是。

并且根据 xmpppy 文档,您只能在对象初始化时指定存在类型,之后无法更改它。但是您可以在初始化后更改状态并显示存在对象的状态。这是按照原始帖子中所示完成的。对于显示状态,有一个匹配的 setShow 方法,就像 setStatus 一样。

发送出席信息与原始帖子中的相同。

NOTE: wanted to mention this to those who want to do what's mentioned in this thread. If one is not familiar with XMPP protocol and stanzas, one might miss some needed info to set proper status. The xmpppy module docs don't seem to explicitly clarify the steps to set presence.

Setting initial presence is easiest, as shown in previous posts in this thread. It sets a default presence (type) of user being available. Not sure what the default "status" and "show" states are, assume blank or "available" also.

However, when setting new status by defining a new presence object to send status, if you initialize the object with defaults (no arguments) as in the original post here, the presence object (or stanza) to be sent is incomplete because it doesn't define a proper presence "type". So depending on the XMPP server you are working with it may or may not take the setting correctly.

The proper way to initialize new presence status object would be like this:

offPres = xmpp.Presence(typ='unavailable',show='unavailable',status='unavailable')

or simply just the following, if toggling between "available/online" and "unavailable/offline" w/o logging on and off XMPP IM session, where we don't care what is shown for status/show state (i.e. the label you see associated with status, like "Offline - away" vs just "offline").

offPres = xmpp.Presence(typ='unavailable')

For custom status like DND, Away, Out to Lunch, etc., that gets a little trickier. I'm not really familiar with XMPP myself but assume you would specify the status and show state value as such (e.g. DND, Away) while setting presence type as "available" or "unavailable" depending on whether you want to appear that way or not.

And based on the xmpppy docs, you can only specify presence type at initialization of the object, can't change it afterwards. But you can change the status and show states for the presence object after initialization. That is done as shown in original post here. For show state, there is a matching setShow method just like setStatus.

Sending the presence is same as in original post.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文