Actions on google 使用“a”时出现问题;for loop";在“上使用解析的json文件构建列表”;谷歌的行动;
下面是我的代码,我解析了一个json文件,并在循环中使用到“for”循环,以便将其打印到控制台上Actions on google 使用“a”时出现问题;for loop";在“上使用解析的json文件构建列表”;谷歌的行动;,actions-on-google,google-assistant-sdk,google-assist-api,Actions On Google,Google Assistant Sdk,Google Assist Api,下面是我的代码,我解析了一个json文件,并在循环中使用到“for”循环,以便将其打印到控制台上 process.env.DEBUG = 'actions-on-google:*'; const apikey = "xxxxxx"; const https = require('https'); const ActionsSdkApp = require("actions-on-google"); const DialogflowApp = require('actions-on-google'
process.env.DEBUG = 'actions-on-google:*';
const apikey = "xxxxxx";
const https = require('https');
const ActionsSdkApp = require("actions-on-google");
const DialogflowApp = require('actions-on-google').DialogflowApp;
const app = new ActionsSdkApp({request, response});
//ActionsSdkApp({request, response} vs DialogflowApp({ request: req, response: res })?????? which one do I use????
function responseHandler(app) {
var topic = app.getArgument('topic');
https.get(`https://example.com/${topic}?apiKey=${apikey}`, (res) => {
let body = "";
res.on('data', data => {
console.log('Reading Data');
body += data.toString();
});
res.on('end', () => {
try {
const profile = JSON.parse(body);
for(let i = 0; i<profile.data.length;i++) {
console.log(" description: " + profile.data[i].description + " title: " + profile.data[i].title + ); // in the json file there are vales called "description" and "title" that I want on my list
}} catch (e) {
app.ask("Sorry, I was unable to load information. Please repeat the search term.");
console.error("error: " + e.message);
}
});
})
process.env.DEBUG='actions on google:';
const apikey=“xxxxxx”;
常量https=require('https');
const ActionsSdkApp=require(“谷歌上的操作”);
const DialogflowApp=require('actions-on-google')。DialogflowApp;
const-app=new-ActionsSdkApp({request,response});
//ActionsSdkApp({request,response}vs DialogflowApp({request:req,res:res})我应该使用哪一个????
功能响应管理器(应用程序){
var topic=app.getArgument('topic');
https.get(`https://example.com/${topic}?apiKey=${apiKey}`,(res)=>{
让body=“”;
res.on('data',data=>{
console.log(“读取数据”);
body+=data.toString();
});
res.on('结束',()=>{
试一试{
const profile=JSON.parse(body);
对于(让i=0;i对于初学者,您使用的库取决于您使用的工具集。如果您使用的是Dialogflow-使用该库。如果您使用的是Actions SDK-使用该库。(除非您有充分的理由使用Actions SDK,否则如果您不确定,您可能应该使用Dialogflow库。)
app.buildList()
命令返回一个,因此您可以使用addItem()
将项目添加到该列表中。没有什么说明您必须将它们链接起来,您可以在循环中一次调用该列表上的addItem()
。我还没有对此进行测试,但您的代码可能类似于:
var list = app.buildList('Things to learn about');
for( let i=0; i<profile.data.length;i++ ){
var title = profile.data[i].title;
var desc = profile.data[i].description;
var key = "key-"+i; // This is what you'll get from the user
var alias = []; // This really should be what the user can say that is equivalent
var item = app.buildOptionItem( key, alias )
.setTitle( title )
.setDescription( desc );
list.addItem( item );
}
app.askWithList( 'Pick one', list );
var list=app.buildList('Things to learning');
对于(让i=0;iHi,我最近又回到了这个项目,我被难住了。我希望制作一个谷歌助手应用程序,根据用户查询检索研究文章列表。例如,如果用户想阅读关于“电池技术”的文章该应用程序将显示一个卡片列表,每张卡片都有标题和文章的URL链接。我的问题是,如果我真的需要一个“别名”变量,可以输入“密钥”吗变量是用户请求的搜索词,最后,如果用户单击卡片,URL将自动打开到文章。虽然不清楚,但听起来你在问一个完全不同的问题。如果是,请继续,并将其作为一个新的StackOverflow问题问。如果你试图澄清你原来的问题,因为这没有回答它,继续更新它。如果这回答了你的问题(关于在一个循环中构建响应),接受或更新答案总是很感激的。对不起,没有提供足够的背景。我在上发表了一篇新的帖子,我放弃了更新,但它不会出现,因为我没有足够的代表点
var list = app.buildList('Things to learn about');
for( let i=0; i<profile.data.length;i++ ){
var title = profile.data[i].title;
var desc = profile.data[i].description;
var key = "key-"+i; // This is what you'll get from the user
var alias = []; // This really should be what the user can say that is equivalent
var item = app.buildOptionItem( key, alias )
.setTitle( title )
.setDescription( desc );
list.addItem( item );
}
app.askWithList( 'Pick one', list );