Zendesk

and James Stiff for the icons

Shows non-solved Zendesk tickets count

Image preview of Zendesk plugin.

zendesk.2m.rb

Edit
Open on GitHub
#!/usr/bin/env ruby

# <xbar.title>Zendesk</xbar.title>
# <xbar.author>Matt Roberts, James Stiff for the icons</xbar.author>
# <xbar.author.github>mattwoberts</xbar.author.github>
# <xbar.image>https://i.imgur.com/e6rdUBh.png</xbar.image>
# <xbar.dependencies>Ruby</xbar.dependencies>
# <xbar.desc>Shows non-solved Zendesk tickets count</xbar.desc>
# <xbar.var>string(VAR_ZENDESK_SUBDOMAIN=""): Your ZenDesk subdomain</xbar.var>
# <xbar.var>string(VAR_ZENDESK_API_KEY=""): Your ZenDesk API key</xbar.var>
# <xbar.var>string(VAR_ZENDESK_EMAIL=""): Your ZenDesk email</xbar.var>

# Configurations
ZENDESK_SUBDOMAIN = ENV["VAR_ZENDESK_SUBDOMAIN"]
ZENDESK_API_KEY = ENV["VAR_ZENDESK_API_KEY"]
ZENDESK_EMAIL = ENV["VAR_ZENDESK_EMAIL"]

require 'net/http'
require 'json'
require 'date'

def fetch_tickets()
  uri = URI("https://#{ZENDESK_SUBDOMAIN}.zendesk.com/api/v2/search.json?query=status<solved")
  req = Net::HTTP::Get.new(uri)
  req.basic_auth "#{ZENDESK_EMAIL}/token", "#{ZENDESK_API_KEY}"
  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
  JSON.parse(res.body)
rescue => e
  []
end

def get_color(count)
  if count > 0
    return "color=white"
  end

  return ""
end

def get_image(count)

  if count > 0
    return "image=iVBORw0KGgoAAAANSUhEUgAAACQAAAAkCAYAAADhAJiYAAAAAXNSR0IArs4c6QAAAJZlWElmTU0AKgAAAAgABQESAAMAAAABAAEAAAEaAAUAAAABAAAASgEbAAUAAAABAAAAUgExAAIAAAARAAAAWodpAAQAAAABAAAAbAAAAAAAAACQAAAAAQAAAJAAAAABQWRvYmUgSW1hZ2VSZWFkeQAAAAOgAQADAAAAAQABAACgAgAEAAAAA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HbVvB7JspyDEPKRA7JrIKkrsCDxCzJYjLxbOFIQYOTNk3cj60bQhxKxqCCb3ghnYPWJkJy2CBjvmsF8j5m04h1pyyt8EV8FvsE3OL3q/RoEyWAU+LXxS90EIZpNxG5gD9jNZ7snPQO7N2CUdU0nSJPBtgSBTCKJc0vIikJeBq8kLo8lwYz3B564KVjxJ0JiV4zLlLdaYJKabgP3scBWLCCK510VIic9GBJHUDux/ncKNuANBeOV9tBWJ6ow7p9sejqz+D+BSRKXeuAtFdlPe9rSEGHm5vQc38lYJ8R6ExnmG92WG5MzoyMNO4FxL8Glc1zXJtdzaDJyLGfgHhCN7TNl8XroAAAAASUVORK5CYII="
end

begin
  tickets = fetch_tickets

  count = 0
  
  if tickets.key?("error")
    puts "Error occurred | " + get_image(count)
    puts "---"
    puts tickets["error"]["title"]
    puts tickets["error"]["message"]
    exit
  end

  if tickets.length > 0
    count = tickets["results"].length
  end

  puts "#{count}" + " | " + get_color(count) + " " + get_image(count)

  puts "---"

  puts "View Tickets | href=https://#{ZENDESK_SUBDOMAIN}.zendesk.com/agent"

rescue StandardError => msg
  puts 'Error occured, please refresh xbar! >' + msg.to_s
end