繁体   English   中英

Twilio:将呼叫转移到 Twilio 中的流

[英]Twilio: forward a call to a flow in Twilio

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

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

我是 twilio 的新手,非常感谢任何帮助

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

这是 twilio 中的 function:

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

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

截图一

截图2

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

您应该将 URL 设置为 webhook 触发器 URL,它看起来像https://webhooks.twilio.com/v1/Accounts/${ACCOUNT_SID}/Flows/${FLOW_SID} 然后调用将能够将其作为正常 webhook 流程的一部分进行请求。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM