A Blog Of Random Coding Techniques & Experiments Robert Askam

How to build a Twitter Bot with Heroku and Twit Post How to build a Twitter Bot with Heroku and Twit 1 year ago

You can see the progress here Twitter Bot Github.
Start by creating an App in Twitter Application Manager. Once created get the following in you app’s dashboard:

  • Consumer Key

  • Consumer Secret

  • Access Token

  • Access Token Secret

Then create a Free Heroku Account here.

Lets Start

Now create to files called bot.js and package.json in a folder.

Packages Required

We will require the Twit package and Node but copy the following and change the details for your app.
"name": "robATwitter",
"version": "1.0.0",
"description": "A twitter bot for my site Rob Askam",
"main": "bot.js",
"scripts": {
"start": "node bot.js"
"keywords": [
"author": "Robert Askam",
"license": "ISC",
"dependencies": {
"rss-tweet-bot": "^0.1.1",
"twit": "^2.2.5",
"path": "^0.12.7",
"express": "^4.14.0"
"engines": {
"node": "7.9.0"

Bot File

Firstly to hide your keys process.env.[key name] and i will show you how to use them on Heroku.
Change the
const stream = TH.stream
track, these are what will be looked for and if found access the function.
const express = require('./node_modules/express');
const app = express();
const path = require('./node_modules/path');
app.set('port', (process.env.PORT || 5000));
app.get('/', function (request, response) {
response.sendFile(path.join(__dirname + '/index.html'));
}).listen(app.get('port'), function () {
console.log('App is running, server is listening on port ', app.get('port'));

const Twit = require('./node_modules/twit');
const TH = new Twit({ // Twit Handler
consumer_key : process.env.consumer_key,
consumer_secret : process.env.consumer_secret,
access_token : process.env.access_token,
access_token_secret : process.env.access_token_secret

const stream = TH.stream('statuses/filter', {
track: ['robert-askam', 'robert-askam.co.uk','www.robert-askam.co.uk']

stream.on('tweet', (tweet) => {

//This will like the tweet if contains the above keywords
},function (err,response) {
//This will retweet the tweet if it contains the above keywords
TH.post('statuses/retweet/:id', {
id: tweet.id_str

//This will add a rely to the selected tweet.
TH.post('statuses/update', {
status: `@${tweet.user.screen_name} Thanks for sharing :)`,
in_reply_to_status_id: tweet.id_str


Add a name for the App in your personal apps.
In the Deployment method select GitHub and link it to your account and App name.