Skip to content

Commit

Permalink
💻 Code: Upgrade md2tgmd version to 0.1.7
Browse files Browse the repository at this point in the history
🐛 Bug: Add markdown rendering error handling
  • Loading branch information
yym68686 committed May 7, 2024
1 parent c0edc52 commit 98eee0e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
17 changes: 13 additions & 4 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,14 @@ async def getChatGPT(update, context, title, robot, message, chatid, messageid):
if "🌐" not in data:
result = result + data
tmpresult = result
if re.sub(r"```", '', result).count("`") % 2 != 0:
if re.sub(r"```", '', result.split("\n")[-1]).count("`") % 2 != 0:
tmpresult = result + "`"
if result.count("```") % 2 != 0:
# if re.sub(r"```", '', result).count("`") % 2 != 0:
# tmpresult = result + "`"
if sum([line.strip().startswith("```") for line in result.split('\n')]) % 2 != 0:
tmpresult = tmpresult + "\n```"
# if result.count("```") % 2 != 0:
# tmpresult = tmpresult + "\n```"
tmpresult = title + tmpresult
if "claude" in title:
tmpresult = claude_replace(tmpresult)
Expand Down Expand Up @@ -219,10 +223,15 @@ async def getChatGPT(update, context, title, robot, message, chatid, messageid):
await context.bot.delete_message(chat_id=chatid, message_id=messageid)
messageid = ''
config.API = ''
tmpresult = f"`{e}`"
tmpresult = f"{tmpresult}\n\n`{e}`"
print(tmpresult)
if lastresult != tmpresult and messageid:
await context.bot.edit_message_text(chat_id=chatid, message_id=messageid, text=escape(tmpresult), parse_mode='MarkdownV2', disable_web_page_preview=True, read_timeout=time_out, write_timeout=time_out, pool_timeout=time_out, connect_timeout=time_out)
if "Can't parse entities: can't find end of code entity at byte offset" in tmpresult:
# await context.bot.edit_message_text(chat_id=chatid, message_id=messageid, text=tmpresult, disable_web_page_preview=True, read_timeout=time_out, write_timeout=time_out, pool_timeout=time_out, connect_timeout=time_out)
await update.message.reply_text(tmpresult)
print(escape(tmpresult))
else:
await context.bot.edit_message_text(chat_id=chatid, message_id=messageid, text=escape(tmpresult), parse_mode='MarkdownV2', disable_web_page_preview=True, read_timeout=time_out, write_timeout=time_out, pool_timeout=time_out, connect_timeout=time_out)

@decorators.GroupAuthorization
@decorators.Authorization
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
--index-url https://pypi.python.org/simple/
requests
tiktoken==0.6.0
md2tgmd==0.1.2
md2tgmd==0.1.7
# jieba
python-dotenv
beautifulsoup4
Expand Down
15 changes: 15 additions & 0 deletions vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": 2,
"builds": [
{
"src": "bot.py",
"use": "@vercel/python"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "bot.py"
}
]
}

0 comments on commit 98eee0e

Please sign in to comment.