Site Moved

This site has been moved to a new location - Bin-Blog. All new post will appear at the new location.

Bin-Blog

Using Twitter, Part 1 - Command Line Twitter Client

You may have heard of Twitter - if not, it is a microblogging platform that allows members to tell each other what they are doing and what they think. There was a huge buzz about twitter recently - made me create an account on twitter. I tried to post through the web interface - but that's not a very intuitive method. The IM method is much better - but it almost never works. So I ended up making another method - the CLI method. Or twittering using the Command Line.

Before going any further, let me say that this method is for Linux. You can use this method in windows as well - but you have to install the windows port for curl. Even if you manage to get this method to work on windows, most of the major advantages of this method will be lost.

This SxSW was huge for twitter - not only did it win the 2007 South by Southwest Web Award, they also got a lot of new members. But lately it is getting some negative reviews as well.

Quick Posting

One of the main feature of twitter is the fast publishing time. You type something into a text box and click submit - and the stuff is published. That's fast! The only problem is that you need to open a browser and type in the URL to get to that text box. I need something faster. The IM method is a bit faster - but it rarely works. This situation will change in the future - but I don't want to wait.

The fastest method I can think of is like this - I press a keyboard combination and an small text box opens up. I type in the status and press Submit and this application will post the data to the twitter site using the Twitter API. The only problem is there is no such application. That's when I decided to do something about it. Of course, I am too lazy to create an entire application for it - so I had to settle for the next best method - posting from a terminal.

Posting from a Terminal

I have configured my system to open the Terminal(konsole) when I press Ctrl+Alt+A. Now all I have to do is create a command, say, 'twitter' that will publish the command line arguments to the twitter site. My input will be something like this.

Ctrl+Alt+A
twitter "Using the CLI Twitter client"

That's fast enough for me.

Twitter API

To create the script, I took a look at the twitter API...

All of the methods (except for the public timeline) require user authentication via Basic Auth. The username is the email address you have stored on Twitter, the password, your password.

....

Updating your Twitter

Done with a HTTP POST using the "status" parameter. status=Walking the dog.

  • http://twitter.com/statuses/update.json
  • http://twitter.com/statuses/update.xml

The simplest API I have ever seen.

Curl

After I saw that API, I realized that I don't need to do any coding to create the application - I just need to use the curl command. curl is a tool to transfer data from or to a server. All I have to do is execute this command...

curl --basic --user "<User>:<Password>" --data-ascii "status=<Twitter status>" "http://twitter.com/statuses/update.json"

In my case it will be

curl --basic --user "binnyva:******" --data-ascii "status=Using Twitter from command line" "http://twitter.com/statuses/update.json"

The Twitter Shell Script

But I don't think you would like to type all that everytime you want to twitter. So we have to put it into an executable file. Also, we have to modify the script to enable the support for command line arguments.

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

Replace the <User> and <Password> with your twitter user id and password. Now save the text to a file called 'twitter' and give the file execute permission. Put this file in any folder in your path(like /usr/local/bin or ~/bin)

Using Twitter CLI

Just call the command with your message as the argument - like this

twitter Writting Content for Bin-Blog about twitter.
twitter "Using Twitter CLI"

For best results, enclose the message within quotes. This will prevent problems when using wild card characters like '?'

Twitter Users

Before I leave, how many of my readers are twitter users? OK, you three - leave a comment with your twitter ID. By the way, I am binnyva.

Another thing - this script is thrown together in a very short time - is there a better, more foolproof way of doing this? If you know one, leave a comment.

30 Comments:

Jayson Joseph Chacko said...

Twitter is pretty addictive initially, but useless after a while! :)

Unless ofcourse if you are Scoble with 1000s of followers.

And hey, that is a cool script...

Anonymous said...

I think Twitter is useless right from the start, not just after a while - but it's great fun.

And the script *is* cool!

So - thanks!

engelen said...

The script is lovely. I'm using it all the time. Thanks a million, binnyva!
However, I have a problem when I reach the end of the line in my console - there's no 'line feed' and what I type is displayed in the same line, overwriting what I've written already.
This is why I personally prefer this version, which prompts me for a string:

echo -n "-> "
read text
curl --basic --user "(User):(Password)" --data-ascii "status=`echo $text|tr ' ' '+'`" "http://twitter.com/statuses/update.json"

Binny V A said...

I use fish as the shell - not bash. In that shell, if you reach the end of the terminal, the text will be wrapped into the next line.

engelen said...

Right. It seems there's a bug in Gnome Terminal which causes the problem I described - Gnome Terminal is nice in other respects though. XTerm works perfectly fine with your script in its original form.

Daniel Black said...

Hi, binnyva,

I just came across this script, and it's wonderful. I tried a couple of the browser plugs, and actually installed Opera 9.22 both at work (MS)and at home (Slackware) just to use the Twitter Opera widget. It works well, but it's gaudy. I then upgraded (a little tentatively, as 1.5 was working nicely, with, I hear, a smaller RAM footprint) to Firefox 2, so I could install Twitbin.

Each of these options is fine, but what I'm after is a little more streamlined. Your script fits right in. I primarily wanted to use Twitter to make posting tasks to RememberTheMilk easier (as they've just linked to the Twitter service), and these extensions/widgets don't obviate the need to have a browser open. If I have it open, there's no reason not to just use the RTM site itself.

Short story long, using cURL was a nice idea. Now, after all this, I have a question, though: the output of cURL is pretty verbose. I'll be looking at the man page, but do you have a hint as to how to turn that down?

Thanks,

Daniel

Binny V A said...

@Daniel
Your best bet is
curl --progress-bar --basic --user "USERNAME:PASSWORD" --data-ascii "status=STATUS" "http://twitter.com/statuses/update.json" >/dev/null

Any other option I am not aware of?

Daniel Black said...

Hi, binny,

I've tried your fix, and unfortunately I can't get it to take my input. I'm playing with tweeterPy right now, which is working nicely. I'm curious to say where these two approaches land.

Thanks,

Daniel

Daniel Black said...

Duh. I just needed the part of your first cURL line that pulled the input. Rather, I needed only pump the results of your first cURL line to /dev/null. I lose any knowledge of success of the tweet; but it's pretty quick.

If I'm adventurous enough, I'll figure out a way to parse the cURL results, to allow for some more flexibility. I assume that pulling timelines is just another cURL call away, as well.

Daniel

PolarBear said...

hi binnyva, great way for a twitter cli, i just added you to follow you, my twitter is polarbear58
namaste

engelen said...

Obviously, your fine script works on a Mac too (OS X, Darwin terminal). Thanks once again!

uziel said...

It sure is great, but needs a few more tweaks. At least more careful preprocessing than just tr ' ' '+' - I mean escaping *, & and + themselves to URL encoding.

SCM said...

Hi there, thx for the script. Im just testing Twitter, ans twittering from the Shell is cool! :) It even works with umaluts (German). I twitter as: st0rz :) cu - scm

Anonymous said...

"$@" <-- add quotes to this and you don't have to quote your text when twittering.

Thanks for the script :)

travis said...

Sweet. Twittering from the command line somehow makes me feel like a bigger nerd.

SmartAssProducts said...

Nice script, which I'm about to try. On Twitter I'm saproducts ... but I rarely ever update! It just seems like such a nuisance and unnecessary and useless ... but it's kind of fun, too. :)

Igor said...

Windows users can use the free and open source Threeter, which was released yesterday :-) More info at http://threeter.devv.com/ Cheers!

John McCollum said...

Nice script! I did something very similar myself using Python and Pycurl - it's increasing my level of tweets significantly! (I'm johnmcc on twitter btw.)

Anonymous said...

Hello there,
great script... but can you tell me how I can cut down on the amount of results I get after executing the script.

I am using tinyme linux distro. The bash shell didn't work well. I had to escape once the results were printed.

Xterm worked perfectly however.

Big question is... How do I just return to a clean prompt that has "Your update has been Sent"?

I'm flashtrader on twitter.

Cheers

Binny V A said...

@Anonymous
Its possible - just send the output of the curl command to /dev/null and echo a success message at the end.

Brian said...

I cannot get a + sign to pass to my twitter update... Any ideas? Quotes or no quotes... Thanks.

nicdm said...

Great Script !

x1101 said...

I have been working on something similar, the issues is i have a habit of use &, and bash keeps messing that up, for now i am using sed to change & to and (sed '/\&/ s//and/g') but i would much rather find a way to just twit an &, any help on that ?

on twitter i am x1101

John said...

Awesome little one liner, thanks!! Now I can get my server to tweet uptime and backup completion messages, and because it's twitter I can pick em up on a multitude of clients and via rss etc..

Joe said...

This is a really cool script, however I'm getting huge verbose after every tweet. I'd like a plain "Tweeted!".

Looking at the API Wiki, it says we need to use OAuth to change "From API" to something else.... How could this be incorporated into the script?

moldor said...

Cool script - one small change. Add the "-s" switch to the "curl" statement, and "> /dev/null" to the end of the curl statement for a nice quiet script.

How to check for the tweet as an argument, and only "read text" if no arguments parsed ?

Hermes J. said...

This is excellent! Thank you so much for this. I was having problems compiling other Twitter CLI clients and it seemed like too much of a hassle. Just tried this on Mac OS X 10.6.3 and it works just fine. :) My twitter is @stadsvargen, btw.

kurtdriver said...
This comment has been removed by a blog administrator.
kurtdriver said...

How can I pipe the output of uptime to this script?
#!/bin/bash
echo "Now publishing to Twitter";
echo -n "Type Here ->";
read text;
curl >& /dev/null --progress-bar --basic --user "kurtdriver:*******" --data-ascii "status= $text+" "http://twitter.com/statuses/update.json";
exit 0

Anonymous said...

Here is a bash script to post to Twitter from the command line on Linux and Mac:

http://360percents.com/posts/command-line-twitter-status-update-for-linux-and-mac/