def on_post(self, req, resp):
"""
Accept slack's message from interactive buttons
"""
try:
form_post = falcon.uri.parse_query_string(req.context['body'])
payload = ujson.loads(form_post['payload'])
if not self.valid_token(payload['token']):
logger.error('Invalid token sent in the request.')
raise falcon.HTTPUnauthorized('Access denied',
'Not a valid auth token')
try:
msg_id = int(payload['callback_id'])
except KeyError as e:
logger.error('callback_id not found in the json payload.')
raise falcon.HTTPBadRequest('Bad Request', 'Callback id not found')
except ValueError as e:
logger.error('Callback ID not an integer: %s', payload['callback_id'])
raise falcon.HTTPBadRequest('Bad Request', 'Callback id must be int')
data = {'msg_id': msg_id,
'source': payload['user']['name'],
'content': payload['actions'][0]['name']}
endpoint = self.config['iris']['hook']['slack']
try:
result = self.iclient.post(endpoint, data)
except MaxRetryError as e:
logger.error(e.reason)
return
if result.status == 400:
raise falcon.HTTPBadRequest('Bad Request', '')
elif result.status is not 200:
raise falcon.HTTPInternalServerError('Internal Server Error', 'Unknown response from the api')
else:
content = process_api_response(result.data)
self.return_slack_message(resp, content)
return
except Exception:
logger.exception('Unable to read payload from slack. Our post body: %s', req.context['body'])
raise falcon.HTTPBadRequest('Bad Request', 'Unable to read the payload from slack')
评论列表
文章目录