Fix unsupported start tag ul

This commit is contained in:
inubimambo
2025-07-12 14:06:54 +08:00
parent 22f2587aee
commit b48ec0a48d

16
main.py
View File

@@ -31,11 +31,25 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE) ->
# Simple markdown to HTML conversion
html = markdown.markdown(response_text)
# Basic cleanup for Telegram
# Basic cleanup for Telegram HTML compatibility
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)
# Handle lists - convert to simple bullet points
html = re.sub(r'<ul>\s*', '', html)
html = re.sub(r'\s*</ul>', '', html)
html = re.sub(r'<li>(.*?)</li>', r'\1\n', html, flags=re.DOTALL)
# Clean up any other unsupported tags that might be present
html = re.sub(r'<ol>\s*', '', html)
html = re.sub(r'\s*</ol>', '', html)
html = re.sub(r'<h[1-6]>(.*?)</h[1-6]>', r'<b>\1</b>\n', html, flags=re.DOTALL)
# Clean up extra whitespace
html = re.sub(r'\n\s*\n', '\n\n', html)
html = html.strip()
# Send the formatted message using HTML parse mode
await update.message.reply_text(html.strip(), parse_mode='HTML')