error out on slack error response
This commit is contained in:
		
							
								
								
									
										28
									
								
								bot.py
									
									
									
									
									
								
							
							
						
						
									
										28
									
								
								bot.py
									
									
									
									
									
								
							@@ -3,6 +3,7 @@ import datetime
 | 
				
			|||||||
import os
 | 
					import os
 | 
				
			||||||
import pathlib
 | 
					import pathlib
 | 
				
			||||||
import time
 | 
					import time
 | 
				
			||||||
 | 
					import sys
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import requests
 | 
					import requests
 | 
				
			||||||
from selenium import webdriver
 | 
					from selenium import webdriver
 | 
				
			||||||
@@ -63,11 +64,18 @@ def filter_word(word, conditions):
 | 
				
			|||||||
def slack_request(method, **kwargs):
 | 
					def slack_request(method, **kwargs):
 | 
				
			||||||
    kwargs['headers'] = {'Authorization': 'Bearer ' + os.environ['SLACK_TOKEN']}
 | 
					    kwargs['headers'] = {'Authorization': 'Bearer ' + os.environ['SLACK_TOKEN']}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return requests.post(
 | 
					    r = requests.post(
 | 
				
			||||||
        f'https://slack.com/api/{method}',
 | 
					        f'https://slack.com/api/{method}',
 | 
				
			||||||
        **kwargs
 | 
					        **kwargs
 | 
				
			||||||
    )
 | 
					    )
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    resp = r.json()
 | 
				
			||||||
 | 
					    if not resp['ok']:
 | 
				
			||||||
 | 
					        print('Slack API responded with error:')
 | 
				
			||||||
 | 
					        print(resp)
 | 
				
			||||||
 | 
					        sys.exit(1)
 | 
				
			||||||
 | 
					    return resp
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class Solver:
 | 
					class Solver:
 | 
				
			||||||
    def __init__(self):
 | 
					    def __init__(self):
 | 
				
			||||||
@@ -171,18 +179,12 @@ if __name__ == '__main__':
 | 
				
			|||||||
        lines = lines + [''.join(row) for row in result['history']]
 | 
					        lines = lines + [''.join(row) for row in result['history']]
 | 
				
			||||||
        
 | 
					        
 | 
				
			||||||
        channel = os.environ['SLACK_CHANNEL']
 | 
					        channel = os.environ['SLACK_CHANNEL']
 | 
				
			||||||
        r = slack_request('chat.postMessage', json={'channel': channel, 'text': '\n'.join(lines)})
 | 
					        msg = slack_request('chat.postMessage', json={'channel': channel, 'text': '\n'.join(lines)})
 | 
				
			||||||
        msg = r.json()
 | 
					        slack_request(
 | 
				
			||||||
 | 
					            'files.upload',
 | 
				
			||||||
        if msg['ok']:
 | 
					            params={'thread_ts': msg['ts'], 'channels': channel},
 | 
				
			||||||
            r = slack_request(
 | 
					            files={'file': (filename, open(filename, 'rb').read())},
 | 
				
			||||||
                '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:
 | 
					    else:
 | 
				
			||||||
        print('Failed to find the word.')
 | 
					        print('Failed to find the word.')
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user