Twilio Studio - 向 API 发出请求时遇到问题

问题描述 投票:0回答:1

我在使用 Twilio Studio 开发 IVR 时遇到问题。 目前,我需要使用 API 发出一些请求。默认情况下,要发出请求,必须通过身份验证端点,以便生成令牌,以便我可以在其他端点中使用它。我的问题是,如何在新函数中使用生成的令牌以便正确发出请求?

到目前为止,我已经尝试调用在新函数中请求令牌的小部件来验证其他事项,但它返回一个错误,指出无法访问内容。

Flow

  • Widget Run Function - Authorization_API1 -> 负责生成token的函数。

  • Widget Split 基于 - tokenCriado1 -> 负责验证返回是否为“成功”的函数。

  • 小部件运行功能 - validaDebito - > 它根据生成的令牌执行新的 API 调用。

我的功能

const axios = require('axios');

exports.handler = function(context, event, callback) {
        
    // Get the CPF/CNPJ from the event
    const cpfCnpj = event.CPF_CNPJ;

    // Check if the CPF/CNPJ was provided
    if (!cpfCnpj) {
        return callback("CPF/CNPJ not provided.");
    }

    // Get the token generated by the Authorization_API1 widget
    const token = event.widgets.Authorization_API1.parsed.token;

    // Check if the token was provided
    if (!token) {
        return callback("Authorization token not provided.");
    }

    // Base URL of the endpoint
    const baseUrl = 'https://app.omnilink.com.br/apigateway/getcliente';

    // Complete URL with the 'CPF_CNPJ' parameter
    const endpointUrl = `${baseUrl}?CPF_CNPJ=${cpfCnpj}`;

    // Make an HTTP request to get the client's data
    axios.get(endpointUrl, {
        headers: {
            Authorization: `Bearer ${token}` // Add the token to the authorization header
        }
    })
    .then(response => {
        // Check if the response was successful
        if (response.status === 200) {
            // Check if ComunicacaoChip and ComunicacaoSatelital are true
            const cliente = response.data.Cliente;
            if (cliente.ComunicacaoChip && cliente.ComunicacaoSatelital) {
                // Client has no debts
                callback(null, { success: true, inadimplente: false });
            } else {
                // Client is in debt
                callback(null, { success: true, inadimplente: true });
            }
        } else {
            // Response was not successful
            callback(`Error in request: ${response.statusText}`);
        }
    })
    .catch(error => {
        // Error making the HTTP request
        callback(`Error obtaining client data: ${error}`);
    });
};

调试信息

Request URL: https://validadebito-4911.twil.io/validaDebito
Request Method: POST
Response Status Code: 500
Response Content Type: application/json

流量数据

 "widgets": {
    "MenuPrincipalSelecao": {},
    "Authorization_API1": {
      "status_code": 200,
      "content_type": "application/json",
      "parsed": {
        "success": true,
        "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJNVE0zTXpGbFkyUXlZbVJsWWpVMk9UVmpOREF3WXpFME5EUXdPVFk0WldNPSIsImp0aSI6ImM3NDQ4NWZlLTUwZTUtNDIxYi04ZmE4LTczZDZmYmJlZDg1ZiIsImlhdCI6IjQvMTAvMjAyNCA3OjE1OjAyIFBNIiwibmJmIjoxNzEyNzc2NTAyLCJleHAiOjE3MTI3ODczMDIsImlzcyI6Imh0dHBzOi8vd3d3Lm9tbmlsaW5rLmNvbS5iciIsImF1ZCI6Ik9tbmlsaW5rIn0.7-nzPWJM4Plnsqqt7ZugksG0iKEbGF1Z-2s5wEAwMHA"
      },
      "body": "{\"success\":true,\"token\":\"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJNVE0zTXpGbFkyUXlZbVJsWWpVMk9UVmpOREF3WXpFME5EUXdPVFk0WldNPSIsImp0aSI6ImM3NDQ4NWZlLTUwZTUtNDIxYi04ZmE4LTczZDZmYmJlZDg1ZiIsImlhdCI6IjQvMTAvMjAyNCA3OjE1OjAyIFBNIiwibmJmIjoxNzEyNzc2NTAyLCJleHAiOjE3MTI3ODczMDIsImlzcyI6Imh0dHBzOi8vd3d3Lm9tbmlsaW5rLmNvbS5iciIsImF1ZCI6Ik9tbmlsaW5rIn0.7-nzPWJM4Plnsqqt7ZugksG0iKEbGF1Z-2s5wEAwMHA\"}"
    },
    "validaDebito": {
      "status_code": 500,
      "Called": "+551150399828",
      "ToState": "SP",
      "body": "{\"message\":\"Cannot read properties of undefined (reading 'Authorization_API1')\",\"name\":\"TypeError\",\"stack\":\"TypeError: Cannot read properties of undefined (reading 'Authorization_API1')\\n    at exports.handler (/var/task/handlers/ZN7acdff34f5d5c6173bca9f9d8d4133a4.js:14:33)\\n    at exports.handler (/var/task/node_modules/runtime-handler/index.js:339:10)\\n    at exports.handler (/var/task/runtime-handler.js:17:17)\\n    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)\"}",
javascript twilio ivr twilio-studio twilio-functions
1个回答
0
投票

看起来您期望小部件对象自动传递给正在运行的函数。 Twilio 文档 [1] 提供了完整的示例,但您需要在“运行函数”小部件中添加函数参数以将数据传递给函数。

以下配置允许您将解析的令牌从 Authorization_API1 小部件响应传递给函数,该值将根据事件对象上的“token”键设置。

Key: token
Value: {{widgets.Authorization_API1.parsed.token}} 

如果您将 Twilio 函数的第 14 行更新为

const token = event.token

然后您应该能够使用令牌来调用您的 API。

我希望这有帮助

[1] https://www.twilio.com/docs/studio/widget-library/run-function#example-random-number-generator

© www.soinside.com 2019 - 2024. All rights reserved.