Build a Pride Astrology SMS Bot with Twilio Studio and Functions

June 30, 2021
Written by
Liz Moy
Twilion

Pride Astrology Twilio Bot

It’s the last day of Pride month! Celebrate with this Pride Astrology app built by Twilio Solutions Engineer Heather Hargreaves. Text hi to 917-540-0961 and get a horoscope based on your astrological sign. You can also find out your celebrity match and receive a lesbian bar to check out based on the #lesbianbarproject.

Read on to find out how Heather built the app using the aztro API, Twilio Studio and Twilio Functions, and text in to find a fun new spot to check out. 🏳️‍🌈

An iphone with a text message exchange between the author and the SMS bot with the texter's horoscope for the day

Prerequisites

  • A Twilio account. If you sign up for a new account through this link, you’ll get an extra $10 in credit when you upgrade.
  • A Twilio phone number. Instructions on how to get one are here.

Set up the Twilio Studio Flow that Sends the SMS

To make a new Twilio Studio flow, log in to your Twilio account and go to the Studio Dashboard. Then, click the blue plus sign and give your flow the name “Pride Astrology.” In the next part of the setup, you will want to scroll down and choose “Import from JSON” from the provided templates.

The part of the Twilio Studio setup wizard where it offers you the option to choose Import from JSON surrounded in a red outlined box

Copy the JSON from this file. Back in the Twilio Studio setup window, delete the empty placeholder brackets, and paste in the JSON copied from the gist. Once you finish the setup, you should see a flowchart like the one below. It looks a bit complex, but you will walk through each step. Hit the Publish button at the top of the flow.

An overview of what the Twilio Studio flow looks like zoomed out so that each widget is visible

Starting from the top, you will greet texters with a message that asks them for their sign. If their message back is a valid astrological sign, then the texter will receive a message that validates their sign was received, and will then move on to a Twilio function that will call the aztro API, a free REST API that provides daily horoscopes and info for sun signs such as mood, lucky color, and compatibility with other signs.

The part of the Twilio Studio flow where it asks the texter to message in their sign


If no valid message is texted in, the texter will receive a message asking if they need help identifying their sign.

Next, you’ll set up a function that calls the aztro API.

Create the Functions that will get Horoscope and Compatibility

In the studio flow, you will see that the next two widgets call two different Twilio Functions. We’ll use Twilio Serverless to set these up.

A screenshot of the Twilio Functions widgets that indicate where you get the list of daily horoscopes and the sign compatibility

Open up the services console and create a new service called pride-astrology. Under settings choose Dependencies. You will need to add axios as a dependency to use it for the API call. Type axios into the module section and click add (this will install the latest version, 0.21.1).

The dependencies section of Twilio Functions where you need to input axios as a dependency

Click the blue Add button and create a function called get-sign. Delete the placeholder code and paste the below code into the function. Hit the blue save button.

exports.handler = function(context, event, callback) {
    const axios = require('axios');
    const sign = event.sign.split(" ")[0];
    console.log("this is the sign", sign)
    let resp;

    axios.post(`https://aztro.sameerkumar.website/?sign=${sign}&day=today`)
    .then(function (response) {
        console.log(response.data);
        resp = response.data;
        return callback(null, resp)
    })
    .catch(function (error) {
        console.log(error);
    });
};

This code takes the sign that the texter provided and makes a POST call to the aztro API to get the texter’s horoscope for that day. It then sends that data back to the studio flow so that it will be saved to the studio flow context and can be passed as a variable to one of your messages. It will also be passed to the next function call.

Now you will create a function to get compatibility. Click the blue Add button and create a function called get-compatibility. Delete the placeholder code and paste the below code into the function. Hit the blue save button.

exports.handler = function(context, event, callback) {
    let compatibility_list = {
        "aries": ["Leo", "Sagittarius"],
        "taurus": ["Virgo", "Capricorn"],
        "gemini": ["Libra", "Aquarius"],
        "cancer": ["Scorpio", "Pisces"],
        "leo": ["Aries", "Sagittarius"],
        "virgo": ["Taurus", "Capricorn"],
        "libra": ["Gemini", "Aquarius"],
        "scorpio": ["Cancer", "Pisces"],
        "sagittarius": ["Aries", "Leo"],
        "capricorn": ["Taurus", "Virgo"],
        "aquarius": ["Gemini", "Libra"],
        "pisces": ["Cancer", "Scorpio"]
    }
    
    const sign = event.sign.toLowerCase().split(" ")[0];
    let compatible_signs = {};
    for (const property in compatibility_list) {
      console.log(`${property}: ${compatibility_list[property]}`);
      if(sign === property) {
        compatible_signs.list = compatibility_list[property]
        compatible_signs.list = compatible_signs.list.join(', ')
        console.log(compatible_signs)
        return callback(null, compatible_signs);
      }
    }
   
};

You will use a JavaScript object with a map that Heather wrote out with the sign compatibility (but no cheating to change your compatibility options here).

Set both of your Functions to public and click deploy all. Now you will go back to the Studio flow to configure your Functions into the proper widgets.

Add in the Variables with Data from the Functions

Click into the configuration panel of the get_horoscope widget by clicking the widget in the flow. Then from the panel, choose the service you created from the dropdown. Choose ui as the environment, and then choose /get-sign as the function. Do the same process for the get_compatibility widget.

The configuration panel of the Twilio Functions widgets indicating where you can choose the function you just created from the dropdown menu

Now your flow will connect to both of those functions. The next step is to check the message body in the send_horoscope widget configuration to send the texter the data returned from the functions. You will do so using the liquid templating language and the variables saved in the studio flow context.

If you imported the JSON in the first few steps, the message body should be populated like the below in the send_horoscope widget. You may change it to make it your own or add additional emojis. Note that you must use the curly braces with the syntax below in order to retrieve the data properly.

The message body of the send message widget that includes headings for the day's horoscope, mood, and compatibility, and uses the templating language to pass in the data

Now you will add in some functionality to send back the texter’s celebrity match and a lesbian bar option.

Add in Function to get Celebrity Matches and Lesbian Bars

The texter will receive the option to find out their celebrity match and a lesbian bar from The Lesbian Bar Project.

If they respond “yes” then there will be one final call to a Twilio function to get their match.

A screenshot of the section of the studio flow where a texter can opt in to see their celebrity match

Navigate back to the Services console and click the blue Add button to create a function called get-matches. Copy the code in this file and paste it into the function. Set it to public and hit save.

The code in this function will take the list of your compatible astrological matches and will then choose a celebrity with one of those signs from a JavaScript object.

When you’re ready, hit deploy all. Then repeat the same process of going back to the studio flow and choosing get-matches as the function from the dropdown.

Lastly, check out the final messages and then connect your flow to a Twilio phone number.

The final messages let the texter know who their ideal match is and a suggestion of a bar that they could check out together (romantic!) It’s also a great way to let more people know about The Lesbian Bar Project.

A screenshot of the end of the flow where the user receives their match and gets a link to the Lesbian Bar Project

The last step is to connect the flow to the Twilio phone number that you purchased. Navigate to the Twilio phone number console and from the dropdown for when a message comes in, choose your studio flow.

The messaging configuration in the phone numbers console where you may choose the studio flow from the dropdown

Now text into the number and experience pride astrology magic!

A screenshot of an iphone with the conversation between the texter and the bot

Conclusion

To learn more about other LGBTQ+ causes to support you can check out this post. And we would love to hear from you if you built this app yourself, or if it inspired you to build something else so please reach out and let us know.

Heather Hargreaves is a Solutions Engineer and member of the LGBTQ+ Twilio Spectrum ERG. You can find her at hhargreaves [at] twilio.com

Liz Moy (she/her) is a queer Developer Evangelist who loves building fun apps with Twilio. You can find her at lmoy [at] twilio.com or on Twitter @ecmoy.