Ali Irfan
Ali Irfan

Reputation: 11

How do I automatically accept subscriptions using python and XMPPPY?

I'm making a chat bot for a game I play and the bot itself is working fine, now what I need to do is make the bot auto-add any requests it gets.

I'm not sure what to do about this, doing some googling I found someone state that def add_friend(self, user): self._send(xmpp.Presence(to=user, typ='subscribed')) self._send(xmpp.Presence(to=user, typ='subscribe')) return True

would do the trick, but I have no idea how to implement it in my code.

Here's my base of my code for the messaging system using Python:

import xmpp

conn = xmpp.Client("domain here..")
if not conn.connect(server=("<server here>", 5223)):
    print "connect failed."
    exit()

if not conn.auth("USER ID", "PASS HERE", "xiff"):
    print "auth failed."
    exit()

roster = None

def message_handler(conn, msg):
    user = roster.getName(str(msg.getFrom()))
    text = msg.getBody()

    print "[%s] %s" % (user, text)

    reply = msg.buildReply("[ECHO] %s" % (text))
    reply.setType("chat")
    conn.send(reply)

conn.RegisterHandler("message", message_handler)
conn.sendInitPresence(requestRoster=1)
roster = conn.getRoster()

while conn.isConnected():
    try:
        conn.Process(10)
    except KeyboardInterrupt:
        break

When a user tries to add the bot, this is what shows up:

<iq to="[email protected]" from="[email protected]/xiff" id="2861886931" type="error">
  <query xmlns="jabber:iq:riotgames:roster">
  <item jid="[email protected]" name="Top Mid Lane NA" subscription="pending_in" />
  </query>
  <error code="501" type="cancel">
  <feature-not-implemented xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" />
  <text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas">The feature requested is not implemented by the recipient or server and therefore cannot be processed.</text>
  </error>
  </iq>
DEBUG: socket       got   <presence to='[email protected]/xiff' from='[email protected]' name='Top Mid Lane NA' type='subscribe'>
  <priority>0</priority>
  </presence>

Any ideas, I've been stuck on this for a few days!

Upvotes: 1

Views: 589

Answers (1)

den-gts
den-gts

Reputation: 21

You should implement presence handler for accepting subscription:

def presence(conn, event):
    if event.getType() == 'subscribe':
        conn.send(xmpp.Presence(to=event.getFrom(), typ='subscribed'))

conn.RegisterHandler('presence', presence)

Upvotes: 2

Related Questions