Saturday 8 April 2017

Returning The Right API Information (Zendesk, Botkit)

I'm building a bot through botkit and it is using 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.

My problem is when the user types the search term 'Jim', it will work correctly and pull back information from the tickets[0] element.

However, when a user types in a word like 'bank', it will not work. 'Bank' would have 2 elements within the array- tickets[0], tickets[1]. I understand that I'm only asking the first element of the array to be outputted, but why does it not output anything when theres more than one element within the tickets array?

Here is the code:

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[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 a link to the botkit documentation: https://github.com/howdyai/botkit

Here is a link to the zendesk API documentation: https://developer.zendesk.com/rest_api/docs/core/tickets#list-tickets



via Writer316

No comments:

Post a Comment