-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetWeather.js
53 lines (49 loc) · 1.31 KB
/
getWeather.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
const axios = require("axios");
async function getWeather(){
const weatherData = [];
const cities = [
"Taipei",
"New Taipei",
"Taoyuan",
"Hsinchu",
"Miaoli",
"Taichung",
"Changhua",
"Nantou",
"Yunlin",
"Chiayi",
"Tainan",
"Kaohsiung",
"Pingtung",
"Yilan",
"Hualien",
"Taitung",
];
for (const city of cities) {
const response = await axios.get(
`http://api.openweathermap.org/data/2.5/weather?q=${city},TW&appid=a958169936ee2edbb98f382aed2d1ea8&units=metric`
);
weatherData.push({
city: response.data.name,
weather: response.data.weather[0].main,
temperature: response.data.main.temp,
});
}
return weatherData;
};
function replyWeather(event,client,prefix) {
const message=event.message.text
if (message === prefix + "w") {
getWeather().then((weatherData) => {
let message = "";
weatherData.forEach((weather) => {
message += ` ${weather.city}: 今天天氣:${weather.weather},溫度:${weather.temperature}°C\n`;
});
return client.replyMessage(event.replyToken, {
type: "text",
text: message,
});
});
}
}
module.exports={replyWeather}