SDK v2 上的 Alexa 和 Lambda 的 HTTP 请求,如何使其工作?

编程入门 行业动态 更新时间:2024-10-09 15:15:20
本文介绍了SDK v2 上的 Alexa 和 Lambda 的 HTTP 请求,如何使其工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 Amazon 提供的 ASK SDK v2 来制作 Alexa 技能,但我遇到了架构问题:

I am playing with ASK SDK v2 provided by Amazon in order to make Skill for Alexa but I face an architectural problem :

首先,HTTP 请求就像一个魅力,但我想当且仅当我的 HTTP 请求完成时才返回语音响应,但我什至不知道这是否可能,因为处理"功能应该返回一些东西(看看评论):

First of all, the HTTP request works like a charm but I would like to return speach response if and only if my HTTP request is complete but I don't even know if it's possible because of the "handle" function that should return something (look at comments) :

const MyIntentHandler = { canHandle(handlerInput) { const request = handlerInput.requestEnvelope.request; return request.type === 'LaunchRequest' || (request.type === 'IntentRequest' && request.intent.name === 'MyIntent'); }, handle(handlerInput) { var options = { host: 'foo', port: 80, path: '/mypath', method: 'GET' }; var req = http.request(options, function(result){ result.on("end", function(){ //I would like to return speak here like that : //return handlerInput.responseBuilder.speak("It works").withSimpleCard("MyTestApp", "It works").getResponse() }) }); req.end(); //And I would like to remove this line to manage response in result.on("end", function(){}) above return handlerInput.responseBuilder.speak("It works").withSimpleCard("MyTestApp", "It works").getResponse(); }, };

有什么办法处理这个问题吗?

Any idea to deal with this ?

推荐答案

我找到了官方的方法:

1) 创建一个管理 http 请求并返回一个 promise 的新函数:

1) Create a new funtion that manage http request and return a promise :

function httpGet(options) { return new Promise(((resolve, reject) => { const request = http.request(options, (response) => { response.setEncoding('utf8'); let returnData = ''; if (response.statusCode < 200 || response.statusCode >= 300) { return reject(new Error(`${response.statusCode}: ${response.req.getHeader('host')} ${response.req.path}`)); } response.on('data', (chunk) => { returnData += chunk; }); response.on('end', () => { resolve(JSON.parse(returnData)); }); response.on('error', (error) => { reject(error); }); }); request.on('error', function (error) { reject(error); }); request.end(); })); }

2) 在 Intent 中,返回一个调用 httpGet 函数的承诺:

2) In the Intent, return a promise in which you call your httpGet function :

const MyIntentHandler = { canHandle(handlerInput) { const request = handlerInput.requestEnvelope.request; return request.type === 'LaunchRequest' || (request.type === 'IntentRequest' && request.intent.name === 'MyIntent'); }, handle(handlerInput) { var options = { host: 'foo', port: 80, path: '/mypath', method: 'GET' }; return new Promise((resolve, reject) => { httpGet(options).then((response) => { resolve(handlerInput.responseBuilder.speak("It is done.").getResponse()); }).catch((error) => { resolve(handlerInput.responseBuilder.speak('Thor is not available at the moment. Please try again later or contact your administrator.') .getResponse()); }); }); }, };

这是正确执行此操作的方法.我的示例基于 alexa petmatch 示例.

This is the way to do it properly. My example is base on alexa petmatch sample.

更多推荐

SDK v2 上的 Alexa 和 Lambda 的 HTTP 请求,如何使其工作?

本文发布于:2023-11-28 03:02:21,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1640709.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:使其   工作   SDK   Alexa   HTTP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!