Skip to content

Instantly share code, notes, and snippets.

@philfreo
Created July 11, 2013 16:45
Show Gist options
  • Save philfreo/5977117 to your computer and use it in GitHub Desktop.
Save philfreo/5977117 to your computer and use it in GitHub Desktop.
Close.io: send a templated email to everyone in a search query
#!/usr/bin/env python
import argparse
from pybars import Compiler
from closeio_api_client.api import CloseIO_API as CloseIO
# We ensure the same templateid doesn't get sent again (see the search query construction)
emails = {
#'Label': ('Search Query', 'Close.io template_id')
'Welcome Email': ('date_created < 2 hours ago', 'tmpl_DCFo6IqNafZMpRZwGWYJnpREf2jYto3LcQ6AucjnD4l'),
}
parser = argparse.ArgumentParser(description='Send some drip emails.')
parser.add_argument('--api_key', required=True, help='Close.io Api Key')
parser.add_argument('--prod', type=bool, default=False, help='Connect to production?')
parser.add_argument('--send', type=bool, default=False, help='Actually send the emails?')
args = parser.parse_args()
client = CloseIO(args.api_key, development=not args.prod)
api_key = client.get('api_key/%s' % args.api_key)
organization_id = api_key['organization_id']
user_id = api_key['user_id']
user = client.get('me')
compiler = Compiler()
for label, drip in emails.items():
query = drip[0]
template_id = drip[1]
print ':::: %s :::: %s ::::' % (label, query)
template = client.get('email_template/%s' % template_id)
template_subject = compiler.compile(template['subject'])
template_body = compiler.compile(template['body'])
#print "(DEBUG) template:", template
has_more = True
offset = 0
while has_more:
resp = client.get('lead', data={'_skip': offset, 'query': "%s and not email_template:%s" % (query, template['id']), '_fields': 'id,name,contacts'})
data = resp['data']
for lead in data:
print "\tLead: ", lead['name']
contact = lead['contacts'][0] if lead['contacts'] else {}
if not contact:
print "\t\tSkipping: Lead has no contact"
continue
if not contact['emails']:
print "\t\tSkipping: Contact has no email"
continue
email_address = contact['emails'][0]['email']
if contact.get('name'):
contact['first_name'] = contact.get('name', '').split()[0].capitalize()
print "\t\tContact: %s (%s)" % (contact['name'], email_address)
# @TODO fixer user to include phone, from_name, from_email for this org.
context = {'lead': lead, 'user': user, 'contact': contact}
email_subject = ''.join(template_subject(context))
print "\t\t(DEBUG) email_subject:", email_subject
email_body = ''.join(template_body(context))
print "\t\t(DEBUG) email_body:", email_body
email = {
'contact_id': contact['id'],
'user_id': user['id'],
'lead_id': lead['id'],
'to': [email_address],
'subject': email_subject,
'body_text': email_body,
'status': 'outbox',
'template_id': template['id']
}
if args.send:
client.post('activity/email', email)
offset += len(data)
has_more = resp['has_more']
@philfreo
Copy link
Author

See https://github.com/elasticsales/closeio-api for closeio_api_client

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment