Bash Shell Script – How to Post to Twitter

By | November 5, 2015
ilovebash

ilovebash

Bash Shell Script – How to Post to Twitter

There is a load of php scripts and methods to enable any half competent developer to post a message to their Twitter profile.

However I wanted to post to twitter from a BASH SCRIPT !!!

Uses for this I have used in the past are some automated cron jobs which could post random content based on the output from one of my other scripts. It could be anything from maybe a currency exchange rate, a welcome to new followers, or even a custom message pulled from random from a config file.

Whatever your use for it then here are the tools you need. It uses the current 2015 OAUTH Twitter authentication so will work.

Bash Post to Twitter – Installation

ok as I suggested, the core of this is a PHP script, but able to be called from a bash script. So first you need to download and unzip this to a location where your bash scripts run from :

wget "http://bashworkz.com/twitter-php.tar"

tar -xvf twitter-php.tar

Next part is to get the keys you need and create the application on Twitter. Log in https://dev.twitter.com/ with your regular user, and create a new app. You need to set it WRITE PERMISSIONS which allows you to connect to it from BASH.

In there note down the consumerKey, consumerSecret, accessToken, and accessSecret (there is an option to generate the accesstokens)

Configuration and Bash script

Next create a php file called something like twitter_send.php and put in this :

<?php

$inMsg = $argv[1];

echo "Sending $inMsg";

require_once 'twitter-php/src/twitter.class.php';

$consumerKey="AAAAAAAAAAAAAAAAAAA";

$consumerSecret="BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";

$accessToken="CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC";

$accessTokenSecret="DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD";

$twitter = new Twitter($consumerKey, $consumerSecret, $accessToken, $accessTokenSecret); try { $tweet =

$twitter->send("$inMsg");

} catch (TwitterException $e) {
echo 'Error: ' . $e->getMessage();
}

?>

Check out -  Run cron job every 90 mins (minutes) - Linux Scheduling, Batch Automation, Scheduled Jobs

Obviously now you can see you need to put in the consumer and access details in the appropriate variables above. Also make sure the twitter-php path is correct to match your installation (you can specify full path if you want)

Testing Bash Post to Twitter

So now you can see from $inMsg = $argv[1]; that the first parameter to the php script is the message, therefore to test the script you can do this :

php twitter_send.php "Hello"

Check your Twitter page and your message should be there.

Now with this test code in hand you can easily add this to any other BASH shell scripts you have and vary the message being sent. One other potential use could be notifications of processes running/completed on your system (clearly you should keep confidential info out of it).

Next tutorial I will write will be similar, but will show you how to post an IMAGE to Twitter.

Dont forget to check out my other BASH tutorials and tips HERE including my recent one how to upload and download a file from Google Drive HERE

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.