Skip to content

Commit 9aef7c1

Browse files
committed
feat(ping): add owner-only ping handler for debug checks
- Introduced a new handler to respond to owner pings with debug information, including bot status and uptime. - Updated main application initialization to include the new ping handler. - Added a response string for the ping command, providing relevant bot and environment details.
1 parent cf432d9 commit 9aef7c1

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""Handler for owner-only ping/debug check."""
2+
3+
import logging
4+
import sys
5+
from datetime import datetime, timezone
6+
7+
from telegram import Update
8+
from telegram.ext import ContextTypes, MessageHandler, filters
9+
10+
from .. import strings
11+
from ..config import Settings
12+
13+
logger = logging.getLogger(__name__)
14+
15+
16+
async def _handle_ping(
17+
update: Update,
18+
context: ContextTypes.DEFAULT_TYPE,
19+
settings: Settings,
20+
) -> None:
21+
"""Respond to owner ping with debug info."""
22+
message = update.effective_message
23+
user = update.effective_user
24+
25+
if not message or not user:
26+
return
27+
28+
if settings.bot_owner_id is None or user.id != settings.bot_owner_id:
29+
return
30+
31+
bot_info = await context.bot.get_me()
32+
uptime_info = datetime.now(timezone.utc).isoformat()
33+
34+
debug_msg = strings.PING_RESPONSE.format(
35+
bot_username=bot_info.username,
36+
python_version=sys.version.split()[0],
37+
timestamp=uptime_info,
38+
)
39+
await message.reply_text(debug_msg)
40+
41+
42+
def create_ping_handlers(settings: Settings) -> list[MessageHandler]:
43+
"""Create handlers for owner ping check."""
44+
45+
async def ping_wrapper(
46+
update: Update, context: ContextTypes.DEFAULT_TYPE
47+
) -> None:
48+
await _handle_ping(update, context, settings)
49+
50+
return [
51+
MessageHandler(
52+
filters.Regex(r"(?i)^electus ci sei\?$") & filters.TEXT,
53+
ping_wrapper,
54+
)
55+
]

src/python_italy_bot/main.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .handlers.announce import create_announce_handlers
1010
from .handlers.id import create_id_handlers
1111
from .handlers.moderation import create_moderation_handlers
12+
from .handlers.ping import create_ping_handlers
1213
from .handlers.settings import create_settings_handlers
1314
from .handlers.spam import create_spam_handler
1415
from .handlers.welcome import create_welcome_handlers
@@ -52,6 +53,8 @@ async def _post_init(application) -> None:
5253
application.add_handler(handler)
5354
for handler in create_announce_handlers(moderation_service, settings):
5455
application.add_handler(handler)
56+
for handler in create_ping_handlers(settings):
57+
application.add_handler(handler)
5558
application.add_handler(create_spam_handler(spam_detector))
5659

5760

src/python_italy_bot/strings.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,15 @@ def mute_success(duration: int | None, reason: str | None) -> str:
156156
def announce_result(success: int, failed: int) -> str:
157157
"""Format announcement result message."""
158158
return f"Annuncio inviato: {success} ok, {failed} falliti."
159+
160+
161+
# =============================================================================
162+
# PING (Owner Debug)
163+
# =============================================================================
164+
165+
PING_RESPONSE = (
166+
"Sono online.\n\n"
167+
"Bot: @{bot_username}\n"
168+
"Python: {python_version}\n"
169+
"Timestamp: {timestamp}"
170+
)

0 commit comments

Comments
 (0)