Twittering from the Command Line

Incessant status updates to your cell phone from the command line? Sure, with a little help from curl and Twitter.

If you've been around the Linux and UNIX world as long as I have (is it really going on 30 years? How could that be?), you'll be familiar with the various attempts at multiperson chat that have come down the pipeline, from simple command-line tools to the curses-based “talk” program to Internet Relay Chat (IRC) chaos. Occasionally fun, but often a complete waste of time, there's still something appealing about having an open line with a circle of friends and colleagues.

A few years ago, that mantle was assumed by the status line in Facebook, where hard-core users update their status throughout the day to reflect the meetings they're attending, conferences they're involved with, dates with their spouses or significant others, concerts, fights with parents and so on. The problem is, that's useful only if the people in your circle are also rabid Facebook fanatics—a shortcoming that's true of any of these services, of course.

Simultaneously, flashmob instigators found that Web-based tools could help them organize, and services like Dodgeball were created. (A flashmob is a spontaneous gathering of people organized by cell phone or text messaging.) Dodgeball was bought by Google and then strung out to die, but the meme of status messages as a form of shared communication continued to evolve, and the latest evolution is a weird, sometimes overly voyeuristic, on-line service called Twitter (visit twitter.com).

During the past few months, I have found Twitter oddly compelling, in a manner perhaps analogous to Jimmy Stewart being unable to tear himself away from his binoculars in Rear Window (even while the breathtakingly gorgeous Grace Kelly was administering to him, but that's another column entirely). Twitter is immediately useful if a group of people are at a conference, allowing you to meet up easily for meals, evening activities, shared cab rides and so on, but it's also rather fun to keep a running commentary of your goings-on and know what your friends and associates are doing too.

Twitter works directly from a Web page and also is completely short message service (SMS)-compliant too, so it's extraordinarily cell-phone-friendly, adding significantly to its utility.

Okay, nice history lesson. What about some sort of shell script, Dave?

What makes Twitter interesting to me is that like so many modern Web services, it has a public application programming interface (API) that makes it both easy and fun to hack and fiddle with. Although some of the so-called Web 2.0 services are still closed, more and more are making their back ends accessible to open queries, creating many possibilities for darn interesting shell scripts and more sophisticated software and mashups.

For this column, I want to show you how incredibly easy it is to update your Twitter status from the Linux command line, and then noodle a bit on how this could be used for useful, interesting or just mundane tasks. To tantalize you, imagine that you could launch a big software build and have it text your cell phone when it was done, rather than you having to sit at your office.

First, though, you can find the API at twitter.com/help/api. Read through it, and you'll find that just about all interactions are done with specially constructed URLs. That's good, because it's easy to manipulate and tweak a string within a shell script.

The most basic use of the Twitter API is to send a status update message to your account. Here's an example of how to do that:

curl --basic --user "$user:$pass" --data-ascii \
  "status=testing123" http://twitter.com/statuses/update.json

You can see one of the big challenges of writing this as a shell script already. User validation is done through sending an account name and password pair, and that means you're probably going to have the password embedded in your script. Never a good idea. But, prompting for it each and every time you want to send an update isn't good either.

Before we address that though, notice how I'm utilizing the wonderful curl utility—a must-have for your Linux distro. If you don't have it, grab a copy from curl.haxx.se. curl makes it very easy to work with Web pages via the command line, and I consider it essential for any modern shell script programmer.

Looking back at the command invoked, you'll notice that the URL to which we are going to send the update is status/update.json. Read the API, and you'll notice that it supports four different output formats, all of which are a pain to parse within a script, unfortunately. One of those is json, and it re-occurs here as the update-receiving URL address.

If you've already worked with Web sites from the command line, you know there are lots of illegal characters that cannot be included in URLs and, by extension, on command lines of utilities that interact with the Web, such as curl. As a result, one of the tasks of our send.twitter.update script will be to make all of the necessary substitutions before sending the new status message to the Twitter server.

On a lightweight service like Twitter, I think it's probably crazy to go through too many hoops to ensure security, so I actually will be including the account name and password in the script. Given some of the suggested applications we'll explore later, it makes sense to create a new Twitter account just for the command-line updates, in which case, a shared password isn't that big a problem anyway.

Here's a first stab at a simple stu (sent twitter update) script:

#!/bin/sh

user="DaveTaylor"
pass="--mypw-- "
curl="/usr/bin/curl"

$curl --basic --user "$user:$pass" --data-ascii \
  "status=`echo $@ | tr ' ' '+'`" \
  "http://twitter.com/statuses/update.json"

exit 0

In use, simply type in the script name and desired status update:

$ stu Writing makes me sleepy
{"user":{"name":"Dave Taylor","description":"Blogger, entrepreneur, 
public speaker, 
dad!","screen_name":"DaveTaylor","profile_image_url":"http:\/\/s3.amazon
aws.com\/twitter_production\/profile_images\/35534842\/dticon_normal.gif"
,"location":"Boulder, 
Colorado","url":"http:\/\/www.AskDaveTaylor.com\/","id":9973392,"protect
ed":false},"created_at":"Sat Jan 12 21:31:37 +0000 
2008","truncated":false,"text":"Writing makes me 
sleepy","source":"web","id":592217322}
$

Eek. That's a scary output, isn't it? So, before wrapping up this column, I strongly suggest that immediately after the invocation of curl, you append >& /dev/null, so you can discard the output. If you want to be fancy, check $? to see whether it's nonzero, but let's talk about that level of improvement in the next column.

--
Dave Taylor is a 26-year veteran of UNIX, creator of The Elm Mail System, and most recently author of both the best-selling Wicked Cool Shell Scripts and Teach Yourself Unix in 24 Hours, among his 16 technical books. His main Web site is at www.intuitive.com, and he also offers up tech support at AskDaveTaylor.com. Follow him on Twitter if you'd like: twitter.com/DaveTaylor (you can also follow Linux Journal on Twitter, twitter.com/LinuxJournal).

Dave Taylor has been hacking shell scripts on UNIX and Linux systems for a really long time. He's the author of Learning Unix for Mac OS X and Wicked Cool Shell Scripts. You can find him on Twitter as @DaveTaylor, and you can reach him through his tech Q&A site: Ask Dave Taylor.

Load Disqus comments