sandudstest/twilio

0.99.9

Connects to Twilio from Ballerina.

Module Overview

The Twilio connector allows you to send SMS, voice, OTP, and WhatsApp messages through the Twilio REST API. You can also send user secrets via SMS or voice message, verify OTP, and add and delete users. It handles basic authentication.

WhatsApp messaging requires users to use predefined message templates. Templates are message formats, which can be used over and over again to message users. Use of templates enables to maintain high-quality content as well as to avoid spam messages. However, this requires the WhatsApp message templates to be predefined.

Basic Operations

The ballerinax/twilio module contains operations to get the Twilio account details, send SMS, send WhatsApp messages, and make voice calls.

Authy Operations

The ballerinax/twilio module contains operations to get Authy app details, add a user, delete a user, get user status, get user secret, request OTP via SMS, request OTP via call, and verify OTP.

Compatibility

Version
Ballerina LanguageSwan Lake Alpha 5
Twilio Basic API2010-04-01
Twilio Authy API Versionv1

Sample

First, import the ballerinax/twilio module into the Ballerina project.

Copy
import ballerinax/twilio;

Obtaining Tokens to Run the Sample

  1. Visit Twilio and create a Twilio Account.
  2. Obtain the following credentials from the Twilio dashboard:
    • Account SId
    • Auth Token
    • X Authy API Secret

You can now enter the credentials in the Twilio endpoint configuration.

Copy
twilio:TwilioConfiguration twilioConfig = {
    accountSId: config:getAsString("ACCOUNT_SID"),
    authToken: config:getAsString("AUTH_TOKEN")
};

twilio:Client twilioClient = new(twilioConfig);

The sendSMS remote function sends an SMS to a given mobile number from another given mobile number with the specified message.

Copy
var details = twilioClient->sendSms(fromMobile, toMobile, message);
if (details is  twilio:SmsResponse) {
    // If successful, print SMS Details.
    io:println("SMS Details: ", details);
} else {
    // If unsuccessful, print the error returned.
    io:println("Error: ", details);
}

The addAuthyUser remote function adds an Authy user with the given email address, phone number, and country code.

Copy
var details = twilioClient->addAuthyUser(email, phone, countryCode);
if (details is  twilio:AuthyUserAddResponse) {
    // If successful, print Authy user Details.
    io:println("Authy user Details: ", details);
} else {
    // If unsuccessful, print the error returned.
    io:println("Error: ", details);
}

The requestOtpViaSms remote function sends an OTP SMS to the mobile number of the given user ID.

Copy
var details = twilioClient->requestOtpViaSms(userId);
if (details is  twilio:AuthyOtpResponse) {
    // If successful, print OTP SMS Details.
    io:println("OTP SMS Details: ", details);
} else {
    // If unsuccessful, print the error returned.
    io:println("Error: ", details);
}

Import

import sandudstest/twilio;Copy

Metadata

Released date: almost 3 years ago

Version: 0.99.9

License: Apache-2.0


Compatibility

Platform: java11

Ballerina version: slalpha5

GraalVM compatible: Yes


Pull count

Total: 20

Current verison: 1


Weekly downloads


Source repository


Keywords

twilio

sms

call

otp

whatsapp

webhook


Contributors

Other versions

1.0.0

0.99.9