Most of us are familiar with Twitter. But we are not much familiar that we can automate the activities like status posting, retweeting, liking, commenting and so on. So,here I'll show you how we can automate some of the activities like getting the twitter data,posting the status and retweeting with and a npm package called . Node.js Twit And we ll deploy the project to github and heroku and keep it running... Let's start by signing in to a , You'll get insight of creating a developer account and will be able to Create an app where you will get the following keys: twitter developer section consumer_key : , : , : , : 'key' consumer_secret 'key' access_token 'key' access_token_secret 'key' Project structure: project---| |--config--config.env--config.js |--botget.js |--botpost.js |--botretweet.js |--Procfile //not needed for deployment //not needed for deployment Installation: Node.js must be installed on your system. In the project folder,In terminal enter the following commands: $ git init -y $ npm i twit Setting up the environment variables: Let's install dotenv to hide the keys when uploading to github and heroku $ npm i dotenv Create a folder config in the project directory.And inside that create a file called config.env: API_KEY=key API_SECRET_KEY=key ACCESS_TOKEN=key ACCESS_TOKEN_SECRET=key Create another file inside project directory as config.js: path = ( ) dotenv = ( ) ( ).config(); dotenv.config({ : }) .exports = { : process.env.API_KEY, : process.env.API_SECRET_KEY, : process.env.ACCESS_TOKEN, : process.env.ACCESS_TOKEN_SECRET } const require 'path' const require 'dotenv' require 'dotenv' // Load config path './config/config.env' //exports the keys module consumer_key consumer_secret access_token access_token_secret Now we are ready to work on the bot scripts. You might want to look Sample data from twitter and before going on further. JSON twit Create a file as botget.js in the root of the project: .log( ) config = ( ) Twit = ( ); T = Twit(config); parameters = { : , } T.get( ,parameters,fetchData); { tweets = data.statuses; ( i= ;i< tweets.length;i++){ } .log(data) } console "Bot is starting" var require './config/config' //imports keys var require 'twit' var new var q 'node' //query to search the twitter,can be any thing count : 2 //no. of data to fetch // search twitter for all tweets containing the word 'suJJJan' 'search/tweets' ( ) function fetchData err, data, response var for var 0 // checks the statuses twiceconsole.log(tweets[i].text);//prints the text console To run the script.Use: $ node botget.js Sample Output: Bot is startingRT @Hello2: @random twitter is best @random twitter is best { : [ { : , : , : , : , : , : [ ], : [ ], : , : , : , : , : , : , : [ ], : , : , : , : , : [ ], : , : , : , : , : , : }, { : , : , : , : , : , : [ ], : [ ], : , : , : , : , : , : , : [ ], : , : , : , : , : , : , : , : , : , : } ], : { : , : , : , : , : , : , : , : , : } } Create a file botpost.js the root the project: .log( ) config = ( ) Twit = ( ); T = Twit(config); tweetThis(); { statsArray = [ , , ]; tweet = { : stat } T.post( ,tweet,tweeted); { (err){ .log( ) } { .log( ) } } } statuses created_at 'Thu Aug 06 10:38:09 +0000 2020' id 1291311562636183600 id_str '129110210662636183559' text "RT @Hello2: @random twitter is best" truncated false entities Object metadata Object source '' in_reply_to_status_id null in_reply_to_status_id_str null in_reply_to_user_id null in_reply_to_user_id_str null in_reply_to_screen_name null user Object geo null coordinates null place null contributors null retweeted_status Object is_quote_status false retweet_count 1 favorite_count 0 favorited false retweeted false lang 'en' created_at 'Sat Aug 01 02:24:27 +0000 2020' id 1289386479903371300 id_str '1289386479903371265' text "@ randomtwitter is best" truncated false entities Object metadata Object source '<a href="http://twitter.com/download/android" rel="nofollow">Twitter for Android</a>' in_reply_to_status_id 128921230498288600 in_reply_to_status_id_str '1289469260498288640' in_reply_to_user_id 832773717648143900 in_reply_to_user_id_str '832773417688143873' in_reply_to_screen_name 'random' user Object geo null coordinates null place null contributors null is_quote_status false retweet_count 1 favorite_count 0 favorited false retweeted false lang 'en' search_metadata completed_in 0.027 max_id 1291322662436183600 max_id_str '1291322662636183559' next_results '?max_id=1289386479904371264&q=random&count=2&include_entities=1' query 'node' refresh_url '?since_id=1291322662646183559&q=random&include_entities=1' count 2 since_id 0 since_id_str '0' as in of console "Bot is starting" var require './config/config' var require 'twit' var new //uncomment this if u want to run the event in certain interval //setInterval(tweetThis,3600000)//every 1 hour(time is in ms) ( ) function tweetThis var "hello" "How ya doin" "I love node" //selects random tweets from the arrayvar stat = statsArray[Math.floor(Math.random()*statsArray.length)] var status 'statuses/update' //posts tweets ( ) function tweeted err, data, response if console "Something is wrong" else console "Works fine" Now we will create a bot which retweets and deploy it to heroku. Create a file as botretweet.js in the root of the project: .log( ); twit = ( ); config = ( ); T = twit(config); reTweet(); { parameters = { : , : , : } T.get( , parameters, { (!err) { T.post( ,{ : retweetId },tweeted); { (response) { .log( ); } .log( ); } } } { .log( ); } }); } console "Bot is started" var require 'twit' var require './config/config' var new // find latest tweet according the query 'q' in parameters //set q as # or @ for hashtags and username.Can keep both as '@tag,#hash' ( ) function reTweet var q '@suJJJan' result_type 'recent' lang 'en' 'search/tweets' ( ) function err, data if //gets the tweetidvar retweetId = data.statuses[0].id_str; //retweets the status in which the 'q' is mentioned 'statuses/retweet/:id' id ( ) function tweeted err, response if console 'Retweeted successfully' // if there was an error while tweeting if (err) { console 'Duplicate tweet or Something went wrong' else console 'Nothing found' Create Procfile in the root of the project folder: Add the code below and save the file. worker:node botretweet.js Pushing Code to Github: Create a account github Create a new repository install if u are familiar with it or else you can directly upload from the Github site git create a file called .gitignore,inside that: node_modules config/config.env save the file. Now use the following commands to push code to github. $ git init $ git add . $ git commit -m $ git remote add origin https: $ git push -u origin master "initial commit" //github.com/username/project.git This will deploy the project to github. Now we will deploy the code to heroku and keep the script running forever. Pushing Code to Heroku: Create a account heroku You'll see new,create new app.. e docs to deploy from browser read th Install heroku cli Follow the commands we ll have to add the keys as below: $ heroku create appname $ heroku config:set API_KEY=yourkeyhere API_SECRET_KEY=yourkeyhere ACCESS_TOKEN=yourkeyhere ACCESS_TOKEN_SECRET=yourkeyhere $ git remote set-url heroku https: $ git push heroku master //git.heroku.com/appname.git The code will be deployed to heroku. Then goto heroku dashboard: you will see the app which you have created select the app select 'resource 'In 'free dynos section', you ll see 'web npm start' (for webapps only) click on edit and turn if off you ll see 'worker node botretweet.js' (worker are typically used for background jobs, queueing systems, and timed jobs) click on edit and turn it on. On adds-on section search 'Heroku-Scheduler' Select it and you ll reach to another dash boardSelect create job. Set time as you like and in 'Run Command' add node botretweet.js and save job You are all set. Your bot will run and retweet every hour or so on as per the time duration you have set. Check the full code here Previously published at https://blogue.tech/blogs/twitter-bot-with-node.js-and-deploy-to-heroku