If you're too busy to Tweet frequently, I made a Twitter Bot just for you!

If you're too busy to Tweet frequently, I made a Twitter Bot just for you!

Play this article

To automate my tweeting, I created a simple Twitter bot to tweet at 8 AM, 4 PM, and 8 PM daily. Also, you have control over the timing of tweets.

Before we get started, if you only want to set up your bot without reading this blog, you can simply clone this Github Repository and follow the video tutorial that is provided below.

Let's do it. The following things are going to be necessary:

API from Twitter: If you already have a Twitter account, you can use that account to create a developer account and gain access to Twitter APIs.

Node.js - If you do not already have NodeJS installed on your computer, you may get it from the official website and install it there.

Twit NPM is a module for the NPM package manager that facilitates easy interaction with the Twitter API.

Cron NPM - We are going to employ this in the configuration of our daily scheduler.

  • Signing Up as a Twitter Developer

The Twitter APIs required to build Twitter Bot are only available to those with a Twitter developer account. Proceed to the developer portal and register for an account there, then build a new project by following the instructions here - Link

We need to provide your app access to read and write, which it will not have by default, you can do so by navigating to: Dashboard > App Setting > User authentication settings

You can follow this Link to setup this properly.

  • Project Setup

Now we get down to the core of setting up our project, the initialisation.

mkdir twt-bot
cd twt-bot
npm init

During npm init, you will be prompted to enter package details such as name, author, description, entry point, etc. In the absence of input, the defaults will apply. You have the basic structure for your project set up; next, we'll add the necessary NPM packages.

npm i twit
npm i cron
npm i dotenv

Now create a .env file and copy paste below code

  • So, let's get to work on that Twitter bot Script

Now Create a file named as bot.js and copy paste below code

const Twit = require("twit");
const client = new Twit({
  consumer_key: process.env.TWITTER_API_KEY,
  consumer_secret: process.env.TWITTER_API_SECRET,
  access_token: process.env.TWITTER_ACCESS_TOKEN,
  access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,

const postTweet = (message) => {
  return new Promise((resolve, reject) => {
        status: message,
      (error, data, response) => {
        if (error) {
        } else {

module.exports = { postTweet };

The preceding code utilises our .env config to securely transmit tokens and secrets for our Twitter app. Following this, we've developed the bot's primary function, postTweet(), which sends out a tweet and returns success data or error information if there's a problem.

Now, create a file randomTweet.js and copy paste below code

const randomTweet = function () {
  this.TWEET = [
    "I'm not going to die. I'm going to live forever, or die trying - Goku",
    "I'm not going to let you die. I'm going to save you. - Naruto",
    "A Lannister always pays his debts -Tyrion Lannister",
    "A lion doesn't concern himself with the opinions of sheep -Tywin Lannister",
    "A mind needs books as a sword needs a whetstone, if it is to keep its edge -Tyrion Lannister",

module.exports = new randomTweet();

Listed above is an array of well-known sayings, and every so often, bot will tweet a phrase from the list randomly.

Now at last create an app.js and copy paste the below code

const CronJob = require("cron").CronJob;
const randomTweet = require("./randomTweet");
const { postTweet } = require("./bot");

// Get random tweet from randomtweet.js
function getTweet() {
  return randomTweet.TWEET[
    Math.floor(Math.random() * randomTweet.TWEET.length)

//Cron Job Setup
new CronJob(
  "* 8,16,20 * * *", // Running a cron job 3 times (8 am, 4 pm and 8 pm) everyday, for more - https://crontab.guru/
  function () {
  "Asia/Kolkata" //Time Zone

The following piece of code will be used to select a quote at random from the list. The cron npm package can be used to broadcast the tweets at a predetermined time, as shown here. According to the code that is presented above, cron will run every day at 8:00 am, 4:00 pm, and 8:00 pm in the Asia/Kolkata timezone. During each of those times, it will call our bot to tweet.

  • Now that everything is set up

We are ready to run our bot; however, before doing so, you can alter your package.json start script by following the instructions below.

"scripts": {
    "start": "node app.js"

and now run following command in your terminal to see magic

npm start

You may set corn to tweet every minute just for the quick demonstration, and this will cause your bot to tweet every minute.

Github Repo

Hope you like this, Thank you

Did you find this article valuable?

Support Aadarsh Kashyap by becoming a sponsor. Any amount is appreciated!