-
Notifications
You must be signed in to change notification settings - Fork 5
/
weatherbot.js
146 lines (135 loc) · 3.21 KB
/
weatherbot.js
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var Slack = require("slack-node");
apiToken = process.env.SLACK_TOKEN;
slack = new Slack(apiToken);
var Forecast = require("forecast");
var forecast = new Forecast({
service: "forecast.io",
key: process.env.FORECAST_KEY,
units: "celcius",
cache: true,
ttl: {
minutes: 5
}
});
var weatherIcons = {
"clear-day": ":sunny:",
"clear-night": ":full_moon_with_face:",
rain: ":umbrella:",
snow: ":snowflake:",
sleet: ":snowflake:",
wind: ":wind_blowing_face:",
fog: ":foggy:",
cloudy: ":cloud:",
"partly-cloudy-day": ":cloud:",
"partly-cloudy-night": ":cloud:"
};
/**
* Constructor
*/
var Weatherbot = (module.exports = function(options) {
this.iconUrl =
"https://cdn.jsdelivr.net/gh/oddcamp/weatherbot@master/assets/bot.png";
this.channel = options.channel;
if (options.locations) {
this.locations = options.locations;
} else {
this.locations = [
{
name: "Trnava",
coords: [48.3775, 17.5883],
people: ["Ivan"]
},
{
name: "Stockholm",
coords: [59.3294, 18.0686],
people: ["Per"]
},
{
name: "Braga",
coords: [41.5472, -8.4464],
people: ["Eduardo"]
},
{
name: "Alcamo",
coords: [37.9973, 12.9072],
people: ["Andrea"]
},
{
name: "Plovdiv",
coords: [42.144, 24.6708],
people: ["Nikolay"]
},
{
name: "Klaipėda",
coords: [55.7052, 21.0178],
people: ["Osvaldas"]
},
{
name: "London",
coords: [51.5006895, -0.1245838],
people: ["Samuel"]
},
{
name: "Älmhult",
coords: [56.5572521,14.1011108],
people: ["Annemieke"]
},
{
name: "Antipolo City",
coords: [14.564150, 121.182820],
people: ["Joakim"]
},
{
name: "Porto",
coords: [41.157944,-8.629105],
people: ["Diana"]
},
{
name: "Lisbon",
coords: [38.722252,-9.139337],
people: ["Joana"]
}
];
}
});
Weatherbot.prototype.postWeatherMessages = function() {
var self = this;
this.locations.forEach(function(location) {
// Retrieve weather information from coordinates
forecast.get(location.coords, function(err, weather) {
if (err) return console.dir(err);
var currentWeather = weather.currently;
var icon = weatherIcons[currentWeather.icon]
? weatherIcons[currentWeather.icon] + " "
: ""; // 'clear-day'
var message =
"Hello " +
location.people.join(", ") +
". It's currently " +
currentWeather.summary +
", " +
icon +
Math.round(currentWeather.temperature) +
"°C (feels like " +
Math.round(currentWeather.apparentTemperature) +
" °C) in " +
location.name;
self.postMessage(message);
});
});
};
Weatherbot.prototype.postMessage = function(message) {
var self = this;
slack.api(
"chat.postMessage",
{
text: message,
channel: self.channel,
username: "weatherbot",
icon_url: self.iconUrl
},
function() {
console.log("Message sent to " + self.channel + ": " + message);
}
);
};