【问题标题】:Problem with post request in Node.js in alexa skillAlexa技能中Node.js中的发布请求问题
【发布时间】:2020-12-01 16:22:59
【问题描述】:

我正在尝试做一个非常简单的发布请求来打开我的 Sonoff,但是当我测试它时,alexa 说响应有问题。我不知道响应中的确切内容。在我的代码下面:

const Alexa = require('ask-sdk-core');
var http = require('http');

const HelloWorldIntentHandler = {
    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'LaunchRequest' 
            || Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'HelloWorldIntent';
    },
      async handle(handlerInput) {
        let speakOutput = "Defaul message"  
        const response = await httpCall();
        
        console.log(response);
        speakOutput = "OK";
            return speakOutput;
        }
};

function httpCall() {
  return new Promise(((resolve, reject) => {
    var options = {
        host: '192.168.X.XXX',
        port: 8081,
        path: '/zeroconf/switch',
        method: 'POST',
        json: {"deviceid":"","data":{"switch":"on"}}
    };
    
    const request = http.request(options, (response) => {
      response.setEncoding('utf8');
      let returnData = '';

      response.on('data', (chunk) => {
        returnData += chunk;
      });

      response.on('end', () => {
        resolve(JSON.parse(returnData));
      });

      response.on('error', (error) => {
        reject(error);
      });
    });
    request.end();
  }));
}

【问题讨论】:

    标签: node.js api http post alexa


    【解决方案1】:

    它不起作用,因为您从 handle 方法返回了一个字符串。

    return speakOutput;
    

    你应该使用ResponseBuilder:

    return handlerInput
             .responseBuilder
             .speak(speakOutput)
             .getResponse();
    

    【讨论】:

      猜你喜欢
      • 2020-07-23
      • 2016-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多