47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
from telegram import Update
|
|
from telegram.ext import MessageHandler, filters
|
|
from llm import clinet
|
|
import re
|
|
|
|
# simple filter for proper markdown response
|
|
def escape_markdown_except_code_blocks(text):
|
|
# Pattern to match text outside of code blocks and inline code
|
|
pattern = r'(```.*?```|`.*?`)|(_)'
|
|
|
|
def replace(match):
|
|
# If group 1 (code blocks or inline code) is matched, return it unaltered
|
|
if match.group(1):
|
|
return match.group(1)
|
|
# If group 2 (underscore) is matched, escape it
|
|
elif match.group(2):
|
|
return r'\_'
|
|
|
|
return re.sub(pattern, replace, text, flags=re.DOTALL)
|
|
|
|
|
|
|
|
async def on_message(update: Update, _):
|
|
msg = await update.message.reply_text("...")
|
|
await update.effective_chat.send_action("TYPING")
|
|
user_id = str(update.message.from_user.id)
|
|
user_name = update.message.from_user.full_name or update.message.from_user.username
|
|
print("msg from:", user_id, user_name)
|
|
try:
|
|
response = clinet.generate(update.message.text, user_id)
|
|
try:
|
|
await msg.edit_text(
|
|
escape_markdown_except_code_blocks(response),
|
|
parse_mode='markdown'
|
|
)
|
|
except Exception as e:
|
|
print(e)
|
|
await msg.edit_text(response)
|
|
except Exception as e:
|
|
print(e)
|
|
await msg.edit_text("failed to generate response")
|
|
|
|
await update.effective_chat.send_action("CANCEL")
|
|
|
|
message_handler = MessageHandler(filters.TEXT & (~filters.COMMAND), on_message)
|
|
|