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 one of my VMs running on AWS. 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 Linux include PIP by default, so the first command may not be necessary on some systems, but I included it just in case.
sudo apt-get install python-pip sudo pip install twython
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.
#!/usr/bin/python import csv, random, sys from twython import Twython, TwythonError # Import quotes dictionary from csv file quotes = dict(csv.reader(open("/home/pi/etc/quotes.csv", mode='r'))) # Remove CSV header row from dictionary to avoid random selection del quotes['quote']
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).
# Select random quote while True: quote = random.choice(quotes.keys()) cite = quotes[quote] motd = u"\"" + quote.decode('utf8') + u"\"\n" + u" \u2014 " + cite.decode('utf8') + u" #motd" # if less than max tweet size, break out of loop and tweet if len(motd) < 278: break
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.
# Twitter authentication settings. Create a Twitter app at https://apps.twitter.com/ and # generate key, secret, etc, and insert them below. apiKey = "" apiSecret = "" accessToken = "" accessTokenSecret = "" # Tweet Message of the Day try: twitter_api = Twython(apiKey,apiSecret,accessToken,accessTokenSecret) twitter_api.update_status(status = motd) print motd except TwythonError as e: print(e)
On my server, 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.
# Tweet random MOTD at 7:00am 0 7 * * * /home/bft/bin/motd_bot.py
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 an Ubuntu Server VM which has been hardened following some of the steps from this page on GitHub. Refer to the Twitter Developer Security Best Practices page for more information.
Thanks for reading.