Module twilio.listener
![](https://bcentral-packageicons.azureedge.net/images/ballerinax_twilio_2.2.1.png)
ballerinax/twilio.listener Ballerina library
Overview
The Twilio Listener connector allows you to listen to Twilio SMS and Call status change events.
- Listen to incoming message events and message status change callback events from the twilio SMS
- Listen to incoming call events and call status change callback events from the twilio Voice Call
This module supports Twilio Basic API 2010-04-01 version.
Prerequisites
Before using this connector in your Ballerina application, complete the following:
- Create Twilio Account to obtain Account SID, Auth Token and Twilio phone number
- Obtain tokens
-
Create a twilio account and follow this link to obtain a twilio phone number. If you use a trail account, you may need to verify your recipient phone numbers before having any communication with them.
-
Go to this follow this link to obtain twilio Account Auth Token.
-
You need to provide port number where the twilio listener listens for any twilio events requests from Twilio service
-
You also need to provide a callback url before running the listener with suffix "onChange" example: <YOUR_CALL_BACK_URL>/onChange
- Configure the connector with obtained tokens
Quickstart
To use the twilio listener in your Ballerina application, update the .bal file as follows:
Note:
Callback URL registration method depends on the event type.
- Twilio SMS
- Incoming Messages
- Callback webhook URL has to be registered in the console under the particular Twilio number.
- Status change events
- Callback webhook URL has to be registered at the time of sending the SMS (from the client connector)
- Twilio Call
- Incoming Call
- Callback webhook URL has to be registered in the console under the particular Twilio number.
- Status change events
- Callback webhook URL has to be registered at the time of making the call (from the client connector)
Step 1 - Import listener
import ballerinax/twilio.'listener as twilioListener;
Step 2 - Create a new listener instance
configurable string & readonly twilioAuthToken = ?; configurable string & readonly callbackUrl = ?; configurable int & readonly port = ?; listener twilioListener:Listener tListener = new (port, twilioAuthToken, callbackUrl);
Step 3 - Add a service
- Add a service with necessary remote functions as below
service / on tListener { remote function onSmsDelivered(twilioListener:SmsStatusChangeEvent event) returns error? { log:printInfo("Delivered", event); } }
- Use
bal run
command to compile and run the Ballerina program.