Let's make a Twitter bot! (Part I)
Tags: twitter, bot, node
Published: 2019-10-18 13:28:10

---
title: Let's make a Twitter bot! (Part I)
published: true
description: The first part of a two part tutorial on making a simple Twitter bot using the Twit module and Twitter API
tags: twitter, bot, node
cover_image: https://thepracticaldev.s3.amazonaws.com/i/mxo1w68hphr4l0wpyvmw.jpg
---

If you've spent much time on Twitter you've likely encountered a Twitter bot or two. A bot is merely a program that automatically interacts with the Twitter API. 

Twitter bots can be useful, irritating, entertaining, or just plain silly. If you've ever thought of making your own Twitter bot but didn't know where to start, you're in the right place! We're going to make a simple Twitter bot using free resources.

In part I of this tutorial, we will focus setting up the various accounts you will need and getting your first automated status update posted to Twitter.

**Things you'll need**
* A [Twitter account](https://twitter.com)
* A [Glitch account](https://glitch.com/)
* [Postman](https://app.getpostman.com/app/download/win64) 
* Some basic JavaScript knowledge
* (For part II) Some kind of content for your bot

## Getting set up on Twitter ##
Before you can do anything else, you need a Twitter developer account. If you don't yet have one, here are the steps.

1. Create a new Twitter account or log into an existing one that you wish to use.

2. Once logged into a Twitter account, go to the [Twitter Developer site](https://developer.twitter.com/) at https://developer.twitter.com/

3. Click "Apply"
![A screenshot of the Twitter developer site with an arrow pointing to the "Apply" link in the top right corner](https://thepracticaldev.s3.amazonaws.com/i/khp0opwejgauiyah36fn.JPG)

4. On the next page, click on "Apply for a developer account"

5. You'll be given a few different options. For the purposes of this tutorial, I recommend selecting "Making a bot" under "Hobbyist." Click "Next" after making your selection(s).
![The "Making a bot" option selected, showing a green check mark in the top right corner](https://thepracticaldev.s3.amazonaws.com/i/fh76m0nqq464tcfz3zhc.JPG)

6. On the next page, verify your information is correct and fill out the required information (your country and name). Click "Next."

7. Next you will be asked to provide details about how you intend to use the API. Select the options you will need and answer the required questions. For this tutorial I have only selected "Yes" for the question "Will your app use Tweet, Retweet, like, follow, or Direct Message functionality?" Depending on your needs, you may select other options. Click "Next."


8. Verify everything is correct and click "Looks good!"

9. Read and agree to the developer agreement (or don't, end tutorial, have a nice day!)

_NB: If you do not have a phone number connected to your Twitter account, you will need one to continue your setup at some point._

Once you have completed the developer application you will need to click a link in your email to verify your email address. 

We are not done with the developer site just yet, but you will need to do some setup on Glitch (your bot host) before you can continue with the rest.

## Setting up your bot host ##

Getting your developer application submitted and approved is just the first step. Now you need to create the app that will be connected to your bot, but in order to do that you need to have your bot's website URL. So, let's take a moment to set that up.

If you don't already have a [Glitch](https://glitch.com) account, go create one now and then come back here.

1. After you have your account, go to [this template](https://glitch.com/edit/#!/twitterbot?path=README.md:1:0) on the Glitch site. 

2. In the top right corner, click the button that says "Remix to Edit" to create a copy on your account.
![A screenshot showing the "Remix to Edit" button in the top right corner of the page](https://thepracticaldev.s3.amazonaws.com/i/8fbl7atxreheiskosz9r.JPG)

3. After Glitch is done creating your site, click the button in the top left that says "Show" and then "In a new window". Copy the URL from this page or keep it open while moving on to the next section.

## Creating your Twitter bot's app ##

Okay, now that you have a URL for your bot's website, you can finish setting things up on the Twitter side.

1. Go back to [developer.twitter.com](https://developer.twitter.com) and mouseover your name in the top right corner. Select "Apps."

2. On the Apps page, click "Create an app"

3. Fill out the information about your app, pasting your new URL in the Website URL box. For this project we do not need to use the Twitter sign in API. I recommend leaving any non-required fields blank.

4. Click "Create" and then "Create" again.

Awesome! Your app has been created. Now you just need to get the API keys for your app and add them to your configuration file on Glitch. 

## Connecting your bot to Twitter ##

Before you can actually interact with the Twitter API, you need to setup your Glitch environment with the API keys. 

1. On the Twitter developer site, in your app's details page (the page you were taken to after creating the app) there is a tab at the top called "Keys and tokens," click it.
![A screenshot of the app details page showing the "Keys and tokens" tab](https://thepracticaldev.s3.amazonaws.com/i/bedor54e2rtixmodcess.JPG)

2. In another tab or window, open your Glitch project and click on the .env file. You should see a file opened in the right hand side that has a spot for all your API keys.
![A screenshot showing the .env file and the location of the API keys in the Glitch environment](https://thepracticaldev.s3.amazonaws.com/i/7j7g40tgjdcw04hhlan7.JPG)

3. Copy and paste the keys from the Twitter developer page to the corresponding location in the .env file. You will need to click "Create" under the "Access token & access token secret" section to generate those values. You do _not_ need to put the keys in quotes.

4. You will also see a spot in the .env file for `BOT_ENDPOINT`. This is what you will put after the slash in your URL to trigger your bot. This will be explained in more detail later. You can put anything you want for this value.
![A screenshot showing the .env file after all the values have been filled in](https://thepracticaldev.s3.amazonaws.com/i/5sdlh1vtxc00rqd2q0wx.JPG)

We're almost there, I promise!

## Sending your first automated Tweet ##

Great news, you are done with all the configuration stuff. Now you can start using your server to interact with the Twitter API. The template you used already comes with some basic stuff set up, so we will focus on that for now.

1. In your Glitch project, click on the "server.js" file and read through the code that is already there. You will see that it's already set up to send a simple "Hello world" post. So let's do it!

2. Open Postman and select the get request type. Put in your project URL plus the bot endpoint you created. (Note: You can also just put this URL into your browser's address bar, but as you build out your bot it will be easier to use Postman.)
![A screenshot showing the Postman interface](https://thepracticaldev.s3.amazonaws.com/i/wpgq20naywdtwcdb91y7.JPG)

3. Click "Send." If everything has been set up correctly, you should see a "Hello world" post on your bot's Twitter timeline.
![Screenshot of "Hello world" on twitter](https://thepracticaldev.s3.amazonaws.com/i/j9u7b3whbcqnuwuy4gme.JPG)

Congratulations, you've just successfully sent a Tweet using the Twitter API, but it's not a bot yet. In order to be a bot it needs to be automated, so let's do that.

## Making it a bot ##

In order to make your app a bot, the process of hitting your endpoint needs to be automated. There are several free tools you can use to accomplish this. For this tutorial, I will be using [UptimeRobot.com](https://uptimerobot.com).

UptimeRobot allows you to access a URL at a set interval that you decide. It will provide a status result in the dashboard indicating if the URL was accessible or not. This is often used to verify that a site is active, but you are going to use it to automatically trigger your bot.

1. Go to [UptimeRobot.com](https://uptimerobot.com) and create an account (or log in if you already have an account).

2. From your account dashboard, click the "Add New Monitor" button.
![Screenshot of the UptimeRobot dashboard showing the Add New Monitor button](https://thepracticaldev.s3.amazonaws.com/i/ul72clnqzp75n16e141b.JPG)

3. Select "HTTP(s)" for the monitor type

4. Provide a "Friendly Name," which is the name that will show on your dashboard, and your endpoint URL. Remember, the endpoint URL is your Glitch URL followed by slash and whatever you set for the BOT_ENDPOINT in your .env file (i.e. something like `https://funky-ricecake.glitch.com/MyAmazingBot456`).

5. Set an interval time. In order to comply with Glitch's terms of service, this should be an interval no less than 25 minutes. Depending on your bot, you can decide how often it should send a message. In the screenshot below, I have set the interval to once every 24 hours. This is the longest monitoring interval that UptimeRobot allows. If you want it to happen less often (i.e. once a week, once a month, etc) you may wish to find a different service.
![Screenshot from UptimeRobot.com showing the new monitor dialogue box](https://thepracticaldev.s3.amazonaws.com/i/3v9fmq2b6rg1njdo4c2s.JPG)

As soon as you click "Create Monitor" UptimeRobot will send the first request to your bot and the first automated status will be posted (which, unless you've already made some changes, will still be "hello world"). If you are not ready for your bot to make a post, you can wait until you have finished setting it up to create the monitor.

---

That's it! At this point you should have a bot that can post a status update. Now, I'm sure that you would like your bot to do more than say "hello world" every day (or, maybe not, you do you). If so, please keep an eye out for part II of this tutorial where we will get some data from some open source APIs and use it to create slightly more interesting Twitter updates.


[Cover image by Peyri Herrera on Flickr](https://www.flickr.com/photos/peyri/48825808/in/photolist-5jfdh-9X9Em-5jfek-rG9Fqw-626SgT-aWfwuk-7ka5Vz-d2EUi-253i2q8-66EEor-9zpYpp-6gLCqZ-7UCbw1-6hMJVU-PYXb6-9Yss3X-9pjpEw-7Puamg-6sGsY3-B1Vr8-aErH4u-6hMJEj-c1z2Fo-6KZEKf-926Ggh-b53aYD-bhNSLR-a1Qb1Q-e2HuW2-7LrZon-6J8xRT-bWFd4x-4gPheP-c2mmos-rNksgk-7t5BDs-5VpEr-6hHzvc-4YAkPo-GoM76-nxPg3-7vLqsV-6hHz64-41Htpk-ab9iZY-9XDEo-a6z7Kp-9BCdMc-wdYcV-4KZkx7)