Last active
August 29, 2015 14:08
-
-
Save Inferis/e59319a535509d842680 to your computer and use it in GitHub Desktop.
Velo script for ye olde Hubot.
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
# Description: | |
# Shows how many velos are free on "nearby" Velo stations. | |
# | |
# Dependencies: | |
# None | |
# | |
# Configuration: | |
# None | |
# | |
# Commands: | |
# hubot is er nog een velo vrij - Shows who needs to go to the shop | |
# hubot is er nog een fiets beschikbaar? | |
# hubot is er nog ne velo vrij? | |
# hubot is er nog een fiets vrij | |
# hubot is er een fiets vrij | |
# hubot velo vrij? | |
# hubot is er nog een stalen ros beschikbaar? | |
# | |
# Author: | |
# inferis based on work by ridingwolf | |
# adjust this array to hold the ids of nearby stations | |
stations = [ 74, 72, 16, 76 ] | |
threshold = 5 | |
module.exports = (robot) -> | |
robot.respond /(is er)? (nog)?\s?(een|ne)?\s?(fiets|velo|tweewieler|stalen ros)?(je|ke)?\s?(beschikbaar|vrij|aanwezig)?(\?)?$/i, (msg) -> | |
status msg | |
status = (msg) -> | |
checkStatus msg, stations.slice(), false | |
checkStatus = (msg, stations, found) -> | |
station = stations.shift() | |
unless station | |
unless found | |
msg.send "Neen. Het zal wandelen worden." | |
return | |
data = "idStation=#{station}" | |
msg.robot.http('https://www.velo-antwerpen.be/CallWebService/StationBussinesStatus.php') | |
.header('Content-Type', 'application/x-www-form-urlencoded') | |
.post(data) (err, res, body) -> | |
matches = /bikes\s+(\d+)\s+slots\s+(\d+)/gi.exec body | |
if matches.length is 3 | |
nrOfBikes = Number(matches[1]) | |
nrOfParking = Number(matches[2]) | |
if nrOfBikes > 0 | |
unless found | |
msg.send "Ja. Er zijn nog #{nrOfBikes} fietskes vrij in station #{station}." | |
if nrOfBikes <= threshold | |
checkStatus msg, stations, true | |
else if nrOfBikes > threshold | |
msg.send "Er zijn ook nog #{nrOfBikes} fietskes vrij in station #{station}." | |
else | |
checkStatus msg, stations, true | |
else | |
checkStatus msg, stations, false | |
else | |
msg.send "Ik weet het niet." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment