Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Managing subscriptions #200

Open
ckcollab opened this Issue Oct 13, 2012 · 4 comments

Comments

Projects
None yet
2 participants

Hey gang, I love all the documentation and work you guys have put into this project! It's been a joy to use so far.

However, I'm stuck on managing subscriptions, and the exact documentation I was so giddy to read doesn't seem to exist!
http://sleekxmpp.com/getting_started/presence.html

I'm trying to make a little chat bot that only accepts subscriptions/only subscribes to people in my database. When that person verifies their account to the chat bot, it will remove them from the roster/unsubscribe.

Am I on the right track here, does that sound like how I'm supposed to do it?!

Right now I have this

class LolKarmaBot(sleekxmpp.ClientXMPP):
    def __init__(self, jid, password):
        sleekxmpp.ClientXMPP.__init__(self, jid, password)

        self.auto_authorize = False
        self.auto_subscribe = False

        self.add_event_handler("session_start", self.start)
        self.add_event_handler("message", self.message)
        self.add_event_handler("roster_update", self.roster_update)
    def roster_update(self, roster):
        #roster = roster.get_items() -- this didn't work, even though
        # https://github.com/fritzy/SleekXMPP/blob/master/sleekxmpp/stanza/roster.py
        # those docs seem to say it should -- I'm new to Python!  Not sure exactly what's going on
        # and dir isn't outputting any 'get_items' method.  I assume I have to convert this roster iq stanza
        # into a roster class?  I'm confused!
        print roster
        print dir(roster)

I can't manipulate that Roster object at all, I'm a little confused. Thanks!

Collaborator

legastero commented Oct 13, 2012

Duh, in roster_update I call self.roster and that seems to be more like it!

Also, I update self.roster.auto_authorize, i think that's right (instead of self.auto_authorize)

Collaborator

legastero commented Oct 13, 2012

So, looking at your requirements, here's how I'd do it:

class LolKarmaBot(ClientXMPP):

    def __init__(self, jid, password):
        super(ClientXMPP, self).__init__(jid, password)

        # Set these to None to disable automatic roster management
        self.auto_authorize = None
        self.auto_subscribe = None

        self.add_event_handler('roster_subscription_request', self._sub_request)
        #...

    #...

    def _sub_request(self, presence):
        if self.backend_says_its_good(presence['from']):
            self.client_roster[presence['from']].authorize()
            # If you want mutual subscriptions
            self.client_roster[presence['from']].subscribe()
        else:
            # Reject unwanted subscriptions
            self.client_roster[presence['from']].unauthorize()   

Thanks! I can't wait to give that a shot, but it appears my chat bot isn't working any more... I don't think I even changed anything but it was constantly trying to reconnect for a few hours while I was out. I get this now:

RECV: <stream:error><invalid-namespace xmlns="urn:ietf:params:xml:ns:xmpp-streams" /></stream:error>

Googling around I found 5.8.1.1. Stream Errors Are Unrecoverable in the XMPP standard, so I must be sending something wrong.

I realized I changed this part of the code to match yours:

class LolKarmaBot(ClientXMPP):
    def __init__(self, jid, password):
        super(ClientXMPP, self).__init__(jid, password)

But that doesn't work, this works though:

lass LolKarmaBot(ClientXMPP):
    def __init__(self, jid, password):
        ClientXMPP.__init__(self, jid, password)

Just so I understand Python a bit better, what's happening -- if you don't mind explaining?!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment