I was first introduced to the concept of the message of the day (MOTD) while working as a SunOS system administrator back in my university days. At our school, the systems used the Unix
fortune command to display random messages and quotes on login. Being a bit nostalgic about these early days of the personal computer era, I decided to build a Message of the Day bot for Twitter. This would give me the pleasure of seeing my own custom messages of the day in my feed, and help me learn the ins and outs of the Twitter API. I have plans to develop more advanced Twitter bots in future, so now was as good a time as any to learn the new API.
As with my previous projects, I am running this Twitterbot using Python on a Raspberry Pi server in my home office. I researched several libraries available for accessing Twitter in Python and decided to use the Twython library to handle the API calls. The Twython library can be installed using the PIP package manager for Python, as shown below. Most newer versions of Raspbian include PIP by default, so the first command may not be necessary on some systems, but I included it just in case.
Once Twython has been installed successfully, the next step is to create a Twitter account for the bot. Please note, you must assign a mobile phone number to this account, at least temporarily. Twitter will not allow apps to be created using an account which does not have a verified mobile phone number.
After creating your new Twitter bot account, go to https://apps.twitter.com/ and click the Create New App button. When prompted, enter the following information:
- Name for the application
- Link to valid website (I used my site homepage)
All other fields can be left blank. Check the boxed labelled Yes, I have read and agree to the Twitter Developer Agreement (assuming you read and agree), then click the button labelled Create your Twitter application. Once the app has been created, click on the Keys and Access Tokens tab to access your new app Consumer (API) keys.
You will also need Access Tokens to enable API access for your bot account. Click on the Regenerate Consumer Key and Secret link to generate these codes. Keep a copy of the Consumer (API) Key, Consumer (API) Secret, Access Token and Access Token Secret to be used later in your bot script. Also note, once this is done you can remove the mobile phone number assigned in Account Settings under the Twitter bot account. It is no longer required to create additional apps.
We’re finally at the point where we can build and run our new Twitterbot. Here is a breakdown of the Python code for my Message of the Day bot. The first section includes standard declarations identifying any required Pythong libraries. It then opens a file named quotes.csv which is a simple comma delimited file of quotes I have collected over the years. The file contains two columns of information, the first column being the
quote, the second column being the
citation associated with the quote, as shown below.
|When opportunity knocks, bolt the door.||Grumpy Cat|
|The unexamined life is not worth living.||Socrates|
The file is read directly into a Python dictionary array for simple access in memory. Unfortunately, this includes the CSV file header row, which the last line of code deletes from the array to ensure it is not selected randomly in error.
The next section of code uses the Python
random.choice function to select a random quote from the quotes array. The quote and citation are assembled in the variable
utf-8 encoding which allows the em dash character to be included as part of the citation. Lastly, some quotes in the quotes.csv source file may be longer than the maximum allowed Tweet size (280 characters). To avoid this issue, the script uses a selection loop that repeats until a Message of the Day (motd) is selected which is less than 278 characters (just to play it safe).
Once a quote has been selected and properly formatted, the script uses the Twython library to submit the Tweet via the Twitter API. For this to function, the Consumer (API) Key, Consumer (API) Secret, Access Token and Access Token Secret generated previously need to be inserted into the appropriate variables in the code below.
On my Raspberry Pi, I created this script with the filename
motd_bot.py. I also enabled the executable bit on this file with the command
chmod +x motd_bot.py. This allows running the script directly from the command line without calling Python first. After a few manual test runs to ensure it was working properly, I used the
crontab -e command and added the following lines in the
crontab file. This scheduled the script to run daily at 7:00am.
One last item to note, as this script includes your Twitter API keys and secrets, it should only be stored and run from a properly secured system. In my example, the script is running on a local Raspberry Pi which has been hardened following the steps on the Securing your Raspberry Pi page and is isolated behind a firewall on the network perimeter. Refer to the Twitter Developer Security Best Practices page for more information.
Thanks for reading.