Carl Worth [Tue, 13 Oct 2020 23:07:01 +0000 (16:07 -0700)]
Fold all of turbot/slack.py up into turbot_flask/turbot.py
Just making the Flask bot self-contained at this point, (since we're
basically about to abandon it in its development for the time being
while we focus on the Lambda-based bot that has access to DynamoDB).
Carl Worth [Tue, 13 Oct 2020 22:28:58 +0000 (15:28 -0700)]
Fix several problems pointed out by flake8
I really should integrate flake8 more decisively into my workflow somehow.
Note: I'm ignoring W503 here since it's contradictory with W504,
(breaking a "line too long" problem at an operator will cause
one of W503 or W504 to fire and there's no way I can see to shut
them both up).
Carl Worth [Tue, 13 Oct 2020 19:57:44 +0000 (12:57 -0700)]
Revamp the Home page of our app to start looking like what we want
This gets rid of the original placeholder blocks which I had gotten
from a tutorial and replaces it with placeholders that look more like
what we are going to want, (a list of hunts and a button to create a
new hunt).
While doing this, we provide some utility functions to create blocks
to reduce the amount of boilerplate we need while constructing these
things.
Carl Worth [Tue, 13 Oct 2020 18:03:30 +0000 (11:03 -0700)]
Introduce a new turbot_interactive function
Now that we've introduced a new entry point for our Slack app, which is
the interactive features, (that is, an entry point that Slack will POST
to when the user clicks on a button or some other interactive element that
our app. provides to them).
Carl Worth [Tue, 13 Oct 2020 03:07:40 +0000 (20:07 -0700)]
Implement url_verification for Slack event handling
This isn't a real event, but it is required to correctly be able to handle
this url_verification challenge before Slack will allow us to register
our URL in its interface.
Carl Worth [Mon, 12 Oct 2020 22:04:30 +0000 (15:04 -0700)]
turbot_lambda: Add Slack signature verification
This will reject any request that does not come from Slack itself,
(as verified by the Slack-specified HMAC algorithm and the
SLACK_SIGNING_SECRET that Slack made available to us and that we
have registered as an encrypted SSM parameter).
Carl Worth [Mon, 12 Oct 2020 21:13:24 +0000 (14:13 -0700)]
turbot_lambda: Inject /rot response into the same channel as the command
Prior to this commit, we were just including the rot result in the
response to the POST request. That was sufficient for Slack to take
the result and present it privately to the user that issued the
command. But that would not allow any other user in the same channel
to see the same result.
Here, instead, we use the Slack WebClient API to inject the result
into the user's channel as a new message. This takes advantage of a
new, encrypted SSM parameter SLACK_BOT_TOKEN to authenticate our
bot. It also requires bundling all of the virtualenv dependencies into
the zip file uploaded to AWS for the lambda, so that's what the
Makefile modifications do here.
Carl Worth [Mon, 12 Oct 2020 20:07:50 +0000 (13:07 -0700)]
turbot_lambda: Parse the command from the request
This will allow us to implement multiple slash commands within a single
Lambda function. Currently just the /rot (also its /rotlambda alias)
command is implemented, but we've got all the structure in place to do
more as necessary.
Carl Worth [Mon, 12 Oct 2020 03:52:01 +0000 (20:52 -0700)]
Switch to parse the x-www-form-urlencoded data in Python
Previously, we were doing this in a mapping template within AWS,
but I decided that that mechanism was more trouble that it was
worth, (it was quite buried in AWS, hard to share across multiple
lambdas, and meanwhile this approach is a single line of code).
Carl Worth [Mon, 12 Oct 2020 03:08:13 +0000 (20:08 -0700)]
Switch from event['args'] to event['text'] for input to the lambda
Since that's what we actually get from Slack, (now that we have the
"mapping template" in place so that we receive a dictionary here rather
than a bunch of x-www-form-urlencoded data).
Carl Worth [Mon, 12 Oct 2020 02:59:54 +0000 (19:59 -0700)]
Rename lamdba_function.lambda_handler to turbot_lambda.turbot_lambda
Now that I realized that the lambda_function.lambda_handler naming
isn't hard-coded in AWS but is instead just a default, (and now that
I've configured it to this instead).
Carl Worth [Sun, 11 Oct 2020 21:12:42 +0000 (14:12 -0700)]
Share the rot() implementation between both Lambda and Flask implementations
This provides a model of how to do code sharing between the two
implementations we have, (with the expectation that the code in the
"turbot" package will be generic and that code in turbot_slack and
turbot_lambda will use that common code while doing whatever
additional marshalling is necessary for their particular application
environments).
Carl Worth [Sun, 11 Oct 2020 20:59:19 +0000 (13:59 -0700)]
Move top-level flask app from turbot/turbot.py to turbot_flask/turbot.py
This is a first baby step toward teasing apart true "library" code in
turbot, (which can be shared by both the Lambda and Flask
implementations), from code that is Flask-specific.
(For symmetry, the lambda directory is renamed turbot_lambda here as well.)
Carl Worth [Sun, 11 Oct 2020 17:35:45 +0000 (10:35 -0700)]
Start implementing code intended to be deployed to AWS Lambda
Here, lambda/rot.py is a copy of the turbot/rot.py code, but massaged
into the calling conventions of AWS Lambda (and without yet any actual
code to integrate with Slack in either direction).
Carl Worth [Fri, 9 Oct 2020 22:41:11 +0000 (15:41 -0700)]
Use a thread for creating the Google Sheet when a new channel is created
This allows us to return quickly so that Slack doesn't retry sending the event.
As implemented in this commit, this code is a bit cheesy in that it
disables the debug mode and error handling in slack_send_message,
(since the prior implementation of that depended on having a current
Flask app, but now that we are calling this function from a thread we
don't have a current app context).
Carl Worth [Wed, 30 Sep 2020 14:04:56 +0000 (07:04 -0700)]
Remove another stale file name from a comment
The documentation example used "token.pickle" but we are now using
".gsheets-token.pickle". Here we switch to just "token file" in the
code comment so the comment is immune to any renaming.
Carl Worth [Tue, 29 Sep 2020 22:08:44 +0000 (15:08 -0700)]
Initial implement of auto-creation of Google sheets
This is implemented via a Slack event listener that's listening for
the "channel_created" event. So, whenever a new channel is created,
this code will create a new Google sheet and inject a message into
the new channel with the URL of the sheet.
Carl Worth [Tue, 29 Sep 2020 22:05:21 +0000 (15:05 -0700)]
Rename .turbot.env to .slack-creds.env
I had originally chosen a generic name hoping that I could cram all
of our various credentials into this one file. But, alas, the Google
docs. credentials really want to be in a JSON file so they will be
separate.
Given that, I'll give each file a specific name, starting with this
Slack-specific file.
Carl Worth [Tue, 29 Sep 2020 22:02:57 +0000 (15:02 -0700)]
Makefile: Fix DO_NOT_DEPLOY implementation to work with multiple files
The --exclude option to rsync expects a single file, so we need to
arrange to pass it multiple times if we have multiple files in the
value of the DO_NOT_DEPLOY variable. Fortunately, this is a perfect
job for patsubst.
Carl Worth [Tue, 29 Sep 2020 18:15:54 +0000 (11:15 -0700)]
Add a listener for the Slack "channel_created" event
Currently this just posts a simple message when a channel is
created. Eventually, we'll want to do something different such as
creating a puzzle sheet and linking/pinning it to the channel.
Carl Worth [Tue, 29 Sep 2020 16:59:42 +0000 (09:59 -0700)]
Abstract out two slack-related functions
The two new functions are slack_is_valid_request and slack_send_reply.
These functions provide some shared code for dealing with slack to
avoid code duplication.
These functions also provide a debug mode (that doesn't use slack at
all). This is a convenience for testing the implementation of slash
commands in a local "make run" session without needing to touch Slack
endpoints at all. (Instead, all requests are considered valid and
slack replies are simply printed to stdout.)
Carl Worth [Tue, 29 Sep 2020 15:35:46 +0000 (08:35 -0700)]
Convert "make run" to use "flask run" instead of direct python script execution
This will simplify some future work (regarding reorganizing the code into
a package, etc.) so that we don't have to try to simultaneously support
running as both a script and as a module.
Carl Worth [Tue, 29 Sep 2020 13:20:59 +0000 (06:20 -0700)]
Use a raw string for a regular expression
Previously, the string had an unrecognized escape sequence ('\*').
With a non-raw string we could have doubled the backslash, but it's
much cleaner to just use a raw string instead, (so that backslashes
are used for regular-expression escaping, and not also for string
escaping).
Carl Worth [Tue, 29 Sep 2020 13:12:55 +0000 (06:12 -0700)]
Fix some code style issues identified by flake8
Some of the things fixed include TAB based indentation of a
documentation string, some unneeded semicolons, a missing exception
name, and an excessively long line.
Carl Worth [Sun, 27 Sep 2020 21:45:34 +0000 (14:45 -0700)]
Add signature verification fo the /rot slash command
This prevents someone abusing our bot by spoofing messages appearing
to have come from Slack. This way we won't reply to any command
request unless it actually comes from Slack.
Carl Worth [Sat, 26 Sep 2020 05:02:45 +0000 (22:02 -0700)]
Restore handling of error events from Slack
This is better than what I had copied from the example a few commits
ago because this properly logs through the python logging system, (so
the error message will appear in the Apache log file).
Carl Worth [Sat, 26 Sep 2020 04:47:32 +0000 (21:47 -0700)]
Fix turbot.wsgi to be able to find its environment variables
We can't rely on the current working directory of the Apache process,
so we need to use an explicit path here in order to find our file
with environment variables in it.
I want to start with this to ensure I can at least get a correct
challenge working. After I get this working this way, I'll plan to put
direct flask code back in place and integrated with the web server via
WSGI.
Carl Worth [Sat, 26 Sep 2020 02:01:48 +0000 (19:01 -0700)]
Initial commit of turbot
Turbot will eventually be a Slack bot for the Halibut That Bass team.
As of this commit, turbot.py is just a simple flask app implementing a
REST API (to query, add, delete TODO items). Additionally, this commit
includes the Makefile pieces to manage python dependencies and to
deploy the program to our server.
There's not yet any actual Slack-application code here yet.