add audio notifications
This commit is contained in:
parent
9cb573db7d
commit
1f384c12ab
2 changed files with 47 additions and 5 deletions
|
|
@ -84,6 +84,22 @@ class AdminBot:
|
||||||
"""Set admin chat ID in database"""
|
"""Set admin chat ID in database"""
|
||||||
self.db.set_admin_chat_id(chat_id)
|
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]):
|
async def notify_new_user(self, user_id: int, username: Optional[str], first_name: Optional[str]):
|
||||||
"""Send notification about new Telegram user"""
|
"""Send notification about new Telegram user"""
|
||||||
username_text = f"@{username}" if username else "без username"
|
username_text = f"@{username}" if username else "без username"
|
||||||
|
|
@ -97,7 +113,19 @@ class AdminBot:
|
||||||
)
|
)
|
||||||
await self.send_notification(message)
|
await self.send_notification(message)
|
||||||
|
|
||||||
async def notify_new_player(self, player_username: str, added_by_user_id: int, added_by_username: Optional[str]):
|
# 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], is_new_gamer: bool = False):
|
||||||
"""Send notification about new tracked player"""
|
"""Send notification about new tracked player"""
|
||||||
added_by_text = f"@{added_by_username}" if added_by_username else f"ID: {added_by_user_id}"
|
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}"
|
lichess_url = f"https://lichess.org/@/{player_username}"
|
||||||
|
|
@ -109,6 +137,19 @@ class AdminBot:
|
||||||
)
|
)
|
||||||
await self.send_notification(message)
|
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):
|
async def status(self, update: Update, context: ContextTypes.DEFAULT_TYPE):
|
||||||
"""Status command - show statistics"""
|
"""Status command - show statistics"""
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,7 @@ class LichessBot:
|
||||||
self.application = None # Will be set when application is created
|
self.application = None # Will be set when application is created
|
||||||
self.counters = MessageCounters() # Message counters
|
self.counters = MessageCounters() # Message counters
|
||||||
|
|
||||||
async def _notify_admin_new_player(self, player_username: str, added_by_user_id: int, added_by_username: Optional[str]):
|
async def _notify_admin_new_player(self, player_username: str, added_by_user_id: int, added_by_username: Optional[str], is_new_gamer: bool = False):
|
||||||
"""Notify admin about newly linked player (always try to send)."""
|
"""Notify admin about newly linked player (always try to send)."""
|
||||||
try:
|
try:
|
||||||
admin_bot = get_admin_bot()
|
admin_bot = get_admin_bot()
|
||||||
|
|
@ -52,7 +52,8 @@ class LichessBot:
|
||||||
await admin_bot.notify_new_player(
|
await admin_bot.notify_new_player(
|
||||||
player_username=player_username,
|
player_username=player_username,
|
||||||
added_by_user_id=added_by_user_id,
|
added_by_user_id=added_by_user_id,
|
||||||
added_by_username=added_by_username
|
added_by_username=added_by_username,
|
||||||
|
is_new_gamer=is_new_gamer
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
@ -355,7 +356,7 @@ class LichessBot:
|
||||||
try:
|
try:
|
||||||
user_obj = update.effective_user
|
user_obj = update.effective_user
|
||||||
await self._notify_admin_new_player(
|
await self._notify_admin_new_player(
|
||||||
username, user_id, user_obj.username if user_obj else None
|
username, user_id, user_obj.username if user_obj else None, is_new_gamer
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Admin notify failed after adding gamer with token: {e}")
|
logger.error(f"Admin notify failed after adding gamer with token: {e}")
|
||||||
|
|
@ -440,7 +441,7 @@ class LichessBot:
|
||||||
try:
|
try:
|
||||||
user_obj = update.effective_user
|
user_obj = update.effective_user
|
||||||
await self._notify_admin_new_player(
|
await self._notify_admin_new_player(
|
||||||
username, user_id, user_obj.username if user_obj else None
|
username, user_id, user_obj.username if user_obj else None, is_new_gamer
|
||||||
)
|
)
|
||||||
logger.info(f"Admin notification processed for player {username}")
|
logger.info(f"Admin notification processed for player {username}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue