Automatically Tweet Random Messages

Now that your Raspberry Pi can tweet we might as well teach it how to tweet a random message from a list of messages. With this script you can let your Pi advertise your web site, your company, your product or some special event.


# autotweet
# This script sends a random tweet form a list every time it's called.
# Use:
#   autotweet           Send a random tweet from the list
#   autotweet 6         Send the 6th tweet from the list
# Author: San Bergmans

# Configuration variables

# Other variables

# Pick a random number
SEED=$(head -c4 /dev/urandom | od -t u4 | awk '{ print $2 }')
RAND=$[ ( $SEED % $NUM_TWEETS + 1 ) ]

if [ $# -ne 0 ] ; then

case $RAND in
    1) TWEET="Tweet number 1";;
    2) TWEET="Tweet number 2";;
    3) TWEET="Tweet number 3";;
    4) TWEET="Tweet number 4";;
    5) TWEET="Tweet number 5";;
    6) TWEET="Tweet number 6";;
    7) TWEET="Tweet number 7";;
    8) TWEET="Tweet number 8";;
    9) TWEET="Tweet number 9";;
   10) TWEET="Tweet number 10";;
    *) TWEET="Default tweet for illegal random numbers";;

# Send the tweet

This script has two configurable variables and a list of messages. TWITTER holds the path to the tweet script. NUM_TWEETS holds the number of messages in the list. Don't forget to update this number when you add messages to the list, otherwise the new message will never be tweeted.

I use /dev/random to generate a random number because it produces better randomness than the bash built in variable $RANDOM when called at fixed times of the day. The device /dev/random uses "system noise" to produce its random numbers. The random SEED produced by /dev/random is limited to the number of tweets using the modulo function to get the final random number.
Picking the appropriate message is a simple case construction, which matches the random number with the required message.

Calling The Autotweet Script

You can call the autotweet script yourself on the command line. Calling it without any parameters will tweet one of the random messages in your list. Calling it with a number will tweet that particular number, unless that number doesn't exist, in which case the default message will be tweeted.
Keep in mind that Twitter will ignore identical tweets which are repeated too fast in succession. This will affect your test especially when you have only a short list of messages.

The real power of the autotweet script is when you use crontab to send the tweets on predefined times of the day. You do that by adding a line to your crontab. Start editing your crontab by typing the command below:

crontab -e

Then add the next lines at the bottom of your crontab:

# Automatically tweet a random message
minutes    hour    *   *   *   /home/pi/bin/autotweet

Minutes and hour must be replaced by the time of the day you want your tweets to be sent. You can have a list of minutes and/or hours by separating them with comma's. Be sure to have at least one newline character at the end of the last line to keep crontab happy!
The default crontab has a lot of useful information about how it has to be set up.

The default editor used by crontab is joe. When you're done editing your crontab exit joe by typing Ctrl-kx. Crontab will then install your new settings.