How to tweet from shell

How to tweet from a shell script, since twitter started using the OAuth authentication scheme and writing scripts/apps for twitter became more challenging to integrate with, this entry is about how to integrate shell scripts into twitter, the application for that can be limitless and may include :

  1. using twitter as monitoring platform.
  2. using twitter to notify you of certain events.
  3. using twitter as an interfacing channel with your machine.
  4. schedule tweets.

This entry is based on (http://benscomputer.no-ip.org/2010/Sep/prem_121905092010.shtml) however I tried to simplify and compress the instructions as much as I could:

  1. verify you have curl installed on your machine (#which curl), if not you have to install it.
  2. download the three attached scripts and place them into one directory (OAuth.sh, TwitterOAuth.sh, tcli.sh).
  3. have at least one valid twitter account (to register the script).

Start by registering the Twitter script on twitter, simply go to http://dev.twitter.com/apps and click on “register a new app” fill the fields, and then click “Register Application”, copy “Consumer Key” and “Consumer Secret” from the application details page.

Create a new file and name it .tcli.rc (any other name would do as well), place the following two lines in it, using the data you acquired from the application details page, place the file in your home (~).

oauth_consumer_key="YOURCONSUMERKEY"
oauth_consumer_secret="YOURCONSUMERSECRET"

Modify the two files (tcli.sh, TwitterOAuth.sh) as a work around to avoid placing them in the path:

in tcli.sh replace

in tcli.sh replace 
OAuth_sh=$(which TwitterOAuth.sh)
(( $? != 0 )) && echo 'Unable to locate TwitterOAuth.sh! Make sure it is in searching PATH.' && exit 1
source "$OAuth_sh"

with

source TwitterOAuth.sh

and in TwitterOAuth.sh

replace

OAuth_sh=$(which OAuth.sh)
(( $? != 0 )) && echo 'Unable to locate OAuth.sh! Make sure it is in searching PATH.' && exit 1
source "$OAuth_sh"

With

source OAuth.sh

 

Now your work as a developer is done, all you need to do is to assume the users role and enable the script to tweet using your account, please login to twitter as the user you want your script to use.

run

./tcli.sh -c statuses_update -s "hello world"

You’ll be provided with a link and a prompt requesting a pin, click on the link, acquire the pin, paste it and click enter, now your script is ready to tweet.

 

You can now integrate it into any other script you’ve got :).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s