-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.js
193 lines (179 loc) · 6.58 KB
/
weather.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
let telegrambot = require("node-telegram-bot-api");
const { reply_markupWeather, statusAllWeather } = require("./dates");
const { weatherApp, yourCityUser } = require("./models/watherModel");
require('dotenv').config()
let token = process.env.BOT_TOKEN;
let bot = new telegrambot(token, { polling: true });
let translate = require("@vitalets/google-translate-api");
const { arrayStatusAir } = require("./data/statusAir");
const { textCommandYourCity, textCommandStart } = require("./data/messages");
require("../weatherApp/configs/db");
let urlData = {
url: "https://api.openweathermap.org/data/2.5/weather?q=",
key: process.env.KEY_URL_API,
};
module.exports = {
urlData,
bot,
};
require("./data/warning");
require("./data/statusAir");
bot.on("message", async (message) => {
console.log(message);
let id = message.from.id;
if (!message.text) return;
bot.sendChatAction(id, "typing");
if (message.text == "/start") {
return bot.sendMessage(
id, textCommandStart(message)
);
}
if (message.text == "/yourcity") {
bot.sendMessage(id, textCommandYourCity);
return;
}
let re = /✅/g;
if (message.text && message.text.match(re)) {
let rejexVal = message.text.replace(/✅/g, "");
fetch(`${urlData.url}${rejexVal}${urlData.key}`).then(async (res) => {
if (res.status == 200) {
let text = `کاربر ${message.from.first_name} از این لحظه به بعد اطلاعات آب و هوایی شهر "${rejexVal}" بصورت روزانه به شما نمایش داده خواهد شد.`;
let dataFindUserID = await yourCityUser.findOne({ userId: id });
if (!dataFindUserID) {
bot.sendMessage(id, text);
yourCityUser.create({
userId: id,
fristName: message.from.first_name,
userName: message.from.username,
cityName: rejexVal,
});
} else if (dataFindUserID) {
let text = `کاربر ${message.from.first_name} شما از قبل شهر ${dataFindUserID.cityName} را بعنوان شهر پیشفرض خود قرار داده اید`;
bot.sendMessage(id, text, {
reply_markup: {
inline_keyboard: [
[{ text: "تغییر شهر", callback_data: "changeCity" }],
],
},
});
return;
}
} else {
bot.sendMessage(
id,
`شهر ${rejexVal} اشتباه می باشد لطفا نام درست را وارد نمایید`
);
}
});
return;
}
let msg = message.text?.toLocaleLowerCase();
let weatherData = await fetch(`${urlData.url}${msg}${urlData.key}`)
.then((res) => res.json())
.then(async (data) => {
if (!data.name) {
return false;
}
await weatherApp.deleteOne({ userId: id });
let findDatas = await weatherApp.findOne({
cityName: data.name,
userId: id,
});
if (!findDatas) {
await weatherApp.create({
cityName: data.name,
countryName: data.sys.country,
statusAir: data.weather[0].description,
temp: Math.floor(data.main.temp - 273.15) + "°c",
minTemp: `${Math.floor(
data.main.temp_max - 273.15
)} °c / ${Math.floor(data.main.temp_min - 273.15)} °c`,
humidity: data.main.humidity,
pressure: data.main.pressure,
speed: data.wind.speed,
deg: data.wind.deg,
clouds: data.clouds.all,
statusAirMain: data.weather[0].main.toLocaleLowerCase(),
userId: id,
});
}
return data;
});
if (!weatherData) {
return bot.sendMessage(id, "نام شهر وارد شده اشتباه می باشد");
}
bot.sendMessage(id, "وضعیت مورد نظر را انتخاب کنید:", reply_markupWeather);
});
bot.on("callback_query", async (ctx) => {
bot.answerCallbackQuery(ctx.id);
bot.sendChatAction(ctx.from.id, "typing");
let data = ctx.data;
let id = ctx.from.id;
if (ctx.data == "changeCity") {
bot.deleteMessage(id, ctx.message.message_id);
bot.sendMessage(ctx.from.id, "شهر خود را وارد نمایید");
await yourCityUser.deleteOne({ userId: id });
return;
}
let findWeatherData = await weatherApp.findOne({ userId: id });
let statusAirTranslate = null;
let cityNameTranslate = null;
try {
statusAirTranslate = (
await translate.translate(findWeatherData.statusAir, { to: "fa" })
).text;
cityNameTranslate = (
await translate.translate(findWeatherData.cityName, { to: "fa" })
).text;
} catch (e) {
statusAirTranslate = findWeatherData.statusAir;
cityNameTranslate = findWeatherData.cityName;
}
if (findWeatherData) {
if (data == "city") {
bot.sendMessage(id, `نام شهر انتخاب شده: ${cityNameTranslate}`);
} else if (data == "country") {
bot.sendMessage(
id,
`کشور انتخاب شده توسط شما: ${findWeatherData.countryName}`
);
} else if (data == "statusAir") {
bot.sendMessage(id, `وضعیت هوا:\n${statusAirTranslate}`);
} else if (data == "temp") {
bot.sendMessage(id, `دما:\n ${findWeatherData.temp}`);
} else if (data == "minTemp") {
bot.sendMessage(id, `حداقل دما:\n${findWeatherData.minTemp}`);
} else if (data == "humidity") {
bot.sendMessage(id, `رطوبت هوا: ${findWeatherData.humidity}`);
} else if (data == "pressure") {
bot.sendMessage(id, `فشار هوا: ${findWeatherData.pressure}`);
} else if (data == "speed") {
bot.sendMessage(id, `سرعت باد: ${findWeatherData.speed}`);
} else if (data == "deg") {
bot.sendMessage(id, `درجه هوا: ${findWeatherData.deg}`);
} else if (data == "clouds") {
bot.sendMessage(id, `تعداد ابر ها: ${findWeatherData.clouds}`);
} else if (data == "totalStatus") {
bot.sendMessage(
id,
statusAllWeather(cityNameTranslate, findWeatherData, statusAirTranslate)
);
}
}
if (data == "inActiveWarning") {
bot.deleteMessage(id, ctx.message.message_id);
await yourCityUser.deleteOne({ userId: id });
let text =
"هشدار روزانه غیرفعال شد جهت فعال سازی مجدد دستور /yourcity را ارسال نمایید";
bot.sendMessage(id, text);
}
if (data == "travel") {
let airMain = findWeatherData.statusAirMain;
arrayStatusAir.forEach((item) => {
if (item[0].split(" ")[0] == airMain) {
let validateData = item[0].replace(airMain, "").trim()
bot.sendMessage(id , validateData)
}
});
}
});