views:

48

answers:

2

I need to set up a jabber bot, using python, that will send messages based on the online/offline availability of several contacts.

I've been looking into pyxmpp and xmpppy, but couldn't find any way (at least nothing straightforward) so check the status of a given contact.

Any pointers on how to achieve this?

Ideally I would like something like e.g. bot.status_of("[email protected]") returning "online"

Thanks!

+2  A: 

I don't think it is possible in the way you want it because the presence of contacts (which contains the information about their availability) is received asynchronously by the bot.

You will have to write a presence handler function and registered it with the connection. This function will get called whenever a presence is received from a contact. The parameter of the call will tell you if the contact is online or not. Depending upon it you can send the message to the contact.

Using xmpppy you do it something like this:

def connect(jid, password, res, server, proxy, use_srv):
    conn = xmpp.Client(jid.getDomain())

    if not conn.connect(server=server, proxy=proxy, use_srv=use_srv):
        log( 'unable to connect to server.')
        return None

    if not conn.auth(jid.getNode(), password, res):
        log( 'unable to authorize with server.')
        return None

    conn.RegisterHandler( 'presence', callback_presence)
    return conn

conn = connect(...)

def callback_presence(sess, pres):
    if pres.getStatus() == "online":
        msg = xmpp.Message(pres.getFrom(), "Hi!")
        conn.send(msg)

PS: I have not tested the code but it should be something very similar to this.

abhin4v
A: 

What you want is done via a <presence type="probe"/>. This is done on behalf of the client, and SHOULD not be done by them (as per the RFC for XMPP IM). Since this is a bot, you could implement the presence probe, and receive the current presence of a given entity. Remember to send the probe to the bare JID (sans resource), because the server responds on behalf of clients for presence probes. This means your workflow will look like:

<presence/> // I'm online!                                        BOT
  <presence from="[email protected]/balcony"/>                   RESPONSE
  <presence from="[email protected]/hallway"/> // and so on...   RESPONSE
<presence type="probe" to="[email protected]"/>               BOT
  <presence from="[email protected]/hallway">                  RESPONSE
     <status>Huzzah!</status>
     <priority>3</priority>
  </presence>

Take a look at that portion of the RFC for more in depth information on how your call flow should behave.

Tim