Skip to content

Instantly share code, notes, and snippets.

@rayterrill
Created July 10, 2017 04:52
Show Gist options
  • Select an option

  • Save rayterrill/c6fcfdbef4a177d8535d165c9784c0e4 to your computer and use it in GitHub Desktop.

Select an option

Save rayterrill/c6fcfdbef4a177d8535d165c9784c0e4 to your computer and use it in GitHub Desktop.
NodeJS Streaming Bot Example
/*
//config.js
module.exports = {
twitter: {
consumer_key: 'CONSUMER_KEY',
consumer_secret: 'CONSUMER_SECRET',
access_token_key: 'ACCESS_TOKEN_KEY',
access_token_secret: 'ACCESS_TOKEN_SECRET'
}
}
*/
twitter = require('ntwitter');
config = require('./config');
function streamHandler(stream) {
//do something when something matches our filter
stream.on('data', function(data) {
replyToUser(data);
});
};
//a callback for errors
var callback = function handleError(error) {
if (error) {
console.error('response status:', error.statusCode);
console.error('data:', error.data);
}
};
//helper function to reply to a user
function replyToUser(data) {
twit.updateStatus('@' + data.user.screen_name + ' YO YO YO!', {in_reply_to_status_id: data.id_str}, callback);
};
// Create a new ntwitter instance
var twit = new twitter(config.twitter);
//start listening to the twitter stream for things that match our criteria
twit.stream('statuses/filter',{ track: 'boomshakalaka, #boomshakalaka'}, function(stream){
console.log('Listening for tweets...');
streamHandler(stream);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment