From b09d3595fe07a7b071ff439c34371e50d66f9c76 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro <=> Date: Tue, 25 Jan 2022 09:50:43 -0800 Subject: [PATCH] error out on slack error response --- bot.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/bot.py b/bot.py index 719d528..ad0994d 100644 --- a/bot.py +++ b/bot.py @@ -3,6 +3,7 @@ import datetime import os import pathlib import time +import sys import requests from selenium import webdriver @@ -63,11 +64,18 @@ def filter_word(word, conditions): def slack_request(method, **kwargs): kwargs['headers'] = {'Authorization': 'Bearer ' + os.environ['SLACK_TOKEN']} - return requests.post( + r = requests.post( f'https://slack.com/api/{method}', **kwargs ) + resp = r.json() + if not resp['ok']: + print('Slack API responded with error:') + print(resp) + sys.exit(1) + return resp + class Solver: def __init__(self): @@ -171,18 +179,12 @@ if __name__ == '__main__': lines = lines + [''.join(row) for row in result['history']] channel = os.environ['SLACK_CHANNEL'] - r = slack_request('chat.postMessage', json={'channel': channel, 'text': '\n'.join(lines)}) - msg = r.json() - - if msg['ok']: - r = slack_request( - 'files.upload', - params={'thread_ts': msg['ts'], 'channels': channel}, - files={'file': (filename, open(filename, 'rb').read())}, - ) - else: - print('Slack API responded with error:') - print(msg) + msg = slack_request('chat.postMessage', json={'channel': channel, 'text': '\n'.join(lines)}) + slack_request( + 'files.upload', + params={'thread_ts': msg['ts'], 'channels': channel}, + files={'file': (filename, open(filename, 'rb').read())}, + ) else: print('Failed to find the word.')