One of the nice things about Tropo is that it does a lot of the heavy lifting for you, letting you add amazing features to existing products. One very popular contact manager is Constant Contact. Since your contacts must opt-in to this service, combining this with Tropo provides you a nice way to keep in touch with them beyond simple email.
In this post I’ll describe how I created a Python script that retrieves your contact list from CC and sends them a text message using Tropo. I’m using a hosted file located on the cloud at Tropo.com for this.
What you’ll need:
- Python
- Your Constant Contact API key (from http://developer.constantcontact.com/)
- Your Constant Contact username and password
- A Tropo account
Let’s start with the easy part, making a hosted Tropo application to send our users a message.
- Log in to tropo.com
- From “Your Applications”, click “Create New Application”
- Click “Tropo Scripting”
- Give your application a name
- For the URL, click “Hosted File” and then “Create a new hosted file for this application”
- Fill in the filename (in this example I’m going to use JavaScript on the Tropo side, so I chose test.js)
- Then just add the following 2 lines of code
call('+' + numberToDial, {
network:"SMS"});
say(msg);
Then just click “Create File” and then “Create Application”. Once our magic monkey finishes creating your application, you will see the settings page for your application.
Next step is to add a phone number to this application. Click “Add a new phone number” and then choose your area code (Tropo only supports SMS from US and Canadian numbers at this time), then click the plus symbol to add this phone number to the application. Once your have completed these steps you’ll be taken back to the application settings page.
That’s almost all we need from Tropo. Let’s take a look at the Python code:
import urllib
import urllib2
from xml.dom import minidom
cc_url = 'https://api.constantcontact.com/ws/customers/{user_name}/contacts' cc_api_key = '{paste_your_api_key_inbetween_the_quotes}'
cc_username = '{user_name}'
cc_password = '{password}'
TROPO_URL = 'http://api.tropo.com/1.0/sessions?action=create&token={The Outbound Messaging Token goes here}'
SMS_MSG = 'Wishing you a happy holidays! - From the Tropo Team'
This first part, shown above, sets up our URLs – one for Constant Contact and one for Tropo. SMS_MSG will contain the content used as the body of your message.
#CC uses Basic Authentication for their API. This code sets up a password manager to take care of that for us passman = urllib2.HTTPPasswordMgrWithDefaultRealm() passman.add_password(None, cc_url, cc_api_key + '%' + cc_username, cc_password) authhandler = urllib2.HTTPBasicAuthHandler(passman) opener = urllib2.build_opener(authhandler) urllib2.install_opener(opener)
There's some heavy lifting going on here! Constant Contact is configured to require Basic Authentication for their API. Python provides us with a really nice password manager so we don't have to worry about encoding this into our HTTP request.
pagehandle = urllib2.urlopen(cc_url)
data = pagehandle.read()
dom = minidom.parseString(data)
entry_node = dom.getElementsByTagName("entry")
This code above sends a request to Constant Contact and parses the XML result. Each of your contacts is stored as an Entry in the XML data. What we want to do is loop over each one of these entries to get the unique ID:
for entry in entry_node:
id = entry.getElementsByTagName("id")
id = id[0].firstChild.nodeValue
pagehandle = urllib2.urlopen(cc_url + '/' + id.rsplit('/', 1)[1])
data = pagehandle.read()
dom = minidom.parseString(data)
contact_node = dom.getElementsByTagName("Contact")
Now we've iterated over all of our entries, and requested for the Constant Contact API to return the detailed record for each Contact. The field that we'll use to send our contacts an SMS is defined as "HomePhone" in the code below:
for contact in contact_node:
phone_num = contact.getElementsByTagName("HomePhone")
phone_num = phone_num[0].firstChild.nodeValue
url = TROPO_URL + '&numberToDial=' + phone_num + '&msg=' + urllib.quote(SMS_MSG)
page = urllib2.urlopen(url)
data = page.read()
Now we've parsed out the phone number for our users and requested that Tropo send them our season's greetings!
Note: If your phone numbers are not in a 10 digit format, this request will fail.
Note 2: The outbound messaging token can be found under the settings for your application.
This is something that could clearly be extended to a wide range of things: running specials, thanking customers, really the possibilities are only limited by your imagination. Also, if you wanted to have Tropo talk to the person instead of send a text message, just make the first line of your hosted application look like this:
call('+' + numberToDial);
To run this, take the script, edit it with your information and run it from a terminal window:
python my_script_name.py
And here's the code in full:
import urllib
import urllib2
from xml.dom import minidom
cc_url = 'https://api.constantcontact.com/ws/customers/{user_name}/contacts' cc_api_key = '{paste_your_api_key_inbetween_the_quotes}'
cc_username = '{user_name}'
cc_password = '{password}'
TROPO_URL = 'http://api.tropo.com/1.0/sessions?action=create&token={The Outbound Messaging Token goes here}'
SMS_MSG = 'Wishing you a happy holidays! - From the Tropo Team'
#CC uses Basic Authentication for their API. This code sets up a password manager to take care of that for us
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, cc_url, cc_api_key + '%' + cc_username, cc_password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler) urllib2.install_opener(opener)
pagehandle = urllib2.urlopen(cc_url)
data = pagehandle.read()
dom = minidom.parseString(data)
entry_node = dom.getElementsByTagName("entry")
for entry in entry_node:
id = entry.getElementsByTagName("id")
id = id[0].firstChild.nodeValue
pagehandle = urllib2.urlopen(cc_url + '/' + id.rsplit('/', 1)[1])
data = pagehandle.read()
dom = minidom.parseString(data)
contact_node = dom.getElementsByTagName("Contact")
for contact in contact_node:
phone_num = contact.getElementsByTagName("HomePhone")
phone_num = phone_num[0].firstChild.nodeValue
url = TROPO_URL + '&numberToDial=' + phone_num + '&msg=' + urllib.quote(SMS_MSG)
page = urllib2.urlopen(url)
data = page.read()
Questions or comments? Post to our forums or send us an email to support@tropo.com.



I’ve been doing my Python Tropo development on top of Google App Engine, for the past couple of years. Out of the box, App Engine uses the
For you pythonistas out there, I merged in some changes today to the “
