Build a Punchy Quiz Bot with Twilio in Eight Minutes
Time to read: 3 minutes
Have you ever wondered what Twilio product you are most like? Now you can text +1 (321) 340-6085 to find out!
These entertaining "What kind of ____ are you?” quizzes have risen in popularity, getting shared an average of 1900 times on social media according to Buzzsumo. Read on to see how you can make a punchy and fun Buzzfeed-style quiz "What Twilio Product are You?" with Twilio.
Set Up and Configure your Bot with a Twilio Number
To follow along with this post, you need two 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
Go to your Autopilot console and under Create a Bot
select Start from Scratch
.
Give your bot a title like what-twilio-product-are-you and click Create bot
.
Select Channels
from the left-hand menu and then 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.
Make a Buzzfeed-Style Quiz with Autopilot
Back in your Autopilot bot console, you should see some default tasks that already have been trained with some samples. For example, the Goodbye
task says "Thank you! Please reach out again if you need anything. Goodbye." and is triggered by phrases such as "that's all", "no thanks", etc. Greeting
is another ready-made task. Let's edit it now.
Edit Greeting
by clicking Program
next to it to include the following JSON that will introduce the bot with a Say action, and then redirect to a new task called main_collect
. The quiz questions will be asked in main_collect
.
Let's make the main_collect
task now. Go back to Tasks
and click Add a Task
, calling it main_collect
. Normally you would probably want to train this task on samples to trigger it, but because we redirect to it from Greeting
, we don't need to worry about samples. This is a simple bot, it just wants to know what Twilio product you are!
First we add a Collect
to our JSON Actions Bin. This asks a series of questions together in a group. We also provide the name of the Collect
flow (in this case it is what_twilio_product_are_you
) followed by a questions array. Each question has a name and a Validate
instruction to limit what the user can answer with. We use the Validate
attribute allowed_values
to provide an array of possible answers. If this was a quiz where there is only one right answer, it might only have one allowed value.
If the user sends in an answer that's not in allowed_values
, the say
in on_failure
will return "Please send a, b, c, or d!" and repeat the question to the user so they can try again. The maximum number of attempts the user can try to answer a question is three times, and then if they do not send an allowed answer, they are redirected back to the greeting
task.
You can then copy and paste the lines highlighted above for each question you want your quiz-bot to ask. The code below includes three questions but you can find the complete Collect
flow with all fifteen questions in this GitHub Gist:
After the last question, the bot redirects to a Twilio Function to tally up all the answers and calculate the all-important answer to "What Twilio Product are you?"
Calculate the Results with JavaScript in a Twilio Function
Make a new Function by clicking the red plus button in your Functions Console. Select a Blank
template and click Create.
Append /buzzfeedquiz
to the path, then copy the path and paste it in your Autopilot bot JSON bin's on_complete: redirect: uri
. Then add the following JavaScript code to get the memory of each of the fifteen answered questions and create an empty response object that will soon be filled.
Then we calculate which answer (A, B, C, or D) was most common in this clean ES6 manner (with O(n) complexity!)
Finally, we return a message containing which Twilio product the user is most like based on their previous answers to the chatbot.
Now text your Twilio number to partake in your quiz!
You can add more questions, more answer choices, more emojis, or a more complex system of tallying up the answers. With people sheltering at home, now is a great time to create entertaining quizzes for personal or professional use. Twilio Autopilot makes it easy for you to get creative with chatbots and quizzes to engage with customers, fans, friends, and family, or just have fun. Let me know online or in the comments what you're building.
- 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.