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.
It is a popular language amongst web programmers, app developers and virtually everyone associated with creating on the internet sphere.
1.The following are needed in creating the twitter bot
Creating Twitter app
Command-line tools of the operating system you are running
Installing the Node.js
On Windows OS
Select the latest stable version on nodejs.org
Install using the default options
Open a new session on command prompt
Type console.log("hello") press the Enter key
Hello is printed.
Close the Node interpreter by pressing CTRL-C repeatedly
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.
- 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.
- 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
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.