1

I have quite simple yet complicated question to ask. I'm developing facebook messenger bot and stuck into one issue:

I have JSON file with structure like:

{ 
  itemname1: 'Name1',
  itemaddress1: 'Address1',
  itemtelephone1: 'Telephone1',
  services1: {
    service1name1: 'servicename1',
    service1price1: 'serviceprice2' 
    }
  itemname2: 'Name2',
  itemaddress2: 'Address2',
  itemtelephone2: 'Telephone2',
  services2: {
    service2name1: 'servicename1',
    service2price1: 'serviceprice2' 
    }, {
    service2name1: 'servicename1',
    service2price1: 'serviceprice2' 
    }
}

And I read it from file.json.

Now to message user, I have to generate JSON object in order Facebook to understand it. And the scheme is:

function sendGenericMessage(sender) {
messageData = {
"attachment": {
  "type": "template",
  "payload": {
    "template_type": "generic",
    "elements": [{
      "title": "First card",
      "subtitle": "Element #1 of an hscroll",
      "image_url": "http://messengerdemo.parseapp.com/img/rift.png",
      "buttons": [{
        "type": "web_url",
        "url": "https://www.messenger.com/",
        "title": "Web url"
      }, {
        "type": "postback",
        "title": "Postback",
        "payload": "Payload for first element in a generic bubble",
      }],
    },{
      "title": "Second card",
      "subtitle": "Element #2 of an hscroll",
      "image_url": "http://messengerdemo.parseapp.com/img/gearvr.png",
      "buttons": [{
        "type": "postback",
        "title": "Postback",
        "payload": "Payload for second element in a generic bubble",
      }],
    }]
  }
}

};

So what I am to achieve is to read from database and generate cards by the count of elements. For example, I have itemname1 and itemname2 in my JSON, so this should result in:

"elements": [{ 
   "name": "First card"
   blah-blah
   }, {
   "name": "Second card"
   blah-blah
   }]

The same goes with buttons. Any ideas how to perform it wisely? Oh yes, I'm using Node.JS with Express and LokiJS for DB implementation. (Though it's more JS algorithm question).

Thank you for your time.

5
  • You should just generate the object and the elements in a loop, and then send the object e.g. using request. It will transform your object into JSON for the request. Commented May 2, 2016 at 6:58
  • @migg That's what I don't understand. What should I do? for (i = 1; i<db.length; i++) { what?} Commented May 2, 2016 at 7:00
  • Are you familiar with node.js and JS? If not then have a look at the answer below, there you have more detail. Commented May 2, 2016 at 7:01
  • If you are unfamiliar with objects in JS, you should check out guides for that, like MDN - Working with Objects or this one on arrays. If the issue is about how to transform JSON strings to objects and back, check out the JSON object Commented May 2, 2016 at 7:13
  • Also, this SO question might have some useful info Commented May 2, 2016 at 7:19

1 Answer 1

2

You don't need to create JSON. You have to work with JS objects and then you can either send them to the client as they are, or use JSON.stringify() to have your JSON.

This way, you will have

var elements = [];
//loop through db elements:
elements.push(element_from_db);
//end_loop

then

messageData.payload.elements = elements;
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, that worked for me just good. I didn't realize that I should work with it as JS Object =)
If my answer helped you solve your issue, please accept it so that future readers will know it also.
Igor Hwang -- can you please paste the code you used for this? var elements = []; //loop through db elements: elements.push(element_from_db); //end_loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.