1 from turbot.blocks import section_block, text_block
2 from turbot.channel import channel_url
3 from boto3.dynamodb.conditions import Key
5 def find_puzzle_for_url(turb, hunt_id, url):
6 """Given a hunt_id and URL, return the puzzle with that URL
8 Returns None if no puzzle with the given URL exists in the database,
9 otherwise a dictionary with all fields from the puzzle's row in
13 response = turb.table.query(
14 IndexName='url_index',
15 KeyConditionExpression=(
16 Key('hunt_id').eq(hunt_id) &
21 if response['Count'] == 0:
24 return response['Items'][0]
26 def puzzle_block(puzzle):
27 """Generate Slack blocks for a puzzle
29 The puzzle argument should be a dictionary as returned from the
30 database. The return value can be used in a Slack command
31 expecting blocks to provide all the details of a puzzle, (its
32 state, solution, links to channel and sheet, etc.).
36 status = puzzle['status']
37 solution = puzzle['solution']
38 channel_id = puzzle['channel_id']
39 url = puzzle.get('url', None)
40 sheet_url = puzzle.get('sheet_url', None)
41 state = puzzle.get('state', None)
45 if status == 'solved':
46 status_emoji = ":ballot_box_with_check:"
48 status_emoji = ":white_square:"
51 solution_str = "*`" + '`, `'.join(solution) + "`*"
55 links.append("<{}|Puzzle>".format(url))
57 links.append("<{}|Sheet>".format(sheet_url))
61 state_str = "\n{}".format(state)
63 puzzle_text = "{}{} <{}|{}> ({}){}".format(
64 status_emoji, solution_str,
65 channel_url(channel_id), name,
66 ', '.join(links), state_str
69 return section_block(text_block(puzzle_text))