Discussions
CPaaS send SMS problem
when i try to send an SMS, CPaaS returns 400 "To phone number is required to send an SMS".
thing is - i have a to: number in my request...
i'm using fetch() in node.js. i know i should include '@avaya/cpaas' and use that API... i have downloaded that package and cribbed the relevant code directly from it though, so my code should work. Anyone see what i've done wrong?
my URI is https://api-us.cpaas.avayacloud.com/v2/Accounts/ACe00011a0467185c8df8f4d768f5d9f7d/SMS/Messages.json and the options (latest try - i've tried variations here):
{
"method": "POST",
"headers": {
"authorization": "Basic UNlMDAwMTFhMDQ2NzE4NWM4ZGY4ZjRkNzY4ZjVkOWY3ZDowMWI2YjdmZjk0ODk0N2MwYmE4OGVmMTg0NDE3ZjBmYg==",
"content-type": "application/x-www-form-urlencoded"
},
"body": "allowMultiple=true&body=a%20reply%20from%20your%20agent....&from=%2B16185740071&statusCallback=https%3A%2F%2Fnick-axp-crm.ue.r.appspot.com%2FSMScarrierHook&statusCallbackMethod=POST&to=%2B19176858541"
}
my code is:
let smsUrl = CPaaS_URL + "/Accounts/" + CPaaS_SID + "/SMS/Messages.json";
let smsBody = {
to: req.body.providerDialogId,
from: CPaaS_Number,
body: req.body.body.elementText.text,
statusCallback: "https://nick-axp-crm.ue.r.appspot.com/SMScarrierHook",
statusCallbackMethod: 'POST',
allowMultiple: true
}
let bodyKeys = Object.keys(smsBody);
bodyKeys.sort();
let bodyVals = [];
bodyKeys.forEach(function (key) {
bodyVals.push(encodeURIComponent(key) + '=' + encodeURIComponent(smsBody[key]));
});
let smsBodyStr = bodyVals.join('&');
let smsOpts = {
method: 'POST',
headers: {
authorization: "Basic " + Buffer.from( CPaaS_SID + ":" + CPaaS_AT, 'utf8').toString("base64"),
'content-type': 'application/x-www-form-urlencoded'
},
body: smsBodyStr
}
console.log( "CPaaS Send: To: " + smsUrl + " Options: " + JSON.stringify( smsOpts, undefined, 4));
let pr = await fetch( smsUrl, smsOpts);
let ob = await pr.json();
console.log( "SMS Send: returned Obj: " + JSON.stringify( ob, undefined, 4));