This content originally appeared on Twilio Blog and was authored by Diane Phan
Who says you can't be romantic during a quarantine? Although it's fun to send flowers, chocolates, and gifts to your loved ones for Valentine's Day, you can also have fun sending a virtual Valentine gram as well!
With the help of Twilio Functions, you can deliver a romantic Valentine's Day gram 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 JavaScript, TwiML, and Twilio Functions to call a phone number and deliver a Valentine's Day message.
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 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 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". The TwiML text box will contain the script for the voice recording that you will hear when you call the number.
Before we move on further, let's take some time to meet our new friend alice
. Alice is a talented friend of Twilio also 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 message. 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 normal voice and recite a line that Leslie Knope said in Parks and Recreation. However, feel free to change the XML to whatever you please. Copy and paste the following XML into the text box and click the Create button at the bottom of the page:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say voice="alice">Happy Valentine's Day! You are a rainbow infused space unicorn!</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.
Create a Twilio Function
In this tutorial, you will be executing the TwiML Bin serverlessly through Twilio Functions. Open another tab on your browser and 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 "valentinegram". Complete the setup by clicking the Next button.
Click on the blue Add + button and select Add Function to build out the Twilio Function. Let's call the function "make_calls" and paste the following JavaScript code:
exports.handler = function(context, event, callback) {
console.log(event);
const from = <'YOUR_TWILIO_NUMBER'>;
const to = <'YOUR_PHONE_NUMBER'>;
const twilioClient = context.getTwilioClient();
const url = <'YOUR_TWIML_BIN_URL>;
return twilioClient.calls
.create({ url, from, to })
.then((result) => {
console.log('Call successfully placed');
return callback(null, 'Valentine gram successfully delivered!');
})
.catch((error) => {
console.log(error);
return callback(error);
});
};
Remember to replace the twilioClient
with the URL from the TwiML Bin page and the from
const 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 as well to test out the function.
The code uses the twilioClient
and is able to make an outbound call by using the calls.create
method. Twilio makes an HTTP request to the URL parameter provided and retrieves the TwiML instructions to make the call. Once this function is executed, your Twilio number makes a call to your personal phone number.
Change the make_calls
function to Public by clicking on the dropdown icon next to Protected.
Finish off this Twilio Function by clicking Save and Deploy All at the bottom of the dashboard.
Send yourself a Valentine's Day gram with Twilio Functions
Take a moment to test out the Twilio Function by taking the unique URL at the bottom of your Twilio Function and appending make_calls
to the end of it so that it looks like http://valentinegram-XXXX.twil.io/make_calls
. Enter it in the address bar of your browser and you should see the success message "Valentine gram successfully delivered!" on the page.
At the same time, your phone should ring! Pick it up, you rainbow infused space unicorn.
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.
If you want to surprise yourself or the person you're sending a Valentine's Day gram with a romantic French line, you can replace the TwiML Bin with the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say voice="man" language="fr-FR">Joyeuse saint Valentin! tu es ma baguette!</Say>
</Response>
Click the Save button at the bottom and enter your same unique Twilio Functions URL to make the call.
Oo la la!
Play around with the TwiML bin and see what kind of Valentine's Day gram you want to send out to your friends! Perhaps you can serenade someone by pasting the lyrics to a song or reciting a poem.
What's next for sending Valentine's Day Grams with Twilio Functions?
Congratulations on using Twilio to send a Valentine's Day gram! 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 ;)
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 making a call with Twilio Functions, you can try to expand on the project by sending a Valentine's Day gram to a list of contacts. Here are some other cool projects you can explore:
- Build a Galentine's Day Compliment Generator with Python, Flask, and Twilio
- Last minute Valentine’s Day Gifts delivered via Twilio Studio
- Build a Google Nest call notification system using Twilio
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.
This content originally appeared on Twilio Blog and was authored by Diane Phan
Diane Phan | Sciencx (2021-02-07T21:53:19+00:00) Send a Valentine’s Day Gram with Twilio Functions. Retrieved from https://www.scien.cx/2021/02/07/send-a-valentines-day-gram-with-twilio-functions/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.