[英]How to use async fulfillment for Actions on Google
I'm trying to create a very simple voice app to query exchange rates by using Google Conversational Actions .我正在尝试使用Google Conversational Actions创建一个非常简单的语音应用程序来查询汇率。
I use slots to get the to and from currencies.我使用插槽来获取和获取货币。 Then I try to call a web hook that in turn calls an API to get the rate between the two currencies.
然后我尝试调用 web 挂钩,该挂钩又调用 API 以获取两种货币之间的汇率。
First I tried defining the fulfillment function as async
with await request
, and then I tried return await request
, but couldn't make the fulfillment function wait for the GET
request to get back.首先,我尝试将履行 function 定义为与
await request
async
,然后我尝试return await request
,但无法使履行 function 等待GET
请求返回。 Then I tried the code below using a Promise
and now it times out.然后我使用
Promise
尝试了下面的代码,现在它超时了。 Not sure how to fix this.不知道如何解决这个问题。
const { conversation } = require('@assistant/conversation');
const functions = require('firebase-functions');
const request = require('request');
const app = conversation();
app.handle('get_exchange_rate', conv => {
conv.session.params.rate = 0;
let fromCcy = "EUR" //conv.session.params.currency_from_slot;
let toCcy = "GBP" //conv.session.params.currency_to_slot;
let url = `https://api.exchangeratesapi.io/latest?base=${fromCcy}&symbols=${toCcy}`;
let options = {
json: true
};
return new Promise((resolve, reject) => {
request(url, options, (error, res, body) => {
if (error) {
console.log(error);
reject(error);
} else if (res.statusCode == 200) {
console.log(body);
conv.session.params.rate = body.rates[toCcy];
resolve();
}
});
});
});
exports.ActionsOnGoogleFulfillment = functions.https.onRequest(app);
Note: All the samples I've found so far in the docs are for trivial sync work, like returning a static message.注意:到目前为止,我在文档中找到的所有示例都是用于琐碎的同步工作,例如返回 static 消息。 I've not been able to find any examples on how to make this work with async work such as this.
我无法找到任何关于如何使用诸如此类的异步工作进行此工作的示例。
Update: I'm doing this from the Google Conversational Actions console found at Actions Console>Webhooks and using the Inline Cloud Functions fulfillment type.更新:我正在从位于Actions Console>Webhooks的 Google Conversational Actions 控制台执行此操作,并使用Inline Cloud Functions实现类型。
The timeout problem somehow went away —literally over night— and I was suddenly able to address the response in my conversation prompts as $session.params.rate
.超时问题不知何故消失了——实际上是一夜之间——我突然能够在我的对话提示中将响应处理为
$session.params.rate
。
It seems like there's something off with the propagation of changes from the Google Actions Console to Firebase.从 Google 操作控制台到 Firebase 的更改传播似乎有些问题。 I also notice that switching from the Develo tab to the Test one takes an awful lot of time to load.
我还注意到从Develo选项卡切换到Test选项卡需要花费大量时间来加载。 There's this message
Your preview is being updated...
with a spinner that sometimes just takes forever.有此消息
Your preview is being updated...
带有有时只需要永远的微调器。 Sometimes it just kind of times out and asks me for the version I want to preview again.有时它只是有点超时,并要求我再次预览要预览的版本。 Then I try it a second time and it's instantly done.
然后我再次尝试它,它立即完成。 Not a very good DX yet.
还不是很好的DX。
I was hitting very similar issues to you using async/await.我使用 async/await 遇到了与您非常相似的问题。
I commented out all the async stuff, and gradually brought it all back in one at a time, and eventually it started working... shrug我注释掉了所有异步的东西,并逐渐将它们一次全部带回来,最终它开始工作......耸了耸肩
I'm using node-fetch in my handler我在我的处理程序中使用 node-fetch
const fetch = require('node-fetch');
/* plus your other regular includes */
app.handle('checkCurrentPrice', async conv => {
const url = 'https://api....';
let body = {"key" : "var"};
const externalRes = await fetch(url, {
method: 'POST',
body: JSON.stringify(body),
headers: {"Content-Type" : "application/json"},
});
const json = await externalRes.json();
});
exports.ActionsOnGoogleFulfillment = functions.https.onRequest(app);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.