Add initial project structure for Telegram bot
- Created Docker configuration files for development and production. - Added Dockerfile for building the bot image. - Implemented configuration loading from environment variables. - Developed main application logic and command handlers. - Included README with setup instructions and usage details. - Added .gitignore and .dockerignore files to exclude unnecessary files. - Provided example environment file (.env.example) for bot token configuration. - Established basic error handling for the bot.
This commit is contained in:
10
handlers/__init__.py
Normal file
10
handlers/__init__.py
Normal file
@@ -0,0 +1,10 @@
|
||||
"""Expose a single register_handlers(app) that registers all handlers."""
|
||||
from telegram.ext import Application
|
||||
|
||||
from . import commands, errors
|
||||
|
||||
|
||||
def register_handlers(app: Application) -> None:
|
||||
app.add_handler(commands.start_handler)
|
||||
app.add_handler(commands.help_handler)
|
||||
app.add_error_handler(errors.error_handler)
|
||||
21
handlers/commands.py
Normal file
21
handlers/commands.py
Normal file
@@ -0,0 +1,21 @@
|
||||
"""Command handlers: /start, /help (and placeholder for more)."""
|
||||
from telegram import Update
|
||||
from telegram.ext import CommandHandler, ContextTypes
|
||||
|
||||
|
||||
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
if update.message:
|
||||
await update.message.reply_text("Hello! I'm your bot. Use /help to see available commands.")
|
||||
|
||||
|
||||
async def help_cmd(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
if update.message:
|
||||
await update.message.reply_text(
|
||||
"Available commands:\n"
|
||||
"/start - Start the bot\n"
|
||||
"/help - Show this help"
|
||||
)
|
||||
|
||||
|
||||
start_handler = CommandHandler("start", start)
|
||||
help_handler = CommandHandler("help", help_cmd)
|
||||
13
handlers/errors.py
Normal file
13
handlers/errors.py
Normal file
@@ -0,0 +1,13 @@
|
||||
"""Global error handler: log exception and notify user."""
|
||||
import logging
|
||||
|
||||
from telegram import Update
|
||||
from telegram.ext import ContextTypes
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def error_handler(update: object, context: ContextTypes.DEFAULT_TYPE) -> None:
|
||||
logger.exception("Exception while handling an update")
|
||||
if isinstance(update, Update) and update.effective_message:
|
||||
await update.effective_message.reply_text("Something went wrong. Please try again later.")
|
||||
Reference in New Issue
Block a user