简体   繁体   English

alexa 技能中 Node.js 中的 post 请求问题

[英]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.

相关问题 Alexa Node.js技能无法达到目的 - Alexa Node.js skill not getting into intent 在AnswerIntentHandler中运行exports.handle-node.js-Alexa技能 - Run exports.handle in AnswerIntentHandler - node.js - alexa skill 具有Alexa技能的节点JS回调 - Node JS callbacks with Alexa skill 如何在自己的服务器上使用带有Node.js的alexa-sdk运行Alexa技能而不使用Lambda drop-in? - How to run Alexa skill with the alexa-sdk on own server with Node.js without Lambda drop-in? 可以使用AWS Lambda,Node.js和Alexa技能套件阅读Facebook帖子的Alexa技能 - Alexa skill which can read Facebook posts using AWS Lambda, Node.js and the Alexa Skills Kit 具有授权的Alexa node.js http请求 - Alexa node.js http request with authorization 使用node.js和AWS Lambda将Alexa技能连接到mysql数据库 - Connecting Alexa skill to mysql database using node.js and aws lambda 添加两个数字连接它们而不是计算amazon alexa技能中的sum node.js - Adding two numbers concatenates them instead of calculating the sum node.js in amazon alexa skill Amazon Echo-Alexa-ContextClient是Node.js中Lambda的空开发技能 - Amazon Echo - Alexa - ContextClient is null developing skill with Lambda in node.js 在node.js中开发的Alexa技能不会显示任何卡片 - Alexa skill developed in node.js doesn't display any card
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM