How to make Twitter bot with Javascript

img Are you in need of creating an automated response to a certain hashtag or any activity on the social networking site Twitter? Then this article is just for you.
How do you go about this?
An application which listens to a hashtag is the Twitter bot.
A Twitter bot as the name sounds is an application used on twitter, used in listening for any activity on the social platform and performs an action in response.
JavaScript, a widely used high level programming language has been around for some time.
It is a popular language amongst web programmers, app developers and virtually everyone associated with creating on the internet sphere.

To create a Twitter bot with JavaScript, you need a runtime environment which allows the JavaScript to be run on every platform; ==Node.js== is an efficient tool for this. Node.js is NOT a programming language, rather it is a runtime environment which allows us to write a Twitter bot.

1.The following are needed in creating the twitter bot

  1. Node.js

  2. Creating Twitter app

  3. Command-line tools of the operating system you are running

  4. Installing the Node.js

    On Windows OS

  5. Select the latest stable version on nodejs.org

  6. Install using the default options

  7. Open a new session on command prompt

  8. Type console.log("hello") press the Enter key

  9. Hello is printed.

  10. Close the Node interpreter by pressing CTRL-C repeatedly

On OSX

Visit nodejs.org and download the latest version or use brew to install nodejs if you have brew.

2. Creating your Twitter App

  • Firstly, you will be needing a Twitter account to continue this process.
    Also you need to add your mobile phone number to your Twitter account.
  • Proceed to Twitter app page to register your app.
    You can name, get the API information for your app and control the way the app works on this page.
    Click Create New App and fill the required fields.
  • Note: The App will be identified with the registered name on other services. It will be noteworthy to choose a name you would like to use for advanced projects.

*You must accept the Developer Agreement to finalize the registration process.

Now that you have completed the registration process, you can now Change Permission by clicking on the "Permissions" tab. This is necessary as the default permission is set as "Read Only" which allows the application to read your tweets only. You can change this to "Read and Write"*. This new set permission enables the app to view and write tweets.

3. Using the command-line tool

  • Open the command prompt tool.
  • Type cd in the terminal and navigate to any directory of your choosing
  • Use mkdir twitterbot to create a folder in the directory
  • Use the cd twitterbot/ to access the folder
  • The Command prompt should not be closed as it will still be used ahead.

To create the JavaScript file and write Hello World!

  • Open your text editor e.g. Vim, Atom, Notepad++ e.t.c.
  • Create a new file and title it app.js and save it in the twitterbot folder previously created.
  • Write console.log("Hello World!") in the app.js. This code is similar to printf in C and System.out.printIn in Java respectively.
  • Save and return to the command prompt previously opened.
  • In the command prompt terminal, type node app.js to run the JavaScript and hit Enter. The Hello World print out will appear.
  • To set up the twitter package, you must first install the Twitter package which we will use to interact with the API. The npm i.e node package manager allows us to install packages to be used on node.js.
  • On cmd tool, type npm install twitter while the package is still in the twitterbot folder.
  • Look inside the folder after a minute and a folder called node_modules will be found. This houses any package added.
  • Return to the text editor and open the app.js file
  • We need to load the twitter package using the require function

Posting Tweets

We can use the twitter REST API to post a tweet.
The procedure is outlined at the documentation page documentation

Listening to Tweets

To listen to tweets, we can use the Twitter Streaming API which informs us of tweets in real-time.
From the documentation page documentation, we can find an example of Streaming API which we can modify to listen to tweets.

AUTHOR

READ NEXT

Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?