Awesome
Ballerina Twilio Connector
Overview
Twilio is a cloud communications platform that allows software developers to programmatically make and receive phone calls, send and receive text messages, and perform other communication functions using its web service APIs.
The Ballerina Twilio connector supports the Twilio Basic API version 2010-04-01, enabling users to leverage these communication capabilities within their Ballerina applications.
Setup guide
Before using the ballerinax-twilio connector you must have access to Twilio API, If you do not have access to Twilio API please complete the following steps:
Step 1: Create a Twilio account
Creating a Twilio account can be done by visiting Twilio and clicking the "Try Twilio for Free" button.
Step 2: Obtain a Twilio phone number
All trial projects can provision a free trial phone number for testing. Here's how to get started.
Notice: Trial project phone number selection may be limited. You must upgrade your Twilio project to provision more than one phone number, or to provision a number that is not available to trial projects.
- Access the Buy a Number page in the Console.
- Enter the criteria for the phone number you need, and then click Search.
- Country: Select the desired country from the drop-down menu.
- Number or Location: Select the desired option to search by digits/phrases, or a specific City or Region.
- Capabilities: Select your service needs for this number.
- Click Buy to purchase a phone number for your current project or sub-account.
Notice: Many countries require identity documentation for Phone Number compliance. Requests to provision phone numbers with these regulations will be required to select or add the required documentation after clicking Buy in Console. To see which countries and phone number types are affected by these requirements, please see twilio's Phone Number Regulations site.
Step 3: Obtain the Twilio account SID with API key and API secret
You can find API keys related information under API keys & tokens section in your Twilio account. If you do not have an API key and a secret, please complete the following steps:
- Access the API keys & tokens page in your Twilio account, and then click on <b>Create API key</b>.
- Enter the criteria for the API Key you need, and then click <b>Create</b>.
- <b>Friendly name:</b> Enter a human-readable name for your API key. It helps you identify the key later, especially if you have multiple API keys.
- <b>Region:</b> Enter the geographical region where the API key will be used.
- <b>Key type:</b> There are different types of API keys you can create,
- <b>Standard:</b> Provides access to all Twilio API functionalities except for managing API keys and configuring accounts and subaccounts.
- <b>Main:</b> Offers the same access as standard keys but also allows managing API keys and configuring accounts and subaccounts.
- <b>Restricted:</b> Allows fine-grained access to specific Twilio API resources, enabling you to set permissions for different API endpoints
- Save the API key SID and the Secret in a safe place to use in your application.
Important: This secret is only shown once. Please make note of it and store it in a safe and a secure location.
- In order to obtain the Account SID of your Twilio account, you can visit your Twilio console.
Quickstart
To use the twilio
connector in your Ballerina application, modify the .bal
file as follows:
Step 1 - Import the module
Import the Twilio module into your Ballerina program as shown below:
import ballerinax/twilio;
Step 2 - Create a new connector instance
To create a new connector instance, add a configuration as follows (You can use configurable variables to provide the necessary credentials):
configurable string apiKey = ?;
configurable string apiSecret = ?;
configurable string accountSid = ?;
twilio:ConnectionConfig twilioConfig = {
auth: {
apiKey,
apiSecret,
accountSid
}
};
twilio:Client twilio = check new (twilioConfig);
Step 3 - Invoke the connector operation
Invoke the sending SMS operation using the client as shown below:
public function main() returns error? {
twilio:CreateMessageRequest messageRequest = {
To: "+XXXXXXXXXXX", // Phone number that you want to send the message to
From: "+XXXXXXXXXXX", // Twilio phone number
Body: "Hello from Ballerina"
};
twilio:Message response = check twilio->createMessage(messageRequest);
// Print the status of the message from the response
io:println("Message Status: ", response?.status);
}
Step 4: Run the Ballerina application
Execute the command below to execute the Ballerina application:
bal run
Examples
The Twilio connector comes equipped with examples that demonstrate its usage across various scenarios. These examples are conveniently organized into three distinct groups based on the functionalities they showcase. For a more hands-on experience and a deeper understanding of these capabilities, we encourage you to experiment with the provided examples in your development environment.
- Account management
- Create a sub-account - Create a subaccount under a Twilio account
- Fetch an account - Get details of a Twilio account
- Fetch balance - Get the balance of a Twilio account
- List accounts - List all subaccounts under a Twilio account
- Update an account - Update the name of a Twilio account
- Call management
- Make a call - Make a call to a phone number via a Twilio
- Fetch call log - Get details of a call made via a Twilio
- List call logs - Get details of all calls made via a Twilio
- Delete a call log - Delete the log of a call made via Twilio
- Message management
- Send an SMS message - Send an SMS to a phone number via a Twilio
- Send a Whatsapp message - Send a Whatsapp message to a phone number via a Twilio
- List message logs - Get details of all messages sent via a Twilio
- Fetch a message log - Get details of a message sent via a Twilio
- Delete a message log - Delete a message log via a Twilio
Issues and projects
The Issues and Projects tabs are disabled for this repository as this is part of the Ballerina library. To report bugs, request new features, start new discussions, view project boards, etc., visit the Ballerina library parent repository.
This repository only contains the source code for the package.
Build from the source
Prerequisites
-
Download and install Java SE Development Kit (JDK) version 17. You can download it from either of the following sources:
Note: After installation, remember to set the
JAVA_HOME
environment variable to the directory where JDK was installed. -
Download and install Ballerina Swan Lake.
-
Download and install Docker.
Note: Ensure that the Docker daemon is running before executing any tests.
-
Export Github Personal access token with read package permissions as follows,
export packageUser=<Username> export packagePAT=<Personal access token>
Build options
Execute the commands below to build from the source.
-
To build the package:
./gradlew clean build
-
To run the tests:
./gradlew clean test
-
To build the without the tests:
./gradlew clean build -x test
-
To run tests against different environments:
./gradlew clean test -Pgroups=<Comma separated groups/test cases>
The following test environments, along with their respective groups of compatible tests, are available to test the connector.
Groups Environment mock_tests Mock server for Twilio API (Default Environment) live_tests Twilio API To run tests against these environments, you can follow this Test Guide.
-
To debug the package with a remote debugger:
./gradlew clean build -Pdebug=<port>
-
To debug with the Ballerina language:
./gradlew clean build -PbalJavaDebug=<port>
-
Publish the generated artifacts to the local Ballerina Central repository:
./gradlew clean build -PpublishToLocalCentral=true
-
Publish the generated artifacts to the Ballerina Central repository:
./gradlew clean build -PpublishToCentral=true
Contribute to Ballerina
As an open-source project, Ballerina welcomes contributions from the community.
For more information, go to the contribution guidelines.
Code of conduct
All the contributors are encouraged to read the Ballerina Code of Conduct.
Useful links
- For more information go to the Twilio package.
- For example demonstrations of the usage, go to Ballerina By Examples.
- Chat live with us via our Discord server.
- Post all technical questions on Stack Overflow with the #ballerina tag.