-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwitterbot.html
More file actions
246 lines (191 loc) · 12.1 KB
/
twitterbot.html
File metadata and controls
246 lines (191 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<title>Ruby Twitter Bot Tutorial - Jennifer Kruse</title>
<!-- Bootstrap Core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom CSS -->
<link href="css/clean-blog.min.css" rel="stylesheet">
<link href="css/application.css" rel="stylesheet">
<!-- Custom Fonts -->
<link href="http://maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet" type="text/css">
<link href='http://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-default navbar-custom navbar-fixed-top">
<div class="container-fluid">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header page-scroll">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<li>
<a href="http://jenniferkruse.me">Home</a>
</li>
</ul>
</div>
<!-- /.navbar-collapse -->
</div>
<!-- /.container -->
</nav>
<!-- Page Header -->
<!-- Set your background image for this header on the line below. -->
<header class="intro-header" style="background-image: url('img/botTutorial/blogHeader.jpg')">
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<div class="post-heading">
<h1 style="color: white">Ruby Twitter Bot Tutorial</h1>
<h2 class="subheading" style="color: white">How to make a simple, easy Twitter bot using the Ruby programming language.</h2>
<span class="meta">January 22, 2015</span>
</div>
</div>
</div>
</div>
</header>
<!-- Post Content -->
<article>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<p>If you're a Twitter user, you've probably seen Twitter bots around. Some famous Twitter bots include <a href="https://twitter.com/DearAssistant">DearAssistant</a>,
<a href="https://twitter.com/accidental575">AccidentalHaiku</a>, and <a href="https://twitter.com/Horse_ebooks">Horse ebooks</a>. In this tutorial you'll learn how to make your own Twitter bot using
<a href="https://www.ruby-lang.org/en/">Ruby</a>, the Twitter API, and <a href="https://www.heroku.com/">Heroku</a>. This tutorial assumes you know a bit about using the command line and basic programming.</p>
<p>Please note, I'm using my Linux operating system (Ubuntu) so some of the commands might be different for you, depending
on what operating system you're using. </p>
<p>First, make sure you have Ruby installed. You can learn how to do that
<a href="https://www.ruby-lang.org/en/documentation/installation/">here</a>. In this tutorial we'll be using the
command line, so it's best to keep your files for this project in their own folder. </p>
<p>Before we get started, you need to make a Twitter account for your bot. Your account will need its own mobile number,
but you can use <a href="https://www.google.com/voice">Google Voice</a> if you've already used your number for a Twitter
account. Once you've made your account, head on over to <a href="https://apps.twitter.com/">Twitter Apps</a> and click
on "Create New App" and fill out the form. The name, description, and website don't really matter that much, so don't
worry if you don't have a website or good name for your application. You can always change it later.</p>
<p>After this, you should be able to click on the "Keys and Access Tokens" to generate and view your tokens. This is how we'll authorize
Twitter later on. Make sure you have tokens under the "Application Settings" and "Your Access Token" headings. You need to keep these keys secret. Give the account at least read and write permissions.</p>
<img class="blogImg" src="img/botTutorial/tutorial1.png">
<p>Next, make a file in your project folder with the ".rb" file extension. The one I'll be using will be called "bot.rb".
For this project, we'll be using the <a href="https://rubygems.org/gems/twitter">Twitter Gem</a>, so type "gem install
twitter" into your command line. </p>
<img class="blogImg" src="img/botTutorial/tutorial2.png">
<p>Next, open up your ruby file in your editor. Since we need to use the Twitter Gem in our program, we have to require it before we do anything else. </p>
<img class="blogImg" src="img/botTutorial/tutorial3.png">
<p>Next, we need to authenticate the Twitter account we just made using the keys from earlier so that we can use
Twitter's API. I have blurred out my keys, and you need to substitute your own. <p>
<img class="blogImg" src="img/botTutorial/tutorial4.png">
<p>To test that you're authenticated and ready to go, type in "ruby bot.rb" (this is how we'll run the program) into the command line. If you get an error about
authentication, then your keys are incorrect. If nothing happens, then we're on the right track. You can now tweet from
your account. Try this line of code and your twitter bot will make its first tweet. (Then feel free to delete the tweet.)</p>
<img class="blogImg" src="img/botTutorial/tutorial5.png">
<p>What you do next depends on what you want your bot to do. I'll leave this up to you. You can find the twitter gem's
documentation <a href="http://www.rubydoc.info/gems/twitter">here</a>. Some great examples of using this gem are
<a href="https://github.com/sferik/twitter/tree/master/examples">here</a>.</p>
<p>After you've got your bot the way you want it, we want to set it up to run automatically. To do that, head on over to
<a href="https://heroku.com">Heroku</a> and sign up. You need to add a credit card number to your account to use the Heroku
Scheduler (how we run the bot automatically), but the scheduler is free and I've never been billed for it. Then,
install the <a href="https://toolbelt.heroku.com/">Heroku Toolbelt</a> so that we can use Heroku with the command
line.</p>
<p>After that's all set up, go to your <a href="https://dashboard.heroku.com/apps">Heroku Dashboard</a> and create a new app
by clicking on the plus symbol in the upper right-hand corner. Then, follow the instructions provided using the command line
to set up your git repository and deploy your bot to heroku. Then, in the command line, type "heroku addons:add scheduler"
to add the <a href="https://addons.heroku.com/scheduler">Heroku Scheduler</a> to your bot. </p>
<p>Then, go back to your dashboard and click on your app. You should be able to click on the Heroku Scheduler to add scheduled
jobs to your bot, like so. </p>
<img class="blogImg" src="img/botTutorial/tutorial6.png">
<p>You can pretty much run your Twitter bot as often or seldom as you want, however, if you run your bot too much then you might
get <a href="https://dev.twitter.com/rest/public/rate-limiting">rate limited</a>. </p>
<p>If your bot isn't running or working, run the command "heroku logs" in your command line to check what's going on. </p>
<p>After that, you're good to go! You can always tweak your bot a little bit and re-deploy to Heroku. Have fun! </p>
<p class="contactText"> Questions? Comments? Don't hesitate to <a href="http://jenniferkruse.me/about">contact me</a>!</p>
<!--
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'jenniferkruse'; // required: replace example with your forum shortname
var disqus_identifier = 'twitterbotTutorial';
var disqus_url = 'http://jenniferkruse.me/blog/post20150123.html';
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript> -->
</div>
</div>
</div>
</article>
<hr>
<!-- Footer -->
<!-- <footer>
<div class="container">
<div class="row">
<div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
<ul class="list-inline text-center">
<li>
<a href="#">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-twitter fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
<li>
<a href="#">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-facebook fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
<li>
<a href="#">
<span class="fa-stack fa-lg">
<i class="fa fa-circle fa-stack-2x"></i>
<i class="fa fa-github fa-stack-1x fa-inverse"></i>
</span>
</a>
</li>
</ul>
<p class="copyright text-muted">Copyright © Your Website 2014</p>
</div>
</div>
</div>
</footer> -->
<!-- jQuery -->
<script src="js/jquery.js"></script>
<!-- Bootstrap Core JavaScript -->
<script src="js/bootstrap.min.js"></script>
<!-- Custom Theme JavaScript -->
<script src="js/clean-blog.min.js"></script>
</body>
</html>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-59909033-1', 'auto');
ga('send', 'pageview');
</script>