Best Flask open-source libraries and packages

Marvin

🤖 Slack Bot layout for Flask and Asyncio to develop Slack event handlers and deploy on AWS Lambda + API Gateway
Updated 5 years ago

Marvin Marvin

Marvin, more fully known as Marvin the Paranoid Android, is an incredibly brilliant but overwhelmingly depressed robot manufactured by the Sirius Cybernetics Corporation and unwilling servant to the crew of the Heart of Gold.

No seriously, what is Marvin?

Marvin is a Slack Bot layout for Flask and Asyncio to develop Slack Event handlers and deploy on AWS Lambda + API Gateway.

Create a Slack bot user

See the Slack's documentation for further guidance on creating your bot (step 1).

Within the Basic Information about your application, copy the Signing Secret necessary to verify requests from Slack.

Signing Secret

Now go to the OAuth & Permissions section and copy the Bot User OAuth Access Token to configure the Bot's credentials.

OAuth

Configure your Application

Fill in your .env file with your SLACK_SIGNING_SECRET and SLACK_API_TOKEN keys obtained in the previous step 1

$ cp .env.example .env

🐣 Build the local environment:

$ make up

To avoid cyclic calls between subscriptions and API calls, set the Slack variables that identify your Bot:

$ make botinfo
{
    "id": "AGOAULRCZ",
    "name": "marvin",
    "profile": {
        "bot_id": CGK8R9V7S",
    },
    "is_bot": true
}

Copy the id value for SLACK_BOT_USER_ID and the bot_id value for SLACK_BOT_ID from your .env file and reload the Docker container.

$ make reload

🚀 Run your local server:

$ make run

Finally, you need a Public URL to subscribe to Slack events, you can use ngrok to create a tunnel to development server and get your public url:

$ make ngrok

ngrok

As you can see in the image, ngrok has assigned to the development server a subdomain dd15a495, so the Public URL is https://dd15a495.ngrok.io/api/v1/slack/events.

Configure your Slack Bot

Continue with the Slacks's documentation to setting up the Events API (step 2) and enter the URL to receive the subscriptions:

Enable Events

Select the Slack Events you want to subscribe to, these events are required for the code examples in your project's app/handlers directory.

Suscribe to bot Event

Finally, install your bot to a workspace (step 3).

AWS Lambda Deployment

Install AWS Command Line Interface and configure the marvin profile

$ pip install awscli --upgrade --user
$ aws configure --profile marvin

Then create a zappa configuration file zappa_settings.json and upload your config.json file to your s3 bucket using the same url as the remote_env field from your zappa_settings.json.

Build your λ docker container:

$ make lambda

Here we go!

$ make zappa cmd=deploy

Available Commands

$ make help
Please use 'make <target>' where <target> is one of
  build         Build docker image
  up            Run docker container and build if the image does not exist
  restart       Restart docker container
  rm            Remove docker container
  reload        Reload docker container
  logs          View output from docker container
  sh            Run bash shell on the container
  ngrok         Create a tunnel to development server
  run           Run a local development server
  botinfo       Show the current Bot info
  requirements  Install pip requirements from $REQUIREMENTS_FILES variable
  lambda        Build λ docker container
  zappa         Run zappa command (usage: make zappa cmd={command})
  test          Run unit tests
  isort         Run isort recursively from your current directory
  coverage      Run unit tests and check the coverage