Skip to content

Commit

Permalink
Merge pull request #6 from citypaul/twitter
Browse files Browse the repository at this point in the history
Twitter commands
  • Loading branch information
mintuz committed Jan 23, 2016
2 parents 208956d + 07a5364 commit 82ed900
Show file tree
Hide file tree
Showing 10 changed files with 192 additions and 90 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ node_modules

# Optional REPL history
.node_repl_history

#Intellij settings
.idea
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down
29 changes: 6 additions & 23 deletions commands/disco.js
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);
};
15 changes: 4 additions & 11 deletions commands/disconnect.js
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);
});
};
13 changes: 13 additions & 0 deletions commands/roll.js
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);
};



61 changes: 61 additions & 0 deletions commands/tweet.js
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);
};



58 changes: 24 additions & 34 deletions commands/weather.js
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');
}

};
79 changes: 58 additions & 21 deletions index.js
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);
}
16 changes: 16 additions & 0 deletions libs/twitter-client-instance.js
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
});
}
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

0 comments on commit 82ed900

Please sign in to comment.