Build a Fortune-Telling Bot for Lunar New Year with Twilio Functions
Time to read: 4 minutes
To celebrate the Lunar New Year, Twilio Evangelist Lizzie Siegle hosted a stream on her Twitch channel to talk about fun stories and traditions that each culture celebrates. Each year, an animal from the Zodiac calendar not only determines your fortune for the year, but also aspects of your life such as career, compatibility, family, and more. This year, it's all about the Tiger.
Whether you celebrate Lunar New Year or not, finding out your fortune for a new year can be interesting! Thus, Lizzie and I did a live pair programming session where we built and deployed an app for users to text in their zodiac animal and receive their fortune for 2022.
We used Twilio Functions to deliver advice and good fortune for the year to your friends and family in a simple and quick text. Since Lunar New Year happens every year, there's no reason not to reuse this project yearly and make it a new tradition to read your fortune through an SMS bot.
In this article, you'll be using TwiML and Twilio Functions to create a bot that tells you your fortune for the year after texting in your zodiac animal.
Tutorial requirements
- A free or paid Twilio account. If you are new to Twilio get your free account now! (If you sign up through this link, Twilio will give you $10 credit when you upgrade.)
- Some prior knowledge in JavaScript or a willingness to learn.
Create a Twilio Function
In this tutorial, you will write code in the Twilio Console and then deploy the project serverlessly through Twilio Functions. In your browser head to the Twilio Console. Click on the (...) icon and scroll down to the Runtime section. Select Functions.
Now that you're on the Twilio Functions dashboard, click on the blue Create Service button and give your service a name such as "newyear-zodiac". Complete the setup by clicking the Next button.
To those who are not familiar with the Zodiac animals, each year is represented by an animal based off of the myth of 12 animals racing to the emperor in order to figure out the order of the zodiac calendar. Each animal has their own characteristics and stories on how they perform during the race. In fact, just like the astrological horoscopes, everyone has an animal associated with the year they were born.
For the Twilio Function, we wrote a series of if/else statements for a unique fortune associated with each of the 12 animals on the zodiac calendar. There are plenty of fun predictions for the new year depending on the animal, so you can look up your own fortunes, or use the ones provided below.
Click on the blue Add + button and select Add Function to build out the Twilio Function. Let's call the function "lny" and paste in the following JavaScript code:
After you are satisfied with the fortunes you'll be sharing, click on the blue Save button. Look down at the bottom left hand corner and click on Deploy All so that you can configure the webhook with the Twilio phone number.
Configure Twilio Functions to your phone number
Click on the (...) icon and select Phone Numbers under Super Network.
If you haven't done so already, search for and purchase a Twilio phone number from the console. Make sure that the phone number you choose is set to the same country or region of your personal number to avoid international fees when you pick up calls from the number.
Select an Active Number that you would like to host the fortune-telling bot on. Scroll down to the Messaging section and configure the phone number with the newly created webhook you just deployed.
Set the section A message comes in to "Function". For Service, find the Twilio Function you just created, also known as "newyear-zodiac". Select "ui" for Environment and set the Function Path to "/lny".
You can refer to the screenshot below to make sure you are on the right track:
Click on the blue Save button at the bottom once you are done.
Find out what your zodiac animal says about your 2021 year!
Take your SMS enabled mobile device and text in your zodiac animal to your Twilio number. Lizzie and I are rats so our year was last year. Here was our fortune:
Zodiac animal fortunes can be interesting food for thought but it's fun to reflect and predict what the new year will look like! At least we can say, 2021 will definitely be better than last year… right?
What's next for building a fortune telling bot with Twilio Functions?
Congratulations on using Twilio to send a text message with a fortune for new years! As you can see, the code can definitely be reused for the coming years. All you have to do is conduct a little research to figure out how the year will go for everyone depending on their zodiac animal. You can even have a little mischievous fun and make up your own predictions for them.
There's no reason not to make it a tradition to revive this bot for every Lunar New Year!
Plus, Twilio Functions is a nifty tool to help you build and deploy your creative projects fast. All you need to do is write a bit of TwiML and JavaScript to bring your ideas to life.
Now that you know the basics of sending an SMS with Twilio Functions, you can try to expand on the project by sending a different fortune each day or build a similar bot for another holiday. Here are some other cool projects you can explore:
- Send a Valentine's Day Gram with Twilio Functions
- Send a Romantic Poem Over the Phone with Twilio CLI
- Explore last minute Valentine’s Day Gifts delivered via Twilio Studio
We hope that you have a safe and happy new year filled with joy and prosperity!
Let me know how you feel about your fortune for 2021 or any cool technologies you're looking forward to exploring by reaching out over email.
Diane Phan is a Developer for technical content on the Twilio Voices team. She loves to help beginner programmers get started on creative projects that involve fun pop culture references. She can be reached at dphan [at] twilio.com or LinkedIn.
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.