This content originally appeared on Twilio Blog and was authored by Dotun Jolaoso
As humans, we generally tend to celebrate anniversaries in the life of a loved one. Examples of such anniversaries can include weddings, birthdays, or job anniversaries. And the frequency at which they occur varies, from yearly, to quarterly and even monthly for some of them.
In this tutorial, we’ll be building a NodeJS app that allows you to celebrate special anniversaries in the life of a loved one using Twilio Programmable Voice.
Prerequisites
To follow along, you’ll need the following:
- A free Twilio account and a Twilio phone number.
- Node.js version 14.16 or above is installed on your machine.
Getting Started
First, create a directory where the project will reside. From the terminal, run the following command:
mkdir twilio_celebrate_anniversary
Next, change into the project directory and run the following command to create a new package.json file in the root of your project directory.
npm init -y
Next, install all the dependencies the project will need. These are:
- dotenv: A library for importing environment variables from a
.env
file. - twilio-node: A Twilio helper library for interacting with Twilio’s REST API.
- moment: A Javascript date library for working with dates.
- node-cron: A library that allows you to schedule task in Node.js
Run the following command to install all of the dependencies at once:
npm install dotenv moment node-cron twilio
Set up Twilio
You will need to obtain the Account SID, Auth Token and phone number associated with your Twilio account. These details can be obtained from the Keys & Credentials section on your Twilio Dashboard.
Head back to the project’s directory and then create a file named .env. Then, add the following details to the file:
TWILIO_ACCOUNT_SID=xxxx
TWILIO_AUTH_TOKEN=xxxx
TWILIO_NUMBER=xxxx
PARTNER_NUMBER=xxxx
Don’t forget to replace "xxxx" with their corresponding values. The PARTNER_NUMBER
field is the phone number of your partner or loved one you’ll be placing a phone call to.
Create an anniversary list
For the purpose of this tutorial, you’ll use a simple JSON file to keep track of all the important anniversaries you need to celebrate with your loved one. In your project directory, create a new file named anniversary.json, and paste the following into the file:
[
{
"anniversary": "birthday",
"day": "2023-09-27",
"frequency": "yearly",
"message": "Happy birthday babe. I love you"
},
{
"anniversary": "wedding anniversary",
"day": "2023-09-28",
"frequency": "yearly",
"message": "Happy wedding anniversary to my beautiful wife. Here's wishing us many more wedding anniversaries to celebrate"
},
{
"anniversary": "job anniversary",
"day": "2022-10-27",
"frequency": "monthly",
"message": "Happy job anniversary to my beautiful wife. I love you"
}
]
Each anniversary entry contains the following details:
anniversary
: The title of the particular anniversary you’re celebrating.day
: The date for which this anniversary occurs.frequency
: The celebration frequency for the anniversary, which can bemonthly
oryearly
. Later down the line, the value of this field will be used to update the anniversary's date.message
: The anniversary message you would like to be said to your partner during the voice call.
You can modify the contents of the anniversary.json file to suit your own needs.
Creating Outbound Voice Calls
In the root of your project’s directory, create a new file named app.js and paste the following code into the file:
require("dotenv").config();
const fs = require("fs/promises");
const moment = require("moment");
const cron = require("node-cron");
const accountSid = process.env.TWILIO_ACCOUNT_SID;
const authToken = process.env.TWILIO_AUTH_TOKEN;
const twilioNumber = process.env.TWILIO_NUMBER;
const client = require("twilio")(accountSid, authToken);
const toNumber = process.env.PARTNER_NUMBER;
async function fetchAnniversaries() {
return JSON.parse(await fs.readFile("./anniversary.json"));
}
async function writeAnniversaries(data) {
return await fs.writeFile("./anniversary.json", JSON.stringify(data));
}
async function celebrateAnniversary() {
const anniversaries = await fetchAnniversaries().catch(err => console.log(err));
for (const [key, anniversary] of anniversaries.entries()) {
const today = moment().utc().format("YYYY-MM-DD");
const anniversaryDay = moment(anniversary.day).format("YYYY-MM-DD");
if (anniversaryDay === today) {
await placeCall(anniversary);
//After making the call, update the anniversary date
anniversary.day = updateAnniversaryDay(anniversary);
anniversaries[key] = anniversary;
}
}
writeAnniversaries(anniversaries).catch((err) => console.log(err));
}
async function placeCall(anniversary) {
client.calls
.create({
twiml: `<Response><Say>${anniversary.message}</Say></Response>`,
to: toNumber,
from: twilioNumber,
})
.then((call) => console.log("Call SID:",call.sid))
.catch((err) => console.log(err));
}
function updateAnniversaryDay(anniversary) {
const days = moment().utc();
if (anniversary.frequency === "monthly") {
return days.add(moment.duration({ days: 30 })).format("YYYY-MM-DD");
}
if (anniversary.frequency === "yearly") {
return days.add(moment.duration({ days: 365 })).format("YYYY-MM-DD");
}
}
At the top of this file, all the dependencies the project will need are imported.
The require("dotenv").config()
function loads the environment variables from the .env file you created earlier.
The celebrateAnniversary()
function fetches all the anniversaries available in the anniversary.json file. Next, it loops through each of them. If there’s a match with an anniversary date and the current date, the placeCall()
function is invoked, and then subsequently, the anniversary day is updated based on the anniversary’s frequency.
The fetchAnniversaries()
and writeAnniversaries()
functions are used to read from and write to the anniversary.json file respectively.
The placeCall()
function is responsible for making the voice call to your loved one. A predefined message will speak to the receiver using the Twilio Markup Language (TwiML). At its core TwiML is an XML document with special tags defined by Twilio. The message to speak to them is obtained from the message
attribute on the anniversary
object.
Automate the application with Cron
Next, you're going to automate app.js to run everyday. Just below the updateAnniversaryDay()
function, add the following function:
cron.schedule("0 8 * * *", async () => {
console.log("Celebrating an anniversary");
try {
await celebrateAnniversary()
} catch (error) {
console.log(error);
}
});
Using the node-cron library which you installed earlier, you’ve defined a Cron schedule that calls the celebrateAnniversary()
function everyday at 8am.
Testing
To test that the app works as expected, you’ll need to make a few changes.
- Update any of the anniversaries in the anniversary.json file to reflect the current date
- Update the
PARTNER_NUMBER
environment variable to be your own, personal, phone number so you can receive the phone call - Update the Cron schedule in app.js to run every two minutes, by updating the call to
cron.schedule
to match the code below:
cron.schedule("*/2 * * * *", async () => {
console.log("running a task");
try {
await celebrateAnniversary()
} catch (error) {
console.log(error);
}
});
Finally, run the following command to start the application:
node app.js
Within two minutes, an outbound phone call should be placed to your number with the anniversary message being read to you.
Conclusion
In this tutorial, using Twilio Voice, we’ve seen how you can build an application that celebrates special anniversaries in the life of a loved one. This tutorial can serve as a starting point for you, if you’re looking to build something special for a loved one using any of Twilio’s product offerings such as Messaging, Video, etc.
Dotun is a backend software engineer who enjoys building awesome tools and products. He also enjoys technical writing in his spare time. Some of his favorite programming languages and frameworks include Go, PHP, Laravel, NestJS and Node.
Website: https://dotunj.dev/
Github: https://github.com/Dotunj
Twitter: https://twitter.com/Dotunj_
Image Credits
- “Anniversary Cake. 3-tier anniversary cake” by Angie Chapman (used in the background of the tutorial's main image) is licensed under CC BY-ND 2.0
- "birthday cake" by waferboard (used in the background of the tutorial's main image) is licensed under CC BY 2.0
This content originally appeared on Twilio Blog and was authored by Dotun Jolaoso
Dotun Jolaoso | Sciencx (2022-12-08T11:01:43+00:00) Celebrate Anniversaries With Your Loved One Using Twilio Voice Calls. Retrieved from https://www.scien.cx/2022/12/08/celebrate-anniversaries-with-your-loved-one-using-twilio-voice-calls/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.