0

I want to fordward a call to a Studio Flow after the agent in flex hangs up so a CSAT survey can play for the user. I created a plugin that calls a function inside Twilio but there is a "Error - 11200" after the forwarding is done.

I replaced the hang up action so it redirects the call to a function in twilio. The function is supossed to send the call to a flow that will play the survey. I suspect the problem has to do with authentication but I can't find much about it.

I'm fairly new to twilio, so any help will be greatly appreciated

This is the part of the plugin that calls the function:

flex.Actions.replaceAction("HangupCall", (payload) => {
    console.log('task attributes: ' + JSON.stringify(payload.task.attributes));
    if (payload.task.attributes.direction === "inbound") {

        // Describe the body of your request
        const body = {
            callSid: payload.task.attributes.call_sid,
            callerId: payload.task.attributes.from,
            destination: '+18xxxxxxxx',
            Token: manager.store.getState().flex.session.ssoTokenPayload.token
        };

        // Set up the HTTP options for your request
        const options = {
            method: 'POST',
            body: new URLSearchParams(body),
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
            }
        };

        // Make the network request using the Fetch API
        fetch('https://TWILIO INSTANCE.twil.io/TransferUtil', options)
            .then(resp => resp.json())
            .then(data => console.log(data));

    } else {
        original(payload);
    }
});

And this is the function in twilio:

const TokenValidator = require('twilio-flex-token-validator').functionValidator;

exports.handler = TokenValidator(async function(context, event, callback) {
  const response = new Twilio.Response();
  response.appendHeader('Access-Control-Allow-Origin', '*');
  response.appendHeader('Access-Control-Allow-Methods', 'OPTIONS, POST, GET');
  response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
  response.appendHeader('Content-Type', 'application/json');

  const client = require('twilio')();
  const callSid = event.callSid;
  const callerId = event.callerId;
  const destination = event.destination;

  console.log('Call Sid:', callSid);
  console.log('Transfer call from:', callerId, 'to:', destination);

  try {
    let url = 'https://studio.twilio.com/v2/Flows/FWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Executions';
    let call = await client.calls(callSid).update({method: 'POST', url: encodeURI(url)});

    console.log(JSON.stringify(call));
    response.appendHeader('Content-Type', 'application/json');
    response.setBody(call);
    callback(null, response);
  }
  catch (err) {
    response.appendHeader('Content-Type', 'plain/text');
    response.setBody(err.message);
    console.log(err.message);
    response.setStatusCode(500);
    callback(null, response);
  }
});

EDIT: In the error Log I get this information:

Screenshot 1

Screenshot 2

  • Is the Twilio Function set to be protected? Or is it public? – philnash Mar 18 '22 at 04:04
  • @philnash It is set as public – Andres Santamaria Mar 22 '22 at 14:24
  • In the [Twilio error logs](https://console.twilio.com/us1/monitor/logs/debugger/events?frameUrl=%2Fconsole%2Fdebugger%3Fx-target-region%3Dus1&currentFrameUrl=%2Fconsole%2Fdebugger%3F__override_layout__%3Dembed%26quickDate%3D24%26x-target-region%3Dus1%26bifrost%3Dtrue) you should be able to see more information about the 11200 error, and if there was an error in the Function there should be logs for that too. Can you share the error message from the Response? And if there's a function error, share whatever details you can from there too. – philnash Mar 22 '22 at 23:04
  • @philnash I edited my post and added the logs information, the function doesn't seem to generate any error – Andres Santamaria Mar 25 '22 at 00:07
  • Ok, so you're getting a 401 which, I think, means the TokenValidator is rejecting your token. If you remove the TokenValidator from the Twilio Function, does it work? Are you deploying this Twilio Function to the same account as your Flex instance? – philnash Mar 25 '22 at 00:14
  • Hi @philnash, I just tried what you suggested but the error remains. Yes, they are in the same account – Andres Santamaria Mar 29 '22 at 12:37

1 Answers1

0

Argh, I read the error wrong. There isn't anything wrong with the Function, the error is coming from the call trying to make a webhook request to the URL https://studio.twilio.com/v2/Flows/FWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Executions. That's the REST API trigger and needs to be requested in the same way as any other API request using your account credentials or API keys.

You should set that URL to the webhook trigger URL, which looks like https://webhooks.twilio.com/v1/Accounts/${ACCOUNT_SID}/Flows/${FLOW_SID}. Then the call will be able to request it as part of a normal webhook flow.

philnash
  • 70,667
  • 10
  • 60
  • 88