error out on slack error response

This commit is contained in:
Joseph Montanaro 2022-01-25 09:50:43 -08:00
parent 13f0994134
commit b09d3595fe

20
bot.py
View File

@ -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(
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('Slack API responded with error:')
print(msg)
else:
print('Failed to find the word.')