Game Quotes Twitter Bot back online! (plus tutorial)

 

After a 2.5 year hiatus caused by a server move, I’ve decided to bring the Game Quotes Twitter Bot back online. Every 12 hours, the bot fetches a random, weird, classic video game quote and image and posts it to the @gamequotesapp Twitter account.

For example:

 

 

For anybody that would like to do something similar, here’s a brief rundown of how to make a simple Twitter bot that reads from a MySQL database of your own.

 

What you’ll need

 

– Web hosting (shared is fine) that runs PHP version 5.4 and supports Curl
– Simple MySQL database to store the content
– PHP script to fetch tweet content from database and format the tweet
– Twitter for PHP (small library for posting to Twitter). You can find it on GitHub here: https://github.com/dg/twitter-php
– Cron job to trigger the script once every 12 hours

 

Create the data source

 

I had a collection of quotes and images that I used when developing the Game Quotes iOS and Windows Phone apps some years back, so I started with this.

Nothing fancy: it’s just a single Google sheet with columns for the relevant items you need when posting a tweet. In my case: game name, game platform, game publisher, quote text, and image file name.

 

 

Download the single sheet as a .tsv file (tab-separated values) file. This is what we’ll be using to populate the database.

 

Create the database and user

 

With a data source ready, we need to put it in a database that the bot can connect to and read from.

Log into your cPanel or equivalent, and create a new MySQL database and user. We’re not doing anything crazy here, so you can use the MySQL Database Wizard.

 


Make note of the following because you’ll need them later:

– Database User
– Database Password
– Database Name
– Table Name

Obligatory warning: Do not share credentials with anyone.

 

Populate the database

 

Now that you have the database created, you need to populate it with the contents of the sheet you exported.

From cPanel (or your equivalent) launch phpMyAdmin or equivalent tool to manage your database.

In phpMyAdmin, make sure the database you created is selected on the left, and click on the IMPORT tab and then complete it as follows:

– Choose your .tsv file.
– Under Format, select CSV.
– Check ‘Replace table data with file’
– In Columns separated with, enter ‘\t’

Note: you can modify these based on your own sheet’s structure.

 

 

Click Go to import your data source into the database.

 

Create your Twitter app

 

There are plenty of tutorials that describe this already so I won’t go into detail, but essentially:

– Sign up for a Twitter account at https://twitter.com
– Go to https://dev.twitter.com and log in with this account
– Create a new app
– Follow the instructions, and make sure you have these: Consumer Key, Consumer Secret, Access Token, Access Secret
– Make sure Access Level is Read and Write (for both Application Settings and Access Token)

Note: I highly recommend creating a test Twitter account for this so you don’t spam your followers!

 

Prepare your server

 

Access your web server using an FTP client, file manager, etc. Create a directory for your project on your server (e.g. ‘myBot’).

Download the Twitter for PHP library from Github and extract it. Copy the /src directory and upload it to the root of your /myBot directory.

Note: You will need to prevent unauthorized access to your script. At very least, create a file called .htaccess with the following line:

 

Options -Indexes

 

Save it, and upload  it to your /myBot directory.

 

Create your PHP script

 

Now the fun begins. Create a new file “BotTweet.php” or whatever you want. Paste the following script into it:

 

Of course make sure to replace the database and Twitter key placeholders with your own. 😉

Here’s a brief description of what’s going on:

– Import the Twitter for PHP library
– Create variables for our database credentials and name
– Fire a BuildTweet function that:
–> Connects to the database
–> Chooses a random number based on the maximum number of rows in your table
–> Fetches that row # from the table
–> Formats the fetched data as a tweet and an image URL
–> Checks to make sure the tweet is formatted properly
– With the tweet structured, we then launch the PostTweet function, passing it the tweet text and image URL that:
–> Echoes the tweet content (useful for testing)
–> Creates variables for your Twitter app credentials
–> Uses the Twitter for PHP library to post the tweet

 

Upload this BotTweet.php file to your /myBot directory.

 

Test your bot

 

To test your bot, open a browser and just enter the following URL:

http://yourwebhost.com/myBot/BotTweet.php

If it’s successful, you should see a blank page with the content of your tweet. If not, you will see an error log on your server (in your /myBot directory) that should give you a hint as to what’s wrong.

 

Schedule your bot via a Cron job

 

If you have to manually enter the URL every time you want your bot to tweet, it’s not much of a bot. To automate this, it’s easy to set up a cron job on your server. From cPanel again, just search for Cron Jobs. On the Cron Jobs panel, just select how often you want your bot to Tweet and in the Command field, enter the command that will launch your script:

 

php /YOUR_HOST/myBot/BotTweet.php

 

Click add, and you’re good to go!

 

If all worked, your bot should now tweet periodically based on how you set it.

Any questions? Feel free to contact me.

 

Leave a Reply

Your email address will not be published. Required fields are marked *