Update chatformat
This commit is contained in:
15
main.py
15
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('<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
|
||||
|
||||
Reference in New Issue
Block a user