2
votes

We are building an IVR and have multiple numbers per city for various campaigns. For example: Metro, Bus, Google, Facebook, Website and etc.

When a call is coming in, we're creating a lead on Salesforce with the number, but we are missing the campaign.

While having access to CallFrom number {{trigger.call.From}} and the CallTo number {{trigger.call.To}} we would like to access the number's friendly name as well.

This Friendly name of the number holds the campaign, is there a way to access it so it'll be sent out with the rest of the info?

Thanks

2

2 Answers

2
votes

Twilio developer evangelist here.

When you receive an incoming call from Twilio you are only sent the request parameters in the documentation which do not include the friendly name of your number.

You could fetch that number by using a Twilio Function as part of your Studio flow. You'll need to pass the from number as an argument to the Function by setting it as a parameter in the widget settings, like this:

Set a Function Parameter called "To" and fill it with <code>{{trigger.call.To}}</code>

As you can see we are using the data trigger.call.To which is the incoming phone number.

Then, your Function would look something like this:

exports.handler = function(context, event, callback) {
  const phoneNumber = event.To;
  const client = context.getTwilioClient();

  client.incomingPhoneNumbers.list(
    {
      phoneNumber: phoneNumber
    },
    (err, data) => {
      if (err) {
        return callback(err);
      }
      const response = { FriendlyName: data.incomingPhoneNumbers[0].friendlyName };
      callback(null, response);
    }
  );
};

That would return the data to your flow and you would be able to access it later in the flow as {{widgets.MY_WIDGET_NAME.parsed.FriendlyName}}.

Let me know if that helps at all.

1
votes

Bit of an old post, just in case this can help someone...

First off, thank you Phil for showing me the way. I had to slightly modify your code to get it to work with the 3.x Twilio Client API, and I added the phoneNumber itself as a fallback value. This version is "known good" and working for me as of date of this post:

exports.handler = function(context, event, callback) {
  const phoneNumber = event.To;
  const client = context.getTwilioClient();

  client.incomingPhoneNumbers.list(
    {
      phoneNumber: phoneNumber
    },
    (err, data) => {
      if (err) {
        return callback(err);
      }
      console.log("data = " + data);
      var fName;
      if (data.length) {
          fName = data[0].friendlyName;
      }
      else {
          fName = phoneNumber;
      }
      const response = { FriendlyName: fName };
      callback(null, response);
    }
  );
};