This repository contains everything needed to deploy a Slackbot on Digital Ocean with Terraform that runs in faasd. The bot was initially created to facilitate remote question and answer sessions at my church by allowing viewers of our live stream to text or email questions in, have a staff member ask their question in the room, and then allow the staff member to send a response back to the sender. The texting is facilitated by Twilio while the email is done by interacting with a Gmail account via IMAP and SMTP.
There are two ways in which data flows through this system.
This process is facilitated by the email to Slack function being triggered once a minute via cron.
To use this setup you will need the following:
The cost will depend on how much you use Twilio. So far a $5 / month Digital Ocean Droplet is working for me. The referral links from above should provide enough credits to try this out for free though.
Though most of the work needed for this setup is handled by the "fbc-slackbot-python" function, there is also a second function called "email-to-slack" that handles checking Gmail. The process here will deploy both functions along with all the infrastructure needed to run them.
Use this tutorial from OpenFaaS to get up and running. The exceptions to the tutorial are:
cd
into the digitalocean-terraform
foldermain.tfvars
to suit your needs (it's been modified from the original one)Before moving forward you will need to setup your Twilio account. Follow the "Setting up Twilio" section of this tutorial. Instead of the URL it talks about using you will need to enter your OpenFaaS URL followed by /function/fbc-slackbot-python/incoming/twilio
. For example: https://faasd.example.com/function/fbc-slackbot-python/incoming/twilio
The page where you enter that URL can be gotten to via https://www.twilio.com/console/phone-numbers/incoming and then clicking on the number you want to use.
Go to your Slack workspace and create a channel named q-and-a
as that is the room that the functions expect to post to.
To use IMAP with a Gmail account you must first go to your settings page and click on "Forwarding and POP/IMAP." On that page you will need to toggle on "Enable IMAP." You will then need to follow the instructions on Less secure apps & your Google Account to either enable less secure apps or create an app password.
The functions in this repository's functions
folder need a few secrets to work:
email-account
: the Gmail account that will be interacted withemail-password
: the password for the Gmail accountimap-server
: the IMAP server to interact with (imap.gmail.com
)slack-bot-token
: the "Bot user OAuth Access Token" from your Slack appsmtp-server
: the SMTP server to interact with (smtp.gmail.com
)twilio-account-sid
: the "Account SID" from Twiliotwilio-auth-token
: the "Auth Token" from Twiliotwilio-number
: the phone number from Twilio used for textsDeploy each secret via a command such as this:
faas-cli secret create imap-server --from-literal imap.gmail.com
functions
folder from the digitalocean-terraform
folder via cd ../functions
Once in that folder, run the following commands:
# Log into your Docker registry
docker login
# Build and deploy the functions
faas-cli up -f stack.yml
Once the fbc-slackbot-python
function is running go back to your Slack app and select "Basic Information" on the left side bar again. Click "Add features and functionality" and select "Event Subscriptions" and then toggle on "Enable Events."
In the "Request URL" field enter your OpenFaaS URL followed by /function/fbc-slackbot-python/incoming/slack
. For example: https://faasd.example.com/function/fbc-slackbot-python/incoming/slack
.
If all goes well, you will see "Verified" in green next to the "Request URL" heading. Once you do, select "Subscribe to events on behalf of users" and click "Add Workspace Event". Select "message.channels" from the available options.
A couple of minutes after creating your Droplet on Digital Ocean you should get an email containing information about how to ssh into it. Once you have this information, ssh to your new host and run crontab -e
so that you can create a new cron job. Press i
, and enter this:
* * * * * curl -sS https://faasd.example.com/function/email-to-slack >/dev/null 2>&1
After you enter this line, press the escape key to exit the editing mode and then press :wq
to write and quit.
This will cause the "email-to-slack" to be called once a minute. Each run will deliver any new emails to Slack.
At this point you should have a working bot! Test it out by sending a text to the Twilio number. You should see it arrive in #q-and-a
. Click on the message and start a thread. In the thread enter a reply and it should show up on your phone. Next, send an email to the address you specified earlier and then respond in the same way.
Currently, there are a few known limitations of this setup:
I am planning to deal with attachments and images going both directions. I am also considering doing something with select reactions to the original message.
I also want to update the Terraform code to store state in Terraform Cloud since doing so is free.
The code here was developed by combining bits and pieces from these sources and random web searches: