Last active
April 17, 2016 11:56
-
-
Save y-a-v-a/68ed3b86b3898c13ccaf53d9405f5ca1 to your computer and use it in GitHub Desktop.
Second lesson - basic twitter bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// load a package that makes tweeting possible | |
var Twitter = require('twitter'); | |
// configure the client so it can authorize itself | |
var client = new Twitter({ | |
consumer_key: 'xyz', | |
consumer_secret: 'abc', | |
access_token_key: '012-xyz', | |
access_token_secret: 'ABC' | |
}); | |
// define a variable | |
var myTweet; | |
// setInterval: do something every x miliseconds | |
setInterval(function() { | |
// assign a value to the variable | |
// in this case a piece of text (a string) with | |
// the time in miliseconds since 01-01-1970 | |
myTweet = 'It is now: ' + Date.now() + ' miliseconds since 1970'; | |
// update your twitter status which means | |
// post a tweet | |
client.post('statuses/update', | |
// the status is the text in 'myTweet' | |
{ status: myTweet}, | |
function() { | |
// when done tweeting, show what Twitter said in repsonse | |
console.log(arguments); | |
} | |
); | |
// 1000 miliseconds * 60 seconds * 2 = every 2 minutes | |
}, 2 * 60 * 1000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To run this program you need to do the following steps:
/Users/yourname/Desktop/twitter-bot
Terminal
app to that location:cd Desktop/twitter-bot
pwd
and verify the output is something like/Users/yourname/Desktop/twitter-bot
npm install twitter
node index.js
Be aware that you need OAuth tokens from Twitter via e.g.
apps.twitter.com
and/or use the tooltwurl
to generate tokens. See for more information: http://dghubble.com/blog/posts/twitter-app-write-access-and-bots/