X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=turbot%2Finteraction.py;h=8c1304af8262f2273810372a26f89a1db9d081fc;hb=b9fc94a0e63e3eea4e06b869d7c771357714178c;hp=947a01f67a48fee942f672551bd03f196dcdad2e;hpb=41931beb0de72a0669600e92dfe4d00b2e596dbf;p=turbot diff --git a/turbot/interaction.py b/turbot/interaction.py index 947a01f..8c1304a 100644 --- a/turbot/interaction.py +++ b/turbot/interaction.py @@ -6,8 +6,7 @@ import turbot.slack import json import re import requests - -TURBOT_USER_ID = 'U01B9QM4P9R' +from botocore.exceptions import ClientError actions = {} commands = {} @@ -85,6 +84,7 @@ def new_hunt_submission(turb, payload, metadata): 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'] @@ -95,6 +95,33 @@ def new_hunt_submission(turb, payload, metadata): "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) @@ -103,59 +130,29 @@ def new_hunt_submission(turb, payload, metadata): "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, } @@ -269,9 +266,6 @@ def puzzle_submission(turb, payload, metadata): 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'] @@ -300,12 +294,8 @@ def puzzle_submission(turb, payload, metadata): 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, @@ -314,32 +304,9 @@ def puzzle_submission(turb, payload, metadata): "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 }