Now that your Raspberry Pi can tweet again 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, some special event, or simply a message of the day.
I used to have a script which could randomly tweet messages before. That script used a random number generator to select a random message from a list. It worked fine, but I think the new way to select a random message from a list is a lot cleverer. I simply sort the list in a random order now and pick the top line from this scrambled new list.
#!/bin/bash # autotweet # # This script sends a random tweet form a list every time it's called. # It uses the twitter.py script which can also be found on my web site. # Use: # autotweet Send a random tweet from the list with # default identity # autotweet identity Send a random tweet from the list with # selected identity # autotweet identity 6 Send the 6th tweet from the list identity # is not optional now # # Author: San Bergmans # www.sbprojects.net # # Configuration variables # Path to twitter python script TWITTER="/home/pi/bin/twitter.py" # Add as many tweets as you like below. Each line will # be a separate tweet. The end of the list is signaled # by EOF. Do not add tweets before the "TWEETS=..." line # or below the EOF line. TWEETS=$(cat << EOF Tweet number 1 Tweet number 2 Tweet number 3 Tweet number 4 Tweet number 5 Tweet number 6 Tweet number 7 Tweet number 8 Tweet number 9 Tweet number 10 EOF ) # End of configurable variables if [ $# = 0 ] then # No parameters given. Used default ID and # and get a random message to tweet. IDENTITY="" LINE=0 elif [ $# = 1 ] then # Only the identity is given as parameter. # Use it to tweet a random message. IDENTITY="$1" LINE=0 else # Both identity and a line number is given # as parameters. Use this ID to tweet message # indicated by line number. IDENTITY="$1" LINE=$2 fi if [ $LINE -lt 0 -o $LINE -gt $(echo -e "$TWEETS" | wc -l) ] then # Use a random message if the line number # is illegal. LINE=0 fi if [ $LINE = 0 ] then # Select a random message from the tweets list MESSAGE=$(echo -e "$TWEETS" | sort -R | head -n1) else # Select the requested message from the tweet list MESSAGE=$(echo -e "$TWEETS" | head -n$LINE | tail -n1) fi # Send the tweet if [ -z "$IDENTITY" ] then # Send it through the default account $TWITTER -m "$MESSAGE" else # Send it through the selected account $TWITTER -i "$IDENTITY" -m "$MESSAGE" fi
Copy/paste the script above into a file called autotweet.sh and save it into your ~/bin directory.
Make it executable by running the command chmod u+x ~/bin/autotweet.sh
After that you can start by deleting the example tweets from the program and fill your program with your own tweets. Remember that there is a limit to the length of your tweets, which is kind of difficult to calculate. Not to worry, the program will exit with an error when your tweet is too long.
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 to the default identity of your twitter.py script. Example: autotweet.sh
Calling it with an identity argument will use the given identity of your twitter.py script. Example: autotweet.sh webmaster
Calling it with an identity and a line number will use the given identity and tweet the selected line. If the selected line number doesn't exist a random message is picked instead. Example: autotweet.sh webmaster 6
Keep in mind that Twitter will ignore identical tweets which are repeated too fast in succession. This will affect your testing capability, especially when you have only a short list of messages to start with.
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 crontab -e
Then add the next lines at the bottom of your crontab:
# Automatically tweet a random message minutes hour * * * /home/pi/bin/autotweet.sh > /dev/null 2>&1
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 current script sends a random message, picked from a single list of messages. But since the script can handle multiple identities, it might be an interesting addition to the script to allow for multiple lists, one per possible identity.