-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_handler.py
More file actions
executable file
·62 lines (52 loc) · 2.08 KB
/
test_handler.py
File metadata and controls
executable file
·62 lines (52 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
"""
Test script for Telegram command handlers
"""
import asyncio
import logging
import sys
from telegram import Update
from telegram.ext import Application, CommandHandler, ContextTypes
# Configure logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.DEBUG
)
logger = logging.getLogger("test_handler")
# Handler functions
async def start_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Handle the /start command."""
logger.info(f"Received /start command from user {update.effective_user.id}")
await update.message.reply_text("Hello! I'm a test bot. I'm working correctly!")
async def help_command(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
"""Handle the /help command."""
logger.info(f"Received /help command from user {update.effective_user.id}")
await update.message.reply_text("This is a test bot to check if command handlers are working.")
async def main() -> None:
"""Start the bot."""
# Get the token from command line
if len(sys.argv) < 2:
print("Usage: python test_handler.py <telegram_token>")
return
token = sys.argv[1]
logger.info(f"Starting bot with token: {token[:5]}...{token[-5:]}")
# Create the Application
application = Application.builder().token(token).build()
# Add command handlers
application.add_handler(CommandHandler("start", start_command))
application.add_handler(CommandHandler("help", help_command))
logger.info("Starting bot in polling mode")
await application.initialize()
await application.updater.start_polling()
logger.info("Bot is running. Press Ctrl+C to stop.")
# Keep the bot running until interrupted
try:
while True:
await asyncio.sleep(1)
except KeyboardInterrupt:
logger.info("Received keyboard interrupt, shutting down...")
finally:
logger.info("Stopping bot")
await application.updater.stop()
await application.shutdown()
if __name__ == "__main__":
asyncio.run(main())