【问题标题】:is this NodeJS API done correctly? - Chatfuel does not parse it as intended这个 NodeJS API 是否正确完成? - Chatfuel 没有按预期解析它
【发布时间】:2017-08-04 08:33:57
【问题描述】:

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

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

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

messenger bot 只发送第一条文本。

我的应用代码:

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 似乎没有解析插入书名的文本对象。

【问题讨论】:

    标签: javascript json node.js formatting chatfuel


    【解决方案1】:

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

    在这里我可以用纯 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);
    

    【讨论】:

    • 我通过“...ask/[parameters]”正确获取参数
    • 我在上一篇文章中点击[enter]添加新行,并提交了一个不完整的评论,这是完整版:我通过“...ask/[parameters]正确获取参数“我有一个打印所有关键字的 console.log(keywords)。他们都在那里。问题是从 MongoDB 返回的承诺没有被 chatfuel 解析。但它确实在控制台中打印,我可以在 chrome 和 firefox 中查询时看到它。您编写的硬编码示例完美运行。
    猜你喜欢
    • 2019-06-28
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 2010-11-14
    • 1970-01-01
    • 2016-01-10
    • 2023-03-08
    相关资源
    最近更新 更多