-
Notifications
You must be signed in to change notification settings - Fork 1
/
lifecycle.rb
50 lines (41 loc) · 2.24 KB
/
lifecycle.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
SlackRubyBotServer::Config.service_class.instance.on :created do |team, _error, options|
raise 'missing ENV["MAILCHIMP_API_KEY"]' unless SlackRubyBotServer::Mailchimp.config.mailchimp_api_key
raise 'missing ENV["MAILCHIMP_LIST_ID"]' unless SlackRubyBotServer::Mailchimp.config.mailchimp_list_id
mailchimp_client = Mailchimp.connect(SlackRubyBotServer::Mailchimp.config.mailchimp_api_key)
mailchimp_list = mailchimp_client.lists(SlackRubyBotServer::Mailchimp.config.mailchimp_list_id)
slack_client = Slack::Web::Client.new(token: team.token)
# fetch profile of the user installing the bot
raise 'missing team.activated_user_id' unless team.activated_user_id
profile = Hashie::Mash.new(slack_client.users_info(user: team.activated_user_id)).user.profile
raise "error fetching user profile for #{team.activated_user_id}" unless profile
# fetch and merge member tags
tags = SlackRubyBotServer::Mailchimp.config.additional_member_tags
tags = tags.call(team, options) if tags.respond_to?(:call)
tags = (tags + team.tags).uniq if team.respond_to?(:tags)
member = mailchimp_list.members.where(email_address: profile.email).first
if member
member_tags = member.tags.map { |tag| tag['name'] }.sort
tags = (member_tags + tags).uniq
if tags == member_tags
SlackRubyBotServer::Service.logger.debug "Skipping #{profile.email} with identical tags (#{tags.join(', ')}), will not be added to #{SlackRubyBotServer::Mailchimp.config.mailchimp_list_id}, #{team}."
next
end
end
# merge fields
merge_fields = SlackRubyBotServer::Mailchimp.config.additional_merge_fields
merge_fields = merge_fields.call(team, options) if merge_fields.respond_to?(:call)
merge_fields = merge_fields.merge(
'FNAME' => profile.first_name.to_s,
'LNAME' => profile.last_name.to_s
)
# subscribe
mailchimp_list.members.create_or_update(
name: profile.name,
email_address: profile.email,
unique_email_id: "#{team.team_id}-#{team.activated_user_id}",
status: member ? member.status : SlackRubyBotServer::Mailchimp.config.member_status,
tags: tags,
merge_fields: merge_fields
)
SlackRubyBotServer::Service.logger.info "Subscribed #{profile.email} to #{SlackRubyBotServer::Mailchimp.config.mailchimp_list_id}, #{team}."
end