diff --git a/modules/r2rio/config.json b/modules/r2rio/config.json new file mode 100644 index 0000000..d24eaef --- /dev/null +++ b/modules/r2rio/config.json @@ -0,0 +1,3 @@ +{ + "api_key": "" +} diff --git a/modules/r2rio/r2rio.js b/modules/r2rio/r2rio.js new file mode 100644 index 0000000..7dcb7b6 --- /dev/null +++ b/modules/r2rio/r2rio.js @@ -0,0 +1,42 @@ +/** + * Module Name: rome2rio + * Description: Maps and ting + */ +var request = require('request'), + _ = require('underscore')._; + +var gmaps = function(dbot) { + this.ApiRoot = 'http://free.rome2rio.com/api/1.2/json/'; + this.commands = { + '~from': function(event) { + var from = event.input[1], + to = event.input[2]; + + request.get({ + 'url': this.ApiRoot + 'Search', + 'qs': { + 'key': this.config.api_key, + 'oName': from, + 'dName': to, + 'currencyCode': 'GBP' + }, + 'json': true + }, function(err, response, body) { + if(!err && body && _.has(body, 'routes')) { + var route = body.routes[0]; + event.reply('If you left right now, it would take you ' + Math.floor(route.duration / 60) + ' hours and ' + + (route.duration % 60) + ' minutes to get the ' + Math.floor(route.distance) + 'KM from ' + from + ' to ' + to + ', and cost you about £' + + route.indicativePrice.price); + } else { + event.reply('Apparently one cannot get from ' + from + ' to ' + to + ' using public transport. Do you accept the challenge?'); + } + }); + + } + }; + this.commands['~from'].regex = [/^from (.*) to (.*)/, 3]; +}; + +exports.fetch = function(dbot) { + return new gmaps(dbot); +};