-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps.coffee
83 lines (71 loc) · 2.83 KB
/
maps.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# Description:
# Interacts with the Google Maps API.
#
# Commands:
# hubot map me <query> - Returns a map view of the area returned by `query`.
module.exports = (robot) ->
fillAddress = (address) ->
if (address.match(/borderlands/i))
return '1109 Pebblewood Way, San Mateo, CA'
else if (address.match(/hhh/i))
return '516 Chesterton Ave, Belmont, CA'
else if (address.match(/airbnb/i))
return '888 Brannan St, San Francisco, CA'
return address
robot.respond /((driving|walking|bike|biking|bicycling) )?directions from (.+) to (.+)/i, (msg) ->
mode = msg.match[2] || 'driving'
origin = fillAddress(msg.match[3])
destination = fillAddress(msg.match[4])
key = process.env.HUBOT_GOOGLE_API_KEY
if origin == destination
return msg.send "Now you're just being silly."
if !key
msg.send "Please enter your Google API key in the environment variable HUBOT_GOOGLE_API_KEY."
if mode == 'bike' or mode == 'biking'
mode = 'bicycling'
url = "https://maps.googleapis.com/maps/api/directions/json"
query =
mode: mode
key: key
origin: origin
destination: destination
sensor: false
robot.http(url).query(query).get()((err, res, body) ->
jsonBody = JSON.parse(body)
route = jsonBody.routes[0]
if !route
msg.send "Error: No route found."
return
legs = route.legs[0]
start = legs.start_address
end = legs.end_address
distance = legs.distance.text
duration = legs.duration.text
response = "Directions from #{start} to #{end}\n"
response += "#{distance} - #{duration}\n\n"
i = 1
for step in legs.steps
instructions = step.html_instructions.replace(/<div[^>]+>/g, ' - ')
instructions = instructions.replace(/<[^>]+>/g, '')
response += "#{i}. #{instructions} (#{step.distance.text})\n"
i++
msg.send "http://maps.googleapis.com/maps/api/staticmap?size=400x400&" +
"path=weight:3%7Ccolor:red%7Cenc:#{route.overview_polyline.points}&sensor=false"
msg.send response
)
robot.respond /(?:(satellite|terrain|hybrid)[- ])?map( me)? (.+)/i, (msg) ->
mapType = msg.match[1] or "roadmap"
location = fillAddress(msg.match[3])
mapUrl = "http://maps.google.com/maps/api/staticmap?markers=" +
escape(location) +
"&size=400x400&maptype=" +
mapType +
"&sensor=false" +
"&format=png" # So campfire knows it's an image
url = "http://maps.google.com/maps?q=" +
escape(location) +
"&hl=en&sll=37.0625,-95.677068&sspn=73.579623,100.371094&vpsrc=0&hnear=" +
escape(location) +
"&t=m&z=11"
msg.send mapUrl
msg.send url