2

When I call from my local number to twilio number, the webhook makes a POST request /place-call and then ends with fall back error url.

Twilio is unable to process the Content-Type of the provided URL. Please see Twilio's documentation on accepted content types for more information on valid Content-Types. You must return a Content-Type for all requests. Requests without a Content-Type will appear in the Debugger as a 502 Bad Gateway error.

place-call.js file

const callerNumber = '+1XXXXXXXXXX';
const callerId = 'client:alice';
const defaultIdentity = 'alice';

exports.handler = function (context, event, callback) {
  var url = 'https://' + context.DOMAIN_NAME + '/incoming';

  const client = context.getTwilioClient();

  var to = event.to;
  if (!to) {
    to = event.To;
    if (!to) {
      console.error("Could not find someone to call");
      to = null;
    }
  }
  //  console.log("To: " + JSON.stringify(event));

  if (!to) {
    client.calls.create({
      url: url,
      to: 'client:' + defaultIdentity,
      from: callerId,
    }, function (err, result) {
      // End our function
      if (err) {
        callback(err, null);
      } else {
        callback(null, result);
      }
    });
  } else if (isNumber(to)) {
    console.log("Calling number:" + to);
    client.calls.create({
      url: url,
      to: to,
      from: callerNumber,

    }, function (err, result) {
      // End our function
      if (err) {
        console.log("Error: " + err);
        callback(err, null);
      } else {
        callback(null, result);
      }
    });
  } else {
    client.calls.create({
      url: url,
      to: 'client:' + to,
      from: callerId,
    }, function (err, result) {
      // End our function
      if (err) {
        callback(err, null);
      } else {
        callback(null, result);
      }
    });
  }
};

function isNumber(to) {
  if (to.length == 1) {
    if (!isNaN(to)) {
      console.log("It is a 1 digit long number" + to);
      return true;
    }
  } else if (String(to).charAt(0) == '+') {
    number = to.substring(1);
    if (!isNaN(number)) {
      console.log("It is a number " + to);
      return true;
    };
  } else {
    if (!isNaN(to)) {
      console.log("It is a number " + to);
      return true;
    }
  }
  console.log("not a number");
  return false;
}

This is the error from the error log:

Twilio is unable to process the Content-Type of the provided URL. Please see Twilio's documentation on accepted content types for more information on valid Content-Types. You must return a Content-Type for all requests. Requests without a Content-Type will appear in the Debugger as a 502 Bad Gateway error.

incoming.js file

//'https://' + context.DOMAIN_NAME + '/incoming'

exports.handler = function(context, event, callback) {
  const twiml = new Twilio.twiml.VoiceResponse();
  twiml.say("Congratulations! You have received your first inbound call! Good bye.");

  callback(null, twiml.toString());
};
  • It sounds like the POST request returns a 5XX status code and fails therefore. Can you post the server code and some context here? – IObert Aug 14 '23 at 07:29
  • Error Log: An 11200 error is an indicator of a connection failure between Twilio and your service Request Log: place-call returns 200 OK hello-world returns 200 OK (This is the fallback url) – Syed Muzammil Ali Aug 14 '23 at 13:30
  • Is your webhook returning valid TWIML? Is the OK payload empty? That would cause the call to disconnect. – jassent Aug 14 '23 at 15:28
  • @IObert I have posted the server code. – Syed Muzammil Ali Aug 15 '23 at 13:43
  • @jassent I think the payload is empty can you please help me set the payload. – Syed Muzammil Ali Aug 15 '23 at 16:25
  • Please provide the code you are using here: 'https://' + context.DOMAIN_NAME + '/incoming'. This appears to be the function that is causing the 5XX error status code. – jassent Aug 15 '23 at 22:44
  • The problem is that you don't return TwiML at all. You create calls manually via the SDK functions, to initiate a call that is already in progress. The callback is triggered by this phone call and now you need to supply the instructions what should happen in this call. I'm not totally sure what your use case is but right now you try to create a new call to the original caller with alice as the sender. – IObert Aug 16 '23 at 07:58
  • @jassent I added incoming.js code, 'https://' + context.DOMAIN_NAME + '/incoming'. – Syed Muzammil Ali Aug 16 '23 at 16:03
  • @IObert I don't find any example on how to return twiml, can you please provide some resource or support in this regard. – Syed Muzammil Ali Aug 16 '23 at 16:04
  • 1
    This area of the documentation shows how to create Twiml using a variety of SDKs/frameworks: https://www.twilio.com/docs/voice/twiml?code-sample=code-say-hello-to-an-inbound-caller&code-language=Node.js&code-sdk-version=4.x – jassent Aug 16 '23 at 16:17

0 Answers0