-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslack.rb
134 lines (118 loc) · 3.12 KB
/
slack.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# frozen_string_literal: true
require "uri"
require "net/https"
require "json"
class Slack
attr_accessor :keys, :message, :post_types, :utm_params
def initialize
@utm_params = {
source: "td_slack",
medium: "organic",
campaign: "tampadevs_meetup_promo"
}
@message = []
end
def self.syndicate(events, dry_run)
return if events.empty?
@payload = payload(events)
if dry_run
puts message_json
else
post
end
end
def self.payload(events)
header = [
{
type: "section",
text: {
type: "mrkdwn",
text: ":balloon: Upcoming events:"
}
},
{
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: ":calendar: Event Calendar",
emoji: true
},
value: "newsletter_tampa_dev",
url: "https://go.tampa.dev/calendar?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: Local Tech Jobs",
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: Newsletter",
emoji: true
},
value: "newsletter_tampa_dev",
url: "https://newsletter.tampa.dev?utm_source=td_slack_syndication&utm_campaign=organic"
}
]
}
]
[header, events.reduce([], :concat), footer].reduce([], :concat)
end
def self.message_json
{
blocks: @payload
}.to_json
end
def self.post
return if @payload.length == 0
targets = [ENV["TD_SLACK_WEBHOOK"], ENV["TBT_SLACK_WEBHOOK"], ENV["TBUX_SLACK_WEBHOOK"]]
targets.each do |t|
uri = URI.parse(t)
https = Net::HTTP.new(uri.host, uri.port)
https.use_ssl = true
request = Net::HTTP::Post.new(uri.request_uri)
request["Accept"] = "application/json"
request.content_type = "application/json"
request.body = {
blocks: @payload
}.to_json
response = https.request(request)
puts "#{response.code} #{response.message}: #{response.body}"
end
end
end