fix: add global error handler to surface silent exceptions to user
This commit is contained in:
+13
-4
@@ -1,15 +1,28 @@
|
||||
import logging
|
||||
from telegram import Update
|
||||
from telegram.ext import ApplicationBuilder, CommandHandler, MessageHandler, filters
|
||||
from config import TELEGRAM_BOT_TOKEN
|
||||
import database as db
|
||||
from handlers import admin as admin_handler
|
||||
from handlers import user as user_handler
|
||||
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
async def error_handler(update: object, context) -> None:
|
||||
logger.error("Exception saat handle update:", exc_info=context.error)
|
||||
if isinstance(update, Update) and update.message:
|
||||
await update.message.reply_text(f"❌ Terjadi kesalahan: {str(context.error)[:200]}")
|
||||
|
||||
|
||||
def main():
|
||||
db.init_db()
|
||||
|
||||
app = ApplicationBuilder().token(TELEGRAM_BOT_TOKEN).build()
|
||||
|
||||
app.add_error_handler(error_handler)
|
||||
|
||||
# --- User commands ---
|
||||
app.add_handler(CommandHandler("start", user_handler.start))
|
||||
app.add_handler(CommandHandler("remember", user_handler.remember))
|
||||
@@ -27,18 +40,14 @@ def main():
|
||||
app.add_handler(CommandHandler("help_admin", admin_handler.help_admin))
|
||||
|
||||
# --- Document upload via caption ---
|
||||
# User: kirim file dengan caption "/upload"
|
||||
app.add_handler(MessageHandler(
|
||||
filters.Document.ALL & filters.CaptionRegex(r"^/upload$"),
|
||||
user_handler.upload_doc
|
||||
))
|
||||
# Admin: kirim file dengan caption "/upload_global"
|
||||
app.add_handler(MessageHandler(
|
||||
filters.Document.ALL & filters.CaptionRegex(r"^/upload_global$"),
|
||||
admin_handler.upload_global
|
||||
))
|
||||
|
||||
# Fallback: dokumen tanpa caption yang valid
|
||||
app.add_handler(MessageHandler(filters.Document.ALL, user_handler.upload_doc))
|
||||
|
||||
print("🤖 Second Brain Bot is running...")
|
||||
|
||||
Reference in New Issue
Block a user