This step-by-step tutorial will go over how to build a Slackbot using Twilio Autopilot and Twilio Functions, saving the user's answers to a MongoDB Atlas database. If you haven't seen part one of this two-part blog series yet, check it out here and make sure you have your Autopilot Slackbot and Function set up before continuing below.

Setup MongoDB

Let's receive and save the data the user gave us. On the MongoDB Cloud Atlas homepage, make a free account. Once logged in, make a new cluster.

Under Cloud Provider & Region, select a cloud provider. For this post, I decided to use Azure. Then select a region (one with a free tier may be preferable.)

For Cluster Tier, you can then select "M0 Sandbox"--your account will only be able to have one cluster at this level. Now you can scroll to the bottom and click the green "Create Cluster" button. This can take 7-10 minutes, so you have time to make a cup of tea and stretch.

Now on the Clusters page underneath Cluster0 which you just made, click "Connect".

You will need to allow an IP address... and because it's hard for Twilio Functions' IP address to predict, we must let your Cluster accept the whole internet: you must allow 0.0.0.0. Do so by clicking "Add Your Current IP Address":

There are three ways to connect to your cluster. For this post, we will be using MongoDB Compass which provides a GUI for MongoDB so click the third option as shown below and follow those instructions. They will give you download links in the next step.

If you don't have Compass downloaded already, you'll need to download the right version for your machine. Compass provides a nice interface that lets us easily query, manipulate, and explore our data. Next copy that connection string they give you for the next part to receive and save input from Autopilot.

Edit the URL to include your username and password and copy that. When you open the MongoDB Compass Community with that modified URL copied, it will recognize you have a connection string and then fill out some of the New Connection data for you.

Click "yes" and then scroll down to click the green "Connect" button. As long as you can connect without errors, we can move on to receiving and saving input from the Slackbot.

Receive and Save Input from Autopilot With Twilio Functions

Go to the Configure Functions section of Twilio Runtime. Under Environmental Variables, click the red "+" button. Name the key something like MONGO_CONNECTION_STRING and for the value, paste in the MongoDB Connection String from the last section. This can now be referenced from any of your Twilio Functions with context.MONGO_CONNECTION_STRING.

Now under Dependencies click the red "+" button to add an npm module. In the name box, type "mongodb" and you can ignore the version box, getting the most recent stable version. Click "Save" and you're good to go!

Next, create a Twilio Function called "Slackbot." Add "/slackbot" to the path, which will be unique to you.

Now replace the following code in your Function with this in order to save user input to variables.