【问题标题】:Accessing Firebase database from Gupshup从 Gupshup 访问 Firebase 数据库
【发布时间】:2017-04-18 17:57:13
【问题描述】:

我正在使用 Gupshup 和 Firebase 构建一个机器人。 我想做两个任务: (i) 我希望存储我与用户的所有对话。 (ii) 当用户打开对话时,我想获取最后 10 个对话。

Gupshup 支持 HTTP GET 和 HTTP POST 方法。

POST方法的代码:

var url3 = "https://My app name.firebaseio.com/Chat.json";
           var header3 = {"Content-Type": "application/json"};
               var param3 = JSON.parse(res).result.fulfillment.speech; // Parsing Result from NLP tool

       context.simplehttp.makePost(url3,JSON.stringify(param3),header3); 

GET方法的代码:

context.simplehttp.makeGet('https://My app name.firebaseio.com/Chat.json', function(c,e){
var res = "Sample response from http put method\n"+e.getresp;
context.sendResponse(res);

每次我通过 HTTP POST 向 Firebase 数据库添加数据时,都会生成一个实时时间,在此时间下添加数据。

聊天:

-KY4yWKeGKIKPf1qf74G:

“嗨”

-KY4yWKfjoztU0EBPe1g:

“您好。有什么可以帮助您的吗?”

-KY4ykQtSus8srqa7okF:

“好吧,这就是交易:今天买一个披萨,然后……”

-KY4ykQtSus8srqa7okG:

“显示一些交易”

当我尝试使用 HTTP GET https://My app name.firebaseio.com/Chat.json 时,会返回以下 JSON:

{"-KY4x81jkuxvT9TjDOfk":"Hello. How can I help you?",
 "-KY4x81kw8zBoaKwAIe-":"hi","-KY4xAQCFDU7SW8PAEHX":"get",
 "-KY4xAR1KEEQNO1KfjnI":"I'm a bit confused by that last part."}

现在如何解析这个 JSON 并访问对话并将其呈现给用户?

或者有什么方法可以直接使用 HTTP GET 访问 Child?

提前致谢

【问题讨论】:

    标签: node.js firebase firebase-realtime-database firebase-cloud-messaging gupshup


    【解决方案1】:
        You can use the following way to access the keys and the data:
        //Parse your JSON if you are getting it using http call using JSON.parse(yourjson);
    
        var json={"-KY4x81jkuxvT9TjDOfk":"Hello. How can I help you?",
         "-KY4x81kw8zBoaKwAIe-":"hi","-KY4xAQCFDU7SW8PAEHX":"get",
         "-KY4xAR1KEEQNO1KfjnI":"I'm a bit confused by that last part."};
       //Getting Keys
    
       var keys=Object.keys(json);
       //Getting Conversations
       for(var i=0;i<keys.length;i++)
       {
        console.log(json[keys[i]);
       } 
    

    【讨论】:

      猜你喜欢
      • 2019-02-16
      • 2017-09-24
      • 1970-01-01
      • 2018-07-28
      • 2021-06-14
      • 2019-04-13
      • 1970-01-01
      • 1970-01-01
      • 2021-04-22
      相关资源
      最近更新 更多