Forked from torpio/Subscribe to Campaign Monitor.coffee
Created
August 17, 2020 00:42
-
-
Save f7en/91823b06da64c5835abc441c22341319 to your computer and use it in GitHub Desktop.
An example script to demonstrate adding a subscriber to Campaign Monitor.
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 example Torpio script creates a Campaign Monitor subscriber from the details entered into its settings. It should give you a good idea how to: | |
1. Add <a href="https://torpio.com/developers/settings_editor.html" target="_blank">settings</a> to a Torpio script. | |
2. Add a subscriber through Torpio's ```service.post 'campaign_monitor'``` service wrapper. | |
### | |
settings = {} | |
settings.client_name = '' | |
settings.list_name = '' | |
settings.email = '[email protected]' | |
settings.first_name = 'Someone' | |
settings.last_name = 'Somewhere' | |
if settings.client_name is "" | |
log.error "Please enter a client name into settings." | |
else if settings.list_name is "" | |
log.error "Please enter a list name into settings." | |
else if !settings.email | |
log.error "Please provide an email address in settings." | |
else if !settings.first_name or !settings.last_name | |
log.error "Please provide a first and last name in settings." | |
else | |
# Get the client ID from the name | |
service.get 'campaign_monitor', 'clients.json', null, (err, status, result) -> | |
handle_api_result err, status, result, (err) -> | |
if !err | |
if !result.length | |
log.error "We couldn't find the client name you entered. Are you sure it's valid and correct?" | |
else | |
client = _.findWhere(result, { Name: settings.client_name }) | |
if !client | |
log.error "We couldn't find the client name you entered. Are you sure it's valid and correct?" | |
else | |
# Get the list ID from the name | |
service.get 'campaign_monitor', 'clients/' + client.ClientID + '/lists.json', null, (err, status, result) -> | |
handle_api_result err, status, result, (err) -> | |
if !err | |
if !result.length | |
log.error "We couldn't find the list name you entered. Are you sure it's valid and correct?" | |
else | |
list = _.findWhere(result, { Name: settings.list_name }) | |
if !list | |
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 = | |
EmailAddress: settings.email | |
Name: settings.first_name + " " + settings.last_name | |
service.post 'campaign_monitor', 'subscribers/' + list.ListID + '.json', data, (err, status, result) -> | |
handle_api_result err, status, result, (err) -> | |
if !err | |
log.info "Subscriber added!" | |
handle_api_result = (err, status, result, fn) -> | |
if err or status >= 500 # Temporary error | |
log.error "We couldn't access Campaign Monitor because of an unknown error." | |
fn yes | |
else if status >= 400 # Request failed | |
if result.Message? | |
log.error result.Message | |
else | |
log.error "There was an error when trying to access Campaign Monitor, 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