0

以下は私がjsonファイルを解析し、それを印刷するために "for"アウトコンソール「forループ」を使用して、「googleによる処理」の解析済みjsonファイルを使用してリストを構築するときの問題

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); 
     } 
    }); 
}) 

}

に私はactions by google documentation上の例を次のようだし、私は私のJSONファイル内のすべてのJSONオブジェクトのリストを作成しようとしていますが、私は困難を抱えています。以下は私の試みです:

function list() { 
    const app = new ActionsSdkApp({request, response}); 
    app.askWithList('Here are a few things I found. Pick one that looks interesting', 
     app.buildList('Things to learn about') 
    // i want add my loop here....but I how would I add it probably?? for(let i = 0; i<profile.data.length;i++) 
      .addItems 
       .setTitle(" title: " + profile.data[i].title) 
       .setDescription(" description: " + profile.data[i].description) 
    ); 
}; 

リストの作成に役立つアドバイスはありますか?また、actionsdkライブラリまたはdialogflowライブラリをインポートしますか?

答えて

0

どのライブラリを使用するかは、使用しているツールの種類によって異なります。 Dialogflowを使用している場合は、そのライブラリを使用してください。 Actions SDKを使用している場合は、代わりにそのSDKを使用してください。 (あなたがわからない場合SDKは、あなたはおそらくDialogflowライブラリを使用する必要があるアクションを使用するには十分な理由がない限り。)

app.buildList()コマンドはListを返しますので、あなたはそれに項目を追加するためにaddItem()を使用することができますリスト。チェーンを張らなければならないということは何も言われていません。あなたのループで一度に1つずつリストに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); 
+0

こんにちは、私は最近このプロジェクトに戻り、私は困惑しています。私はユーザーのクエリに基づいて研究記事のリストを取得するGoogleのアシスタントアプリケーションを作ることを望んでいる。たとえば、ユーザーが「バッテリー技術」で記事を読んでみたい場合、アプリはカードのリストを表示し、各カードカードにはタイトルと記事へのURLリンクがあります。私の質問は、 "エイリアス"変数が本当に必要かどうかです。 "キー"変数はユーザーが要求する検索語で、最後にユーザーがカードをクリックすると、URLは自動的に記事に開きます。 –

+0

これは明らかではありませんが、まったく異なる質問をしているようです。もしそうなら、新しいStackOverflow質問としてそれを聞いてください。これが答えなかったために元の質問を明確にしようとしているなら、それを更新してください。これがあなたの質問に答えるなら(それはループで応答を構築することでした)、答えを受け入れるか、アップアップするかは常に高く評価されます。 – Prisoner

+0

十分なバックグラウンドを与えないと申し訳ありません。私は新しい投稿をhttps://stackoverflow.com/questions/48402373/building-a-list-in-actions-on-googleに投稿しました。私はupvoteを与えましたが、私は十分な担当者がいないために表示されません。ポイント –

関連する問題