Google 连接到 Firebase 函数的操作无法调用外部 api?
Google Action connected to Firebase function unable to call external api?
目前我有一个 google 使用 ActionSDK 和 NodeJS 构建的动作,其中实现托管在 Firebase 云函数上。我现在想做的只是将 google 操作的输入传递给外部 api 但它似乎无法发送它?
'use strict';
const {actionssdk} = require('actions-on-google');
const functions = require('firebase-functions');
const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
const app = actionssdk({debug: true});
app.intent('actions.intent.MAIN', (conv) => {
conv.ask('Hello, this is Patrick.');
});
app.intent('actions.intent.TEXT', (conv, input) => {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.googleapis.com/customsearch/v1?key={apikey}&cx={searchID}&q=" + input, true);
xhr.send();
xhr.onreadystatechange = function () {
conv.ask(this.readyState.toString());
}
exports.myFunction = functions.https.onRequest(app);
但这总是给我这样的回应:
My test app isn't responding right now. Try again soon.
并且在错误选项卡中显示:
{
"error": "No response has been set. Is this being used in an async call that was not returned as a promise to the intent handler?"
}
我不知道为什么会这样,因为我是新手。感谢您的帮助!
如果您使用的是免费层级的 Firebase,您将无法使用外部 API。您需要启用计费并升级到付费层才能访问。
更新
您可能需要使用 Promise 来发回响应。查看 问题。
目前我有一个 google 使用 ActionSDK 和 NodeJS 构建的动作,其中实现托管在 Firebase 云函数上。我现在想做的只是将 google 操作的输入传递给外部 api 但它似乎无法发送它?
'use strict';
const {actionssdk} = require('actions-on-google');
const functions = require('firebase-functions');
const XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
const app = actionssdk({debug: true});
app.intent('actions.intent.MAIN', (conv) => {
conv.ask('Hello, this is Patrick.');
});
app.intent('actions.intent.TEXT', (conv, input) => {
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://www.googleapis.com/customsearch/v1?key={apikey}&cx={searchID}&q=" + input, true);
xhr.send();
xhr.onreadystatechange = function () {
conv.ask(this.readyState.toString());
}
exports.myFunction = functions.https.onRequest(app);
但这总是给我这样的回应:
My test app isn't responding right now. Try again soon.
并且在错误选项卡中显示:
{ "error": "No response has been set. Is this being used in an async call that was not returned as a promise to the intent handler?" }
我不知道为什么会这样,因为我是新手。感谢您的帮助!
如果您使用的是免费层级的 Firebase,您将无法使用外部 API。您需要启用计费并升级到付费层才能访问。
更新
您可能需要使用 Promise 来发回响应。查看