28Apr
send-receive-sms-in-apex-using-twilio-api
By: Cody Reandeau On: April 28, 2022 In: APEX Developer Solutions Comments: 0

Has your manager or client ever asked, “And then can we send someone a text message?” or “Could they just text us the response, and we’ll put it into the database?”

If you are not sure how to start integrating the sending and receiving of SMS messages into your Oracle APEX application, then the Twilio API might be just what you’re looking for!

In today’s blog post, I’ll walk through the steps to get your APEX application sending and receiving SMS messages.

Step 1: Create a Twilio account

Step 2: Send your first SMS

Step 3: Integrate Twilio and APEX

Step 4: Use a Webhook to receive SMS responses

Step 1: Create a Twilio account

Setting up a Twilio account is easy, simply go to https://www.twilio.com/try-twilio and fill in the required fields.

You will receive a confirmation email with a link to confirm your email. Follow the link provided in the email, type in a preferred phone number that can receive SMS messages, and verify that number. Once you’ve confirmed your number, you can then access your console.

There will be instructions on how to get a free phone number on your console home page. Note that this is a trial account at this point, and you only get one free phone number. You can upgrade to a paid account at any time, allowing you to have more phone numbers. For more information on your trial account, go here.

Note that on your Twilio Console homepage, you will see three items under “Account Info”:

  • Account SID – Account String Identifier to uniquely identify your Twilio account.
  • Auth Token – Authentication token to authenticate API requests.
  • My Twilio phone number – Your free Twilio phone number.

These three items will be needed when making your REST API calls.

Step 2: Send your first SMS

Luckily, Twilio provides us with some Postman Collections for Twilio so we can test requests. You can get started with postman collections here. After the Twilio collection is in Postman, we can send our first SMS!

Under the Twilio collection, look for the request called “Post Send an SMS.”

Select this, head over to the “Params” tab and enter your Account SID.

Next, head over to the “Authorization” tab and enter the Username (Account SID) and Password (Auth Token).

Next, head over to the “Body” tab and enter some “Body” text, the “From” phone number, and the “To” phone number.

Now click the “Send” button, and you should have received your first SMS from Twilio, congrats!

Step 3: Integrate Twilio and APEX

But how do we convert this to use it in an Oracle APEX application? My preferred method is to look at the curl snippet generated by Postman after the request has been made. If you head over to the right pane on Postman, you should see a symbol like this “</>” Here, you can check out code snippets in multiple languages (You can also check out code snippets in the Twilio API documentation). The curl snippet should look something like this (Notice line 3 here. You will need to copy the token value that immediately follows “Basic,” which Twilio generates):

Great, now let’s turn this into packaged PL/SQL procedure and utilize apex_web_service:

Get the source code here: https://github.com/codyreandeau/Twilio-Oracle-APEX/blob/main/twilio.pkb

And there you have it! From here, we can call this procedure from anywhere in our APEX application.

Step 4: Use a Webhook to receive SMS responses

Twilio uses Webhooks to handle replies to users. If you go to your Twilio console, you can set up a webhook for your Twilio phone number:

We can set up a simple ORDs endpoint in APEX and use that as our webhook and use TwiML (“Twilio Markup Language,” which is Twilio’s XML messaging format) to send the response back to the user.

A couple of things to note:

  • The webhook needs to be visible from Twilio. So, either the link to the webhook needs to be publicly available, or you need to use an API Gateway of some sort (which is generally preferable).
  • When it comes to securing your webhook, Twilio offers different methods.

Get the source code here: https://github.com/codyreandeau/Twilio-Oracle-APEX/blob/main/twilio_webhook.sql

Notice the “From,” “To,” and “Body” parameters in the above code snippet. These are a few of the many request parameters Twilio provides for your webhook. Make sure you also define these parameters in your endpoint: 

Now, when a user responds to an SMS message, they will get a response back!
As you can see, integrating SMS messaging into an APEX application can be done quickly and easily using Twilio. So, the next time someone asks, “Can we SMS with APEX?” you can immediately respond with, “I already know how to do that!”

Happy APEXing,

Cody

Share this:
Share

Leave reply:

Your email address will not be published. Required fields are marked *