How do I set a Jabber status with python-xmpp?

风流意气都作罢 提交于 2019-12-10 15:54:40

问题


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?


回答1:


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 ...




回答2:


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.



来源:https://stackoverflow.com/questions/2473487/how-do-i-set-a-jabber-status-with-python-xmpp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!