我如何在 google 助手的 (index.js) webhook 中调用 REST API
How can I call a REST API in (index.js) webhook for google assistant
我正在使用对话流为 google 助手制作应用程序。我的应用程序使用 webhook(功能部署在 firebase 上)。
要点是——我想调用 returns JSON 的 REST API URL(来自 index.js),然后解析 JSON响应并提取一些值。然后对该值进行一些操作,并将该值发送给google助手。
代码如下:
'use strict';
process.env.DEBUG = 'actions-on-google:*';
const App = require('actions-on-google').DialogflowApp;
const functions = require('firebase-functions');
// a. the action name from the make_name Dialogflow intent
const SOME_ACTION = 'some_action';
//----global variables-----
const http = require('https');
var body = "";
var value="";
exports.addressMaker = functions.https.onRequest((request, response) => {
const app = new App({request, response});
console.log('Request headers: ' + JSON.stringify(request.headers));
console.log('Request body: ' + JSON.stringify(request.body));
function makeMessage (app) {
var req = http.get("https://some_url_of_API", function(res)
{
res.writeHead(200, {"Content-Type": "application/json"});
res.on("data", function(chunk){ body += chunk; });
res.on('end', function()
{
if (res.statusCode === 200) {
try {
var data = JSON.parse(body);
value=data.word; //----getting the value----
} catch (e) {
console.log('Status:', res.statusCode);
console.log('Error parsing JSON!');
}
} else {
console.log('Status:', res.statusCode);
}
});
});
app.tell('Alright, your value is '+value);
}
let actionMap = new Map();
actionMap.set(SOME_ACTION, makeMessage);
app.handleRequest(actionMap);
});
我可以得到消息 "Alright, your value is",但不能得到值。我认为它没有调用 URL.
这里有两个可能的问题。
首先,您需要使用 Firebase 的付费版本之一才能在 Google 之外进行 URL 调用。您可以使用 "blaze" 计划,该计划确实需要信用卡,但仍然可以免费使用。
第二个是您的代码在从 REST 调用获取结果的回调之外调用 app.tell()
。所以发生的事情是你正在调用,然后在你得到结果之前立即调用 app.tell()
。
为了做你想做的事,你可能想要更像这样的东西:
function makeMessage (app) {
var req = http.get("https://some_url_of_API", function(res)
{
var body = '';
res.writeHead(200, {"Content-Type": "application/json"});
res.on("data", function(chunk){ body += chunk; });
res.on('end', function()
{
if (res.statusCode === 200) {
try {
var data = JSON.parse(body);
value=data.word; //----getting the value----
// Send the value to the user
app.tell('Alright, your value is '+value);
} catch (e) {
console.log('Status:', res.statusCode);
console.log('Error parsing JSON!');
}
} else {
console.log('Status:', res.statusCode);
}
});
});
}
我正在使用对话流为 google 助手制作应用程序。我的应用程序使用 webhook(功能部署在 firebase 上)。 要点是——我想调用 returns JSON 的 REST API URL(来自 index.js),然后解析 JSON响应并提取一些值。然后对该值进行一些操作,并将该值发送给google助手。
代码如下:
'use strict';
process.env.DEBUG = 'actions-on-google:*';
const App = require('actions-on-google').DialogflowApp;
const functions = require('firebase-functions');
// a. the action name from the make_name Dialogflow intent
const SOME_ACTION = 'some_action';
//----global variables-----
const http = require('https');
var body = "";
var value="";
exports.addressMaker = functions.https.onRequest((request, response) => {
const app = new App({request, response});
console.log('Request headers: ' + JSON.stringify(request.headers));
console.log('Request body: ' + JSON.stringify(request.body));
function makeMessage (app) {
var req = http.get("https://some_url_of_API", function(res)
{
res.writeHead(200, {"Content-Type": "application/json"});
res.on("data", function(chunk){ body += chunk; });
res.on('end', function()
{
if (res.statusCode === 200) {
try {
var data = JSON.parse(body);
value=data.word; //----getting the value----
} catch (e) {
console.log('Status:', res.statusCode);
console.log('Error parsing JSON!');
}
} else {
console.log('Status:', res.statusCode);
}
});
});
app.tell('Alright, your value is '+value);
}
let actionMap = new Map();
actionMap.set(SOME_ACTION, makeMessage);
app.handleRequest(actionMap);
});
我可以得到消息 "Alright, your value is",但不能得到值。我认为它没有调用 URL.
这里有两个可能的问题。
首先,您需要使用 Firebase 的付费版本之一才能在 Google 之外进行 URL 调用。您可以使用 "blaze" 计划,该计划确实需要信用卡,但仍然可以免费使用。
第二个是您的代码在从 REST 调用获取结果的回调之外调用 app.tell()
。所以发生的事情是你正在调用,然后在你得到结果之前立即调用 app.tell()
。
为了做你想做的事,你可能想要更像这样的东西:
function makeMessage (app) {
var req = http.get("https://some_url_of_API", function(res)
{
var body = '';
res.writeHead(200, {"Content-Type": "application/json"});
res.on("data", function(chunk){ body += chunk; });
res.on('end', function()
{
if (res.statusCode === 200) {
try {
var data = JSON.parse(body);
value=data.word; //----getting the value----
// Send the value to the user
app.tell('Alright, your value is '+value);
} catch (e) {
console.log('Status:', res.statusCode);
console.log('Error parsing JSON!');
}
} else {
console.log('Status:', res.statusCode);
}
});
});
}