Sunday, 9 April 2017

How to use API and JSON Correctly?

I have a bot that is built with botkit and using a Zendesk api to pull information.

I have a function that asks a user for a search term and the bot searches relevant information about that search term. It pulls the information from the Zendesk API and outputs the answer.

I don't understand when accessing the object's value some values are able to be outputted while some are not.

For example, if the user submits 'jim' as the search term. I can pull relevant information by doing this: tickets[0].id + tickets[0].priority + tickets[0].subject + tickets[0].description.

When I do something like this: tickets[4]- I get undefined values.

The full code of what I'm trying to do is this:

    controller.hears(['SEARCH TICKET',/search ticket/gi,  /^.{0,}jirabot.
{0,}$/],
   ['direct_message','direct_mention','mention','ambient'],function(bot,message) 
{

  // start a conversation to handle this response.
  bot.startConversation(message,function(err,convo) {

convo.ask('What are you looking for?',function(response,convo) {

  zendesk.search.list('query='+response.text+'&sort_by=priority&sort_order=desc').then(function(tickets){

  console.log(tickets);
bot.reply(message, 'The Ticket ID Number: ' + tickets[3] + tickets[0].id + '\n The Ticket Priority: ' + tickets[0].priority + '\n The Ticket Subject: ' + tickets[0].subject + '\n The Ticket Description: \n'+ tickets[0].description + '\n');
convo.next();
}
    });

 });

});

}); 

Here is what the JSON looks like:

{
"results": [(in here is the information like ticket subject, priority, id, 
etc.],
"facets": null,
"next_page": null,
"previous_page": null,
"count": 2
}

How do I get the value for count? I get undefined when I do tickets[4].



via traveler316

No comments:

Post a Comment