Automating tweets on Linux

Sylvain Saubier (http://SystemicResponse.com)

2015 May 6

Unless stated otherwise, code examples are bash commands.

There is a very simple way to automate tweets on GNU/Linux, using only two simple tools:

Twidge is a command-line client for twitter. It allows you to tweet, send direct messages, see retweets of your tweets, etc. from a terminal.
At is a GNU/Linux basic tool used to plan the execution of programs. It writes scripts in a system folder (/var/spool/cron/atjobs/ for me) and executes them as planned.
Crontab is a GNU/Linux basic tool used to plan the execution of programs in a hourly/monthly/daily/etc. way. It queries a file (/var/spool/cron/crontabs/[username] for me) which lists the path to the scripts and the way they have to be executed.

Twidge

twidge setup will help you to link Twidge with your twitter account.
Once done, you can tweet with:

# Tweet from stdin
echo "This is a tweet." | twidge --config=/conf/file update
# Tweet from string
twidge --config=/conf/file update "This is a tweet."

aliasing twidge commands

twidge --config=/conf/file can be aliased in your $HOME/.bashrc file, in order to facilitate the execution of Twidge for your account without having to write the whole command each time.

#########
# .bashrc
#########
alias twidge.me="twidge --config=/home/me/.twidge/twidgerc.me"

Now, twidge.me is the same as twidge --config=/home/me/.twidge/twidgerc.me:

# Tweet from stdin
echo "This is a tweet." | twidge.me update
# Tweet from string
twidge.me update "This is a tweet."

However, using aliases in a shell script is not that easy, and it is thus preferable to simulate your alias in your script as follows:

###################
# my_bash_script.sh
###################
# Putting my alias in a variable
v_twidgeCmd="twidge --config=/home/me/.twidge/twidgerc.me"
# Using the variable as an alias
$v_twidgeCmd update "This is a tweet."

At and Cron

At

at allows you to plan futute execution of commands. Though, it does not allow for daily/monthly/etc. execution.

# Execute script at 9pm in 7 days (once)
at 9pm + 7 days -f /path/to/script.sh
# Execute script tomorrow (once)
at tomorrow -f /path/to/script.sh
# Execute script at 11am, on May 6 (once)
at 11am May 6 -f /path/to/script.sh

Cron

crontab allows for the planning of the execution of script in a hourly/daily/etc. way.

To edit the “planning file”, type crontab -e (you might need to execute it with superuser privileges: sudo crontab -e), and write as follows:

##############
# crontab file
##############
# [...]
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
# [minutes] [hours] [day of month] [month] [day of week] [command]

# Execute script every day at 9am
  0         9       *               *       *     /path/to/script.sh
# Execute script every day in july at 9am
  0         9       *               7       *     /path/to/script.sh
# Execute script every monday in july at 7am30
  30        7       *               7       1     /path/to/script.sh
  30        7       *               7       Mon   /path/to/script.sh
# Execute script every 5 minutes
  */5       *       *               *       *     /path/to/script.sh
# Execute script every 5 hours
  *         */5     *               *       *     /path/to/script.sh

See manual for more info: type man 5 crontab or check it here: crontab(5) at crontab.org.

Automating tweets

Let’s create a file to store the tweeting command.

#############
# tweeting.sh
#############
# 1. Putting my alias in a variable
v_twidgeCmd="twidge --config=/home/me/.twidge/twidgerc.me"
# 2. Putting my tweet in a string
v_myTweet="This is my tweet."
# 3. Tweet the string
$v_twidgeCmd update $v_myTweet

You can store your tweets line by line in a text file, as follows:

############
# tweets.txt
############
Tweet number 1, it is.
Tweet numéro 2, oui.
Tweet no 3, which comes in third.
Tweet n°4, on the fourth line.

and then tweet the one after the other them using head and sed commands:

#############
# tweeting.sh
#############
# [...]
# 3. Tweet the first line of a text file
head -n 1 tweets.txt | $v_twidgeCmd update
# 4. Delete (d) the first line (1) of the text file
sed '1d' -i tweets.txt

Now that our file is ready, we can automate it using at or crontab:

# Execute our tweeting script at 7pm in 3 days (once)
at 7pm + 3 days -f /path/to/tweeting.sh

# Start editing the crontab file and add your script to it
crontab -e

##############
# crontab file
##############
# [...]
# [minutes] [hours] [day of month] [month] [day of week] [command]
# Execute script at 7:45am everyday from monday to friday
  45        7       *              *       Mon-Fri       /path/to/tweeting.sh

All done!