The following .js code doesn’t seem to result in any AMD callbacks. I’ve used PostMan to call /amdCallback with the appropriate value keys (i.e. ‘human’, ‘fax’, etc)… and that works correctly (prints the appropriate .logs)… but running ‘/makeCall’ alone (through PostMan or my Android app) doesn’t seem to create any AMD callbacks… I’m fairly certain it’s just something simple I’m missing… all my other callback work perfectly.
const express = require('express');
const bodyParser = require('body-parser');
const twilio = require('twilio');
const app = express();
const port = process.env.PORT || 3000;
app.use(bodyParser.urlencoded({ extended: true })); //true?
app.use(bodyParser.json());
const accountSid = 'AC3fqwertyuiopasdfghjkl2f';
const authToken = '7f13qwertyuiopasdfghjkl34';
const client = twilio(accountSid, authToken);
//make a call
app.post('/makeCall', (req, res) => {
client.calls
.create({
machineDetection: "Enable", // Use "DetectMessageEnd"? Still no amdcallback
asyncAmd: false,
asyncAmdStatusCallback: 'https://sheltered-garden-1234.herokuapp.com/amdCallback',
asyncAmdStatusCallbackMethod: "POST",
twiml: '<Response><Say voice="alice">Please wait while we connect your call...</Say></Response>',
to: req.body.to,
from: req.body.from,
statusCallback: 'https://sheltered-garden-1234.herokuapp.com/status-callback',
statusCallbackEvent: ["initiated", "ringing", "answered", "completed"],
statusCallbackMethod: "POST"
})
.then(call => res.send(`Call initiated with SID: ${call.sid}`))
.catch(err => res.status(500).send(err.message));
});
//status callback
app.post('/status-callback', (req, res) => {
console.log('Status Callback Received:'); // Log here works as expected
console.log('Event: ', req.body.CallStatus);
console.log('Call SID: ', req.body.CallSid);
console.log('From: ', req.body.From);
console.log('To: ', req.body.To);
res.status(200).send('Status callback received');
});
//AMD callback
app.post('/amdCallback', (req, res) => {
console.log('Received AMD Callback:', req.body); //nothing shows up (except with POSTMAN)
const answeredBy = req.body.AnsweredBy;
console.log('AMD Result: ', answeredBy);
switch(answeredBy) {
case 'human':
console.log('Call answered by a human.');
break;
case 'machine_start':
console.log('Call answered by an answering machine.');
break;
case 'fax':
console.log('Call answered by a fax machine.');
break;
default:
console.log('Unknown response.');
break;
}
res.sendStatus(200);
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
I’ve tried testing with PostMan which worked… admittedly I’m new to web application… but apart from this issue it’s been plain sailing.