· 10 years ago · Jun 07, 2015, 05:09 PM
1How to make an ebooks bot
2
3
4or: I Am _ebooks And So Can You!!
5Everyone 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.
6
7Depending 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.
8
9Important 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.
10
11Important 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.
12
13But enough talk, have at you!
14
150. Request Twitter Archive
16We'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.
17
181. Install Ruby
19The tools we'll be using rely on the Ruby programming language. We don't have to understand it, but our computer will!
20
21For Linux:
22Safe 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 ruby-devel or base-devel for compiling some gems. I trust you.
23
24For OSX:
25If 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:
26
27ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)"
28
29brew install homebrew/versions/ruby193
30For Windows:
31We 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.
32
33Also, we'll need the associated DevKit. We'll use C:\Ruby193\devkit as the destination.
34
35In the command prompt (Windows+R to open Run menu, type cmd then Enter) we run these three lines to tie them together:
36
37cd C:\Ruby193\devkit
38ruby dk.rb init
39ruby dk.rb install
402. Install twitter_ebooks
41This step should now be painless! On Linux or Windows, we run the following from your command prompt/terminal:
42
43gem install twitter_ebooks -v 2.2.6
44For OSX we need a slight tweak:
45
46sudo gem install twitter_ebooks -v 2.2.6
47There should be a lot of messages about Fetching/Building/Successfully Installed/Installing scroll by—that's all very good news!
48
49[#thankUbasedMISPY]
50Let 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.
51
523. Create Twitter Account
53When 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.
54
55The e-mail address has to differ from our main account's. With GMail, something like me+my_ebooks@gmail.com will arrive to the same person as me@gmail.com, so that's an easy way to do it.
56
57When 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.
58
594. Create Twitter App
60With 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.
61
62Name, 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.
63
645. Get Credentials
65First 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.
66
67This 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.
68
69Down 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.
70
71After 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.
72
736. Prepare Bot Folder
74We 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
75
76ebooks new my_ebooks
77This will make a new my_ebooks directory, which has a couple files and two folders: corpus and model.
78
797. Consume the Corpus
80[Definitely let me know if you get errors here. I built this part.]
81
82Let'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!
83
84We'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.
85
86From the terminal/command prompt, navigate to the my_ebooks folder from above (use cd) and run:
87
88ebooks consume corpus/my_ebooks.csv
89It may take a minute, but you'll get a message about the "corpus being consumed" which is a good thing, despite sounding vaguely sinister.
90
91[A DIVERSION]
92OK, we've done a lot of work so far. Let's enjoy some fruit of that labor now as a breather. Run
93
94ebooks gen model/my_ebooks.model
95to see the program piece shards of your own tweets together into a statement of its own. Repeat as desired.
96
978. Finishing Touches
98With 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.
99
100Download these files to replace the ones in your my_ebooks directory:
101run.rb
102bots.rb
103
104We 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:
105
106CONSUMER_KEY = "MY_API_KEY_HERE"
107CONSUMER_SECRET = "MY_API_SECRET_HERE"
108OAUTH_TOKEN = "MY_ACCESS_TOKEN_HERE"
109OAUTH_TOKEN_SECRET = "MY_ACCESS_TOKEN_SECRET_HERE"
110
111ROBOT_ID = "ebooks" # leave this as ebooks.
112TWITTER_USERNAME = "my_ebooks"
113TEXT_MODEL_NAME = "my_ebooks"
114You 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.
115
116Now save the file, and we're done!
117
1189. Run, Robot, Run
119In the terminal/command prompt, let's navigate to our my_ebooks directory and do:
120
121ruby run.rb
122You have an _ebooks of your very own! I hope you love your _ebooks as much as I love mine. (which is very, very much.)
123
124If 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.