With the work i did with Jsoup as part of
I rewrote and simplified the Twilio code:
So now:
//Requires Jsoup in classpath: https://jsoup.org
function sendSms(to, from, body, accountSid, authToken)
{
function buildBase64AuthString(accountSid, authToken)
{
var mergedAuthString = accountSid + ':' + authToken
var base64 = Java.type('javax.xml.bind.DatatypeConverter').printBase64Binary(mergedAuthString.getBytes("UTF-8"))
return base64.toString()
}
with (new JavaImporter(org.jsoup))
{
var doc = Jsoup.connect('https://api.twilio.com/2010-04-01/Accounts/' + accountSid + '/Messages.json')
.method(Java.type('org.jsoup.Connection.Method').POST)
.header('Content-Type', 'application/x-www-form-urlencoded')
.header('Authorization', 'Basic ' + buildBase64AuthString(accountSid, authToken))
.data("To", to,
"From", from,
"Body", body
)
.timeout(30000)
.ignoreContentType(true)
.execute()
var resBody = doc.body()
}
return resBody
}
function spinify(body)
{
var parsed = JSON.parse(body)
var stringified = JSON.stringify(parsed)
var spin = S(stringified)
return spin
}
var twilioSms = sendSms('+1 123-123-1234',
'+1 555-555-5555',
'This is a test message',
'myAccountSid',
'myAuthToken'
)
execution.setVariable('responseBody', spinify(twilioSms))