Пример #1
0
 def _process_init_users(self, data):
     usrs = data['users']
     for user in usrs:
         user_id = user['id']
         user_name = user['name']
         user_real_name = user['profile']['real_name_normalized']
         UserManager.add_user(user_id, user_name, user_real_name, None)
Пример #2
0
    def get_messages(self, room, count=200):
        url_base = "https://slack.com/api/"

        if room.room_type == 'channel':
            url_action = 'channels.history?'
        elif room.room_type == 'group':
            url_action = 'groups.history?'
        else:
            raise NameError('Cannot retrieve messages of unknown type')

        url_token = "token=" + str(self._token)
        url_channel = "&channel=" + str(room.hash_id) + "&count=" + str(count)
        url = url_base + url_action + url_token + url_channel

        r = self._request(self.GET, url)
        if r.status_code != 200:
            raise Exception("Could not connect to Slack HTTP API")
        self.logger.info("REQUEST RESPONSE: channels.history" + str(r.json()))

        msgs = []
        room_id = room.hash_id
        for obj in r.json()['messages']:
            self.logger.info("abcdef" + str(obj))

            if 'user' in obj:
                user_id = obj['user']
            elif 'bot_id' in obj:
                user_id = obj['bot_id']
                if UserManager.find(user_id) is None:
                    UserManager.add_user(user_id, obj['username'])

            else:
                raise UserNotFoundError("Unknown type of user")

            msg = Message(room_id, obj['text'], user_id, obj['ts'])
            msgs.append(msg)

        return msgs #  r.json()