You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 31, 2023. It is now read-only.
This is a bootstrap for setting up a Twitter bot with Node.js using the `twit` library. The bot will favorite and retweet what you specify when configuring it. It will also reply to followers with a selection of canned responses.
28
+
</details>
29
+
30
+
>Since doing this initial guide I have discovered the awesomeness of Zeit's now, the master branch of this project will now use `now` for deployment, if you want to use Heroku you can refer to the [deploy with Heroku][dep-with-heroku] branch 👍.
As a primer for this, there is a great post by [@amanhimself](https://twitter.com/amanhimself) on making your own twitter bot. This is an expansion on that with further detail on configuration on Heroku.
35
+
This is a bootstrap for setting up a simple Twitter bot with Node.js using the `twit` module. The bot will retweet what you specify when configuring it. It will also reply to followers with a selection of canned responses.
36
+
37
+
As a primer for this, there is a great post by [@amanhimself](https://twitter.com/amanhimself) on making your own twitter bot. This is an expansion on that with further detail on configuration and deployment with `now`.
31
38
32
39
Before starting the clock you'll need to set up some accounts if you don't have them already.
33
40
34
41
## What you'll need
35
42
36
43
- Twitter account
37
-
- Development environment with Node.js and NPM
38
-
* c9 account
39
-
* Node.js
40
-
* NPM
41
-
- Heroku account
44
+
- Development environment with Node.js and npm
45
+
-[Zeit account](https://zeit.co/login)
42
46
43
47
## Setup twitter
44
48
45
-
Set up an application on the Twitter account you want to favorite and retweet from via: [https://apps.twitter.com/app/new](https://apps.twitter.com/app/new)
49
+
Set up an application on the Twitter account you want to retweet from via: [https://apps.twitter.com/app/new](https://apps.twitter.com/app/new)
46
50
47
51
As an example, I'll configure the old [@DroidScott](twitter.com/droidscott) twitter account I have so you can follow along.
48
52
49
-
Straight forward enough for the twitter application, just make sure you add your phone number to your Twitter account before clicking the **Create your Twitter application** button.
53
+
Straight forward enough for the twitter application, make sure you add your phone number to your Twitter account before clicking the **Create your Twitter application** button.
50
54
51
55

52
56
53
57
You should now be in the 'Application Management' section where you will need to take note of your keys. You should have your 'Consumer Key (API Key)' and 'Consumer Secret (API Secret)' already available. You'll need to scroll to the bottom of the page and click the **Create my access token** to get the 'Access Token' and 'Access Token Secret' take note of all four of them as you'll need them when setting up the bot.
54
58
55
59
## Setup development environment
56
60
57
-
For this I'm just going to say use [Cloud9](https://c9.io/) as you can be up and running in minutes with one of the pre made Node.js environments.
61
+
If you don't already have a dev environment with node installed then for a quick-start I'd suggest using [Cloud9](https://c9.io/) you can be up and running in minutes with one of the pre made Node.js environments.
58
62
59
-
Note that if you choose to use Heroku and/or Cloud9 IDE in building this (like I do in this guide) in some regions you will be prompted to give a credit card number to create these accounts.
63
+
Note that in some regions you will be prompted to enter credit card information to use Cloud9 you will not be charged, there are other options to use like [Glitch](glitch.com) if you don't have a credit card. For this guide I'm going to be using Cloud9 which is what will be in the images.
60
64
61
65

62
66
63
67
## Set up the bot
64
68
65
-
In the project tree delete the example project files of `client`, `package.json`, `README.md` and `server.js`. You won't need them, but you can leave them there if you desire.
69
+
In the project tree delete the example project files of `client`, `node_modules`, `package.json`, `README.md` and `server.js`. You won't need them, but you can leave them there if you so desire.
70
+
71
+

66
72
67
73
In your new Node.js c9 environment go to the terminal and enter:
The environment project tree should look something like this:
81
+
The environment project tree will look something like this:
76
82
77
83
```text
78
84
twitter-bot-bootstrap/
@@ -93,7 +99,7 @@ twitter-bot-bootstrap/
93
99
94
100
## Node dependencies
95
101
96
-
Before configuring the bot we'll need to install the dependencies, cd into the project folder with `cd tw*` this will move you to `:~/workspace/twitter-bot-bootstrap (master) $ ` from the terminal enter:
102
+
Before configuring the bot we'll need to install the dependencies, cd into the project folder with `cd tw*`in the terminal this will move you to `:~/workspace/twitter-bot-bootstrap (master) $ ` from the terminal enter:
97
103
98
104
```shell
99
105
npm install
@@ -110,19 +116,16 @@ Here is an example of the `dependencies` in the `package,json` file:
If you get any `WARN` messages such as `npm WARN package.json twitter-bot@1.0.0 No repository field` this will not break the bot so it's safe to ignore.
125
-
126
129
Now you can configure the bot. From the terminal enter:
127
130
128
131
```shell
@@ -146,7 +149,7 @@ The `.env` file is where we can configure our bot, here we set what we want to s
>NOTE none of the `.env` items have quotes `''` round them.
152
+
>NOTE none of the `.env` items have quotes `''` round them or spaces between the key and the value `KEY=value`
150
153
151
154
```text
152
155
TWITTER_CONSUMER_KEY=Fw***********P9
@@ -164,6 +167,8 @@ TWITTER_RETWEET_RATE=120
164
167
TWITTER_SEARCH_COUNT=20
165
168
```
166
169
170
+

171
+
167
172
That should be it. Go to the terminal, enter `npm start` and you should get some output:
168
173
169
174

@@ -172,137 +177,29 @@ Check the Twitter account:
172
177
173
178

174
179
175
-
## Heroku
176
-
177
-
Cool, now we have a bot that we can test on our dev environment, but we can't leave it there. We'll need to deploy it to Heroku.
178
-
179
-
If you haven't done so already, set up a [Heroku account](https://signup.heroku.com) then select **Create a new app** from the dropdown box on the top right of your dashboard. On the next screen, name the app if you want and then click **Create App**.
180
-
181
-

182
-
183
-
You'll be presented with your app dashboard and instructions for the deployment method.
184
-
185
-

186
-
187
-
Your app name should be displayed on the top of your dashboard. You'll need this when logging in with the Heroku CLI.
188
-
189
-

190
-
191
-
## Heroku CLI
192
-
193
-
We're going to deploy initially via the Heroku Command Line Interface (*CLI*).
194
-
195
-
On your c9 environment terminal, log into Heroku [it should be installed by default]
196
-
197
-
```shell
198
-
heroku login
199
-
```
200
-
201
-
Enter your credentials.
202
-
203
-
```shell
204
-
cd twitter-bot-bootstrap
205
-
git init
206
-
heroku git:remote -a your-heroku-app-name
207
-
```
208
-
209
-
Deploy your application.
210
-
211
-
```shell
212
-
git add .
213
-
git commit -am 'make it better'
214
-
git push heroku master
215
-
```
216
-
217
-
You should get build output on the terminal:
218
-
219
-

220
-
221
-
Then check the output with:
222
-
223
-
```shell
224
-
heroku logs -t
225
-
```
226
-
227
-
All good? Cool! :sunglasses:
228
-
229
-
## Heroku variables
180
+
You now have a tweet bot, if you want to have this deployed so it's not just running from your machine or from the c9 machine [which is against their terms of service] then we can go over that next.
230
181
231
-
Now that we have our bot on Heroku we need to add environment variables to store our Twitter keys. This is because the `.env` file where we stored our keys is listed in the `.gitignore` file, which tells git not to upload that file to Heroku. It also makes it so if in the future we want to add our code to GitHub we don't have to worry about the `.env` file making our keys public, because the file will automatically be ignored.
182
+
## Deploy with now
232
183
233
-
All you need to do is go to the console of your Heroku app and select the 'Settings' sections and add in your Twitter keys from the `.env` file. Click the 'Reveal Config Vars' button and add in the variables with their corresponding values:
234
-
235
-
```text
236
-
CONSUMER_KEY
237
-
CONSUMER_SECRET
238
-
ACCESS_TOKEN
239
-
ACCESS_TOKEN_SECRET
240
-
```
241
-
242
-
Once you have the Heroku vars set up, take a look at the `config.js` file of this project. You are going to delete this line:
184
+
Got your [Zeit account](https://zeit.co/login) set up? Now is the time if not, then install `now` from the terminal:
243
185
244
186
```shell
245
-
require('dotenv').config();
187
+
npm i -g now
246
188
```
247
189
248
-
You're now ready to deploy to Heroku again. Your console commands should look something like this:
190
+
Then `now` from the terminal and you will be prompted to enter your email, you will be sent a confirmation email, click the link and you're ready to go!
249
191
250
-
```shell
251
-
git add .
252
-
git commit -m 'add environment variables'
253
-
git push heroku master
254
-
```
192
+

255
193
256
-
Then you can check the Heroku logs again with:
194
+
If you take a look at the `package.json` file in the `"scripts"` section you see there is one for `"deploy"` this is the command to deploy the bot to `now`, so from the terminal:
257
195
258
196
```shell
259
-
heroku logs -t
197
+
npm run deploy
260
198
```
261
199
262
-
You should now have a bot you can leave to do its thing forever more, or until you decide you want to change the search criteria :smile:
200
+
This will use all our environment variables we defined within our `.env` file for use on the now servers.
263
201
264
-
## Heroku deployment via GitHub
265
-
266
-
You can also deploy your app by connecting to GitHub and deploy automatically to Heroku each time your master branch is updated on GitHub, this is straight forward enough.
267
-
268
-
Go to the ‘Deploy’ dashboard on Heroku, select GitHub as the deployment method. If you have connected your GitHub account to your Heroku account then you can search for the repository. If you forked this repo, then you can just enter `twitter-bot-bootstrap` and **Search**. You can then click the **Connect** button and now you can auto deploy from GitHub!
269
-
270
-

271
-
272
-
## Heroku troubleshooting
273
-
274
-
What do you mean it crashed!?
275
-
276
-

277
-
278
-
Ok, I found that sometimes the `worker` is set as `web` and it crashes out. Try setting the `worker` again:
279
-
280
-
```shell
281
-
heroku ps:scale worker=0
282
-
heroku ps:scale worker=1
283
-
```
284
-
285
-
Or try this to be sure there are no web roles:
286
-
287
-
```shell
288
-
heroku scale web=0 worker=1
289
-
```
290
-
291
-
If that still crashes out then try setting the `Resources` on the Heroku dashboard, I found if you toggle between the `web`, `heroku` and `worker` it usually settles down. Basically you need to be set to the **`worker`** Dyno this is what causes the `Error R10 (Boot timeout)` crashes because it's trying to use one of the other resources when it should be using the **`worker`** Dyno.
292
-
293
-

294
-
295
-
Other useful Heroku commands I use:
296
-
297
-
```shell
298
-
heroku restart
299
-
```
300
-
301
-
By default you can only push your master branch if you are working on a development branch i.e. `dev` branch. If you want to test on Heroku, then you can use:
302
-
303
-
```shell
304
-
git push heroku dev:master
305
-
```
202
+
You will get terminal output with a URL for where your bot is located, click the link and you can watch it get built.
306
203
307
204
## Handy tip
308
205
If you want to add this to your own GitHub repo and don't want to share your API keys :key: with the world then you should turn off tracking on the `.env` file. From the terminal enter this git command:
@@ -311,14 +208,17 @@ If you want to add this to your own GitHub repo and don't want to share your API
311
208
git update-index --assume-unchanged .env
312
209
```
313
210
314
-
I have added my most used git command I use in this [gist](https://gist.github.com/spences10/5c492e197e95158809a83650ff97fc3a)
211
+
I have added my most used git commands I use in this [repo][cheetsheets] I use it on a daily basis, please feel free to use it.
Please fork this repository and contribute back using pull requests.
318
218
319
219
Any contributions, large or small, major features, bug fixes and integration tests are welcomed and appreciated but will be thoroughly reviewed and discussed.
320
220
321
-
####Links
221
+
## Links
322
222
323
223
Credit for the inspiration for this should go to [@amanhimself](https://twitter.com/amanhimself) and his posts on creating your own twitter bot.
324
224
@@ -334,7 +234,7 @@ Credit for the inspiration for this should go to [@amanhimself](https://twitter.
0 commit comments