diff --git a/integrator-default-profile/connectors/twilio_connector_sample/automation.bal b/integrator-default-profile/connectors/twilio_connector_sample/automation.bal index 3b601284..16cd59f3 100644 --- a/integrator-default-profile/connectors/twilio_connector_sample/automation.bal +++ b/integrator-default-profile/connectors/twilio_connector_sample/automation.bal @@ -3,7 +3,23 @@ import ballerinax/twilio; public function main() returns error? { do { - twilio:Message twilioMessage = check twilioClient->createMessage({To: "+15551234567", From: "+15550000000", Body: "Hello from WSO2 Integrator!"}); + // initiate call from twilio + twilio:Call twilioCall = check twilioClient->createCall({ + To: toNumber, + From: fromNumber, + Url: twimlUrl, + StatusCallback: statusCallbackUrl, + StatusCallbackMethod: "POST", + StatusCallbackEvent: ["initiated", "ringing", "answered", "completed"] + }); + string? callSid = twilioCall?.sid; + log:printInfo("Call initiated", sid = callSid ?: ""); + + // initiate a message from twilio + twilio:Message twilioMessage = check twilioClient->createMessage({To: toNumber, From: fromNumber, Body: "Hello from WSO2 Integrator!"}); + string? messageSid = twilioMessage?.sid; + log:printInfo("Message sent", sid = messageSid ?: ""); + } on fail error e { log:printError("Error occurred", 'error = e); return e; diff --git a/integrator-default-profile/connectors/twilio_connector_sample/config.bal b/integrator-default-profile/connectors/twilio_connector_sample/config.bal index 560bb785..0fd8eed4 100644 --- a/integrator-default-profile/connectors/twilio_connector_sample/config.bal +++ b/integrator-default-profile/connectors/twilio_connector_sample/config.bal @@ -1,3 +1,7 @@ - configurable string accountSid = ?; configurable string authToken = ?; +configurable string fromNumber = ?; +configurable string toNumber = ?; +configurable string statusCallbackUrl = ?; +configurable string twimlUrl = ?; +configurable string callMessage = "Hello! This is a call from WSO2 Integrator."; diff --git a/integrator-default-profile/connectors/twilio_connector_sample/main.bal b/integrator-default-profile/connectors/twilio_connector_sample/main.bal index 8b137891..3b31b423 100644 --- a/integrator-default-profile/connectors/twilio_connector_sample/main.bal +++ b/integrator-default-profile/connectors/twilio_connector_sample/main.bal @@ -1 +1,20 @@ +import ballerinax/trigger.twilio; +import ballerina/http; +import ballerina/log; +configurable int port = 8090; +configurable int twimlPort = 8091; + +listener twilio:Listener twilioListener = new (port); + +// Serves TwiML so Twilio knows what to say when the call is answered +// ngrok must forward to localhost:8091 for this endpoint +service /twiml on new http:Listener(twimlPort) { + resource function post voice() returns http:Response { + http:Response twimlResponse = new; + twimlResponse.setHeader("Content-Type", "application/xml"); + twimlResponse.setPayload(string `${callMessage}`); + log:printInfo(`TwiML ${callMessage} response sent`); + return twimlResponse; + } +} \ No newline at end of file diff --git a/integrator-default-profile/connectors/twilio_trigger_sample/main.bal b/integrator-default-profile/connectors/twilio_trigger_sample/main.bal index 42e6b3c5..e76a4ed0 100644 --- a/integrator-default-profile/connectors/twilio_trigger_sample/main.bal +++ b/integrator-default-profile/connectors/twilio_trigger_sample/main.bal @@ -1,79 +1,87 @@ -import ballerina/log; import ballerinax/trigger.twilio; +import ballerina/log; -listener twilio:Listener twilioListener = new (listenOn = listenerPort); +listener twilio:Listener twilioListener = new (listenerPort); -service twilio:SmsStatusService on twilioListener { - remote function onAccepted(twilio:SmsStatusChangeEventWrapper event) returns error|() { - do { - } on fail error err { - // handle error - return error("unhandled error", err); - } - } - - remote function onQueued(twilio:SmsStatusChangeEventWrapper event) returns error|() { - do { - } on fail error err { - // handle error - return error("unhandled error", err); - } - } - - remote function onSending(twilio:SmsStatusChangeEventWrapper event) returns error|() { - do { - } on fail error err { - // handle error - return error("unhandled error", err); - } - } - - remote function onSent(twilio:SmsStatusChangeEventWrapper event) returns error|() { - do { - } on fail error err { - // handle error - return error("unhandled error", err); - } - } - - remote function onFailed(twilio:SmsStatusChangeEventWrapper event) returns error|() { - do { - } on fail error err { - // handle error - return error("unhandled error", err); - } - } - - remote function onDelivered(twilio:SmsStatusChangeEventWrapper event) returns error|() { - do { - } on fail error err { - // handle error - return error("unhandled error", err); - } - } - - remote function onUndelivered(twilio:SmsStatusChangeEventWrapper event) returns error|() { - do { - } on fail error err { - // handle error - return error("unhandled error", err); - } - } - - remote function onReceiving(twilio:SmsStatusChangeEventWrapper event) returns error|() { - do { - } on fail error err { - // handle error - return error("unhandled error", err); - } - } - - remote function onReceived(twilio:SmsStatusChangeEventWrapper event) returns error|() { - do { - log:printInfo(event.toJsonString()); - } on fail error err { - // handle error - return error("unhandled error", err); - } +// Call status service +service twilio:CallStatusService on twilioListener { + + remote function onQueued(twilio:CallStatusEventWrapper event) returns error? { + log:printInfo("Call queued", callSid = event.CallSid ?: ""); + } + + remote function onRinging(twilio:CallStatusEventWrapper event) returns error? { + log:printInfo("Call ringing", callSid = event.CallSid ?: ""); + } + + remote function onInProgress(twilio:CallStatusEventWrapper event) returns error? { + log:printInfo("Call in progress", callSid = event.CallSid ?: ""); + } + + remote function onCompleted(twilio:CallStatusEventWrapper event) returns error? { + log:printInfo("Call completed", + callSid = event.CallSid ?: "", + duration = event.CallDuration ?: ""); + } + + remote function onBusy(twilio:CallStatusEventWrapper event) returns error? { + log:printInfo("Call busy", callSid = event.CallSid ?: ""); + } + + remote function onFailed(twilio:CallStatusEventWrapper event) returns error? { + log:printInfo("Call failed", callSid = event.CallSid ?: ""); + } + + remote function onNoAnswer(twilio:CallStatusEventWrapper event) returns error? { + log:printInfo("Call no answer", callSid = event.CallSid ?: ""); + } + + remote function onCanceled(twilio:CallStatusEventWrapper event) returns error? { + log:printInfo("Call canceled", callSid = event.CallSid ?: ""); } } + +// SMS status service +service twilio:SmsStatusService on twilioListener { + + remote function onAccepted(twilio:SmsStatusChangeEventWrapper event) returns error? { + log:printInfo("SMS accepted", messageSid = event.MessageSid ?: ""); + } + + remote function onQueued(twilio:SmsStatusChangeEventWrapper event) returns error? { + log:printInfo("SMS queued", messageSid = event.MessageSid ?: ""); + } + + remote function onSending(twilio:SmsStatusChangeEventWrapper event) returns error? { + log:printInfo("SMS sending", messageSid = event.MessageSid ?: ""); + } + + remote function onSent(twilio:SmsStatusChangeEventWrapper event) returns error? { + log:printInfo("SMS sent", messageSid = event.MessageSid ?: ""); + } + + remote function onFailed(twilio:SmsStatusChangeEventWrapper event) returns error? { + log:printInfo("SMS failed", messageSid = event.MessageSid ?: ""); + } + + remote function onDelivered(twilio:SmsStatusChangeEventWrapper event) returns error? { + log:printInfo("SMS delivered", messageSid = event.MessageSid ?: ""); + } + + remote function onUndelivered(twilio:SmsStatusChangeEventWrapper event) returns error? { + log:printInfo("SMS undelivered", messageSid = event.MessageSid ?: ""); + } + + remote function onReceiving(twilio:SmsStatusChangeEventWrapper event) returns error? { + log:printInfo("SMS receiving", messageSid = event.MessageSid ?: ""); + } + + remote function onReceived(twilio:SmsStatusChangeEventWrapper event) returns error? { + string fromNumber = event.From ?: ""; + string msgBody = event.Body ?: ""; + log:printInfo("SMS received", + messageSid = event.MessageSid ?: "", + 'from = fromNumber, + body = msgBody); + } +} \ No newline at end of file