javascriptjsonnode.jsformattingchatfuel

is this NodeJS API done correctly? - Chatfuel does not parse it as intended


Chatfuel does not parse the entire json string that the JSON-API returns. Any help is welcome.

While the JSON returned from the API looks like this (in postman):

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

the messenger bot only sends the first text.

my code for the app:

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

I have tried changing the order, adding more hardcoded text both before and after the string(s) returned.

In postman everything looks like it should, but chatfuel does not seem to parse text objects with book-titles inserted.


Solution

  • It looks like your code is correct. Please make sure that you are getting parameters with HTTP request.debug books value without using postman under answer.then(function(books){

    Here I am able to do this code in pure 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);