Alexa Voice Skill Lambda Node.JS 访问端点间歇性失败

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

我想弄清楚别人创建的 Alexa 语音技能(电源控制器)发生了什么。在 AWS Lambda 上使用 Node.JS V12 创建代码的简单技巧。引发打开事件时,代码应调用 API 端点以获取 ID,然后调用另一个端点来检查状态,然后再调用另一个端点以告知该 ID 打开。

实际发生的是 getID 端点工作一次或两次然后停止。当它确实起作用时,下一个端点每次都会失败。我已经检查并且返回的 ID 是正确的 - 所以我知道 getID 的端点工作正常。我也检查了令牌,这也是有效和正确的。

我创建了一个小型 iOS 应用程序,它使用完全相同的方法来访问设备,使用相同的 API 和相同的端点,并且它每次都能完美运行,很高兴基于 AWS EC2 的 Web 服务器能够正常运行。所以我有点不知所措,无法理解到底是什么失败了。

我对 Alexa 的东西很陌生,但它是一个必须输入场景的需求(现在也是个人挑战!)

这里是相关代码——出于显而易见的原因,我不得不删除实际的 url 等。

   const https = require('https');
   const myUrl = ‘companyname.co.uk;
   const util = require('util');

  async function handlePowerControl(request, context) {
    let  token =  request.directive.endpoint.scope.token;
    console.log("Token: " + token)
    let requestMethod = request.directive.header.name;
    let responseHeader = request.directive.header;

    const idResult = await getID(token);
    let idData = JSON.parse(idResult);

    let deviceID = idData.data[0].device[0].id;
    console.log("Device ID: " + deviceID);
    
    let status = await getDeviceStatus(deviceID, token);
    }


   function getID(token) {
 //this function works intermittently from looking at the logs when it
 //fails it just goes to req.end() it never does any of the res stuff
 //other times it does - i make no changes just start the process again
    const options = {
        host: myUrl,
        path: '/api/devices/power?type=lights',
        method: 'GET',
        headers: {
            'Accept': 'application/xxxx',
            'Authorization': 'Bearer ' + token,
            'N-Meta': 'android;development;1.0 (31);5.0.2;Android SDK built for x86' 
        }
    };

    return new Promise(((resolve, reject) => {
        const req = https.request(options, res => {
            res.setEncoding('utf8'); 
            let responseString = "";
            res.on('data', chunk => {
                responseString = responseString + chunk;
            });
            res.on('end', () => {
                resolve(responseString);
            });
            res.on('error', (error) => {
                reject(error);
            });

        });
        req.end();
    }));
}


  function getDeviceStatus(deviceID, token) { 
 //this function works never works - logs i have added indicate nothing happens after promise
const options = {
        host: myUrl,
        path: '/api/devices/'+deviceID+'/onOff',
        method: 'GET',
        headers: {
                'Accept': 'application/xxxx',
            'Authorization': 'Bearer ' + token,
            'N-Meta': 'android;development;1.0 (31);5.0.2;Android SDK built for x86'
        }
    };

    return new Promise(((resolve, reject) => {
        const req = https.request(options, res => {
            res.setEncoding('utf8');
            let responseString = "";
            res.on('data', chunk => {
                responseString = responseString + chunk;
            });
            res.on('end', () => {
                resolve(responseString);
            });
            res.on('error', (error) => {
                reject(error);
            });

        });
        req.end();
    }));
    }

我没有写任何这段代码,所以我不知道为什么要完成某些事情。但是我已经使用与此处概述的完全相同的信息在 iOS 中重新创建了 API 调用,并且工作正常,所以一定是 Node.js 代码中的内容。

在我将 Alexa 扔出窗外之前,我们将不胜感激地收到任何建议/建议/指示!谢谢!

node.js aws-lambda alexa
© www.soinside.com 2019 - 2024. All rights reserved.