views:

40

answers:

2

Hi, i'm using xmpp in python, and i can send messages but how i can receive?

A: 

I must register a handler and process:

def messageCB(sess,mess):
    print 'MESSAGE'*100
    nick=mess.getFrom().getResource()
    text=mess.getBody()
    #print mess,nick
    print text

client.RegisterHandler('message',messageCB)

if 1:
while 1:
    client.Process(1)
diegueus9
A: 

Good post. I notice this code snippet is also in the logger example in xmpppy sourceforge website.

I wonder if it is possible to reply to incoming messages. The code above only receives and the nickname resource ID does not indicate who the sender is (in terms of JID format, user@server) unless xmpppy can translate that appropriately. So how might one take the received message nd "echo" it back to the sender? Or is that not easily possible with the xmpppy library and need to find a different XMPP library?

David
of course you can:This is a snippet, put it in the func messageCBclient = xmpp.Client('yourdomain.com')client.connect(server=('talk.google.com',5223))client.RegisterHandler('message',messageCB)client.auth(USERNAME, PASSWD, 'talk.google.com')client.sendInitPresence()message = xmpp.Message(to, msg)message.setAttr('type', 'chat')client.send(message)
diegueus9
thanks, but the problem is how do you define the "to" field for the message when you don't know who the sender will be? The handler simply listens for messages to user from anyone. I tested the code and the resource ID (or nick) returned is more like a session ID as it isn't anything like "user" or "user@server". I haven't tested but my assumption is that you can't simply pass the nick value received as the "to" value of the outgoing message you will send, or can you?
David
if you see mess.getFrom() you get something like [email protected]/nick
diegueus9
thanks. that's what I needed to know.
David
Say, I notice that this code snippet, when used with conn.sendInitPresence() and kept running for a while, only stays online for some time before the server thinks the user is offline even though the code/app is still running. I have to quit the app and restart it to reregister presence. Any ideas what presence handlers or what code to add to "keep alive" the user's presence while the tool is running?FYI, I'm logged in against Openfire XMPP server if that matters.
David