commands["/rot"] = rot
+def find_hunt_for_channel(turb, channel_id, channel_name):
+ """Given a channel ID/name find the id/name of the hunt for this channel
+
+ This works whether the original channel is a primary hunt channel,
+ or if it is one of the channels of a puzzle belonging to the hunt.
+
+ Returns a tuple of (hunt_name, hunt_id) or (None, None)."""
+
+ hunts_table = turb.db.Table("hunts")
+ response = hunts_table.get_item(Key={'channel_id': channel_id})
+
+ if 'Item' in response:
+ item = response['Item']
+ return (item['hunt_id'], item['name'])
+
+ # So we're not a hunt channel, let's look to see if we are a
+ # puzzle channel with a hunt-id prefix.
+ hunt_id = channel_name.split('-')[0]
+
+ response = hunts_table.scan(
+ FilterExpression='hunt_id = :hunt_id',
+ ExpressionAttributeValues={':hunt_id': hunt_id}
+ )
+
+ if 'Items' in response:
+ item = response['Items'][0]
+ return (item['hunt_id'], item['name'])
+
+ return (None, None)
+
def puzzle(turb, body, args):
"""Implementation of the /puzzle command
a modal dialog for user input instead)."""
channel_id = body['channel_id'][0]
+ channel_name = body['channel_name'][0]
trigger_id = body['trigger_id'][0]
- hunts_table = turb.db.Table("hunts")
- response = hunts_table.get_item(Key={'channel_id': channel_id})
+ (hunt_id, hunt_name) = find_hunt_for_channel(turb, channel_id, channel_name)
- if 'Item' in response:
- hunt_name = response['Item']['name']
- hunt_id = response['Item']['hunt_id']
- else:
+ if not hunt_id:
return bot_reply("Sorry, this channel doesn't appear to "
- + "be a hunt channel")
+ + "be a hunt or puzzle channel")
view = {
"type": "modal",