[英]Problem with post request in Node.js in alexa skill
I'm trying to do a very simple post request to switch on my Sonoff but when I test it, alexa say that there's somthing wrong with response.我正在尝试做一个非常简单的发布请求来打开我的 Sonoff,但是当我测试它时,alexa 说响应有问题。 I don't know what's exactly in the response.
我不知道回复中的确切内容。 Below my code:
在我的代码下面:
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(); })); }
It doesn't work because you return a string from your handle
method.它不起作用,因为您从
handle
方法返回一个字符串。
return speakOutput;
You should use a ResponseBuilder
:您应该使用
ResponseBuilder
:
return handlerInput
.responseBuilder
.speak(speakOutput)
.getResponse();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.