Twilio:将呼叫转发到 Twilio 中的流程

发布于 2025-01-15 10:37:08 字数 2978 浏览 3 评论 0原文

我想在 Flex 中的代理挂断后将呼叫转发到 Studio Flow,以便可以为用户播放 CSAT 调查。 我创建了一个调用 Twilio 内部函数的插件,但转发完成后出现“错误 - 11200”。

我替换了挂断操作,以便它将调用重定向到 twilio 中的函数。该函数应该将调用发送到将播放调查的流程。我怀疑问题与身份验证有关,但我找不到太多相关信息。

我对 twilio 相当陌生,所以任何帮助将不胜感激

这是插件调用该函数的部分:

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);
    }
});

这是 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);
  }
});

编辑: 在错误日志中,我得到以下信息:

屏幕截图 1

屏幕截图 2

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紧拥背影 2025-01-22 10:37:08

呃,我读错了错误。该函数没有任何问题,错误来自尝试向 URL 发出 Webhook 请求的调用 https://studio.twilio.com/v2/Flows/FWXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/Executions 。这是 REST API 触发器,需要使用您的帐户凭据或 API 密钥以与任何其他 API 请求相同的方式进行请求。

您应该将该 URL 设置为 Webhook 触发器 URL,该 URL 类似于 https://webhooks.twilio.com/v1/Accounts/${ACCOUNT_SID}/Flows/${FLOW_SID}。然后,调用将能够将其请求作为正常 Webhook 流程的一部分。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文