upd
This commit is contained in:
@ -1,12 +1,13 @@
|
||||
"""Scheduler for daily birthday notifications."""
|
||||
import telebot
|
||||
from datetime import datetime
|
||||
from typing import Optional
|
||||
from typing import Optional, Dict, List, Tuple
|
||||
from collections import defaultdict
|
||||
from apscheduler.schedulers.blocking import BlockingScheduler
|
||||
from apscheduler.triggers.cron import CronTrigger
|
||||
import pytz
|
||||
from database import get_db_session, User, Chat, UserChat
|
||||
from messages import format_birthday_greeting
|
||||
from messages import format_birthday_greeting, format_multiple_birthdays_greetings
|
||||
from config import Config
|
||||
|
||||
|
||||
@ -27,7 +28,10 @@ def send_birthday_notifications(bot: telebot.TeleBot) -> None:
|
||||
if not users_with_birthday:
|
||||
return
|
||||
|
||||
# For each user, send greetings to all their chats
|
||||
# Group users by chat: chat_id -> list of (first_name, theme, user_id)
|
||||
chat_users: Dict[int, List[Tuple[str, str, int]]] = defaultdict(list)
|
||||
|
||||
# For each user, find all their chats
|
||||
for user in users_with_birthday:
|
||||
# Get all chats where user is a member
|
||||
user_chats = db.query(Chat).join(UserChat).filter(
|
||||
@ -35,14 +39,14 @@ def send_birthday_notifications(bot: telebot.TeleBot) -> None:
|
||||
Chat.bot_is_admin == True
|
||||
).all()
|
||||
|
||||
greeting = format_birthday_greeting(user.first_name, user.preference_theme)
|
||||
|
||||
for chat in user_chats:
|
||||
try:
|
||||
# Check if user is still in chat
|
||||
member = bot.get_chat_member(chat.chat_id, user.user_id)
|
||||
if member.status not in ['left', 'kicked']:
|
||||
bot.send_message(chat.chat_id, greeting)
|
||||
chat_users[chat.chat_id].append(
|
||||
(user.first_name, user.preference_theme, user.user_id)
|
||||
)
|
||||
except telebot.apihelper.ApiTelegramException as e:
|
||||
# Handle errors: user blocked bot, bot removed from chat, etc.
|
||||
if e.error_code == 403:
|
||||
@ -57,6 +61,31 @@ def send_birthday_notifications(bot: telebot.TeleBot) -> None:
|
||||
except Exception:
|
||||
# Other errors - continue
|
||||
pass
|
||||
|
||||
# Send greetings grouped by chat
|
||||
for chat_id, users_data in chat_users.items():
|
||||
try:
|
||||
if len(users_data) == 1:
|
||||
# Single user - use simple format
|
||||
first_name, theme, user_id = users_data[0]
|
||||
greeting = format_birthday_greeting(first_name, theme, user_id)
|
||||
bot.send_message(chat_id, greeting, parse_mode='HTML')
|
||||
else:
|
||||
# Multiple users - use special format
|
||||
greeting = format_multiple_birthdays_greetings(users_data)
|
||||
bot.send_message(chat_id, greeting, parse_mode='HTML')
|
||||
except telebot.apihelper.ApiTelegramException as e:
|
||||
# Handle errors: bot removed from chat, etc.
|
||||
if e.error_code == 403:
|
||||
# Bot was blocked or removed from chat
|
||||
chat = db.query(Chat).filter(Chat.chat_id == chat_id).first()
|
||||
if chat:
|
||||
chat.bot_is_admin = False
|
||||
db.commit()
|
||||
# Silently continue for other errors
|
||||
except Exception:
|
||||
# Other errors - continue
|
||||
pass
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user