Skip to content

Instantly share code, notes, and snippets.

@debreczeni
Last active February 1, 2019 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debreczeni/681cc038bbb1db41ea7976d988627a53 to your computer and use it in GitHub Desktop.
Save debreczeni/681cc038bbb1db41ea7976d988627a53 to your computer and use it in GitHub Desktop.
Zendesk Ticket search based on search query like "email@address.com" and with results including comments. Using the official Zendesk API v2 ruby gem: https://github.com/zendesk/zendesk_api_client_rb
require 'zendesk_api'
class Zendesk
attr_accessor :client
def initialize
@client = ZendeskAPI::Client.new do |config|
# Mandatory:
config.url = "<- your-zendesk-url ->" # e.g. https://mydesk.zendesk.com/api/v2
# Basic / Token Authentication
config.username = "login.email@zendesk.com"
# Choose one of the following depending on your authentication choice
config.token = "your zendesk token"
config.password = "your zendesk password"
# OAuth Authentication
config.access_token = "your OAuth access token"
# Optional:
# Retry uses middleware to notify the user
# when hitting the rate limit, sleep automatically,
# then retry the request.
config.retry = true
# Logger prints to STDERR by default, to e.g. print to stdout:
require 'logger'
config.logger = Logger.new(STDOUT)
# Changes Faraday adapter
# config.adapter = :patron
# Merged with the default client options hash
# config.client_options = { :ssl => false }
# When getting the error 'hostname does not match the server certificate'
# use the API at https://yoursubdomain.zendesk.com/api/v2
end
end
def find_tickets_including_comments(search_query)
client.search(query: "type:ticket #{search_query}").map do |ticket|
ticket.to_h.slice(*%w[created_at subject]).merge(
'comments' => ticket.comments.map do |comment|
comment.to_h.slice(*%w[created_at via body])
end
)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment