문제

I'm trying to access my google talk contacts' custom status messages with xmpppy. I'm made it this far:

import xmpp
import sys

userID   = 'myname@gmail.com' 
password = 'mypassword'
ressource = 'Script'

jid  = xmpp.protocol.JID(userID)
jabber  = xmpp.Client(jid.getDomain(), debug=[])

connection = jabber.connect(('talk.google.com',5222))
auth = jabber.auth(jid.getNode(), password, ressource)

jabber.sendInitPresence(requestRoster=1)
myroster = jabber.getRoster()

the roster object myroster now contains my contacts, but the custom status message is not included.

myroster.getStatus('oneofmyfriends@gmail.com')

returns None

looking at the 'raw roster', I can see that the resources dictionary is empty

u'oneofmyfriends@googlemail.com': {'ask': None, 'resources': {}, 'name': u'Some Name', 'groups': [], 'subscription': u'both'}

The weird thing is that I have gotten this to work today, but I the code might have been slightly different, but I can't figure out what exactly I did differently...

Any help would be greatly appreciated!

Cheers,

Martin

도움이 되었습니까?

해결책

It's a timing issue. Add a handler with:

jabber.RegisterHandler('presence', myPresenceHandler)

def myPresenceHandler(self, con, event):
  fromjid = event.getFrom().getStripped()
  status = myroster.getStatus(fromjid)

BEFORE connecting. Then make sure to call jabber.Process() in a loop. The issue is that with your code, you'll sometimes receive presence stanzas before you look at the roster object, and sometimes after.

다른 팁

Here's one thing I've found, which was not clear to me when I first started working with xmpp. Friending is two-way.

Using presence stanzas
(a) You can "subscribe" to your friend, and your friend can return "subscribed".
(b) Your friend can "subscribe" to you, and you can return "subscribed".

Your friend will be in your roster if either (a) or (b) has happened.
You will be in your friends roster if either (a) or (b) has happened.

However...
You will not see their status unless you "subscribe" to your friend - (a) must happen
They will not see your status unless they "subscribe" to you - (b) must happen.

Most XMPP clients (pidgin, trillian, etc) will automatically make you send "subscribe" back to your friend when you send them "subscribed" (after they've sent you "subscribe"). XMPPPY does not do this out of the box. You must code it to do this.

This could explain why you weren't seeing status. Or if this doesn't cover your situation, it might be informative to someone else.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top