Home
Forums
New posts
Search forums
What's new
New posts
New resources
New profile posts
Latest activity
Resources
Latest reviews
Search resources
Members
Current visitors
New profile posts
Search profile posts
DMCA Policy
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
FEEL FREE TO SHARE TUTORIALS, YOUR SKILS & KNOWLEDGE ON CODING, SCRIPTS, THEMES, PLUGINS OR ANY RESOURCES YOU HAVE WITH THE COMMUNITY-
Click Here To Post Your Request,
JOIN COMPUTER REPAIR FORUM
Home
Forums
TUTORIALS
CODING TUTORIALS
Node.js
How to Create a Twitter Bot with Node.js
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="codeguru" data-source="post: 62" data-attributes="member: 2"><p><img src="https://davidwalsh.name/demo/twitterbot.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p>Twitter bots have been in the news over the past few years due to election meddling, not only in the United States but stretching across the globe. There are, however, good and logical reasons for creating Twitter bots. In order to see how easy it was to create a Twitter bot, for good or evil, I decided to create my own Twitter bot. Five minutes of work and I had a working bot — let’s see how it’s done!</p><p></p><p>The first step in creating a Node.js Twitter bot is <a href="https://apps.twitter.com/" target="_blank">creating an app on the Twitter website</a>:</p><p></p><p><img src="https://davidwalsh.name/demo/TwitterCreateApp.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p>Provide the required information and you’ll have the ability to create access token and consumer information.</p><p></p><p>The next step is downloading the <a href="https://github.com/ttezel/twit" target="_blank">twit</a> Node.js resource:</p><p></p><p></p><p>yarn install twit</p><p></p><p></p><p>With twit available, create an instance of Twit with the access token consumer information you were given by the Twitter app website:</p><p></p><p></p><p>const Twit = require('twit')</p><p></p><p>const T = new Twit({</p><p> consumer_key: 'YOUR_INFO_HERE',</p><p> consumer_secret: 'YOUR_INFO_HERE',</p><p> access_token: 'YOUR_INFO_HERE',</p><p> access_token_secret: 'YOUR_INFO_HERE',</p><p> timeout_ms: 60 * 1000,</p><p>});</p><p></p><p></p><p>Now the action can happen. Here are a few examples of basic Twitter bot functionality:</p><p></p><p></p><p>// Post a tweet</p><p>T.post(</p><p> 'statuses/update',</p><p> { status: 'This is an automated test!' },</p><p> (err, data, response) => {</p><p> console.log(err, data, response);</p><p> }</p><p>)</p><p></p><p>// Retweet a given tweet</p><p>T.post('statuses/retweet/:id', { id: '697162548957700096' })</p><p></p><p></p><p>Let’s think of a more practical example: using the Stream API to “like” any tweet you are mentioned in:</p><p></p><p></p><p>const stream = T.stream('statuses/filter', { track: ['@davidwalshblog'] });</p><p></p><p>stream.on('tweet',</p><p> tweet => {</p><p> console.log('tweet received! ', tweet)</p><p> T.post(</p><p> 'statuses/retweet/:id',</p><p> { id: tweet.id },</p><p> (err, data, response) => {</p><p> console.log(err, data, response);</p><p> }</p><p> )</p><p> }</p><p>);</p><p></p><p></p><p>Getting a Twitter bot up and running takes minimal effort, which is why it’s important that services like Twitter protect its users from evil-doers. Bad guys aside, there are plenty of good reasons to create a Twitter bot, whether it be for internal analytics, promotion, or even creating your own Twitter app. Thank you to Tolga Tezel for creating an amazing JavaScript resource for interacting with Twitter!</p></blockquote><p></p>
[QUOTE="codeguru, post: 62, member: 2"] [IMG]https://davidwalsh.name/demo/twitterbot.png[/IMG] Twitter bots have been in the news over the past few years due to election meddling, not only in the United States but stretching across the globe. There are, however, good and logical reasons for creating Twitter bots. In order to see how easy it was to create a Twitter bot, for good or evil, I decided to create my own Twitter bot. Five minutes of work and I had a working bot — let’s see how it’s done! The first step in creating a Node.js Twitter bot is [URL='https://apps.twitter.com/']creating an app on the Twitter website[/URL]: [IMG]https://davidwalsh.name/demo/TwitterCreateApp.png[/IMG] Provide the required information and you’ll have the ability to create access token and consumer information. The next step is downloading the [URL='https://github.com/ttezel/twit']twit[/URL] Node.js resource: yarn install twit With twit available, create an instance of Twit with the access token consumer information you were given by the Twitter app website: const Twit = require('twit') const T = new Twit({ consumer_key: 'YOUR_INFO_HERE', consumer_secret: 'YOUR_INFO_HERE', access_token: 'YOUR_INFO_HERE', access_token_secret: 'YOUR_INFO_HERE', timeout_ms: 60 * 1000, }); Now the action can happen. Here are a few examples of basic Twitter bot functionality: // Post a tweet T.post( 'statuses/update', { status: 'This is an automated test!' }, (err, data, response) => { console.log(err, data, response); } ) // Retweet a given tweet T.post('statuses/retweet/:id', { id: '697162548957700096' }) Let’s think of a more practical example: using the Stream API to “like” any tweet you are mentioned in: const stream = T.stream('statuses/filter', { track: ['@davidwalshblog'] }); stream.on('tweet', tweet => { console.log('tweet received! ', tweet) T.post( 'statuses/retweet/:id', { id: tweet.id }, (err, data, response) => { console.log(err, data, response); } ) } ); Getting a Twitter bot up and running takes minimal effort, which is why it’s important that services like Twitter protect its users from evil-doers. Bad guys aside, there are plenty of good reasons to create a Twitter bot, whether it be for internal analytics, promotion, or even creating your own Twitter app. Thank you to Tolga Tezel for creating an amazing JavaScript resource for interacting with Twitter! [/QUOTE]
Insert quotes…
Verification
Post reply
Richest Freecoded User
Most Freecoin
freecoded
4,976 Freecoin
J
Johnhendrick
736 Freecoin
S
Smith16
683 Freecoin
Davy200
590 Freecoin
P
Peterparker87
475 Freecoin
nathan69
426 Freecoin
Laureine
415 Freecoin
A
anajeen
395 Freecoin
C
codeguru
282 Freecoin
Tekera
267 Freecoin
Home
Forums
TUTORIALS
CODING TUTORIALS
Node.js
How to Create a Twitter Bot with Node.js
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top