SMS gateway to Cosm using Tropo

Here I present a terrible way to use Tropo to create an SMS gateway to Cosm feeds while waiting for the official Cosm SMS gateway to exit beta status.

This stores your Cosm API key in plain text (I don’t know how to avoid this) so it is a terrible solution, but a very easy one.

Background

I read http://cosm.com/docs/quickstart/curl.html
curl --request POST
--data '{"title":"My feed", "version":"1.0.0"}'
--header "X-ApiKey: YOUR_API_KEY_HERE"
--verbose
and
http://api.cosm.com/v2/feedshttps://www.tropo.com/docs/scripting/capturing_incoming_text.htm
currentCall.initialText

How to translate that into python? See http://stackoverflow.com/questions/111945/is-there-any-way-to-do-http-put-in-python.

The app

Create a new Tropos application and use a hosted file.
** If you don’t know how to setup Tropo to do the following, see Appendix 1.

File Name: smstocosm.py
File Text:

import urllib2

val = currentCall.initialText
say(val)
test = ('{ "version":"1.0.0", "datastreams":[{"id":"potvalue", "current_value":"%s"} ]}' % val)
contenttype = "X-ApiKey"

#Edit to your Cosm api key and feedurl,
apikey = "COSM-API-KEY"
feedurl = "http://api.cosm.com/v2/feeds/YOUR-FEED-NUMBER"

opener = urllib2.build_opener(urllib2.HTTPHandler)
request = urllib2.Request(feedurl, data=test)
request.add_header(contenttype, apikey)
#request.add_header('Content-Type', 'your/contenttype')
request.get_method = lambda: 'PUT'
url = opener.open(request)

This file can later be edited at https://www.tropo.com/hosting/.
** If you don’t know what your Cosm Api Key or feed URL is, see appendix 3.

Now add a new phone number for your app.
** see appendix 2 if you’re unsure how.

Text that number with a value (e.g. “45”). Wait a few seconds and Tropo will text you back that same value (acts as a confirmation message that Tropo got it). Then wait another 30 seconds or so and refresh your Cosm feed URL. Voila! Your new value shows up there.

Todo next for me, connect a pot to an Arduino Uno + GPRS shield from Sparkfun, have the pot values sent to Cosm via SMS.

===

Appendix One.

https://www.tropo.com/docs/scripting/creating_first_application.htm
1) Start by clicking on the “Your Applications” Quick Link.
2) Click on the link to Create New Application near the top right of the new page.
Click on the Tropo Scripting icon.
4) For Tropo Scripting Application Name, enter in a name like Tropo Test.
5) Click on the “Use a Hosted File” link and choose “Create a new hosted file for this application”.
The File Name can be whatever you’d like, it just needs to end with the language extension you intend to use (.rb for Ruby, .php for PHP, etc). For the File Text, see above.

Appendix Two.

8a) To the right of the Skype phone number is an “Add a new phone number” link – go ahead and click on it.
8b) Select your preferred area code and then click the + sign next to the area code to add it to your application. Click the X at the top right to close the window and you’ll be returned to your application settings page.
9) That’s it!

Appendix Three.

Note: If you don’t have an API key for Cosm yet, follow http://community.cosm.com/quickstart_no_code (actually that is for the old pachube interface, I updated it for the new Cosm interface below):

Login to Cosm and click on “Keys” in the top right corner (or go to https://cosm.com/users/USERNAME/keys, where USERNAME should be replaced by whatever username you chose when you signed up).

Click the blue “+key” button (https://cosm.com/users/USERNAME/keys/new)

Give it some label, select “use any public feed”, and select permissions options ‘read’ and ‘update’ (because you will want to use this key both to retrieve and to update Cosm datastreams). Ignore “advanced restrictions”.
Click “create”.

After clicking “Create” note down the feed ID that you have just created (e.g. http://www.pachube.com/feeds/15556 would be feed ID 15556).