import json
import re
import requests
-
-TURBOT_USER_ID = 'U01B9QM4P9R'
+from botocore.exceptions import ClientError
actions = {}
commands = {}
function above."""
state = payload['view']['state']['values']
+ user_id = payload['user']['id']
name = state['name']['name']['value']
hunt_id = state['hunt_id']['hunt_id']['value']
url = state['url']['url']['value']
"Hunt ID can only contain letters, "
+ "numbers, and underscores")
+ # Check to see if the hunts table exists
+ hunts_table = turb.db.Table("hunts")
+
+ try:
+ exists = hunts_table.table_status in ("CREATING", "UPDATING",
+ "ACTIVE")
+ except ClientError:
+ exists = False
+
+ # Create the hunts table if necessary.
+ if not exists:
+ hunts_table = turb.db.create_table(
+ TableName='hunts',
+ KeySchema=[
+ {'AttributeName': 'channel_id', 'KeyType': 'HASH'},
+ ],
+ AttributeDefinitions=[
+ {'AttributeName': 'channel_id', 'AttributeType': 'S'},
+ ],
+ ProvisionedThroughput={
+ 'ReadCapacityUnits': 5,
+ 'WriteCapacityUnits': 5
+ }
+ )
+ return submission_error("hunt_id",
+ "Still bootstrapping hunts table. Try again.")
+
# Create a channel for the hunt
try:
response = turb.slack_client.conversations_create(name=hunt_id)
"Error creating Slack channel: {}"
.format(e.response['error']))
- if not response['ok']:
- return submission_error("name",
- "Error occurred creating Slack channel "
- + "(see CloudWatch log")
-
- user_id = payload['user']['id']
channel_id = response['channel']['id']
# Create a sheet for the channel
sheet = turbot.sheets.sheets_create(turb, hunt_id)
+ channel_id = response['channel']['id']
+
# Insert the newly-created hunt into the database
- hunts_table = turb.db.Table("hunts")
+ # (leaving it as non-active for now until the channel-created handler
+ # finishes fixing it up with a sheet and a companion table)
hunts_table.put_item(
Item={
'channel_id': channel_id,
- "active": True,
+ "active": False,
"name": name,
"hunt_id": hunt_id,
- "url": url,
- "sheet_url": sheet['url']
+ "url": url
}
)
# Invite the initiating user to the channel
turb.slack_client.conversations_invite(channel=channel_id, users=user_id)
- # Message the channel with the URL of the sheet
- turb.slack_client.chat_postMessage(channel=channel_id,
- text="Sheet created for this hunt: {}"
- .format(sheet['url']))
-
- # Create a database table for this hunt's puzzles
- table = turb.db.create_table(
- TableName=hunt_id,
- AttributeDefinitions=[
- {'AttributeName': 'channel_id', 'AttributeType': 'S'}
- ],
- KeySchema=[
- {'AttributeName': 'channel_id', 'KeyType': 'HASH'}
- ],
- ProvisionedThroughput={
- 'ReadCapacityUnits': 5,
- 'WriteCapacityUnits': 4
- }
- )
-
- # Message the hunt channel that the database is ready
- turb.slack_client.chat_postMessage(
- channel=channel_id,
- text="Welcome to your new hunt! "
- + "Use `/puzzle` to create puzzles for the hunt.")
-
return {
'statusCode': 200,
}
This is the modal view presented to the user by the puzzle function
above."""
- print("In puzzle_submission\npayload is: {}\nmetadata is {}"
- .format(payload, metadata))
-
meta = json.loads(metadata)
hunt_id = meta['hunt_id']
hunt_channel_id = meta['hunt_channel_id']
puzzle_channel_id = response['channel']['id']
- # Create a sheet for the puzzle
- sheet = turbot.sheets.sheets_create_for_puzzle(turb, hunt_dash_channel)
-
# Insert the newly-created puzzle into the database
table = turb.db.Table(hunt_id)
-
table.put_item(
Item={
"channel_id": puzzle_channel_id,
"name": name,
"puzzle_id": puzzle_id,
"url": url,
- "sheet_url": sheet['url']
}
)
- # Find all members of the hunt channel
- members = turbot.slack.slack_channel_members(turb.slack_client,
- hunt_channel_id)
-
- # Filter out Turbot's own ID to avoid inviting itself
- members = [m for m in members if m != TURBOT_USER_ID]
-
- turb.slack_client.chat_postMessage(channel=puzzle_channel_id,
- text="Inviting members: {}".format(str(members)))
-
- # Invite those members to the puzzle channel (in chunks of 500)
- cursor = 0
- while cursor < len(members):
- turb.slack_client.conversations_invite(
- channel=puzzle_channel_id,
- users=members[cursor:cursor + 500])
- cursor += 500
-
- # Message the channel with the URL of the puzzle's sheet
- turb.slack_client.chat_postMessage(channel=puzzle_channel_id,
- text="Sheet created for this puzzle: {}"
- .format(sheet['url']))
return {
'statusCode': 200
}