From 3c1653a3a306909cc86a7b88e52e657cdad0595c Mon Sep 17 00:00:00 2001 From: inubimambo Date: Sat, 12 Jul 2025 14:01:46 +0800 Subject: [PATCH] Update chatformat --- main.py | 15 +++++++++++++-- pyproject.toml | 3 ++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 7a1a521..61bff77 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,6 @@ import os +import re +import markdown from dotenv import load_dotenv from flowise import Flowise, PredictionData from telegram import Update @@ -26,8 +28,16 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) -> response_text = "" for item in response: response_text += item["text"] - # Escape special characters for markdown v2 - await update.message.reply_text(response_text) + + # Simple markdown to HTML conversion + html = markdown.markdown(response_text) + # Basic cleanup for Telegram + html = html.replace('', '').replace('', '') + html = html.replace('', '').replace('', '') + html = re.sub(r'

(.*?)

', r'\1\n\n', html, flags=re.DOTALL) + + # Send the formatted message using HTML parse mode + await update.message.reply_text(html.strip(), parse_mode='HTML') if __name__ == "__main__": @@ -52,6 +62,7 @@ if __name__ == "__main__": if not TELEGRAM_API_KEY: raise ValueError("TELEGRAM_API_KEY not found in environment variables") application = Application.builder().token(TELEGRAM_API_KEY).build() + application.add_handler(CommandHandler("start", start)) application.add_handler(MessageHandler(filters.TEXT, handle_message)) # Run the bot diff --git a/pyproject.toml b/pyproject.toml index c56aa2e..b167db7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,8 @@ requires-python = ">=3.13" dependencies = [ "python-telegram-bot (>=21.10,<22.0)", "flowise (>=1.0.4,<2.0.0)", - "python-dotenv (>=1.0.1,<2.0.0)" + "python-dotenv (>=1.0.1,<2.0.0)", + "markdown (>=3.4.0,<4.0.0)" ]