tommorow and week
This commit is contained in:
40
bot.py
40
bot.py
@@ -3,6 +3,7 @@ import logging
|
||||
import os
|
||||
from datetime import datetime
|
||||
|
||||
from aiogram.exceptions import TelegramForbiddenError
|
||||
from aiogram import Bot, Dispatcher, F, types
|
||||
from aiogram.filters import Command
|
||||
from aiogram.fsm.context import FSMContext
|
||||
@@ -12,7 +13,12 @@ from aiogram.client.default import DefaultBotProperties
|
||||
from apscheduler.schedulers.asyncio import AsyncIOScheduler
|
||||
from dotenv import load_dotenv
|
||||
|
||||
from weather import get_daily_weather_summary, get_coords_by_city
|
||||
from weather import (
|
||||
get_daily_weather_summary,
|
||||
get_coords_by_city,
|
||||
get_tomorrow_weather_summary,
|
||||
get_weekly_weather_summary
|
||||
)
|
||||
import database as db # Импортируем нашу базу данных
|
||||
|
||||
load_dotenv()
|
||||
@@ -32,6 +38,10 @@ def get_main_keyboard():
|
||||
return ReplyKeyboardMarkup(
|
||||
keyboard=[
|
||||
[KeyboardButton(text="🌤 Погода сейчас")],
|
||||
[
|
||||
KeyboardButton(text="📅 Погода на завтра"),
|
||||
KeyboardButton(text="📆 Прогноз на неделю")
|
||||
],
|
||||
[
|
||||
KeyboardButton(text="📍 Изменить город"),
|
||||
KeyboardButton(text="⏰ Изменить время")
|
||||
@@ -56,6 +66,10 @@ async def send_weather_update(user_id: int):
|
||||
weather_text = await get_daily_weather_summary(user["lat"], user["lon"])
|
||||
try:
|
||||
await bot.send_message(chat_id=user_id, text=weather_text)
|
||||
except TelegramForbiddenError:
|
||||
logging.info(f"Пользователь {user_id} заблокировал бота. Удаляем из рассылки.")
|
||||
scheduler.remove_job(str(user_id))
|
||||
await db.delete_user(user_id)
|
||||
except Exception as e:
|
||||
logging.error(f"Ошибка отправки пользователю {user_id}: {e}")
|
||||
|
||||
@@ -155,6 +169,30 @@ async def cmd_weather_now(message: types.Message):
|
||||
weather_text = await get_daily_weather_summary(user["lat"], user["lon"])
|
||||
await loading_msg.edit_text(weather_text)
|
||||
|
||||
@dp.message(F.text == "📅 Погода на завтра")
|
||||
async def cmd_weather_tomorrow(message: types.Message):
|
||||
user = await db.get_user(message.from_user.id)
|
||||
|
||||
if not user or not user["lat"]:
|
||||
await message.answer("Сначала отправьте локацию через команду /start.")
|
||||
return
|
||||
|
||||
loading_msg = await message.answer("⏳ Узнаю прогноз на завтра...")
|
||||
weather_text = await get_tomorrow_weather_summary(user["lat"], user["lon"])
|
||||
await loading_msg.edit_text(weather_text)
|
||||
|
||||
@dp.message(F.text == "📆 Прогноз на неделю")
|
||||
async def cmd_weather_week(message: types.Message):
|
||||
user = await db.get_user(message.from_user.id)
|
||||
|
||||
if not user or not user["lat"]:
|
||||
await message.answer("Сначала отправьте локацию через команду /start.")
|
||||
return
|
||||
|
||||
loading_msg = await message.answer("⏳ Собираю прогноз на 7 дней...")
|
||||
weather_text = await get_weekly_weather_summary(user["lat"], user["lon"])
|
||||
await loading_msg.edit_text(weather_text)
|
||||
|
||||
# --- ВОССТАНОВЛЕНИЕ ПЛАНИРОВЩИКА ---
|
||||
async def restore_jobs():
|
||||
"""Загружает задачи из БД в планировщик после перезапуска бота"""
|
||||
|
||||
Reference in New Issue
Block a user