Sending a Text Message in the Middle of Workflow Using SMS – Script Task version

Hi Questetra users! I’m Hatanaka, Questetra’s CTO.

This post is a continuation of the last blog post.

Sending a Text Message in the Middle of Workflow Using SMS

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

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

M230: Auto Executing Complicated Data Processing (ECMAScript)

The Workflow diagram will be modified to the following.

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

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

Data Item Name Data-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 except for the actual script.

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

Here is the entire script.


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 that was accessed, it retrieves the SMS destination phone number and the text body.
engine.findDataByName() is an API for a Script Task which is for retrieving the value of a Data Item that is specified by 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 Twilio’s API using the API for the 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 using basic(), and the parameters in the application/x-www-form-urlencoded format in the request-body using 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 a 201 response is returned when the request is successfully completed, an exception is thrown so that the Script Task outputs a processing failure when it is not 201 response.
engine.log() is an API for logging 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 the App development. An access log like the following figure will be output when accessing 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.

1 thought on “Sending a Text Message in the Middle of Workflow Using SMS – Script Task version”

  1. Pingback: Sending a Text Message in the Middle of Workflow Using SMS – Service Task Add-on version – Questetra Support

Comments are closed.

Discover more from Questetra Support

Subscribe now to keep reading and get access to the full archive.

Continue reading

Scroll to Top