diff --git a/.gitignore b/.gitignore index 4796eb8..287da1f 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,6 @@ node_modules # Optional REPL history .node_repl_history + +#Intellij settings +.idea diff --git a/README.md b/README.md index 5b52c9a..f5fc517 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,12 @@ Not yet on npm so you'll have to do it the good'ol fasioned way with a cheeky gi ### Action Commands * `node index.js disco` - Command to turn your BB8 Unit into a shining disco ball in the night * `node index.js weather --city="manchester" --country="uk" --api-key="ABCD"` - Command to turn your BB8 Unit into your very own weather reporter, uses OpenWeather so be sure to get your own API key +* `node index.js tweet --hash-tag="bb8" --delay=5000` - Command to search twitter and run the first hashtag it finds as a command. Eg a tweet "#disco #bb8" would run the `disco` command --consumer-key xxx --consumer-secret xxx --access-token-key xxx --access-token-secret xxx # Contributors * [@mintuz](http://twitter.com/mintuz) * [@shaunbent](http://twitter.com/shaunbent) +* [@citypaul](http://twitter.com/paulhammond) # Want to contribute? diff --git a/commands/disco.js b/commands/disco.js index 5365786..d8b9682 100644 --- a/commands/disco.js +++ b/commands/disco.js @@ -1,25 +1,8 @@ -var bb8 = require('../libs/bb8-instance')(); -var config = require('../libs/bb8-instance').config; - -module.exports = function() { - - if(bb8) { - - bb8.connect(function() { - - console.log('Connected to ' + config.BB8_LOCAL_NAME); - console.log('Let\'s Party!!'); - - bb8.randomColor(); - - setInterval(function() { - - bb8.randomColor(); - - }, 1000); - - }); - - } +module.exports = function (bb8) { + console.log('Let\'s Party!!'); + bb8.randomColor(); + setInterval(function () { + bb8.randomColor(); + }, 1000); }; diff --git a/commands/disconnect.js b/commands/disconnect.js index 36e1faf..3182dde 100644 --- a/commands/disconnect.js +++ b/commands/disconnect.js @@ -1,12 +1,5 @@ -var bb8 = require('../libs/bb8-instance')(); -var config = require('../libs/bb8-instance').config; - -module.exports = function() { - - if(bb8) { - bb8.disconnect(function() { - console.log('Disconnected from ' + config.BB8_LOCAL_NAME); - }); - } - +module.exports = function (bb8) { + bb8.disconnect(function () { + console.log('Disconnected from ' + config.BB8_LOCAL_NAME); + }); }; diff --git a/commands/roll.js b/commands/roll.js new file mode 100644 index 0000000..ea4b3dd --- /dev/null +++ b/commands/roll.js @@ -0,0 +1,13 @@ +module.exports = function (bb8) { + console.log('Let\'s Roll!!'); + + setInterval(function () { + + var direction = Math.floor(Math.random() * 360); + bb8.roll(150, direction); + + }, 1000); +}; + + + diff --git a/commands/tweet.js b/commands/tweet.js new file mode 100644 index 0000000..cd5003d --- /dev/null +++ b/commands/tweet.js @@ -0,0 +1,61 @@ +var TwitterClient = require('../libs/twitter-client-instance'), + _ = require('lodash'), + alreadyTriggeredTweets = {}; + +function getLatestStatus(tweets) { + + if(tweets && !tweets.errors) { + + var latestTweet = tweets.statuses[0]; + + if(alreadyTriggeredTweets[latestTweet.id_str]) { + return false; + } + + alreadyTriggeredTweets[latestTweet.id_str] = true; + + return tweets.statuses[0]; + } + + return false; + +} + +var executeTwitterCommand = function (bb8, options) { + + var twitter = TwitterClient(options), + hashTag = options.hashTag || 'bb8code'; + + twitter.get('search/tweets', {q: hashTag}, function (error, tweets) { + + var latestStatus = getLatestStatus(tweets); + + if(latestStatus) { + + var command = _.trim(_.replace(_.replace(latestStatus.text, /#/g, ''), hashTag, '')), + user = latestStatus.user; + + console.log('Twitter Command issued by: ', user.name + ' (' + user.screen_name + ')'); + console.log('Issued command: ', command); + + require('./' + command)(bb8); + } + }); +}; + +module.exports = function (bb8, options) { + var intervalDelay = options.delay || 10000; + + console.log('Let\'s get tweets!'); + + var commanderExecuter = _.partial(executeTwitterCommand, bb8, options); + + commanderExecuter(); + + setInterval(function () { + commanderExecuter(); + }, intervalDelay); +}; + + + diff --git a/commands/weather.js b/commands/weather.js index 22f9ce6..eb974a3 100644 --- a/commands/weather.js +++ b/commands/weather.js @@ -1,50 +1,40 @@ -var bb8 = require('../libs/bb8-instance')(); -var config = require('../libs/bb8-instance').config; var WeatherFactory = require('../libs/open-weather-factory'); -module.exports = function(options) { +module.exports = function (bb8, options) { - if(bb8 && (process.env.WEATHER_KEY || options.accessToken)) { - - bb8.connect(function() { - - var weatherRequester = WeatherFactory({ - accessToken: options.accessToken || process.env.WEATHER_KEY, - city: options.city || 'manchester', - country: options.country || 'uk' - }); - - var WEATHER_ID = process.env.WEATHER_KEY; - - console.log('Connected to ' + config.BB8_LOCAL_NAME); - console.log('Rain Rain go away, come back another day!'); + if (process.env.WEATHER_KEY || options.accessToken) { + var weatherRequester = WeatherFactory({ + accessToken: options.accessToken || process.env.WEATHER_KEY, + city: options.city || 'manchester', + country: options.country || 'uk' + }); - // Every 10 seconds, lets poll the weather - setInterval(function() { + console.log('Rain Rain go away, come back another day!'); - weatherRequester(function (error, weatherData) { + // Every 10 seconds, lets poll the weather + setInterval(function () { - if(!error && weatherData) { + weatherRequester(function (error, weatherData) { - if(weatherData.main.temp >= 8) { - bb8.color('yellow'); - } else if (weatherData.main.temp >= 20) { - bb8.color('orange'); - } else if (weatherData.main.temp >= 25) { - bb8.color('red'); - } else { - bb8.color('blue'); - } + if (!error && weatherData) { + if (weatherData.main.temp >= 8) { + bb8.color('yellow'); + } else if (weatherData.main.temp >= 20) { + bb8.color('orange'); + } else if (weatherData.main.temp >= 25) { + bb8.color('red'); + } else { + bb8.color('blue'); } - }); - }, 10000); + } + }); - }); + }, 10000); } else { - console.log('BB8 Config isnt set or the WEATHER_KEY env for openweather is not present'); + console.log('WEATHER_KEY env for openweather is not present'); } }; diff --git a/index.js b/index.js index 467ab39..67aefe2 100644 --- a/index.js +++ b/index.js @@ -1,43 +1,80 @@ var program = require('commander'); var packageFile = require('./package.json'); +var bb8 = require('./libs/bb8-instance')(); +var config = require('./libs/bb8-instance').config; + +var executeCommand = function (command, options) { + if (bb8) { + bb8.connect(function () { + require(command)(bb8, options) + }); + } +}; program.version(packageFile.version); // Utility Actions program - .command('setup') - .description('Command to setup BB8 With your Mac') - .action(require('./commands/setup')); + .command('setup') + .description('Command to setup BB8 With your Mac') + .action(require('./commands/setup')); program - .command('disconnect') - .description('Command to disconnect from your BB8 Unit') - .action(require('./commands/disconnect')); + .command('disconnect') + .description('Command to disconnect from your BB8 Unit') + .action(function (options) { + executeCommand('./commands/disconnect', options); + }); // Real Actions program - .command('disco') - .description('Command to make your BB8 Unit A disco ball') - .action(require('./commands/disco')); + .command('disco') + .description('Command to make your BB8 Unit A disco ball') + .action(function () { + executeCommand('./commands/disco'); + }); + +program + .command('weather') + .description('Command to get the weather colour from your BB8 Unit') + .option('-c, --city ', 'City name such as manchester') + .option('-cc, --country ', 'Country name such as uk') + .option('-t, --access-token ', 'API Key') + .action(function(options) { + executeCommand('./commands/weather', options); + }); program - .command('weather') - .description('Command to get the weather colour from your BB8 Unit') - .option('-c, --city ', 'City name such as manchester') - .option('-cc, --country ', 'Country name such as uk') - .option('-t, --access-token ', 'API Key') - .action(require('./commands/weather')); + .command('github') + .description('Command to get notifications of new issues and PRs') + .option('-t, --access-token ', 'API Key') + .action(require('./commands/github')); + +program + .command('roll') + .description('BB8 will roll!') + .action(function () { + executeCommand('./commands/roll'); + }); + program - .command('github') - .description('Command to get notifications of new issues and PRs') - .option('-t, --access-token ', 'API Key') - .action(require('./commands/github')); + .command('tweet') + .description('BB8 will respond to tweets!') + .option('-#, --hash-tag ', 'Hashtag to search for. Defaults to "#bb8bbc"') + .option('-d, --delay ', 'Interval delay for retrieving new tweets. Defaults to 10000') + .option('--consumer-key ', 'Twitter api consumer key') + .option('--consumer-secret ', 'Twitter api consumer secret') + .option('--access-token-key ', 'Twitter api access token key') + .option('--access-token-secret ', 'Twitter api access token secret') + .action(function (options) { + executeCommand('./commands/tweet', options) + }); try { - program.parse(process.argv); + program.parse(process.argv); } catch (e) { - console.error(e); + console.error(e); } diff --git a/libs/twitter-client-instance.js b/libs/twitter-client-instance.js new file mode 100644 index 0000000..de210c3 --- /dev/null +++ b/libs/twitter-client-instance.js @@ -0,0 +1,16 @@ +var Twitter = require('twitter'); + +module.exports = function(options) { + + var consumerKey = process.env.TWITTER_CONSUMER_KEY || options.consumerKey; + var consumerSecret = process.env.TWITTER_CONSUMER_SECRET || options.consumerSecret; + var accessTokenKey = process.env.TWITTER_ACCESS_TOKEN_KEY || options.accessTokenKey; + var accessTokenSecret = process.env.TWITTER_ACCESS_TOKEN_SECRET || options.accessTokenSecret; + + return new Twitter({ + consumer_key: consumerKey, + consumer_secret: consumerSecret, + access_token_key: accessTokenKey, + access_token_secret: accessTokenSecret + }); +} \ No newline at end of file diff --git a/package.json b/package.json index 25f5fd7..f46dc5b 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,10 @@ "lodash": "^4.0.0", "noble": "^1.3.0", "request": "^2.67.0", - "sphero": "^0.7.0" + "sphero": "^0.7.0", + "twitter": "^1.2.5" + }, + "devDependencies": { + "nodemon": "^1.8.1" } }