def post(self): logger.info('New message from bot received.') json = request.get_json() logger.debug(f'Message payload: {json}') logger.debug(f'Message headers: {request.headers}') try: bot_id = json['channel'] bearer_token = request.headers['Authorization'].split( "Bearer ", 1)[1] conversation = get_conversation_checked(bot_id=bot_id, used_api_key=bearer_token) if not conversation: logger.warning('') return auth_denied() except Exception: logger.exception('Exception during auth.') return auth_denied() logger.info('Request authorized, handling message.') handle(conversation, json) return jsonify({ 'ok': True, 'channel': bot_id, 'ts': generate_timestamp() })
def send(self, payload: dict): data = json.dumps(payload) logger.debug(f'Sending payload: {payload}') headers = self.__prepare_header(generate_timestamp(), data) logger.debug(f'With headers: {headers}') response = requests.post(self.url, data=data, headers=headers) logger.debug(f'Response: {response}')
def post(self, api_key, bot_id): logger.info('New message from web hook bot received.') logger.debug(f'API key: {api_key}, bot id: {bot_id}') json = request.get_json() conversation = get_conversation_checked(bot_id=bot_id, used_api_key=api_key) if not conversation: logger.warning(f'No conversation found for bot {bot_id}.') return auth_denied() logger.info('Request authorized, handling message.') handle(conversation, json) return jsonify({ 'ok': True, 'channel': bot_id, 'ts': generate_timestamp() })
def convert_message(bot: TwoWayBot, roman_payload: dict, conversation: dict) -> dict: bot_id = roman_payload['botId'] logger.info(f'Processing message from bot {bot_id}') timestamp = generate_timestamp() logger.debug(f'Converting message: {roman_payload}') return { 'token': bot.bot_token, 'team_id': bot_id, # TODO determine what is in our sense team id, lets assume this is only one team 'api_app_id': bot_id, 'event': convert_event(bot_id, timestamp, roman_payload), 'type': 'event_callback', 'event_id': roman_payload['messageId'], 'event_time': timestamp, 'authed_users': [x['id'] for x in conversation['members']] }
def convert_conversation(bot: TwoWayBot, roman_payload: dict, conversation: dict) -> dict: bot_id = roman_payload['botId'] logger.info(f'Converting conversation for bot {bot_id}') logger.debug(f'Conversation: {conversation}') user = conversation.get('creator') if conversation.get( 'creator') else conversation['members'][0]['id'] timestamp = generate_timestamp() return { 'token': bot.bot_token, 'api_app_id': bot_id, 'team_id': bot_id, # TODO determine what is in our sense team id, lets assume this is only one team 'event': convert_event(user, timestamp, conversation), 'type': 'event_callback', 'event_id': f'{bot_id}:{conversation["id"]}', 'event_time': timestamp, 'authed_users': [x['id'] for x in conversation['members']], }