0
votes

I am trying to build a statuscallback into my web app. I have set up a Twilio function:

exports.handler = function(context, event, callback) {
    let twiml = new Twilio.twiml.VoiceResponse();

    if(event.To) {
      const attr = isAValidPhoneNumber(event.To) ? 'number' : 'client';

      const dial = twiml.dial({
        callerId: event.From,
      });
      dial[attr]({
          statusCallbackEvent: 'completed',
          statusCallback: 'https://pink-dragonfly-9052.twil.io/calls/events',
          statusCallbackMethod: 'POST'
      }, event.To);
    } else {
      twiml.say('Thanks for calling!');
    }

     callback(null, twiml);
};

function isAValidPhoneNumber(number) {
  return /^[\d\+\-\(\) ]+$/.test(number);
}

And I am trying to receive the statuscallback in the backend of my application with:

const Router = require('express').Router;
const router = new Router();

router.get('https://pink-dragonfly-9052.twil.io/calls/events', (req, res) => {
  console.log("requested");
});

But the back-end router does not receive the response. I have set up the 'https://pink-dragonfly-9052.twil.io/calls/events' url in my TwiML as the status callback URL as well.

How do I receive the statuscallback then?

Thanks a lot.

1

1 Answers

1
votes

Twilio developer evangelist here! 👋

On a quick look, your function configuration looks almost correct. As a tip, you'll find debug information in the Twilio console.

Link to Debugger in Twilio console

The thing that could be incorrect is that you're pointing the status callback back to the function iteslf (https://pink-dragonfly-9052.twil.io/calls/events). This should be the URL to your express application. This application has to be publicly available so that the callback can be received.

Using express you have to define the endpoint URL like e.g. your-domain.io/statuscallback and change your function configuration to point to this URL.

statusCallback: 'your-domain.io/statuscallback'

The router definition should be then something along these lines:

router.get('/statuscallback', function (req, res) {
  res.send('Birds home page')
})

In case your application is running only locally you can use a tool like ngrok to make it publicly available.

I hope this helps. :)