1

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.

1 Answers1

0

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);
navjotdhanawat
  • 333
  • 1
  • 3
  • 13
  • I am getting the parameters in correctly through "...ask/[parameters]" – Michael Rulle Aug 04 '17 at 09:23
  • i hit [enter] to add new line in the previous post, and submitted an incomplete comment, here's the full version: I am getting the parameters in correctly through "...ask/[parameters]" i had a console.log(keywords) that printed all the keywords. And they are all there. the problem is that the promise returned from the MongoDB is not being parsed by chatfuel. But it does print in the console, and i can see it when querying in both chrome and firefox. the hardcoded example you wrote works perfectly. – Michael Rulle Aug 04 '17 at 09:36