take screenshot of process and post to slack

This commit is contained in:
Joseph Montanaro 2022-01-24 18:32:34 -08:00
parent 48027afd0e
commit 5e65d818b5
3 changed files with 37 additions and 4 deletions

View File

@ -10,7 +10,9 @@ steps:
- name: run
image: joyzoursky/python-chromedriver:3.9-selenium
environment:
SLACK_HOOK_URL:
from_secret: slack_hook_url
SLACK_TOKEN:
from_secret: slack_token
SLACK_CHANNEL:
from_secret: SLACK_CHANNEL
commands:
- bash run.sh

1
.gitignore vendored
View File

@ -1 +1,2 @@
**/__pycache__*
*.png

34
bot.py
View File

@ -60,6 +60,15 @@ def filter_word(word, conditions):
return True
def slack_request(method, **kwargs):
kwargs['headers'] = {'Authorization': 'Bearer ' + os.environ['SLACK_TOKEN']}
return requests.post(
f'https://slack.com/api/{method}',
**kwargs
)
class Solver:
def __init__(self):
print('Launching web browser')
@ -119,6 +128,12 @@ class Solver:
history.append(row_hist)
return history
def capture_board(self):
board = self.driver.execute_script("return document.querySelector('game-app').shadowRoot.querySelector('#board')")
filename = f'wordlebot_{datetime.datetime.now().strftime("%Y-%m-%d")}.png'
board.screenshot(filename)
return filename
def solve(self):
print('Attempting to solve')
for i in range(6):
@ -128,6 +143,9 @@ class Solver:
conditions = self.read_row(i)
if len(conditions['correct']) == 5:
time.sleep(3) # wait for the message that comes up when you win
self.body.click() # dismiss it
time.sleep(0.5) # wait for overlay to clear
return {
'word': self.words[0],
'history': self.get_history(i + 1),
@ -141,7 +159,8 @@ class Solver:
if __name__ == '__main__':
solver = Solver()
try:
result = solver.solve()
if result := solver.solve():
filename = solver.capture_board()
finally:
solver.driver.close()
@ -150,6 +169,17 @@ if __name__ == '__main__':
wordle_num = (datetime.date.today() - datetime.date(2022, 1, 17)).days + 212
lines = [f"Wordle {wordle_num}: {result['iterations']}/6"]
lines = lines + [''.join(row) for row in result['history']]
requests.post(os.environ['SLACK_HOOK_URL'], json={'text': '\n'.join(lines)})
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('Failed to find the word.')