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 rich text 60 minute meetup reminders #2

Closed
wants to merge 1 commit 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
103 changes: 74 additions & 29 deletions event.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,34 @@
# frozen_string_literal: true

require "date"


def truncate_string(text, max_length = 1000)
paragraphs = text.split("\n")

paragraphs.each do |string|
puts string
puts
end

selected_paragraphs = []
current_length = 0

paragraphs.each do |paragraph|
paragraph_length = paragraph.length + 2 # Adding 2 for the "\n\n" that will be re-inserted
if current_length + paragraph_length > max_length
selected_paragraphs << '...'
break
else
selected_paragraphs << paragraph
current_length += paragraph_length
end
end

truncated_text = selected_paragraphs.join("\n")

truncated_text.rstrip # Remove any trailing newlines
end


class MeetupEvent
# Can be used like so:
# \n:clock1: #{parse_duration(group['eventSearch']['edges'][0]['node']['duration'])
Expand Down Expand Up @@ -31,48 +58,66 @@ def self.format_slack(group)

return unless within_next_two_weeks?(group["eventSearch"]["edges"][0]["node"]["dateTime"])

event_blocks = [{
if group["eventSearch"]["edges"][0]["node"]["venue"]
group["eventSearch"]["edges"][0]["node"]["location"] = if group["eventSearch"]["edges"][0]["node"]["venue"]["name"] != "Online event"
":round_pushpin: <https://www.google.com/maps/dir/?api=1&destination=#{group["eventSearch"]["edges"][0]["node"]["venue"].map { |k, v| "#{k}=#{URI.encode_www_form_component(v)}" }.join("&")}|#{group["eventSearch"]["edges"][0]["node"]["venue"].values.join(", ")}>"
else
":computer: Online event"
end
end

event_blocks = [
{
type: "section",
text: {
type: "mrkdwn",
text: "*#{group["name"]}* - *#{group["eventSearch"]["edges"][0]["node"]["title"]}*\n:calendar: #{DateTime.parse(group["eventSearch"]["edges"][0]["node"]["dateTime"]).strftime("%A, %d %B %Y, %I:%M %p")}\n:busts_in_silhouette: #{group["eventSearch"]["edges"][0]["node"]["going"]} going"
text: <<-HEREDOC
*#{group["name"]}* - *#{group["eventSearch"]["edges"][0]["node"]["title"]}*
:clock1: #{DateTime.parse(group["eventSearch"]["edges"][0]["node"]["dateTime"]).strftime("%I:%M %p")}
#{group["eventSearch"]["edges"][0]["node"]["location"]}
:busts_in_silhouette: #{group["eventSearch"]["edges"][0]["node"]["going"]} going
HEREDOC
},
accessory: {
type: "image",
image_url: group["eventSearch"]["edges"][0]["node"]["imageUrl"],
alt_text: "#{group["name"]} - #{group["eventSearch"]["edges"][0]["node"]["title"]}"
}
},
{
type: "actions",
elements: [
{
type: "button",
text: {
type: "plain_text",
text: ":dart: RSVP",
emoji: true
},
url: group["eventSearch"]["edges"][0]["node"]["eventUrl"]
}
]
},
{
type: "divider"
}]
{
type: "actions",
elements: [
{
type: "button",
text: {
type: "plain_text",
text: ":meetup: Event Details",
emoji: true
},
url: group["eventSearch"]["edges"][0]["node"]["eventUrl"]
}
]
},
{
"type": "rich_text",
"elements": [
{
"type": "rich_text_quote",
"elements": [
{
"type": "text",
"text": truncate_string(group["eventSearch"]["edges"][0]["node"]["description"]),
}
]
}
]
}
]

if group["name"] == "Tampa Devs"
event_blocks[0][:text][:text].prepend(":tampadevs: ")
end

if group["eventSearch"]["edges"][0]["node"]["venue"]
event_blocks[0][:text][:text] += if group["eventSearch"]["edges"][0]["node"]["venue"]["name"] != "Online event"
"\n\n:round_pushpin: <https://www.google.com/maps/dir/?api=1&destination=#{group["eventSearch"]["edges"][0]["node"]["venue"].map { |k, v| "#{k}=#{URI.encode_www_form_component(v)}" }.join("&")}|#{group["eventSearch"]["edges"][0]["node"]["venue"].values.join(", ")}>"
else
"\n\n:computer: Online event"
end
end

event_blocks
end

Expand Down
2 changes: 1 addition & 1 deletion main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def initialize
end

def fetch
groups = JSON.parse(Net::HTTP.get(URI("https://events.api.tampa.dev/")))
groups = JSON.parse(Net::HTTP.get(URI("http://localhost:8787/next-hour")))

sorted_events = []
formatted_events = []
Expand Down
74 changes: 17 additions & 57 deletions slack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,67 +31,27 @@ def self.syndicate(events, dry_run)

def self.payload(events)
header = [
# {
# type: "section",
# text: {
# type: "mrkdwn",
# text: "_:loudspeaker: 60 minutes until this event:_"
# }
# },
{
type: "section",
text: {
type: "mrkdwn",
text: ":balloon: Upcoming events:"
"type": "header",
"text": {
"type": "plain_text",
"text": ":loudspeaker: 60 minutes until this event"
}
},
{
type: "divider"
}
},
]

footer = [
{
type: "actions",
elements: [
{
type: "button",
text: {
type: "plain_text",
text: ":earth_americas: All Meetups",
emoji: true
},
value: "see_all_meetups",
url: "https://tampa.dev?utm_source=td_slack_syndication&utm_campaign=organic"
},
{
type: "button",
text: {
type: "plain_text",
text: ":zap: Events API",
emoji: true
},
value: "events_api",
url: "https://github.com/TampaDevs/events.api.tampa.dev"
},
{
type: "button",
text: {
type: "plain_text",
text: ":briefcase: Hire a Developer",
emoji: true
},
value: "events_api",
url: "https://talent.tampa.dev?utm_source=td_slack_syndication&utm_campaign=organic"
},
{
type: "button",
text: {
type: "plain_text",
text: ":newspaper: Tampa Tech News",
emoji: true
},
value: "news_tampa_dev",
url: "https://news.tampa.dev?utm_source=td_slack_syndication&utm_campaign=organic"
}
]
}
]
# puts events

puts JSON.pretty_generate(events.reduce([], :concat))

[header, events.reduce([], :concat), footer].reduce([], :concat)
[header, events.reduce([], :concat)].reduce([], :concat)
end

def self.message_json
Expand All @@ -103,7 +63,7 @@ def self.message_json
def self.post
return if @payload.length == 0

targets = [ENV["TD_SLACK_WEBHOOK"], ENV["TBT_SLACK_WEBHOOK"], ENV["TBUX_SLACK_WEBHOOK"]]
targets = [ENV["TD_SLACK_WEBHOOK"]]

targets.each do |t|
uri = URI.parse(t)
Expand Down