1

Chatfuel 不会解析 JSON-API 返回的整个 json 字符串。欢迎任何帮助。

虽然从 API 返回的 JSON 看起来像这样(在邮递员中):

{
    "messages": [
        {
            "text": "i think you should study"
        },
        {
            "text": "Mikrobiologi"
        }
    ]
}

信使机器人只发送第一个文本。

我的应用程序代码:

router.get('/ask/:question', function(req, res){
  var output = [];
  var keywords = req.params.question.split(' ');
  var answer = qHandler.search(keywords);
  answer.then(function(books){
    output.push({text: 'i think you should study'})
    for (var i = books.length; i > 0; i--){
      output.push({text: books[i-1].title});
      if (i-1 > 0){
        output.push({text: ' and '});
      }
    };
    res.send({messages: output});
  });
});

我尝试更改顺序,在返回的字符串之前和之后添加更多硬编码文本。

在邮递员中,一切看起来都应该如此,但 chatfuel 似乎没有解析插入书名的文本对象。

4

1 回答 1

0

看起来你的代码是正确的。请确保您在不使用 postman 的情况下获取带有 HTTP request.debug 书籍值的参数answer.then(function(books){

在这里,我可以在纯 javascript 中执行此代码

var books = [
    {
        title: 'asdfasdf'
    },{
        title: 'asdfasdf'
    },{
        title: 'asdfasdf'
    },{
        title: 'asdfasdf'
    },{
        title: 'asdfasdf'
    }]

var output = [];
output.push({ text: 'i think you should study' })
for (var i = books.length; i > 0; i--) {
    output.push({ text: books[i - 1].title });
    if (i - 1 > 0) {
        output.push({ text: ' and ' });
    }
};
console.log(output);
于 2017-08-04T08:50:53.853 回答