diff --git a/.github/workflows/push-events-daily.yml b/.github/workflows/push-events-daily.yml new file mode 100644 index 0000000..c495818 --- /dev/null +++ b/.github/workflows/push-events-daily.yml @@ -0,0 +1,24 @@ +name: "Push events daily" + +on: + workflow_dispatch: + schedule: + - cron: '0 15 * * *' + +jobs: + syndicate: + name: "Post events" + runs-on: ubuntu-latest + + steps: + - name: "Check out repository" + uses: actions/checkout@v3 + + - uses: ruby/setup-ruby@v1 + with: + bundler-cache: true + + - run: bundle exec ruby main.rb --destinations=TD + env: + SYN_ENV: production + TD_SLACK_WEBHOOK: ${{ secrets.TD_SLACK_WEBHOOK }} diff --git a/event.rb b/event.rb index ac2637d..2b48652 100644 --- a/event.rb +++ b/event.rb @@ -20,17 +20,9 @@ def self.parse_duration(iso8601_duration) parts.join(", ") + " long" end - def self.within_next_two_weeks?(date_string) - date = Date.parse(date_string) - today = Date.today - date >= today && date <= (today + 14) - end - def self.format_slack(group) return if group["unifiedEvents"]["count"] == 0 - return unless within_next_two_weeks?(group["unifiedEvents"]["edges"][0]["node"]["dateTime"]) - event_blocks = [{ type: "section", text: { @@ -57,12 +49,10 @@ def self.format_slack(group) } ] }, - { - type: "divider" - }] + ] if group["name"] == "Tampa Devs" - event_blocks[0][:text][:text].prepend(":tampadevs: ") + event_blocks[0][:text][:text] = ":tampadevs:" + " " + event_blocks[0][:text][:text] end if group["unifiedEvents"]["edges"][0]["node"]["venue"] diff --git a/main.rb b/main.rb index 18457b4..0b9651c 100644 --- a/main.rb +++ b/main.rb @@ -2,6 +2,7 @@ require "json" require "net/http" +require 'optparse' require_relative "event" require_relative "slack" @@ -12,8 +13,18 @@ def initialize @dry_run = ENV["SYN_ENV"] != "production" end - def fetch - groups = JSON.parse(Net::HTTP.get(URI("https://events.api.tampa.dev/"))) + def fetch(announcement_type, destinations) + + events_url = case announcement_type + when :weekly + 'https://events.api.tampa.dev?within_days=7' + when :daily + 'https://events.api.tampa.dev?within_hours=24' + when :hourly + 'https://events.api.tampa.dev?within_hours=1' + end + + groups = JSON.parse(Net::HTTP.get(URI(events_url))) sorted_events = [] formatted_events = [] @@ -31,14 +42,48 @@ def fetch formatted_events << event unless event.nil? end + # fencepost + formatted_events[0...-1].each do |element| + element << { type: "divider" } + end + if formatted_events.empty? puts "No events to post, exiting with nothing to do." exit end - Slack.syndicate(formatted_events, @dry_run) + Slack.syndicate(formatted_events, announcement_type, destinations, @dry_run) end end -syn = EventSyndicator.new -syn.fetch +def main + options = {} + OptionParser.new do |opts| + opts.banner = "Usage: main.rb [options]" + + # Boolean argument for --daily, --hourly, or --weekly + announcement_types = [:daily, :hourly, :weekly] + opts.on("--daily", "Set announcement type to daily") do + options[:announcement_type] = :daily + end + opts.on("--hourly", "Set announcement type to hourly") do + options[:announcement_type] = :hourly + end + opts.on("--weekly", "Set announcement type to weekly") do + options[:announcement_type] = :weekly + end + + # Argument for --destinations= + opts.on("--destinations=DESTINATIONS", "Comma-separated list of destinations") do |destinations| + options[:destinations] = destinations.split(',') + end + end.parse! + + announcement_type = options[:announcement_type] || :weekly + destinations = options[:destinations] || ['TD', 'TBT', 'TBUX'] + + syn = EventSyndicator.new + syn.fetch(announcement_type, destinations) +end + +main diff --git a/slack.rb b/slack.rb index e239bd7..ac80984 100644 --- a/slack.rb +++ b/slack.rb @@ -17,25 +17,33 @@ def initialize @message = [] end - def self.syndicate(events, dry_run) + def self.syndicate(events, announcement_type, destinations, dry_run) return if events.empty? - @payload = payload(events) + @payload = payload(events, announcement_type) if dry_run puts message_json else - post + post destinations end end - def self.payload(events) + def self.payload(events, announcement_type) + header_text = case announcement_type + when :weekly + ":balloon: Happening This Week" + when :daily + ":earth_americas: Happening Today" + when :hourly + ":loudspeaker: Happening Soon" + end header = [ { - type: "section", + type: "header", text: { - type: "mrkdwn", - text: ":balloon: Upcoming events:" + type: "plain_text", + text: header_text, } }, { @@ -44,6 +52,7 @@ def self.payload(events) ] footer = [ + { type: "divider" }, { type: "actions", elements: [ @@ -101,7 +110,12 @@ def self.payload(events) } ] - [header, events.reduce([], :concat), footer].reduce([], :concat) + elements = [header, events.reduce([], :concat)] + if @announcement_type == 'weekly' + elements << footer + end + + elements.reduce([], :concat) end def self.message_json @@ -110,10 +124,12 @@ def self.message_json }.to_json end - def self.post + def self.post(destinations) return if @payload.length == 0 - targets = [ENV["TD_SLACK_WEBHOOK"], ENV["TBT_SLACK_WEBHOOK"], ENV["TBUX_SLACK_WEBHOOK"]] + targets = destinations.map do |channel| + ENV["#{channel}_SLACK_WEBHOOK"] + end targets.each do |t| uri = URI.parse(t)