Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add daily reminders for meetups happening today #3

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/push-events-daily.yml
Original file line number Diff line number Diff line change
@@ -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 }}
14 changes: 2 additions & 12 deletions event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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"]
Expand Down
55 changes: 50 additions & 5 deletions main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "json"
require "net/http"
require 'optparse'
require_relative "event"
require_relative "slack"

Expand All @@ -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 = []
Expand All @@ -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=<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
36 changes: 26 additions & 10 deletions slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
},
{
Expand All @@ -44,6 +52,7 @@ def self.payload(events)
]

footer = [
{ type: "divider" },
{
type: "actions",
elements: [
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand Down