-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from citypaul/twitter
Twitter commands
- Loading branch information
Showing
10 changed files
with
192 additions
and
90 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,3 +32,6 @@ node_modules | |
|
||
# Optional REPL history | ||
.node_repl_history | ||
|
||
#Intellij settings | ||
.idea |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; | ||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; | ||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
} | ||
|
||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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>', 'City name such as manchester') | ||
.option('-cc, --country <country>', 'Country name such as uk') | ||
.option('-t, --access-token <accessToken>', '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>', 'City name such as manchester') | ||
.option('-cc, --country <country>', 'Country name such as uk') | ||
.option('-t, --access-token <accessToken>', 'API Key') | ||
.action(require('./commands/weather')); | ||
.command('github') | ||
.description('Command to get notifications of new issues and PRs') | ||
.option('-t, --access-token <accessToken>', '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 <accessToken>', 'API Key') | ||
.action(require('./commands/github')); | ||
.command('tweet') | ||
.description('BB8 will respond to tweets!') | ||
.option('-#, --hash-tag <hashTag>', 'Hashtag to search for. Defaults to "#bb8bbc"') | ||
.option('-d, --delay <delay>', 'Interval delay for retrieving new tweets. Defaults to 10000') | ||
.option('--consumer-key <consumerKey>', 'Twitter api consumer key') | ||
.option('--consumer-secret <consumerSecret>', 'Twitter api consumer secret') | ||
.option('--access-token-key <accessTokenKey>', 'Twitter api access token key') | ||
.option('--access-token-secret <accessTokenSecret>', '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); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}); | ||
} |
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