-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailchimp.rb
253 lines (208 loc) · 7.81 KB
/
mailchimp.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
require "rubygems"
require "kramdown"
require "front_matter_parser"
require "yaml/store"
require "MailchimpMarketing"
require "roadie"
unless File.exist?("config.yaml")
puts "You need to create a file called config.yaml that contains your Mailchimp API key and some other configuration info. Look in mailchimp.rb for a template."
exit
end
# config.yaml template:
# ---
# api_key: "foo"
# list_id: "bar"
# data_center: "blee"
# from_name: "Frodo Baggins"
# reply_to: "[email protected]"
CONFIG = YAML.load_file("config.yaml")
API_KEY = CONFIG["api_key"]
LIST_ID = CONFIG["list_id"]
DATA_CENTER = CONFIG["data_center"]
FROM_NAME = CONFIG["from_name"]
REPLY_TO = CONFIG["reply_to"]
$db = YAML::Store.new "campaigns.yaml"
$client = MailchimpMarketing::Client.new
$client.set_config( { api_key: API_KEY, server: DATA_CENTER } )
$tag_ids = {}
tag_lookup = $client.lists.list_segments( LIST_ID )
tag_lookup["segments"].each do |segment|
$tag_ids[segment["name"]] = segment["id"]
end
def update_tags
$tag_ids.each do |tag_name, tag_id|
$client.lists.update_segment( LIST_ID, tag_id, { "name" => tag_name } )
end
end
def create_new_campaign( with_slug: )
$db.transaction do
if $db[with_slug] != nil
puts "You already have a message with that slug!"
return
end
end
filename = "messages/#{with_slug}.md"
unless File.exist?(filename)
puts "There's no file at #{filename}..."
return
end
parsed_content = FrontMatterParser::Parser.parse_file( filename )
header = parsed_content.front_matter
response = $client.campaigns.create(
{ "type" => "regular",
"recipients" => { "list_id" => LIST_ID },
"settings" => { "subject_line" => header["subject_line"],
"title" => header["title"],
"preview_text" => header["preview"],
"from_name" => FROM_NAME,
"reply_to" => REPLY_TO,
"auto_footer" => false },
"tracking" => { "opens" => true,
"html_clicks" => true }
} )
campaign_id = response["id"]
$db.transaction do
unless $db[with_slug]
$db[with_slug] = { "id" => campaign_id,
"subject_line" => header["subject_line"],
"title" => header["title"],
"preview_text" => header["preview"],
"created_at" => Time.now }
end
end
update_campaign_content( with_slug: with_slug )
return campaign_id
end
TEMPLATE_PATH = "template.html"
YIELD_STRING = "__YIELD__"
# I'm putting this here, rather than in the template, because Markdown has
# problems with the pipe character used in Mailchimp's special codes and
# this was the easiest way to solve the problem!
UNSUBSCRIBE_CONTENT_HTML = <<~HEREDOC
<hr/>
<p class="meta">You’re receiving this message because you are a member in good standing of the <a href="https://society.robinsloan.com">Society of the Double Dagger</a>. I’m Robin Sloan, author of the novels Sourdough and Mr. Penumbra’s 24-Hour Bookstore.</p>
<p class="meta">You can <a href="*|UNSUB|*">unsubscribe from all emails</a> instantly.</p>
HEREDOC
UNSUBSCRIBE_CONTENT_TEXT = <<~HEREDOC
You're receiving this message because you are a member in good standing of the Society of the Double Dagger. I'm Robin Sloan, author of the novels Sourdough and Mr. Penumbra's 24-Hour Bookstore.
You can unsubscribe from all emails instantly: *|UNSUB|*
HEREDOC
def update_campaign_content( with_slug: )
$db.transaction do
unless $db[with_slug]
puts "There's no campaign with that slug..."
return
end
end
filename = "messages/#{with_slug}.md"
parsed_content = FrontMatterParser::Parser.parse_file(filename)
header = parsed_content.front_matter
to_tags = header["tags"]
# By including this dummy condition, you force Mailchimp to dynamically
# recalculate the segment each time, which is what you want; without it,
# I found that Mailchimp would sometimes send to "stale" segments.
# It's a bit gross, I know!
dummy_condition = [{ "condition_type" => "TextMerge",
"field" => "HASH",
"op" => "is",
"value" => "foo"
}]
segment_options = { "match" => "any",
"conditions" => dummy_condition + to_tags.map do |tag|
{ "condition_type" => "StaticSegment",
"field" => "static_segment",
"op" => "static_is",
"value" => $tag_ids[tag]
}
end
}
markdown_content = parsed_content.content
html_content = Kramdown::Document.new(markdown_content).to_html
template = File.open(TEMPLATE_PATH).read
html_email = template.gsub(YIELD_STRING, html_content + UNSUBSCRIBE_CONTENT_HTML)
document_to_be_inlined = Roadie::Document.new(html_email)
inlined_html_email = document_to_be_inlined.transform
inlined_html_email = inlined_html_email.gsub("%7C", "|") # come ON
text_email = markdown_content.strip + "\n\n" + UNSUBSCRIBE_CONTENT_TEXT
$db.transaction do
if $db[with_slug]
campaign_id = $db[with_slug]["id"]
$client.campaigns.update( campaign_id,
{ "recipients" => { "list_id" => LIST_ID,
"segment_opts" => segment_options },
"settings" => { "subject_line" => header["subject_line"],
"title" => header["title"],
"preview_text" => header["preview"],
"from_name" => FROM_NAME,
"reply_to" => REPLY_TO,
"auto_footer" => false },
"tracking" => { "opens" => true,
"html_clicks" => true }
} )
$client.campaigns.set_content( campaign_id, {"html" => inlined_html_email, "plain_text" => text_email } )
created_at = $db[with_slug]["created_at"]
$db[with_slug] = { "id" => campaign_id,
"subject_line" => header["subject_line"],
"title" => header["title"],
"preview_text" => header["preview"],
"to_tags" => to_tags,
"created_at" => created_at,
"modified_at" => Time.now }
end
end
end
def test_campaign( with_slug:, to_email: )
$db.transaction do
if $db[with_slug]
campaign_id = $db[with_slug]["id"]
response = $client.campaigns.send_test_email( campaign_id,
{ "test_emails" => [to_email],
"send_type" => "html" } )
puts response
puts "Sent test email!"
else
puts "There's no campaign with that slug..."
return
end
end
end
def delete_campaign( with_slug: )
$db.transaction do
if $db[with_slug]
campaign_id = $db[with_slug]["id"]
$client.campaigns.remove(campaign_id)
$db[with_slug] = nil
else
puts "There's no campaign with that slug..."
return
end
end
puts "Deleted campaign with slug #{with_slug}."
end
unless ARGV.length > 1
puts "You need some arguments!"
exit
end
slug = ARGV[0]
case ARGV[1]
when "create"
id = create_new_campaign( with_slug: slug )
puts "Created new campaign with slug #{slug} and id #{id}"
when "delete"
delete_campaign( with_slug: slug )
when "update"
id = update_campaign_content( with_slug: slug )
puts "Updated campaign with slug #{slug}"
when "test"
if ARGV[2]
test_campaign( with_slug: slug, to_email: ARGV[2] )
else
test_campaign( with_slug: slug, to_email: "[email protected]" )
end
when "delete"
delete_campaign( with_slug: slug )
when "send"
puts "Sending from the command line is disabled. Go press the button in Mailchimp!"
else
puts "That's not a valid command."
end