Setting up Twilio Function for Sending Text and Whatsapp Messages

Search for a command to run...

No comments yet. Be the first to comment.
Several times, I have been involved in starting a project/feature and realising that there exist issues that are yet to be discussed, either with external teams, other departments, or the relations team of a third-party provider. This has made me spe...

The SOLID principles are a set of five principles, amongst others, guiding how software is designed to be less fragile, less rigid, more testable, maintainable, and readable. It influences how we write code in such a way that future changes do not th...

GraphQL is a query language and a runtime. As a query language, unlike SQL(Structured Query Language), it never interacts with a database. It speaks with the API directly about what it needs and a server-side program that runs your queries using the ...
I recently installed Bun on my windows system. This article is a step-by-step guide on the process involved in doing so. It also includes the challenges I went through to ensure a successful installation. I will assume you have wsl on your local mach...
Disclaimer: This write-up is not written to tell you which is better or which will come out victorious. Instead, the writer seeks to help you understand what they are and the inherent differences between the two of them. The internet today is at an a...
The joy of having a scalable, secure messaging service, while not bothering about infrastructure. Allowing you to move fast from writing business logic to production is what Twilio function offers. Today, we will look at the step by step process of setting up Twilio functions for sending Text and Whatsapp messaging. We will be creating two functions. The first one will send Whatsapp messages and the second one will send text messages.




After inputting the name and clicking on the Next button. It creates a service.
We next add a function to the service by clicking on the add button at the top left side of the screen.

After setting up a function we need to configure it to send Whatsapp messages. The following steps below explain how to do it.
Copy URL option.WHEN A MESSAGE COMES IN field.let twiml = new Twilio.twiml.MessagingResponse();
twiml.message('Hello World');

To test what has been done so far, we need to go to step five(5) of sending Whatsapp message above to get the Whatsapp number and sandbox name.
On your device open your Whatsapp application and send join to the number to get it connected.
Send any message to the Whatsapp number and get the hello world message specified in twiml.message('Hello World');.
To send text messages we need to set up a new function. On creating the new function, we set the necessary environment variable and visibility of the function.
To set environment variables we need to follow this few steps:


Every function by default has their visibility set to "Protected" which means we need to have a valid Twilio signature to our request headers to invoke the function. For this tutorial, we will be using the "Public" option that allows us to invoke the function from the browser. To set the visibility of a function we:

We go back to the function block, delete every code in it, make the function asynchronous and add a try-catch block.
exports.handler = async function(context, event, callback) {
try {
catch(error) {
}
}
Next we:
const twilioClient = context.getTwilioClient();
Set the variables to, from and body using the event parameter or a pre-defined value.
let from = event.From;
let to = event.To;
let body = event.Body;
await twilioClient.messages
.create({
body,
to,
from,
});
Add the return statement both in the try block and also the catch block.
return callback(null, 'success'); to the try block and return callback(error); to the catch block.
The code will finally look like.
exports.handler = async function(context, event, callback) {
try {
const twilioClient = context.getTwilioClient();
let from = event.From;
let to = event.To;
let body = event.Body;
await twilioClient.messages
.create({
body,
to,
from,
});
return callback(null, 'success');
} catch(error) {
return callback(error);
}
}
To test the SMS sending we
Copy URL option.?From=+15095550100&To=+15105550100&Body=Hello%20World
It is important to note that if you are using a trial account. From will be your trial phone number found on your dashboard. To will be the number used in creating the Twilio account or any other phone number you add to it. If you have made it this far, thanks. We looked at how to set up Twilio functions to send SMS and Whatsapp messages. How it does not need installing Twilio in your codebase and allows your messaging service to run outside your application. Drop your comments and feedback in the comment section and click on the like button. Thanks.