Skip to content

Instantly share code, notes, and snippets.

@alexellis
Last active May 31, 2016 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexellis/1e9331fed0275ed49d89b785ea93c57e to your computer and use it in GitHub Desktop.
Save alexellis/1e9331fed0275ed49d89b785ea93c57e to your computer and use it in GitHub Desktop.
Twitter2SlackBot - watches Hashtags and pushes them into your selected slack channel.
"use strict"
// Needs a config.json file with slack and twitter keys/secrets/tokens.
// Sample config at end of file.
let util = require('util')
var Twitter = require('twitter');
let config = require('./config.json');
let async = require('async');
let fs = require('fs');
let Slackbots = require('slackbots');
var client = new Twitter(config.twitter);
var bot = new Slackbots(config.slack);
var stream = client.stream('statuses/filter', {track: process.argv[2]});
let write_queue = async.queue((task, done) => {
fs.writeFile("tweets/" + task.id + ".json", JSON.stringify(task.content), (err) => {
var posting = task.content.text +"\n @" + task.content.user.screen_name + " ("+task.content.user.location+") "+ task.content.user.lang;
bot.postMessageToChannel(config.slack.channel, task.content.text, {}).then(() => {
console.log("Posted tweet by: "+ task.content.user.screen_name);
done();
}).catch((e) => {
console.error(e);
});
});
}, 2);
bot.on('start',() => {
console.log("Bot started, let's watch the hashtag #"+process.argv[2])
stream.on('data', (tweet) => {
write_queue.push({content: tweet, id: tweet.id });
console.log(tweet.text);
// console.log(JSON.stringify(tweet));
});
stream.on('error', function(e) {
console.error(e);
console.error(e.stack);
process.exit(-1);
});
})
/*
{
"twitter": {
"consumer_key": "",
"consumer_secret": "",
"access_token_key": "",
"access_token_secret": ""
},
"slack": {
"token": "",
"channel": "",
"name": ""
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment