Hello folks I am Aditya. I develop Chat Bots, mostly on Messenger and Twitter Bot.
Before we start writing any code let’s first set up all these requirements
- A Twitter Account and a Twitter App
- A command line interface which you probably already have (Command Prompt, Terminal …)
- A text editor to write the codes (Sublime Text recommended)
NOTE: If you want to host it on a web server then you will be needing a Heroku account. Heroku lets you host your Twitter Bot online for free. But for the sake of simplicity let’s not talk about it just yet.
1. CREATING A TWITTER APP
You can’t create a bot without a twitter account. Go create one here.
Awesome, you have a fresh new Twitter account. Now we’ll be needing something called API keys. API keys are the ‘username and password’ for Bots. Your Bot will require this to make API calls, or in simple words, interact with the data on Twitter’s server.
To get your API keys, you need to first create a Twitter app. Go to apps.twitter.com to get this started.
Click on ‘Create New app’. Then on the next page fill up all the details. If you don’t have a website just put a placeholder in the Website field. You can leave out the Callback URL field. Tick the Developer Agreement and then finally click on ‘Create Your Twitter application’ at the bottom.
We now have a Twitter application. Let’s get the API keys.
On the next page go to ‘Key and access Tokens‘ tab. We’ll need four different key from here out of which 2 (Consumer Key and Consumer Secret) are already created. We need to generate a thing called “access tokens”. On the bottom of the page, click on the “Create my access tokens”.
Now you shall have the following information:
- Consumer Key
- Consumer Secret
- Access Token
- Access Token Secret
Never ever share your keys. I have deleted this app so don’t try doing anything stupid with it.
Now we need to create a new file called
By the way, you can get the code on my Github page here. But I highly recommend typing the code rather than copying.
2. Installing and Setting up Node JS
Now, let’s install Node JS. The instructions will vary a bit depending on your operating system, so just follow the official download and installation guide it’s pretty simple.
Once you have Node JS installed, fire up your command line and type
node --version. If you get something like ‘v6.10.3‘ then your installation was successful. The number you get back is the version of Node.
npm --version. You should get a back a number like before.
If you get an error during this process please re-install Node. Great! We now have all the requirements fulfilled.
3. Setting Up The Project
Hold your horses folks. Few more minutes and then we’ll get into coding. Lets set up our project first. This process takes about a minute or so.
Create a folder to store all your bot files. Inside the Folder you should have the
config.js file we created earlier. Create another file called
bot.js (you can name it anything you want but it should have .js extension). This is the file where we’ll be writing all our codes.
Finally open your command line and then navigate to this folder. Then type the following command
This will set up our
package.json file which will contain all the metadata of our project like project name, project version, licence and most importantly the dependencies. Dependencies are the node modules that we use in our program.
It’s not necessary to fill the details. In fact you can leave everything blank. Just press enter to skip the field.
Finally you should have a
package.json file created on your main folder. You folder should have 3 files by now
4. Installing Twit Module
We’ll be using a Node Module called Twit. This module helps us communicate with Twitter Server using our API keys. This is the only module we’ll need for our Twitter Bot.
Open your terminal and navigate to the main folder. Run the following command
npm install --save twit
This will install the Twit Node Module. If you check your
package.json file you should see Twit in your dependencies. Every time you install a node module it’ll automatically be added to your
package.json file. But it’s necessary that on your command line you must be on the same path where you want to install the module.
I think I’ll need to divide this tutorial into 3 parts because its already getting lengthy.