Skip to content

Instantly share code, notes, and snippets.

@donalod
Last active May 12, 2019 23:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save donalod/02d9bb058d8c845771a37f544d160ba9 to your computer and use it in GitHub Desktop.
Save donalod/02d9bb058d8c845771a37f544d160ba9 to your computer and use it in GitHub Desktop.
tito_to_slack_ticket_counts.rb
#!/usr/bin/env ruby
require "httparty"
p "Running iNOG Tito ticket script at #{Time.now.to_s}"
# Replace <replace_with_yours> with your specific tokens, data, or urls
slack_webhook_url = "<replace_with_yours>"
tito_token = "<replace_with_yours>"
# Change the high level values for your bot
event = "iNOG::13"
twitter_publicity_tweet = "https://twitter.com/intent/retweet?tweet_id=1125512268157018113"
linkedin_share_post = "https://www.linkedin.com/feed/update/urn:li:activity:6531449684345524224"
upper_bound = 140
percent_attendance_expected = 70
# ==============================
ticket_pages = `curl -s --request GET --url 'https://api.tito.io/v3/inog/13/tickets' --header 'Authorization: Token token=secret_live_j-jrzNHBPsxk9DyNTqAC' --header 'Accept: application/json' | jq -r '.meta.total_pages'`
ticket_pages = ticket_pages.to_i
if !ticket_pages.is_a? Integer
abort("Ticket pages is not an Integer")
end
p "#{ticket_pages.to_s}"
ticket_count,n = 0,1
while n <= ticket_pages+1
tickets = 0
tickets = `curl -s --request GET --url 'https://api.tito.io/v3/inog/13/tickets/?page=#{n}' --header 'Authorization: Token token=#{tito_token}' --header 'Accept: application/json' | jq -r '.tickets[].state' | grep -i complete | wc -l`
p "Tickets: #{tickets.to_s}"
tickets = tickets.gsub(/\s+/, ' ').strip.to_i
ticket_count = ticket_count + tickets
p "#{ticket_count.class.to_s}"
p "Ticket count: #{ticket_count.to_s}"
n += 1
end
if ticket_count <= 0 || ticket_count == "0" || ticket_count > upper_bound
status = "Error retrieving ticket count or no tickets completed. "
end
status ||= ""
percent_registered = ((ticket_count.to_f/upper_bound.to_f)*100).round
tickets_left = upper_bound - ticket_count
p "#{percent_registered}"
current_attendance_expected = ((percent_attendance_expected.to_f/100)*ticket_count).round
max_attendance_expected = ((percent_attendance_expected.to_f/100)*upper_bound).round
calculations_based_on = "We currently expect *#{current_attendance_expected.to_s}* people to show up (using *#{percent_attendance_expected.to_s}%* attendance of *#{ticket_count}*).\n\n _Note:_ *#{percent_attendance_expected}%* of *#{upper_bound}* is aiming for *#{max_attendance_expected}* to attend."
result = HTTParty.post(slack_webhook_url.to_str,
body: {
text: "#{status}:star: *#{event}* :star: has *#{tickets_left.to_s}* tickets left with a *completed* ticket count of *#{ticket_count.to_s}* (which is *#{percent_registered.to_s}%* of the *#{upper_bound.to_s}* ticket pool)",
attachments: [
title: "Stay Connected and Spread the Love",
title_link: "https://inog.net",
author_name: "admin@inog.net",
author_link: "mailto:admin@inog.net",
text: "#{calculations_based_on}",
fallback: "See in dashboard at https://ti.to/inog/13/admin",
actions: [
{
type: "button",
text: "Tweet Event :pray:",
url: "#{twitter_publicity_tweet}"
},
{
type: "button",
text: "Linkedin Post :link:",
url: "#{linkedin_share_post}"
},
{
type: "button",
text: "Ticket Dashboard 🖥",
url: "https://ti.to/inog/13/admin"
}
],
footer: "From: Tickebot :inog: with :heart:",
]
}.to_json,
:headers => { 'Content-Type' => 'application/json' } )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment