Update chatformat
This commit is contained in:
15
main.py
15
main.py
@@ -1,4 +1,6 @@
|
|||||||
import os
|
import os
|
||||||
|
import re
|
||||||
|
import markdown
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
from flowise import Flowise, PredictionData
|
from flowise import Flowise, PredictionData
|
||||||
from telegram import Update
|
from telegram import Update
|
||||||
@@ -26,8 +28,16 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
|
|||||||
response_text = ""
|
response_text = ""
|
||||||
for item in response:
|
for item in response:
|
||||||
response_text += item["text"]
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|
||||||
@@ -52,6 +62,7 @@ if __name__ == "__main__":
|
|||||||
if not TELEGRAM_API_KEY:
|
if not TELEGRAM_API_KEY:
|
||||||
raise ValueError("TELEGRAM_API_KEY not found in environment variables")
|
raise ValueError("TELEGRAM_API_KEY not found in environment variables")
|
||||||
application = Application.builder().token(TELEGRAM_API_KEY).build()
|
application = Application.builder().token(TELEGRAM_API_KEY).build()
|
||||||
|
application.add_handler(CommandHandler("start", start))
|
||||||
application.add_handler(MessageHandler(filters.TEXT, handle_message))
|
application.add_handler(MessageHandler(filters.TEXT, handle_message))
|
||||||
|
|
||||||
# Run the bot
|
# Run the bot
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ requires-python = ">=3.13"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"python-telegram-bot (>=21.10,<22.0)",
|
"python-telegram-bot (>=21.10,<22.0)",
|
||||||
"flowise (>=1.0.4,<2.0.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)"
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user