Participate in Local Issues with a SMS Chatbot
Time to read: 5 minutes
If you have opinions about how your city is run, it’s more important than ever to make your voice heard. This blog post will go over how to build a SMS chatbot using Twilio Autopilot, Twilio SendGrid, Twilio Functions, TwiML Bins, and JavaScript to engage in local civic issues.
One example might be San Francisco, where the city is planning to permanently cut 40 of 68 bus lines and free bus rides for youth.
In this blog post, concerned citizens can tell members of the San Francisco County Transportation Authority to not cut bus lines. Read on for a step-by-step process to build the bot, and you can text the +14153068517 number to see the example!
Set Up your SMS Chatbot
To follow along with this post, you need three things:
- A Twilio account - sign up for a free one here and receive an extra $10 if you upgrade through this link
- A Twilio phone number with SMS capabilities - configure one here
- A SendGrid account - make one here
Go to your Autopilot console and under Select a Bot
click Start from Scratch
.
Give your bot a title like MuniCall and click Create bot
.
Click save.
Using Twilio Functions and TwiML Bins for Calls to Decision Makers
Click All Tasks
and add a task called call
.
On the same line as that Call
task, click Train
to add samples that will trigger this task. Samples are different words or phrases the user might say to call their representatives, running this task. For the Call
task you might add samples like "phone", "voicemail", "call representatives", "call reps", "dial", "leave a voicemail", and other similar phrases.
After adding your samples, click "Switch to program task" near the top-right.
Replace the JSON in the JSON bin with the following which uses a Collect
Action to ask a series of questions and group them and a user's answers together.
After asking the user all the questions in the questions
array, this task redirects to a Twilio Function to make the phone calls using JavaScript. Create a new Function here by clicking the red plus button. Make a blank function and give it a name like "Muni Call" and add on to the path as shown below. Don't forget to replace the Function path in your call
task above!
Replace the code in the Function with the following JavaScript:
To make and customize your own civic engagement bot, you would replace the bodyCall
with your own message to representatives about the local issue you want to change and then replace numbers
to the phone numbers of the representatives you wish to call. Don't forget to include your name and precinct in the text!
First we access the memory to get the user's answers and save them as variables name
and zipCode
. bodyCall
is a short transcript (of what the phone call will say to each representative) to be texted back to the user and numbers
is an object containing the names of who to call and their corresponding phone numbers. We loop through that object and make an outbound call to each number with answering machine detection (AMD) enabled, passing it a URL to a TwiML bin containing the following TwiML:
You can read more on generating dynamic TwiML and templating with Twilio Bins here.
Once we reach the end of the object, go back in the Twilio Function to see that a text message is returned to the user confirming that each representative was called.
Contacting Decision Makers with Twilio SendGrid and Twilio Functions
To contact decision makers on a different medium, we're going to go back to your bot in your Autopilot console and add a new task called Email
. On the same line as Email
, click Train
to add some samples that will trigger this task such as "email", "email reps", "email representatives", "email SFMTA", "send email", and similar phrases.
Then click "Switch to program task" near the top-right and add the following JSON that uses a similar Collect flow to the Call
task:
The last two questions in the `Collect` flow above are more open-ended: the user can type out a longer response or they can send "default" for the chatbot to instead use a default response.
Grab your SendGrid API key. In the Twilio Functions Configuration section, save it as the environment variable SENDGRID_API_KEY
like so:
Now it can be referenced with context.SENDGRID_API_KEY
in any of your Twilio Functions.
Make another new Twilio Function that the email
task will redirect to. Give it a name and path like this, and don't forget to replace the Function path in your email
task.
In your Function, replace the code with the following JavaScript:
This code imports SendGrid at the top, gets the answers to each question in the email
task, and checks if the user sent "default" for the last two questions. If so, we send ready-made blurbs on why Muni is important and create an array called messages
to send bulk emails with SendGrid. When each email is sent, the chatbot sends a confirmation message back to the user. Check out this blog post for more information on sending bulk emails with SendGrid in Node.js.
You can test your chatbot in the Twilio console's Autopilot simulator, but now let's also hook up the bot to a Twilio phone number so your friends can text it!
Configure your Bot with a Twilio Number
Go to the Autopilot console and select Channels
from the left-hand menu. Click Programmable Messaging
.
Copy that Messaging URL and in a new tab configure your Twilio number in your phone number console.
If you don't have a Twilio number yet, go to the Phone Numbers section of your Twilio Console and search for a phone number in your country and region, making sure the SMS checkbox is ticked.
In the Messaging
section of your purchased number, in the A Message Comes In
section, set the Webhook
to be your Messaging URL and hit Save.
Now you can get out your phone, text the number, and share the number with people to contact San Francisco County Transportation Authority representatives, or whoever you would like to reach! The complete code for this blog post and chatbot can be found here on GitHub.
What's Next for Civic-Minded Chatbots?
Twilio makes it easy to programmatically call, text, email, and generally communicate. You can make your chatbot more complex by adding other tasks to provide information such as:
- resources on helping keep Muni funded
- facts or statistics on public transit
- option to provide an email confirmation to the user (so they get emailed a copy of the email sent to representatives)
Of course, you can customize this code to whatever issues you are most passionate about. If you’re interested in using coding for social change, check out this interview with Sachin Medhekar who is trying to help fight homelessness, how to create successful advocacy campaigns, or see the work Twilio.org is doing! Here's to hoping we can help keep mass transportation funded and better serve those who rely on it. Thanks for reading and let us know online or in the comments what you're building to make the world a better place!
- Twitter: @lizziepika
- GitHub: elizabethsiegle
- Email: lsiegle@twilio.com
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.