Created
January 29, 2016 19:57
-
-
Save shoemaker/ff9eb44da7e4e2adbec3 to your computer and use it in GitHub Desktop.
Coffee for Hubot
This file contains hidden or 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
// Description: | |
// Listens for coffee-related strings and chimes in with quotes and coffee shop suggestions. | |
// Dependencies: | |
// none | |
// Configuration: | |
// HUBOT_FOURSQUARE_CLIENTID - A Foursquare client ID. | |
// HUBOT_FOURSQUARE_CLIENTSECRET - A foursquare client secret. | |
// HUBOT_FOURSQUARE_LATLONG - The latitude and longitude of your location, for coffee shop suggestions. Format: 44.9789949,-93.2717354 | |
// Commands: | |
// coffee - responds to the room with a quote about coffee. | |
// hubot coffee me - responds to the user with a nearby coffee recommendation. | |
// Notes: | |
// Drop this file in your Hubot "scripts" folder. | |
// Author: | |
// shoemaker | |
// Gather environment variables | |
var clientId = process.env.HUBOT_FOURSQUARE_CLIENTID; | |
var clientSecret = process.env.HUBOT_FOURSQUARE_CLIENTSECRET; | |
var latlong = process.env.HUBOT_FOURSQUARE_LATLONG; | |
var quotes = [ | |
'No one can understand the truth until he drinks of coffee\'s frothy goodness. - Sheik Abd-al-Kadir', | |
'What goes best with a cup of coffee? Another cup. - Henry Rollins', | |
'A morning without coffee is like sleep.', | |
'I believe humans get a lot done, not because we\'re smart, but because we have thumbs so we can make coffee. - Flash Rosenberg', | |
'As soon as you sit down to a cup of hot coffee, your boss will ask you to do something which will last until the coffee is cold.', | |
'Coffee is the best thing to douse the sunrise with. - Terri Guillemets', | |
'Way too much coffee. But if it weren\'t for the coffee, I\'d have no identifiable personality whatsoever. - David Letterman', | |
'Sleep is a symptom of caffeine deprivation.', | |
'Decaffeinated coffee is the devil\'s blend.', | |
'Decaffeinated coffee is kind of like kissing your sister. - Bob Irwin', | |
'Decaf? No, it\'s dangerous to dilute my caffeine stream.', | |
'Coffee, the finest organic suspension ever devised. - Star Trek: Voyager', | |
'Coffee smells like freshly ground heaven. - Jessi Lane Adams', | |
'A yawn is a silent scream for coffee.', | |
'I never drink coffee at lunch. I find it keeps me awake for the afternoon. - Ronald Reagan', | |
'Coffee coffee coffee coffee coffee coffee coffee coffee!' | |
]; | |
var prefixes = [ | |
'Coffee? May I suggest ', | |
'If you\'re looking for a nearby coffee place, check out ', | |
'For coffee you should go to ', | |
'I hear that a great place for coffee is ', | |
'My friends say they like the coffee at ', | |
'Mmmmmmmm, coffee. Try ', | |
'I love coffee! Take a walk to ', | |
'Sweet, delicious coffee. Make a trip to ', | |
'If I were human I\'d get a cup of coffee at ', | |
'Coffee coffee coffee coffee coffee! Get some at ' | |
]; | |
/* | |
* This is where the magic happens. | |
*/ | |
module.exports = function(robot) { | |
// Listener for a random quote about coffee. | |
robot.hear(/(coffee)(?!\sme)/, function(response) { | |
response.send(response.random(quotes)); | |
}); | |
// Listener for a coffee shop suggestion. | |
robot.respond(/coffee me/i, function(response) { | |
if (!clientId || !clientSecret || !latlong) { | |
response.send('Sorry, no coffee. Missing one of the following configurations: HUBOT_FOURSQUARE_CLIENTID, HUBOT_FOURSQUARE_CLIENTSECRET, HUBOT_FOURSQUARE_LATLONG.'); | |
return; | |
} | |
try { | |
// Build the 4SQ request URL. | |
var url = 'https://api.foursquare.com/v2/venues/explore?ll=' + latlong + | |
'§ion=coffee&openNow=1&client_id=' + clientId + | |
'&client_secret=' + clientSecret + | |
'&v=20150715'; | |
// Make the request to 4SQ. | |
robot.http(url) | |
.header('Accept', 'application/json') | |
.get()(function(err, res, body) { | |
var data = JSON.parse(body); // Turn the string into an object. | |
if (err || !data || data.meta.code !== 200) { // Check for an error | |
robot.emit('error', 'I had a problem looking up a place for coffee.') | |
} else { | |
var huResponse = ''; | |
var venues = data.response.groups[0].items; | |
if (venues.length == 0) { | |
response.send('Drat! No coffee places found nearby.'); | |
} else { | |
// Find a random venue from the results. | |
var venue = venues[getRandomInt(0, venues.length)].venue; | |
// Craft the response message. | |
huResponse = prefixes[getRandomInt(0, prefixes.length)]; | |
huResponse += venue.name + ': '; | |
if (venue.location.address) { huResponse += venue.location.address + ' '; } | |
if (venue.location.city) { huResponse += venue.location.city + ' '; } | |
if (venue.location.state) { huResponse += venue.location.state; } | |
// huResponse += '. http://maps.google.com/maps?q=' + venue.name + ',' + venue.location.address + ',' + venue.location.city + ',' + venue.location.state + '&hl=en&sll=' + venue.location.lat +',' + venue.location.lng + '&hnear=' + venue.name + ',' + venue.location.address + ',' + venue.location.city + ',' + venue.location.state + '&t=m&z=11'; | |
response.send(huResponse); | |
} | |
} | |
}); | |
} catch (ex) { | |
robot.emit('error', 'I had a problem looking up a place for coffee. ' + ex); | |
} | |
}); | |
// Generic error handler. | |
robot.error = function(err, response) { | |
robot.logger.error('Encountered error: ' + err); | |
if (response) { response.send('Coffee does not compute!'); } | |
}; | |
}; | |
// BEGIN HELPER FUNCTIONS | |
/* | |
* Generate a random number between min and max. | |
*/ | |
function getRandomInt(min, max) { | |
return Math.floor(Math.random() * (max - min)) + min; | |
}; | |
// END HELPER FUNCTIONS | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment