Posting Status Updates to Twitter Using Erlang

My buddy Raja was heading up to Tahoe for the first time last weekend, and was asking about road conditions, chain control, etc. I showed him the California DOT I-80 status page which provides status updates in a simple text file:

			
		I 80 
		    [IN THE SAN FRANCISCO BAY AREA]
		    THERE IS A DENSE FOG ADVISORY IN EFFECT AT THE SAN FRANCISCO-OAKLAND BAY 
		BRIDGE /IN SAN FRANCISCO/ - MOTORISTS ARE ADVISED TO DRIVE WITH CAUTION

		    [IN NORTHERN CALIFORNIA & THE SIERRA NEVADA]
		    NO TRAFFIC RESTRICTIONS ARE REPORTED FOR THIS AREA.
		
While this is pretty easy to bookmark on your mobile phone and access on the road, we figured it would be a neat service to post updates of this resource to Twitter. I also wanted to mess around a bit more with Erlang, and figured this would be a fun, quick project. So after a couple of early AM hacking sessions the i80x140 Twitter account now provides updates for I-80 road conditions in the SF Bay Area and the Sierra Nevadas. Biggest challenge was the parsing of semi structured data...Erlang is definitely not the best language for string parsing/manipulation. To quote the Sendmail team: "Erlang's treatment of strings as lists of bytes is as elegant as it is impractical"

It's built-in HTTP support, on the other hand, is pretty robust. While there are fully fledged implementations of Twitter API clients in Erlang, you can post a status update easily with a little bit of Erlang's built-in HTTP module:

 
 %% start inets...
 inets:start(),	
	
 %% Create the Basic Auth token . . .	
 UsernameAndPassword=lists:append([Username, ":", Password]),
 EncodedUsernameAndPassword=base64:encode_to_string(UsernameAndPassword),
 BasicAuthHeaderValue=lists:append("Basic ", EncodedUsernameAndPassword),

 %% POST a status update to Twitter . . .
 TwitterEndpoint="http://twitter.com/statuses/update.xml",
 Headers=[{"Authorization", BasicAuthHeaderValue}],
 ContentType="application/x-www-form-urlencoded",
 MessageBody="status=Hello World!",
 
 {ok, {{_, 200, _}, _, _}}=http:request(post, {TwitterEndpoint, Headers, ContentType, MessageBody}, [],[]),

Not much too it really. Only issue I ran into during this exercise was Twitter's suppression of duplicate or recurring status updates. It isn't clear what the window is on duplicate/recurrence detection, but for a service where the status can go from "NO TRAFFIC RESTRICTIONS ARE REPORTED FOR THIS AREA." to "THERE IS A DENSE FOG ADVISORY IN EFFECT..." and then back to "NO TRAFFIC RESTRICTIONS ARE REPORTED FOR THIS AREA." within an hour, the suppression of these updates would be problematic. The simple fix for this is to append a timestamp or other unique token. Obviously, this is less than ideal as it eats into the 140 character limit, but until Twitter provides a better spam algorithm or account classification scheme, this is really the only way to avoid duplicate/recurring status suppression.