-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (132 loc) · 4.31 KB
/
index.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
require("newrelic");
require("dotenv").config();
const tmi = require("tmi.js");
const axios = require("axios");
const express = require("express");
const app = express();
const http = require("http").createServer(app);
const io = require("socket.io")(http);
const path = require("path");
app.use(express.static(path.join(__dirname, "app")));
app.get("/", (req, res) => {
res.sendFile(__dirname + "/app/index.html");
});
const BotName = process.env.BOT_NAME;
const ChannelName = process.env.CHANNEL_NAME;
const mapBoxToken = process.env.MAPBOX_TOKEN;
const accuweatherAPIKey = process.env.WEATHER_TOKEN;
const unit = process.env.UNIT || "F";
const language = process.env.LANGUAGE_WEATHER || "en";
let msgError;
let username;
const opts = {
identity: {
username: BotName,
password: process.env.TOKEN,
},
channels: [ChannelName],
};
const client = new tmi.client(opts);
const weatherObject = {
city: "",
state: "",
country: "",
temperature: "",
weatherText: "",
weatherIcon: "",
};
function getCurrentWeather(localCode) {
const URL = `http://dataservice.accuweather.com/currentconditions/v1/${localCode}?apikey=${accuweatherAPIKey}&language=${language}`;
try {
axios.get(URL).then((response) => {
try {
if (unit.toLowerCase() === "f") {
weatherObject.temperature = `${response.data[0].Temperature.Imperial.Value}°F`;
weatherObject.unitTemperature = "";
} else if (unit.toLowerCase() === "c") {
weatherObject.temperature = `${response.data[0].Temperature.Metric.Value}°`;
}
weatherObject.weatherText = response.data[0].WeatherText;
let icon_number =
response.data[0].WeatherIcon <= 9
? "0" + String(response.data[0].WeatherIcon)
: String(response.data[0].WeatherIcon);
weatherObject.weatherIcon = `https://developer.accuweather.com/sites/default/files/${icon_number}-s.png`;
io.emit("weather", weatherObject);
client.say(
ChannelName,
`/me Hi @${username}. The wheater in ${weatherObject.city} is ${weatherObject.temperature}. Look at overlay to get more information.`
);
} catch (err) {
client.say(ChannelName, msgError);
}
});
} catch (err) {
client.say(ChannelName, msgError);
}
}
function getLocalization(lon, lat) {
const URL = `http://dataservice.accuweather.com/locations/v1/cities/geoposition/search?apikey=${accuweatherAPIKey}&q=${lat}%2C${lon}&language=${language}`;
try {
axios.get(URL).then((response) => {
try {
if (response.data.ParentCity.LocalizedName) {
weatherObject.city = response.data.ParentCity.LocalizedName;
} else {
weatherObject.city = response.data.LocalizedName;
}
weatherObject.state = response.data.AdministrativeArea.LocalizedName;
weatherObject.country = response.data.Country.LocalizedName;
let localCode = response.data.Key;
getCurrentWeather(localCode);
} catch (err) {
client.say(ChannelName, msgError);
}
});
} catch (err) {
client.say(ChannelName, msgError);
}
}
function getCoordinate(city) {
const URL = `https://api.mapbox.com/geocoding/v5/mapbox.places/${encodeURI(
city
)}.json?access_token=${mapBoxToken}`;
try {
axios.get(URL).then((response) => {
try {
const lon = response.data.features[0].geometry.coordinates[0];
const lat = response.data.features[0].geometry.coordinates[1];
getLocalization(lon, lat);
} catch (err) {
client.say(ChannelName, msgError);
}
});
} catch (err) {
client.say(ChannelName, msgError);
}
}
client.on("message", (target, context, message, bot) => {
username = context.username;
if (bot) {
return;
}
msgError = `/me ${context.username} we couldn't find the weather. try again`;
const COMMAND_NAME = "!weather";
const command = message.split(" ")[0];
if (command === COMMAND_NAME) {
const city = message.replace(COMMAND_NAME, "");
if (city) {
getCoordinate(city);
} else {
client.say(target, `${context.username} please inform a city`);
}
}
});
client.on("connected", () => {
client.say(ChannelName, "Bot is on!");
console.log("Bot is on!");
});
client.connect();
http.listen(3000, () => {
console.log("listening on *:3000");
});