Wanted to share a proof of concept that I had put together:
In other examples such as: Send SMS through Service Task (http-connector) and Twilio (Script Based), we send SMS messages with twilio, but there was never the ability to receive a response back from the recipient of the SMS. Until now!
So consider the following scenario: You need to ask a question to a recipient and have them provide you some response. A more concrete example could be that you are looking to find out the number of free beds at a shelter, someone counts the number of beds and responds to the SMS with the number of free beds.
So we end up with the following:
So the overall flow is:
- Start the “Get Feedback” process and include the
phoneNumber
process variable. - Twilio SMS is sent, and we wait for a message to come back to engine.
- In Twilio we have it setup so that every SMS that is sent to our Twilio phone number, calls a Twilio “Function”; that function runs a web service call to the
/message
Rest End-point of Camunda. - The “SMS Capture” process captures the SMS message from Twilio, and runs a script that correlates the received SMS back to the “Get Feedback” process to the “Receive Response” receive task.
- If the Response is not received within 1 minute, we send a reminder message, every minute for 5 minutes.
- Once response is received, we send a confirmation SMS which is essentially a echo back to the recipient to confirm we received their message / count of the number of available beds.
Here is a more complex example:
So how does it all work:
For the twilio SMS, we use the Jsoup code we outline in: https://forum.camunda.io/t/send-sms-through-service-task-http-connector-and-twilio-script-based.
In Twilio we setup the the following:
Setup a twilio phone number that supports SMS and having people send SMS’ to that number.
In Twilio setup a function: Twilio | Login
In the function configure the following script:
exports.handler = function(context, event, callback) {
var got = require('got');
var requestPayload = {
"messageName" : "sms",
"processVariables" : {
"smsData" : {"value" : JSON.stringify(event), "type": "Json"}
},
"resultEnabled" : true
};
got.post('http://CAMUNDA_URL_GOES_HERE/engine-rest/message',
{ body: JSON.stringify(requestPayload),
headers: {
'accept': 'application/json',
'content-type': 'application/json'
},
json: true
}).then(function(response) {
console.log(response.body)
callback(null, response.body);
}).catch(function(error) {
callback(error)
});
};
This script creates a simple POST request to the camunda engine to the /message
endpoint, and takes the event
variable in twilio and passes it into camunda.
We use the From
property as Message Number that we correlate against.
The event
variable in twilio looks something like this:
{
"ToCountry": "CA",
"ToState": "QC",
"SmsMessageSid": "SMa2f94d80c31edb524746e25462b9ef90",
"NumMedia": "0",
"ToCity": "LACHINE",
"FromZip": "",
"SmsSid": "SMa2f94d80c31edb524746e25462b9ef90",
"FromState": "ON",
"SmsStatus": "received",
"FromCity": "OTTAWA",
"Body": "This is my Test Message",
"FromCountry": "CA",
"To": "+15555555555",
"ToZip": "",
"NumSegments": "1",
"MessageSid": "SMa2f94d80c31edb524746e25462b9ef90",
"AccountSid": "ACfc89cbc4febdd9958087bfb8c389968e",
"From": "+14444444444",
"ApiVersion": "2010-04-01"
}
Next in the Twilio Console in the Phone Number config (Twilio | Login), you set the number to call the Function when a SMS is received to your Twilio phone number:
Then we Start the “Get Feedback” process.
The Process is setup with:
the following:
The “Correlate SMS with Waiting Feedback” uses the following Javascript:
var smsData = execution.getVariable('smsData')
var phoneNumberRaw = smsData.prop('From').value()
var smsBodyResponse = smsData.prop('Body').value()
var correlation = execution.getProcessEngineServices()
.getRuntimeService()
.createMessageCorrelation(phoneNumber)
.setVariable('smsBodyResponse', smsBodyResponse)
.correlateWithResult();
And the Receive Response task has a Message name of ${phoneNumber}
.
We used the phone number as the correlation key because it was the unique id for the “conversation”.
Another way to have multiple conversations with a single recipient would be have add a “key” to the SMS responses.
Example:
- “What is the current bed count?”
- “23”
vs
- “convo123: What is the current bed count?”
- “convo456: What is the current number of people in line?”
- “convo123: 15”
- “convo456: 40”
Would be interested in use cases that come to mind by other forum members!