Created
February 21, 2014 17:02
-
-
Save torpio/9138359 to your computer and use it in GitHub Desktop.
Receives an FLG 360 webhook and adds a subscriber to a MailChimp list.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CoffeeScript | |
### | |
This script receives a webhook from FLG 360 and adds the lead's email address to MailChimp. Here's how to use: | |
1. In FLG 360, go to 'Settings > Lead Groups > Lead Types > Edit' and add a webhook to this script's webhook URL (below) on any event ('Received' for example). | |
2. Type in your MailChimp list name (case sensitive). | |
3. Authorize your MailChimp account by clicking the red 'Authorize' button. | |
4. All done, switch the script on and trigger the event in FLG 360 to make sure it works. | |
### | |
settings = {} | |
settings.list_name = '' | |
if !settings.list_name | |
log.error "Please enter a list name into settings to subscribe the email address to." | |
else | |
# Get the list ID from the name | |
data = | |
filters: | |
list_name: settings.list_name | |
service.get 'mailchimp', 'lists/list.json', data, (err, status, result) -> | |
handle_api_result err, status, result, (err) -> | |
if !err | |
if !result.total | |
log.error "We couldn't find the list name you entered. Are you sure it's valid and correct?" | |
else | |
# Now subscribe the email address | |
data = | |
id: result.data[0].id | |
update_existing: yes | |
email: | |
email: request.body.email | |
merge_vars: | |
FNAME: request.body.firstname | |
LNAME: request.body.lastname | |
service.post 'mailchimp', 'lists/subscribe.json', data, (err, status, result) -> | |
handle_api_result err, status, result, (-> null) | |
handle_api_result = (err, status, result, fn) -> | |
if err or status is 503 # Temporary error | |
log.error "The tweet couldn't be sent because of an unknown error." | |
fn yes | |
else if status isnt 200 # Request failed | |
if result.error? | |
log.error result.error | |
else | |
log.error "There was an error when trying to access MailChimp, but we didn't get any details about what went wrong. Maybe try again." | |
fn yes | |
else | |
fn() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment