add audio notifications

This commit is contained in:
vrubelroman 2025-11-18 19:39:52 +03:00
parent 9cb573db7d
commit 1f384c12ab
2 changed files with 47 additions and 5 deletions

View file

@ -84,6 +84,22 @@ class AdminBot:
"""Set admin chat ID in database"""
self.db.set_admin_chat_id(chat_id)
async def _call_tts_service(self, text: str):
"""Call TTS service with given text"""
try:
import urllib.parse
import aiohttp
encoded_text = urllib.parse.quote(text)
tts_url = f"http://192.168.8.111:7901/TTS?text={encoded_text}"
async with aiohttp.ClientSession() as session:
async with session.get(tts_url, timeout=aiohttp.ClientTimeout(total=5)) as response:
if response.status == 200:
logger.info(f"TTS service called successfully: {text}")
else:
logger.warning(f"TTS service returned status {response.status}")
except Exception as e:
logger.error(f"Failed to call TTS service: {e}")
async def notify_new_user(self, user_id: int, username: Optional[str], first_name: Optional[str]):
"""Send notification about new Telegram user"""
username_text = f"@{username}" if username else "без username"
@ -96,8 +112,20 @@ class AdminBot:
f"Имя: {name_text}"
)
await self.send_notification(message)
# Call TTS service with user count
try:
import sqlite3
with sqlite3.connect(self.db.db_path) as conn:
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) FROM telegram_users")
total_users = cursor.fetchone()[0]
tts_text = f"Появился новый пользователь {total_users}"
await self._call_tts_service(tts_text)
except Exception as e:
logger.error(f"Failed to call TTS for new user: {e}")
async def notify_new_player(self, player_username: str, added_by_user_id: int, added_by_username: Optional[str]):
async def notify_new_player(self, player_username: str, added_by_user_id: int, added_by_username: Optional[str], is_new_gamer: bool = False):
"""Send notification about new tracked player"""
added_by_text = f"@{added_by_username}" if added_by_username else f"ID: {added_by_user_id}"
lichess_url = f"https://lichess.org/@/{player_username}"
@ -108,6 +136,19 @@ class AdminBot:
f"Добавил: {added_by_text}"
)
await self.send_notification(message)
# Call TTS service if this is a new gamer
if is_new_gamer:
try:
import sqlite3
with sqlite3.connect(self.db.db_path) as conn:
cursor = conn.cursor()
cursor.execute("SELECT COUNT(DISTINCT username) FROM gamers")
total_gamers = cursor.fetchone()[0]
tts_text = f"Добавлен новый игрок {total_gamers}"
await self._call_tts_service(tts_text)
except Exception as e:
logger.error(f"Failed to call TTS for new gamer: {e}")
async def status(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
"""Status command - show statistics"""