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 :
- using twitter as monitoring platform.
- using twitter to notify you of certain events.
- using twitter as an interfacing channel with your machine.
- 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:
- verify you have curl installed on your machine (#which curl), if not you have to install it.
- download the three attached scripts and place them into one directory (OAuth.sh, TwitterOAuth.sh, tcli.sh).
- 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 (~).
Modify the two files (tcli.sh, TwitterOAuth.sh) as a work around to avoid placing them in the path:
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"
and in TwitterOAuth.sh
OAuth_sh=$(which OAuth.sh) (( $? != 0 )) && echo 'Unable to locate OAuth.sh! Make sure it is in searching PATH.' && exit 1 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.
./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 :).