How to Send a Romantic Poem Over the Phone with Twilio CLI

February 09, 2021
Written by
Diane Phan
Twilion
Reviewed by

header - How to Send a Romantic Poem Over the Phone with Twilio CLI

Do you ever listen to Soulja Boy's song "Kiss Me Thru the Phone" and wonder, "How can I kiss someone through the phone?"

Whether you have or not, you're probably looking for new creative ways to express your feelings to your loved ones this Valentine's Day.

With the help of Twilio and your command line, you can deliver a romantic Valentine's Day poem over the phone to your gal pals, best buddies, a special someone, or to yourself! With a project as fun as this, it's hard to restrict yourself from using it on only one day out of the year.

In this article, you'll be using TwiML and the Twilio CLI to call a phone number and deliver a poem.

Tutorial requirements

Set up the Twilio CLI environment

If this is your first time installing the Twilio CLI, check out the Twilio CLI quickstart page for instructions on how to install it on your machine.

For developers using a Mac, it's very convenient and easy to install the CLI using Homebrew, so make sure you check out how to install Homebrew on your machine if you don’t have it already. Then, you can run the command brew tap twilio/brew && brew install twilio to install the Twilio CLI.  

For Windows developers, make sure you install npm first, as you will need it to install the CLI with the command npm install twilio-cli -g.

Once you have the Twilio CLI installed, type twilio login to log in to your account. You’ll need your Account SID and Auth Token from the Twilio Console to login.

Create a Twilio Bin

TwiML Bins work well with Twilio Functions because they help you explore Twilio's capabilities to the fullest. By using Twilio's Markup Language (TwiML), you save yourself the hassle of having to write complex code or connect your own web server in order to execute a simple call.

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.

On the Twilio Console, click on the (...) icon and scroll down to the Runtime section. Select TwiML Bins. Click on the red (+) sign to create a new TwiML Bin.

Give the TwiML Bin a friendly name such as "valentinesdaygram". In the next step, you’ll populate the TwiML text box with the script for the voice recording that you will hear when you call the number.

Before you move on further, take some time to meet Alice. Alice is a talented friend of Twilio, known as a text-to-speech (TTS) engine that converts text into a human-sounding voice. Thus, Alice is the perfect way to deliver the Valentine’s Day poem. Alice is capable of speaking in 26 total dialects and offers the voice of a man or a woman.

For now, we'll have Alice speak in their default voice and recite some verses from Eavan Boland's poem, "Code", attributed to Grace Murray Hopper. However, feel free to change the XML to whatever you please. Copy and paste the following XML into the text box, replacing anything already inside, and click the Create button at the bottom of the page:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Say voice="alice" language="en-US">
        Maker of the future, if the past
        is fading from our view with the light
        outside your window and the single file
        of elements and animals, and all the facts
        of origin and outcome, which will never find
        their way to you or shelter in your syntax--

        it makes no difference to us.
        We are still human.  There is still light
        in my suburb and you are in my mind--
        head bowed, old enough to be my mother--
        writing code before the daylight goes.
        I am writing at a screen as blue,
        as any hill, as any lake, composing this
        to show you how the world begins again:
        One word at a time.
        One woman to another.
</Say>
</Response>

You should be able to see the properties of your TwiML bin at the top of the page. Leave this page open as you'll need to use the TwiML URL in the next section.

screenshot of TwiML Bin properties and configuration

Send yourself a romantic poem with Twilio  

Take a moment to test out the TwiML bin by making a call to your personal phone number. To do this, run the following command from your command line, taking care to replace the placeholder values with your own. Take the URL from the TwiML Bin page and set that as the value for the url line.

Replace the from value with your Twilio phone number in E.164 format such as "+19140201400". For the time being, use your personal phone number in E.164 format for the to value so that the call is directed to your device.

twilio api:core:calls:create --from <YOUR_TWILIO_NUMBER> --to <YOUR_PHONE_NUMBER> --url <YOUR_TWIML_BIN_URL>

In addition, you can also run a cURL command if you prefer. Replace the values accordingly to execute the call successfully:

curl -X POST \
    --data-urlencode "Url=<YOUR_TWIML_BIN_URL>" \
    --data-urlencode "To=<YOUR_PHONE_NUMBER>" \
    --data-urlencode "From=<YOUR_TWILIO_NUMBER>" \
    "https://api.twilio.com/2010-04-01/Accounts/<YOUR_ACCOUNT_SID>/Calls" \
    -u "<YOUR_ACCOUNT_SID>:<YOUR_AUTH_TOKEN>"

gif of pink anime cell phone with bunnies

Wow, what a beautiful and powerful poem!

Is Alice's voice not swoon worthy enough for you? No worries, as I mentioned earlier, Alice is capable of speaking 26 different dialects and can change to a man or a woman.

Let's experiment with the TwiML voices in the next section.

Change Alice's TwiML Voice

Go back to the valentinesdaygram TwiML Bin on the Twilio Console. Look at the documentation for TwiML Voices to see all of the dialects you can choose from.

Let's make Alice sound more like a woman by specifying her voice to woman. Replace the TwiML Bin with the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Say voice="woman" language="en-US">
        Maker of the future, if the past
        is fading from our view with the light
        outside your window and the single file
        of elements and animals, and all the facts
        of origin and outcome, which will never find
        their way to you or shelter in your syntax--

        it makes no difference to us.
        We are still human.  There is still light
        in my suburb and you are in my mind--
        head bowed, old enough to be my mother--
        writing code before the daylight goes.
        I am writing at a screen as blue,
        as any hill, as any lake, composing this
        to show you how the world begins again:
        One word at a time.
        One woman to another.
</Say>
</Response>

If you want to send a more lighthearted yet relevant poem, here's another snippet you can use:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
    <Say voice="man" language="en-US">
        Roses are Red, Violets are Blue. Unexpected '{' on line 32.
    </Say>
</Response>

Click the Save button at the bottom and rerun the commands above to execute the call.

Send a poem over text message

If you feel like going the extra mile for that someone special, consider sending a text message along with the phone call! Run the following command in your command line and expect a poem in your text message inbox:

twilio api:core:messages:create \
  --from "<YOUR_TWILIO_NUMBER>" \
  --to "<YOUR_PHONE_NUMBER>" \
  --body "Roses are Red, Violets are Blue. Unexpected '{' on line 32."

Wait a minute… then check your phone!

gif of pink cell phone from an anime with text that says "incoming..."

Who said knowing how to use computers can't be romantic?

What's next for sending romantic poems over the phone with Twilio?

Congratulations on using Twilio to send a poem over phone and text! As you can see, not only is coding fun, but it can be used to impress your loved ones with a surprise they surely weren't expecting. Just make sure they don't ignore the call ;)

There's so much you can accomplish with the Twilio CLI to help you build fast and test out the power of Twilio. Check out the examples on the docs to learn more about quick commands you can use on the Twilio CLI.

Now that you know the basics of making a call with Twilio CLI, you can try to expand on the project by sending a poem to a list of contacts. Here are some other cool projects you can explore:

Let me know how you're sharing the love in February this year 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.