Skip to content

Instantly share code, notes, and snippets.

@torpio
Created February 21, 2014 16:53
Show Gist options
  • Save torpio/9138153 to your computer and use it in GitHub Desktop.
Save torpio/9138153 to your computer and use it in GitHub Desktop.
An example script to demonstrate adding a subscriber to Campaign Monitor.
# 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