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.

Igor Hwang
  • 72
  • 7
  • 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. – crackmigg May 02 '16 at 06:58
  • @migg That's what I don't understand. What should I do? for (i = 1; i – Igor Hwang May 02 '16 at 07:00
  • Are you familiar with node.js and JS? If not then have a look at the answer below, there you have more detail. – crackmigg May 02 '16 at 07:01
  • If you are unfamiliar with objects in JS, you should check out guides for that, like [MDN - Working with Objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects) or [this one on arrays](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array). If the issue is about how to transform JSON **strings** to objects and back, check out [the JSON object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON) – noppa May 02 '16 at 07:13
  • Also, [this SO question](http://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation) might have some useful info – noppa May 02 '16 at 07:19

1 Answers1

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;
Tudor Constantin
  • 26,330
  • 7
  • 49
  • 72
  • 1
    Thank you, that worked for me just good. I didn't realize that I should work with it as JS Object =) – Igor Hwang May 02 '16 at 10:48
  • If my answer helped you solve your issue, please accept it so that future readers will know it also. – Tudor Constantin May 02 '16 at 12:36
  • 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 – ChicagoDude Dec 22 '16 at 05:03