开发者

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

开发者 https://www.devze.com 2022-12-23 16:01 出处:网络
How do 开发者_如何学运维I set a GChat or jabber status via python? Right now I\'ve got this: import xmpp

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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消