or: I Am _ebooks And So Can You!!
THIS TUTORIAL IS DEPRECATED. FOLLOW THIS TUTORIAL INSTEAD.
This uses an old version of the _ebooks library and also does not work on Windows any longer.
Everyone should have an _ebooks bot on Twitter, if they want one. What follows may not be the best way to do it, but I hope it is the easiest way to get a good one.
Depending on your platform, familiarity with using a terminal/command prompt, and weird errors you have to Google or ask me about on Twitter, the process should take somewhere between 30 minutes and 2 hours.
Important Note 1: Wherever I say my_ebooks, you’ll want to use whatever your _ebooks bot is using as a twitter username. I recommend keeping it all lowercase to simplify things.
Important Note 2: You will need to register an app with Twitter to make your bot. Twitter may require a mobile phone number be associated with the account to do this. It seems to be inconsistent, so you may not need one, but it is possible this will be a roadblock.
But enough talk, have at you!
0. Request Twitter Archive
We’ll want our Twitter archive to use as a source for the my_ebooks robot’s tweets later on, so go to your Account Settings page and hit the Request Your Archive button. That’s it. A link will be e-mailed soon(ish.) Once we get that e-mail, save the file somewhere we can find it later.
1. Install Ruby
The tools we’ll be using rely on the Ruby programming language. We don’t have to understand it, but our computer will!
Safe to assume you already have or know how to get ruby, yeah? Probably
apt-get install ruby or
pacman -Sy ruby or similar. You’ll also need
base-devel for compiling some gems. I trust you.
If we’re on Mavericks: done. Skip to 2. For older versions, you’ll find it easiest to get via Homebrew. In a terminal window, run these two commands:
ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)" brew install homebrew/versions/ruby193
We need to get and install Ruby 1.9.3 from RubyInstaller. This will install to
C:\Ruby193 by default. We do want to check off Add Ruby executables to your path during the install.
Also, we’ll need the associated DevKit. We’ll use
C:\Ruby193\devkit as the destination.
In the command prompt (Windows+R to open Run menu, type
cmd then Enter) we run these three lines to tie them together:
cd C:\Ruby193\devkit ruby dk.rb init ruby dk.rb install
2. Install twitter_ebooks
This step should now be painless! On Linux or Windows, we run the following from your command prompt/terminal:
gem install twitter_ebooks -v 2.2.6
For OSX we need a slight tweak:
sudo gem install twitter_ebooks -v 2.2.6
There should be a lot of messages about Fetching/Building/Successfully Installed/Installing scroll by—that’s all very good news!
Let me take a quick moment to thank @m1sp/mispy for putting together the twitter_ebooks library we use throughout this tutorial. The behavior logic we’ll be stealing later on to make this as easy as it can be is also by mispy.
3. Create Twitter Account
When logged out of our regular Twitter account (or in a Private Browsing/Incognito window) let’s enter info for the new _ebooks account into the “Sign Up For Twitter” box at Twitter.com.
The e-mail address has to differ from our main account’s. With GMail, something like firstname.lastname@example.org will arrive to the same person as email@example.com, so that’s an easy way to do it.
When the confirmation e-mail arrives, be sure to follow the link it provides in the window used for setting up this new account; it won’t work in a window where our main account is logged in.
4. Create Twitter App
With our new account confirmed, go to the Twitter Applications page and login with your bot account’s username/password then click the Create New App button.
Name, Description and Website are stored on each tweet made from the application; fill in whatever someone curious enough to look into where your _ebooks’ tweets originate should see. Agree to the terms and create the app.
5. Get Credentials
First we want to enable our application to actually make tweets. Click on the Permissions tab and choose the Read, Write and Access direct messages option, then the Update Settings button.
This change can take a few minutes, so go to the API Keys tab but don’t do anything unless the Access level field shows the right permissions. Reload the page periodically until it does.
Down at the bottom, click the Create my access token button. Again, we may have to refresh the page in a minute or two after doing this to actually see credentials under the Your access token heading.
After this we have 4 magic numbers on the page that you will need later and that you shouldn’t share with anyone else. They are API key and API secret towards the top of the page, then Access token and Access token secret further down.
6. Prepare Bot Folder
We need a place for our bot to live, so let’s make a folder for it somewhere (anywhere, really) on the computer. Using the terminal/command prompt, navigate to where you’d like to make the folder and then run
ebooks new my_ebooks
This will make a new
my_ebooks directory, which has a couple files and two folders:
7. Consume the Corpus
[Definitely let me know if you get errors here. I built this part.]
Let’s open the Twitter archive (
tweets.zip) and drop its
tweets.csv file into the
corpus directory mentioned above. Rename it to
my_ebooks.csv. This file has all your tweets to date in it!
We’re going to make a simple language model for the bot to write tweets with. Thankfully, twitter_ebooks does the heavy lifting for us here.
From the terminal/command prompt, navigate to the
my_ebooks folder from above (use
cd) and run:
ebooks consume corpus/my_ebooks.csv
It may take a minute, but you’ll get a message about the “corpus being consumed” which is a good thing, despite sounding vaguely sinister.
OK, we’ve done a lot of work so far. Let’s enjoy some fruit of that labor now as a breather. Run
ebooks gen model/my_ebooks.model
to see the program piece shards of your own tweets together into a statement of its own. Repeat as desired.
8. Finishing Touches
With twitter_ebooks and our language model, there are tons of possibilities with some knowledge of Ruby programming. But we said this would be easy, right? We’re going to use some off-the-shelf behaviors for this tutorial.
Download these files to replace the ones in your
We need to set a few things in
bots.rb; opening it in our text editor of choice, we’ll plug in our Magic Numbers (API key and API secret; Access token and Access token secret) and our bot’s name toward the top of the file, like so:
CONSUMER_KEY = "MY_API_KEY_HERE" CONSUMER_SECRET = "MY_API_SECRET_HERE" OAUTH_TOKEN = "MY_ACCESS_TOKEN_HERE" OAUTH_TOKEN_SECRET = "MY_ACCESS_TOKEN_SECRET_HERE" ROBOT_ID = "ebooks" # leave this as ebooks. TWITTER_USERNAME = "my_ebooks" TEXT_MODEL_NAME = "my_ebooks"
You can also provide a list of users not to talk to, or some special words that your bot will like, or some words you DO NOT want it to tweet on the lines that follow.
Now save the file, and we’re done!
9. Run, Robot, Run
In the terminal/command prompt, let’s navigate to our
my_ebooks directory and do:
You have an _ebooks of your very own! I hope you love your _ebooks as much as I love mine. (which is very, very much.)
If this window stays open and you don’t lose internet access, the bot should chug along happily forevermore! It will respond to mentions and will try tweet on its own every 2 hours, more or less. Those are some big “if”s, though. If you’d like a better permanent solution, jump over to How To Deploy an _ebooks Bot.