TechJunkie is a BOX20 Media Company

Home Social Media Twitter How To Write a Twitter Bot

How To Write a Twitter Bot

How To Write a Twitter Bot

I’m not a coder and never will be but that hasn’t stopped me writing a simple Twitter bot to help me with my social media channels. I think I was asked to write this tutorial on purpose. If I can write a Twitter bot, anyone can!

Twitter bots can perform some basic but useful functions. I won’t pretend I figured it all out for myself because I didn’t. There are some good guides out there but I have added a few bits of my own experience to this one.

Why write a Twitter bot?

Aside from the stock answer of ‘because you can’, why would you want to write a Twitter bot? If you run a small business and don’t have time to keep up with Twitter, if you want to increase your presence without the effort, if you want to interact more or automate the boring stuff, all is possible with a bot.

The bot I created simply retweets to help keep the account ticking over while I’m doing other things. Other bots can check your grammar, send alerts that meet certain criteria, alert you to earthquakes and all sorts of neat stuff. I kept it simple but there’s no reason that you have to do the same.

Before you get writing, make sure to read Twitter’s automation rules. It outlines what you can and cannot do with Twitter bots. The rules are simple and only take a minute or two to read.

Write your Twitter bot

There are lots of bots and a few ways to write them. Some use Python or Node.js while others use simple Google Scripts. As I’m not a programmer, I liked the idea of a Google Script hosted in the cloud so I did that. I used this page as a guide as this guy is much cleverer than I.

  1. You will need a Twitter account for the bot to use. Set one up and sign in using that account.
  2. You will also need to create a Twitter application for the bot to use. Create one on this page. Give it a random URL, descriptive name and add any information you want. You may need to apply for a developer account to get access to this page, you may not.
  3. Once created, select Modify App Permissions and allow Read, Write and Access Direct Messages.
  4. Select Keys and Access Tokens and Create My Access Token. Leave the page open as we will need those keys in a minute.
  5. Visit this page to access the bot scripts. Grant the app access to your data when requested.
  6. Enter the Twitter Consumer Key, Consumer Secret, Access Token and Access Secret you got from Twitter in Step 3.
  7. Add your search phrases for the bot to use. This determines what your Twitter bot will retweet so choose carefully.
  8. Select Save once you have your search terms.

Once you select Save, the bot is live. It will perform periodic searches for the terms you entered and will retweet them. It is a very simple bot that demonstrates how simple it can be to automate something usually mundane.

Code a Twitter bot

If you’re more interested in coding a Twitter bot, that is fairly straightforward too. I used this site as inspiration and the bot worked fine. You will need a couple of software tools to get this working but it doesn’t take long.

  1. You will need Twit, a Twitter API and js which is a software install.
  2. Follow Steps 1-3 above if you haven’t already.
  3. Open a terminal or CMD window on the computer with Twit and Node.js installed.
  4. Type ‘npm init’ and hit Enter. Fill in the information is asks for.
  5. Type ‘npm install twit –save’ and hit Enter to create the dependency which allows the two apps to talk to each other.
  6. Open a text editor and create a file in the same directory and call it index.js.

Open index.js and type:

var Twit = require('twit')

var T = new Twit({

    consumer_key:         ' KEY ',

    consumer_secret:      ' KEY ',

    access_token:         ' KEY ',

    access_token_secret:  ' KEY ',


var users = ["USERID", "USERID", "USERID"];

var stream ='statuses/filter', {follow: users});

stream.on('tweet', function (tweet) {

    if (users.indexOf(tweet.user.id_str) > -1) {

        console.log( + ": " + tweet.text);'statuses/retweet/:id', { id: tweet.id_str }, function (err, data, response) {




  1. Where you see KEY, enter the corresponding key from Twitter.
  2. Where you see USERID, type the numeric String ID of the Twitter user. Type their username into this page to get the ID.

Once done, save your file and type ‘node index.js’ and hit Enter to run it.

Again, this is not my work but was originally written by Omar Sinan. I just made it more accessible.

When Will the FCC Stop Robocalls? What Can you Do For Now

Read Next 

Leave a Reply

Your email address will not be published. Required fields are marked *


Dec 6, 2018

963 Articles Published