forked from devschile/huemul
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtemblor.js
More file actions
98 lines (94 loc) · 3.53 KB
/
temblor.js
File metadata and controls
98 lines (94 loc) · 3.53 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Description:
// Muestra los últimos temblores significativos en cualquier país del 🌍.
//
// Dependencies:
// None
//
// Configuration:
// None
//
// Commands:
// hubot temblores - Muestra los últimos temblores >= a 6° en el 🌍.
// hubot temblores <país> - Muestra los últimos temblores >= a 6° en el país seleccionado.
//
// Author:
// @jorgeepunan
module.exports = robot => {
robot.respond(/temblores( .*)?/i, res => {
const country = res.match[1] ? res.match[1].trim().toUpperCase() : null
const minMagnitude = 6 // Con un temblor menor a 6 grados ni me muevo de la silla menos de la cama asi q este es el mínimo.
const fetch = robot
.http('https://earthquake.usgs.gov')
.path('/fdsnws/event/1/query')
.query({ format: 'geojson', minmagnitude: minMagnitude }) // {starttime: 'YYYY-MM-DDTHH:mm:ss-04:00'}
fetch.get()((error, response, body) => {
if (error) return robot.emit('error', error, res, 'temblor')
if (response.statusCode !== 200) { return robot.emit('error', new Error(`Response statusCode is ${response.statusCode}`), res, 'temblor') }
const { features: earthquakes } = JSON.parse(body)
const earthquakesFilter = earthquakes.filter(({ properties: { place } }) => {
return country ? new RegExp(country, 'i').test(place) : true
})
if (robot.adapter.constructor.name === 'SlackBot') {
const options = {
as_user: false,
link_names: 1,
icon_url: 'https://www.usgs.gov/sites/all/themes/usgs_palladium/favicons/apple-touch-icon.png',
username: 'USGS',
unfurl_links: false
}
if (earthquakesFilter.length === 0) {
const text = `Por suerte, ningún temblor mayor a ${minMagnitude} grados en ${country || 'todo el mundo'}.`
options.attachments = [
{
fallback: text,
text: text
}
]
return robot.adapter.client.web.chat.postMessage(res.message.room, null, options)
}
options.attachments = earthquakesFilter.slice(0, 5).map(({ properties: { place, mag, time, title, url } }) => {
const fallback = `${title}: \n- Lugar: ${place} \n- Magnitud: ${mag} (richter) \n- Fecha/Hora: ${new Date(
time
).toString()} \n- Enlace: ${url}`
return {
fallback: fallback,
color: '#36a64f',
title: title,
title_link: url,
fields: [
{
title: 'Lugar',
value: place,
short: true
},
{
title: 'Magnitud',
value: `${mag} (richter)`,
short: true
},
{
title: 'Fecha',
value: new Date(time).toString(),
short: true
}
]
}
})
robot.adapter.client.web.chat.postMessage(res.message.room, null, options)
} else {
if (earthquakesFilter.length === 0) {
return res.send(`Por suerte, ningún temblor mayor a ${minMagnitude} grados en ${country || 'todo el mundo'}.`)
}
res.send(
earthquakesFilter
.slice(0, 5).map(({ properties: { place, mag, time, title, url } }) => {
return `${title}: \n- Lugar: ${place} \n- Magnitud: ${mag} (richter) \n- Fecha/Hora: ${new Date(
time
).toString()} \n- Enlace: ${url}`
})
.join('\n\n')
)
}
})
})
}