From: Carl Worth Date: Wed, 14 Oct 2020 00:43:02 +0000 (-0700) Subject: Add list of active hunts to the turbot Home tab X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=c4aff7da7e521b1fac517c89410318a0e71b7829;p=turbot Add list of active hunts to the turbot Home tab Finally starting to hook this Lambda up to our database for the first time. --- diff --git a/turbot/commands.py b/turbot/commands.py index 0ba1bcc..1b7098c 100644 --- a/turbot/commands.py +++ b/turbot/commands.py @@ -1,7 +1,7 @@ import requests import turbot.rot -def rot(slack_client, body, args): +def rot(turb, body, args): """Implementation of the /rot command The args string should be as follows: diff --git a/turbot/events.py b/turbot/events.py index dcb2d0d..e8ea0b8 100644 --- a/turbot/events.py +++ b/turbot/events.py @@ -1,9 +1,10 @@ import turbot.views -def app_home_opened(slack_client, body): +def app_home_opened(turb, body): user_id = body['event']['user'] - view = turbot.views.home(user_id, body) - slack_client.views_publish(user_id=user_id, view=view) + print("In app_home_opened function") + view = turbot.views.home(turb, user_id, body) + turb.slack_client.views_publish(user_id=user_id, view=view) return "OK" events = { diff --git a/turbot/views.py b/turbot/views.py index ae897cc..aa0f010 100644 --- a/turbot/views.py +++ b/turbot/views.py @@ -29,18 +29,21 @@ def button(label, name): "value": name } -def home(user_id, body): +def home(turb, user_id, body): """Returns a view to be published as the turbot home tab for user_id The body argument is a dictionary as provided by the Slack request. The return value is a dictionary suitable to be published to the Slack views_publish API.""" + response = turb.db.Table("hunts").scan() + hunts = response['Items'] + return { "type": "home", "blocks": [ - section(text("This is (or soon will be) a list of " - + "available puzzle hunts)")), + section(text("*Active hunts*")), + *[section(text(hunt['name'])) for hunt in hunts if hunt['active']], actions(button("New hunt", "new_hunt")) ] } diff --git a/turbot_lambda/turbot_lambda.py b/turbot_lambda/turbot_lambda.py index b6f4a36..b34c6f7 100644 --- a/turbot_lambda/turbot_lambda.py +++ b/turbot_lambda/turbot_lambda.py @@ -4,6 +4,7 @@ import boto3 import requests import json import os +from types import SimpleNamespace import turbot.actions import turbot.commands @@ -23,6 +24,12 @@ response = ssm.get_parameter(Name='SLACK_BOT_TOKEN', WithDecryption=True) slack_bot_token = response['Parameter']['Value'] slack_client = WebClient(slack_bot_token) +db = boto3.resource('dynamodb') + +turb = SimpleNamespace() +turb.slack_client = slack_client +turb.db = db + def error(message): """Generate an error response for a Slack request @@ -99,7 +106,7 @@ def event_callback_handler(body): type = body['event']['type'] if type in turbot.events.events: - return turbot.events.events[type](slack_client, body) + return turbot.events.events[type](turb, body) return error("Unknown event type: {}".format(type)) def turbot_interactive_or_slash_command(event, context): @@ -167,6 +174,6 @@ def turbot_slash_command(body): args = body['text'][0] if command in turbot.commands.commands: - return turbot.commands.commands[command](slack_client, body, args) + return turbot.commands.commands[command](turb, body, args) return error("Command {} not implemented".format(command))