-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
256 lines (211 loc) · 7.13 KB
/
main.py
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import logging
import os
from datetime import datetime
from telegram import (
InlineKeyboardButton,
InlineKeyboardMarkup,
KeyboardButton,
ReplyKeyboardMarkup,
Update,
)
from telegram.error import TelegramError
from telegram.ext import (
ApplicationBuilder,
CallbackQueryHandler,
CommandHandler,
ContextTypes,
ConversationHandler,
MessageHandler,
filters,
)
from api.api import weather_daily, weather_hourly
from utils.constants import BUTTON, CLOUD_STATUS, COMMAND, CONTENT
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
level=logging.INFO,
)
LOCATION = 0
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""
Start the bot, save user language and request current location.
"""
assert context.user_data is not None
assert update.effective_chat is not None
assert update.effective_user is not None
keyboard = [
[
KeyboardButton(
text=BUTTON["es"]["inline_location"], request_location=True
)
]
]
try:
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=COMMAND["es"]["start"],
reply_markup=ReplyKeyboardMarkup(
keyboard=keyboard, one_time_keyboard=True, resize_keyboard=True
),
)
except TelegramError as e:
logging.error(e.message)
else:
return LOCATION
async def location(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""
Set the attached location to conversation user data.
"""
assert context.user_data is not None
assert update.effective_chat is not None
assert update.effective_message is not None
assert update.effective_message.location is not None
context.user_data["location"] = {
"latitude": update.effective_message.location.latitude,
"longitude": update.effective_message.location.longitude,
}
try:
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=COMMAND["es"]["location_saved"],
)
except TelegramError as e:
logging.error(e.message)
else:
return ConversationHandler.END
async def forecast_hour(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""
Weather information by hour.
"""
assert update.effective_chat is not None
assert context.user_data is not None
if "location" not in context.user_data:
await location_not_found(update, context)
weather_report = weather_hourly(
lat=context.user_data["location"]["latitude"],
lon=context.user_data["location"]["longitude"],
)
content = CONTENT["es"]["forecast_hour_head"].format(
datetime.now().strftime("%d/%m/%Y")
)
for hour in range(len(weather_report.hourly.time)):
time = datetime.strptime(
weather_report.hourly.time[hour], "%Y-%m-%dT%H:%M"
)
temp = weather_report.hourly.temperature_2m[hour]
app_temp = weather_report.hourly.apparent_temperature[hour]
prec_prob = int(weather_report.hourly.precipitation_probability[hour])
cloudcover = int(weather_report.hourly.cloudcover[hour])
cloudcover_emoji = CLOUD_STATUS[
"clear"
if cloudcover <= 33
else ("partly_cloudy" if cloudcover <= 66 else "cloudy")
]
content += CONTENT["es"]["forecast_hour_body"].format(
time.strftime("%H:%M"), cloudcover_emoji, temp, app_temp, prec_prob
)
keyboard = [
[
InlineKeyboardButton(
BUTTON["es"]["inline_forecast"], callback_data=0
)
]
]
try:
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=content,
reply_markup=InlineKeyboardMarkup(keyboard),
)
except TelegramError as e:
logging.error(e.message)
async def forecast_day(update: Update, context: ContextTypes.DEFAULT_TYPE):
"""
Daily forecast location based weather data.
"""
assert update.effective_chat is not None
assert context.user_data is not None
if "location" not in context.user_data:
await location_not_found(update, context)
weather_report = weather_daily(
lat=context.user_data["location"]["latitude"],
lon=context.user_data["location"]["longitude"],
)
temp_min = weather_report.daily.temperature_2m_min[0]
temp_max = weather_report.daily.temperature_2m_max[0]
app_temp_min = weather_report.daily.apparent_temperature_min[0]
app_temp_max = weather_report.daily.apparent_temperature_max[0]
# uv_index_max = weather_report.daily.uv_index_max[0]
# uv_index_clear_max = weather_report.daily.uv_index_clear_sky_max[0]
# prec_hours = weather_report.daily.precipitation_hours[0]
prec_prob_max = weather_report.daily.precipitation_probability_max[0]
content = CONTENT["es"]["forecast_day"].format(
datetime.now().strftime("%d/%m/%Y"),
temp_min,
app_temp_min,
temp_max,
app_temp_max,
int(prec_prob_max),
)
keyboard = [
[
InlineKeyboardButton(
BUTTON["es"]["inline_forecast"], callback_data=0
)
]
]
try:
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=content,
reply_markup=InlineKeyboardMarkup(keyboard),
)
except TelegramError as e:
logging.error(e.message)
async def location_not_found(
update: Update, context: ContextTypes.DEFAULT_TYPE
):
assert update.effective_chat is not None
try:
await context.bot.send_message(
chat_id=update.effective_chat.id,
text=COMMAND["es"]["location_not_found"],
)
except TelegramError as e:
logging.error(e.message)
else:
return
async def remove_button(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
assert query is not None
try:
await query.answer()
await query.edit_message_text(text=COMMAND["es"]["remove"])
except TelegramError as e:
logging.error(e.message)
else:
return
async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE):
assert update.message is not None
try:
await update.message.reply_text(text=COMMAND["es"]["cancel"])
except TelegramError as e:
logging.error(e.message)
else:
return ConversationHandler.END
if __name__ == "__main__":
app = ApplicationBuilder().token(os.environ["TELEGRAM_BOT_TOKEN"]).build()
app.add_handlers(
[
ConversationHandler(
entry_points=[CommandHandler("start", start)],
states={
LOCATION: [MessageHandler(filters.LOCATION, location)]
},
fallbacks=[CommandHandler("cancel", cancel)],
),
CommandHandler("forecast_hour", forecast_hour),
CommandHandler("forecast_day", forecast_day),
CallbackQueryHandler(remove_button),
]
)
app.run_polling()