Friday, 19 February, 2021 UTC


Summary

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 Ox.
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 2021.
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:
exports.handler = function(context, event, callback) { let twiml = new Twilio.twiml.MessagingResponse(); const inbMsg = event.Body.toLowerCase().trim(); if(inbMsg == "ox") { twiml.message('You will not live it up this year. Play the long-horn game. Patience, young padawan.'); } else if (inbMsg == "tiger") { twiml.message("Do not doze off. You will host people. You will get money back, getting back what you put in."); } else if (inbMsg == "dragon") { twiml.message("You will not be slayed this year, nor will you sit on the throne. You will move around either for work or play.") } else if (inbMsg == "snake") { twiml.message("Good fortune will come from hidden sources or from those who do not show their true colors. Good will could come from far away. Be patient.") } else if (inbMsg == "horse") { twiml.message("Small irritations will come from others, not you. If things continue on the path they are on, focus on not annoying people"); } else if (inbMsg == "goat") { twiml.message("Do not stick your neck out. This is a year of change, and good things take time. Do not take things personally.") } else if(inbMsg == "monkey") { twiml.message("There is no alarm clock in your cosmic charts this year. You need to take initiative.") } else if(inbMsg == "rooster") { twiml.message("Unfold your wings. Flex your thighs. Get some sun. Fly off, rise higher as other plebeians stay below. Check your hubris too.") } else if(inbMsg == "dog") { twiml.message("The ox is in your way. Dogs will run around you, hating on you. It's ok, stars will shine on you, turning small misfortunes into benefits. "); } else if(inbMsg == "pig") { twiml.message("You might be forgiven for thinking there are 2 zodiacs: one with 11 animals, and 1 with you here. They have not forsaken you. You will rely on yourself this year: small gains are possible and will come through hard work."); } else if(inbMsg == "rat") { twiml.message("Do not spend an insane amount of money on friends. If you invest, returns will be slow. Stay patient. A little effort on your part, and this year will be better than last.") } else { twiml.message("Text in your zodiac animal."); } return callback(null, twiml); }; 
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.