HI, Questetra users! I’m HATANAKA, Questetra CTO.

This post is the continuation of the last blog post.

Sending a Text Message in the Middle of Workflow Using SMS

In the post, I used a Throwing Message Intermediate Event (HTTP) to realize the API request to Twilio.

I would like to substitute that part with a Script Task daringly.
I will make a Script Task to call “SMS API”.

M230: Auto Executing Complicated Data Processing (ECMAScript)

The Workflow diagram is modified to as the following.

  1. At “Input” Step, destination (phone number) and a message text are entered.
  2. “Send” (auto) Step is a Script Task, and it calls “SMS API”.

There is no change in Data Items. Here, I will show you again.

Data Item Type Required Permission at “Input” Step
Title String type single line No Editable
Phone number String type single line Yes Editable
Regular expression to be satisfied: \+81\d{9,10}
Placeholder: +8175205XXXX
Description:

Please start with the country code (+81).<br>Only numeric without a hyphen.
Text body String type multiple lines Yes Editable
Max Length: 100

Settings of the Script Task. First, other than the script.

Allow to clear the required item. Uncheck
Token will move to Error Boundary Event when processing fails. Uncheck
Script Engine Nashorn

Here is the script, the entire of it.


const accountSID = '[Account ID of Twilio (AccountSID)]';
const authToken = '[token used for accessing API (AUTHTOKEN)]';
const fromPhone = '[The phone number to call]';

const apiUrl = 'https://api.twilio.com/2010-04-01/Accounts/' + accountSID + '/Messages.json';
const toPhone = engine.findDataByName('Phone number');
const message = engine.findDataByName('Text body');

const response = httpClient.begin()
  .basic(accountSID, authToken)
  .formParam('From', fromPhone)
  .formParam('To', toPhone)
  .formParam('Body', message)
  .post(apiUrl);

const responseCode = response.getStatusCode();
const responseBody = response.getResponseAsString();
engine.log("response: " + responseCode);
engine.log(responseBody);

if (responseCode != 201) {
  throw "response is not 201: " + responseCode;
}

I will explain part by part.


const accountSID = '[Account ID of Twilio (AccountSID)]';
const authToken = '[token used for accessing API (AUTHTOKEN)]';
const fromPhone = '[The phone number to call]';

A Script Task cannot retrieve variables that have been configured within the App.
Since there is no choice, the same thing is defined as a variable (constant) at the beginning of the script.
Replace it according to your Twilio account.


const apiUrl = 'https://api.twilio.com/2010-04-01/Accounts/' + accountSID + '/Messages.json';
const toPhone = engine.findDataByName('Phone number');
const message = engine.findDataByName('Text body');

Next, using the API to access, it retrieves the SMS destination phone number and the Text body.
"engine.findDataByName()" is an API for Script Task which is for retrieving the value of Data Item of specified by the name.

[R2301]: Script Data Retrieving / Assignment


const response = httpClient.begin()
  .basic(accountSID, authToken)
  .formParam('From', fromPhone)
  .formParam('To', toPhone)
  .formParam('Body', message)
  .post(apiUrl);

Next, it accesses the API of Twilio using API for Script Task.

[R2300]: Java Classes available in Script Task

Execute the POST request with httpClient.begin() ... post() and return the response.
On the way, it specifies the basic authentication header by basic(), and the parameters in application/x-www-form-urlencoded format to request-body by formParam().


const responseCode = response.getStatusCode();
const responseBody = response.getResponseAsString();
engine.log("response: " + responseCode);
engine.log(responseBody);

if (responseCode !== 201) {
  throw "response is not 201: " + responseCode;
}

The status code and the response body are extracted from the response, and the log is output.
Since 201 response is returned when the request is successfully completed, an exception is thrown so that the script task becomes “processing failure” when it is not 201 response.
engine.log() is an API for log output. It is the same thing as I mentioned in the previous post.

Using Log Output for Debugging of Script Task

That’s all, for App development. An access log like the following figure will be output when accessing to Twilio’s API. A similar log will be output even if it fails.

Next time I would like to work on creating a Service Task (Add-on) based on this Script Task. Well, see you around. Well, see you around.