from errbot import BotPlugin, botcmd, arg_botcmd, webhook


class SparkWebhook(BotPlugin):
    """
    Webhook Receiver for Spark Backend
    """

    @webhook('/incoming')
    def incoming(self, request):
        self.log.debug('Got Request: {}'.format(request))

        handlers = {'messages': self.process_message,
                    'memberships': self.process_membership}
        handlers[request['resource']](request)
        return "OK"

    def process_message(self, request):
        if request['data']['personId'] == self._bot.bot_identifier:
            #don't bother processing messages from the bot.
            return
        message = self._bot.decrypt_message(request['data']['id'])
        self.log.debug('To is group: {}'.format(message.is_group))
        self._bot.callback_message(message)
        return

    def process_membership(self, request):
        if request['event'] == 'deleted':
            if request['data']['personId'] == self._bot.bot_identifier:
                self.log.debug('Removed from room: {}'.format(request['data']['roomId']))
                self._bot._rooms.discard(self._bot.query_room(request['data']['roomId']))
            else:
                #TODO Not the bot, so don't really care until all the RoomOccupant stuff is implemented
                pass
        elif request['event'] == 'created':
            if request['data']['personId'] == self._bot.bot_identifier:
                self.log.debug('Added to room: {}'.format(request['data']['roomId']))
                self._bot._rooms.add(self._bot.query_room(request['data']['roomId']))
            else:
                #TODO see above
                pass
        return

@@ -370,16 +370,63 @@
 mbership
+,%0A                    'room': self.process_room
 %7D%0A      
@@ -439,17 +439,25 @@
 lers
-%5B
+.get(
 request
-%5B
+.get(
 'res
@@ -466,10 +466,32 @@
 rce'
-%5D%5D
+), self.process_unknown)
 (req
@@ -1736,24 +1736,389 @@
     pass%0A        return%0A
+%0A    def process_room(self, request):%0A        #Probably the least interesting of the events. This indicates a title change, or moderation change.%0A        #Just refresh the bots rooms%0A        self._bot._rooms = self._bot.rooms()%0A        return%0A%0A    def process_unknown(self, request):%0A        self.log.debug('Got unknown request: %7B%7D'.format(request))%0A        return%0A

Add event handler for rooms, and unknown handler