简体   繁体   English

从 Dialogflow 上的循环设置参数输出上下文

[英]Set Parameters Output Context from Looping on Dialogflow

i make some loop for calling data from firebase, how to set document id as parameters for my output context when i selected the data from document?我为从 firebase 调用数据做了一些循环,当我从文档中选择数据时,如何将文档 ID 设置为我的输出上下文的参数?

this my code for function daftaKota这是我的函数 daftaKota 的代码

function daftarKota(agent){
const query = db.collection('kota');
return query.get().then(s =>{
    if (s.empty){
        agent.add('belum ada kota yang didaftarkan oleh Pemilik');
        agent.add('untuk mengakses menu lainnya silahkan ketikan "menu"');
        agent.context.set('menu',2);
    } else {
      agent.add('berikut daftar kota');
      s.forEach(doc =>{
        agent.add(new Suggestion(doc.data().nama_kota));
        agent.context.set('lihat-toko',5,{'id_kota' : doc.id,'nama_kota' : doc.data().nama_kota});
      });
    }
});

this my code for function daftarToko这是我的函数 daftarToko 的代码

function daftarToko (agent){
const context = agent.context.get('lihat-toko');
const idKota = context.parameters.id_kota;
const nKota = agent.parameters.kota;

const query = db.collection('toko').where('id_kota','==',idKota);
return query.get().then(s =>{
  if (s.empty){
    agent.add('Belum ada Toko yang didaftarkan di kota ini');
    agent.add('untuk mengakses kota lainnya silahkan ketikan "kembali"');
    agent.context.set('order',2);
  }else{
    agent.add('berikut daftar toko di kota '+nKota);
    s.forEach(doc => {
      agent.add(new Card({title : doc.data().nama_toko, imageUrl : doc.data().gambar_toko}));
      agent.add(new Suggestion(doc.data().nama_toko));
      agent.context.set('lihat-kue',5,{'id_toko' : doc.id});
    });
  }
});

and this the Intent Map这是意图图

intentMap.set('Daftar Kota',daftarKota);
intentMap.set('Daftar Toko',daftarToko);

this my intent "Daftar Kota"这是我的意图“Daftar Kota” 意图达夫塔哥打

this intent show the city from database using suggestion此意图使用建议从数据库中显示城市建议

when i selected the other suggestion city like Yogyakarta, Jakarta, or Bandung, the parameters still set on Banjarmasin.当我选择其他建议城市如日惹、雅加达或万隆时,参数仍然设置在 Banjarmasin。

this my API response after i select Yogyakarta这是我选择日惹后的 API 响应

{
  "responseId": "9e1daa4d-31f8-4a62-a939-813be357a634-19db3199",
  "queryResult": {
    "queryText": "Yogyakarta",
    "parameters": {
      "kota": "Yogyakarta"
    },
    "allRequiredParamsPresent": true,
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            "Belum ada Toko yang didaftarkan di kota ini"
          ]
        }
      },
      {
        "text": {
          "text": [
            "untuk mengakses kota lainnya silahkan ketikan \"kembali\""
          ]
        }
      }
    ],
    "outputContexts": [
      {
        "name": "projects/jastip-21e34/agent/sessions/771d2ffc-b490-51f3-7da7-78b91faa8ad3/contexts/order",
        "lifespanCount": 2
      },
      {
        "name": "projects/jastip-21e34/agent/sessions/771d2ffc-b490-51f3-7da7-78b91faa8ad3/contexts/lihat-toko",
        "lifespanCount": 4,
        "parameters": {
          "kota": "Yogyakarta",
          "nama_kota": "Banjarmasin",
          "id_kota": "qCjS54XPf1lAtECUFTTw",
          "kota.original": "Yogyakarta"
        }
      }
    ],
    "intent": {
      "name": "projects/jastip-21e34/agent/intents/f14ab0fa-b506-419d-a360-a8eb7cd84b93",
      "displayName": "Daftar Toko"
    },
    "intentDetectionConfidence": 1,
    "diagnosticInfo": {
      "webhook_latency_ms": 236
    },
    "languageCode": "id"
  },
  "webhookStatus": {
    "message": "Webhook execution successful"
  }
}

see at paramers :见参数:

  1. i selected "kota : Yogyakarta",我选择了“哥打:日惹”,
  2. but the id_kota is the document id of nama_kota "Banjarmasin", not the id of "Yogyakarta"但 id_kota 是 nama_kota "Banjarmasin" 的文档 ID,而不是 "Yogyakarta" 的 ID

You're not showing the query that you're using, or where you're storing the parameters you get, but in your loop you're not actually checking to see if nama_kota matches the kota that is sent through the parameters.您没有显示您正在使用的查询,或者您存储获得的参数的位置,但是在您的循环中,您实际上并没有检查nama_kota与通过参数发送的kota匹配。 So it is changing the context every time it goes through the loop, and ends up with the new parameters from the last time through.所以它每次通过循环时都会改变上下文,并以上次的新参数结束。

One solution would be to check if they match and, when they do, set the context.一种解决方案是检查它们是否匹配,并在匹配时设置上下文。

  s.forEach(doc =>{
    agent.add(new Suggestion(doc.data().nama_kota));
    if( parameters.kota === doc.data().nama_kota ){
      agent.context.set('lihat-toko',5,{'id_kota' : doc.id,'nama_kota' : doc.data().nama_kota});
    }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM