find_puzzle_for_url,
find_puzzle_for_puzzle_id,
puzzle_update_channel_and_sheet,
- puzzle_id_from_name
+ puzzle_id_from_name,
+ puzzle_blocks
)
import turbot.rot
import turbot.sheets
def puzzle(turb, body, args):
"""Implementation of the /puzzle command
- The args string is currently ignored (this command will bring up
- a modal dialog for user input instead)."""
+ The args string can be a sub-command:
+
+ /puzzle new: Bring up a dialog to create a new puzzle
+
+ Or with no argument at all:
+
+ /puzzle: Print details of the current puzzle (if in a puzzle channel)
+ """
+
+ if args == 'new':
+ return new_puzzle(turb, body)
+
+ if len(args):
+ return bot_reply("Unknown syntax for `/puzzle` command. " +
+ "Use `/puzzle new` to create a new puzzle.")
+
+ # For no arguments we print the current puzzle as a reply
+ channel_id = body['channel_id'][0]
+ response_url = body['response_url'][0]
+
+ puzzle = puzzle_for_channel(turb, channel_id)
+
+ if not puzzle:
+ hunt = hunt_for_channel(turb, channel_id)
+ if hunt:
+ return bot_reply(
+ "This is not a puzzle channel, but is a hunt channel. "
+ + "If you want to create a new puzzle for this hunt, use "
+ + "`/puzzle new`.")
+ else:
+ return bot_reply(
+ "Sorry, this channel doesn't appear to be a hunt or a puzzle "
+ + "channel, so the `/puzzle` command cannot work here.")
+
+ blocks = puzzle_blocks(puzzle, include_rounds=True)
+
+ requests.post(response_url,
+ json = {'blocks': blocks},
+ headers = {'Content-type': 'application/json'}
+ )
+
+ return lambda_ok
+
+commands["/puzzle"] = puzzle
+
+def new_puzzle(turb, body):
+ """Implementation of the "/puzzle new" command
+
+ This brings up a dialog box for creating a new puzzle.
+ """
channel_id = body['channel_id'][0]
trigger_id = body['trigger_id'][0]
view=view)
if (result['ok']):
- submission_handlers[result['view']['id']] = puzzle_submission
+ submission_handlers[result['view']['id']] = new_puzzle_submission
return lambda_ok
-commands["/puzzle"] = puzzle
-
-def puzzle_submission(turb, payload, metadata):
+def new_puzzle_submission(turb, payload, metadata):
"""Handler for the user submitting the new puzzle modal
- This is the modal view presented to the user by the puzzle function
- above."""
+ This is the modal view presented to the user by the new_puzzle
+ function above.
+ """
# First, read all the various data from the request
meta = json.loads(metadata)