Update chatformat

This commit is contained in:
inubimambo
2025-07-12 14:01:46 +08:00
parent f7fbd48a26
commit 3c1653a3a3
2 changed files with 15 additions and 3 deletions

15
main.py
View File

@@ -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('<strong>', '<b>').replace('</strong>', '</b>')
html = html.replace('<em>', '<i>').replace('</em>', '</i>')
html = re.sub(r'<p>(.*?)</p>', 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

View File

@@ -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)"
]