-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsend_monthly_project_email.py
174 lines (148 loc) · 4.98 KB
/
send_monthly_project_email.py
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
# See: https://github.com/jonathandion/awesome-emails#templates
#
# 1. Get the CSV of pitch dataset, and filter for past month.
#
# 1. Fetch the template with default section content
#
# GET /templates/{template_id}/default-content
#
# 1. Rebuild project section with data from CSV
#
# 1. Create campaign, and schedule to send later
#
# POST /campaigns
#
# 1. Replace the template sections with data
#
# PUT /campaigns/{campaign_id}/content
#
# 1. Schedule the campaign to send
#
# POST /campaigns/{campaign_id}/actions/schedule
#
# 1. Notify slack of the scheduled campaign with archive_url to preview.
from datetime import datetime, timedelta
from jinja2 import Template
from mailchimp3 import MailChimp
import os
import pystache
import pytz
from commands.utils.slackclient import CustomSlackClient
def str2bool(v):
return v.lower() in ("yes", "true", "t", "1")
DEBUG = str2bool(os.getenv('DEBUG', ''))
MAILCHIMP_API_KEY = os.getenv('MAILCHIMP_API_KEY')
MAILCHIMP_API_USER = os.getenv('MAILCHIMP_API_USER')
MAILCHIMP_LIST_ID = os.getenv('MAILCHIMP_LIST_ID')
MAILCHIMP_TEMPLATE_ID = int(os.getenv('MAILCHIMP_TEMPLATE_ID'))
MAILCHIMP_SECTION_NAME = os.getenv('MAILCHIMP_SECTION_NAME')
SLACK_API_TOKEN = os.getenv('SLACK_API_TOKEN')
SLACK_ANNOUNCE_CHANNEL = os.getenv('SLACK_ANNOUNCE_CHANNEL_ORG')
def get_project_data():
projects = [
{
'name': 'HomeTO',
'tags': [
'working group',
],
'description': 'We empower at risk, low income, and homeless individuals through inclusive digital tools.',
},
{
'name': 'Women and Color',
'tags': [
'working group',
],
'description': 'Online community of talented women and people of colour available for speaking opportunities at tech-related events. We’ve launched! We are now planning to expand into more cities in Canada and the USA.',
},
{
'name': 'Civic Tech 101',
'tags': [
'learning group',
],
'description': 'Introduce you to Civic Tech Toronto and learn about one another. Please go here if it’s your first time!',
},
]
return projects
projects = get_project_data()
mc_client = MailChimp(mc_api=MAILCHIMP_API_KEY, mc_user=MAILCHIMP_API_USER)
template = mc_client.templates.default_content.all(template_id=MAILCHIMP_TEMPLATE_ID)
sections_data = template['sections']
# TODO
template = """
{%- if learning_groups -%}
<h2>Learning Groups</h2>
<ul>
{%- for group in learning_groups %}
<li>
<strong>{{ group.name }}.</strong>
{{ group.description }}
</li>
{%- endfor %}
</ul>
{%- endif %}
{% if working_groups -%}
<h2>Working Groups</h2>
<ul>
{%- for group in working_groups %}
<li>
<strong>{{ group.name }}.</strong>
{{ group.description }}
</li>
{%- endfor %}
</ul>
{%- endif -%}
"""
template = Template(template.strip())
context = {
'learning_groups': [p for p in projects if 'learning group' in p['tags']],
'working_groups': [p for p in projects if 'working group' in p['tags']],
}
content = template.render(**context)
if DEBUG:
print(content)
exit(0)
sections_data['projects'] = content
campaign_data = {
'recipients': {
'list_id': MAILCHIMP_LIST_ID,
},
'settings': {
'subject_line': 'Pitch recap for {}'.format('March'),
'from_name': 'Civic Tech Toronto',
'reply_to': '[email protected]',
'template_id': MAILCHIMP_TEMPLATE_ID,
},
'type': 'regular',
}
campaign = mc_client.campaigns.create(campaign_data)
content_data = {
'template': {
'id': MAILCHIMP_TEMPLATE_ID,
'sections': sections_data,
}
}
mc_client.campaigns.content.update(campaign_id=campaign['id'], data=content_data)
def calculate_send_time():
d = timedelta(days=1)
send_time = datetime.utcnow().replace(tzinfo=pytz.utc) + d
return send_time
send_time = calculate_send_time()
rounded_send_time = send_time.replace(minute=15*(send_time.minute // 15))
mc_client.campaigns.actions.schedule(campaign_id=campaign['id'], data={'schedule_time': rounded_send_time})
list_data = mc_client.lists.get(MAILCHIMP_LIST_ID)
tmpl_vars = {
'subscriber_count': list_data['stats']['member_count'],
# TODO: Convert this time from UTC to ET
'send_date': rounded_send_time.strftime('%a, %b %-m @ %-I:%M') + rounded_send_time.strftime('%p').lower(),
'preview_url': campaign['archive_url'],
}
thread_template = open('templates/send_monthly_project_email.txt').read()
thread_content = pystache.render(thread_template, tmpl_vars)
if DEBUG or not SLACK_API_TOKEN:
print(thread_content)
else:
sc = CustomSlackClient(SLACK_API_TOKEN)
sc.bot_thread(
channel=SLACK_ANNOUNCE_CHANNEL,
thread_content=thread_content,
)