Skip to content

Instantly share code, notes, and snippets.

@y-a-v-a
Last active April 17, 2016 11:56
Show Gist options
  • Save y-a-v-a/68ed3b86b3898c13ccaf53d9405f5ca1 to your computer and use it in GitHub Desktop.
Save y-a-v-a/68ed3b86b3898c13ccaf53d9405f5ca1 to your computer and use it in GitHub Desktop.
Second lesson - basic twitter bot
// 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);
@y-a-v-a
Copy link
Author

y-a-v-a commented Apr 17, 2016

To run this program you need to do the following steps:

  • Save the file in a directory. For example in /Users/yourname/Desktop/twitter-bot
  • Go with the Terminal app to that location: cd Desktop/twitter-bot
  • Check if you are on the right path by typing pwd and verify the output is something like /Users/yourname/Desktop/twitter-bot
  • Install the dependency package: npm install twitter
  • Run the program: node index.js

Be aware that you need OAuth tokens from Twitter via e.g. apps.twitter.com and/or use the tool twurl to generate tokens. See for more information: http://dghubble.com/blog/posts/twitter-app-write-access-and-bots/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment