lid

ColdFusion Twilio Rest API Integration

In this blog, we will show you how to send SMS in your ColdFusion web application using Twilio Rest API. Here is the exposition of integration process and attached the sample code to send the SMS to user phone.

Before you can receive phone calls and send messages, you'll need to sign up for a Twilio account and purchase a Twilio phone number. You can try below the link to login or create a new account. You can use this link to create a new account.

We need to login to admin console in twilio to generate the Auth_Token & Account_SID & Twilio_Number.

twilio credential console

Login into the Twilio console panel and go to the 'Number' menu, click on it. Here you can see the 'Get Number' and click on it. If you don't currently own a Twilio phone number with SMS capabilities, you'll need to buy one. After navigating to the Buy a Number page, check the 'SMS' box and click 'Search'. For the security and avoid spamming reasons, twilio doesn't want to "spoof" other people's mobile phone numbers. So far they only allow your "From" number to be assigned by the twilio. There you will see the list available phone number here. Twilio will generate you the new phone to use via API.

To send a new outgoing message, make an HTTP POST to this Messages list resource URI

If you want to send messages while in trial mode, you must first verify your 'To' phone number with Twilio. You can verify your phone number by adding it to your Verified Caller IDs in the console. When creating a new message via the API, you must include the 'To' parameter. This value should be either a destination phone number or a Channel address. You also need to pass a Body or MediaUrl containing the message's content.

When we posting to the twilio rest API server, all of the HTTP request must be authenticated along with the your registered SID and password as the auth token as the api credentials and this check will happened for every HTTP request.

When Twilio receives your request to send an SMS via the REST API, it will check that you have included a valid Twilio phone number in the form field. Twilio will then either queue the SMS or return this HTTP error in its response to your request. We can check the error details using these Twilio response

If your request did not produce any of the errors, then twilio's HTTP response will include the SID of the new message. A sample of the twilio response might look like this,

Lest start with the coldfusion code.

In the application.cfc, have created the object for Twilio service using an 'onApplicationStart' method. The Twilio service component is available inside the 'com' folder. For authenticating the Twilio API, passed the twilioAccountSID and twilioAuthToken in the init function. For sending an SMS we need call the 'send SMS' function with from mobile number(which is generated by the Twilio console) to the mobile number( you need to provide user mobile number) and body of the message to receive the user.

In the sendSMS.cfm we just call the sendSMS() to send a new SMS to the users. Here we passed the 'toPhone' that's receiver user phone number, 'fromPhone ' which is generated by the Twilio and 'bodyOfSMS' included the text content to send a user. For example, I just passed hardcode values as in the parameter. We can create the form to send the dynamic messages for the users.

In order to make calls or send messages through the Twilio API, you need to get a Twilio phone number. In the below section we are going to see how the calls can be made via Twilio. Below we can see the code to access the function in twilio to make twilio call.

The below code exlains how we are going to make a phone call. its very similar code to the sendSMS(). Instead we need to call makecall() function in the above code which accepts from and to numbe as the required parameter. Then using that we are going to access the Twilio url to make calls. Code is mentioned below.




lid