0

我正在使用Gupshup和Firebase构建bot。 我希望完成两项任务: (i)我希望存储与用户的所有对话。 (ii)当用户打开对话时,我想获取最近10次对话。从Gupshup访问Firebase数据库

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将数据添加到火力地堡数据库,生成实时在哪个数据被添加。

聊天:

-KY4yWKeGKIKPf1qf74G:

“喜”

-KY4yWKfjoztU0EBPe1g:

“你好,有什么可以帮你吗?”

-KY4ykQtSus8srqa7okF:

“好这里是交易:买比萨饼今天GE ...”

-KY4ykQtSus8srqa7okG:

“表现出一定的交易”

当我尝试使用HTTP GET https://My应用程序名称.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吗?

在此先感谢

回答

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