🎨 Use html.escape to Escape Special Characters

This commit is contained in:
洛水居室 2022-12-25 15:49:36 +08:00
parent 5f65dc18a5
commit 0218ac7648
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC

View File

@ -1,4 +1,5 @@
import contextlib import contextlib
import html
from typing import List from typing import List
from telegram import Update, Chat, ChatMember, ChatMemberOwner, ChatMemberAdministrator from telegram import Update, Chat, ChatMember, ChatMemberOwner, ChatMemberAdministrator
@ -32,17 +33,17 @@ class GetChat(Plugin):
self.gacha_log = GachaLog() self.gacha_log = GachaLog()
async def parse_group_chat(self, chat: Chat, admins: List[ChatMember]) -> str: async def parse_group_chat(self, chat: Chat, admins: List[ChatMember]) -> str:
text = f"群 ID<code>{chat.id}</code>\n" f"群名称:<code>{chat.title}</code>\n" text = f"群 ID<code>{chat.id}</code>\n群名称:<code>{chat.title}</code>\n"
if chat.username: if chat.username:
text += f"群用户名:@{chat.username}\n" text += f"群用户名:@{chat.username}\n"
sign_info = await self.sign_service.get_by_chat_id(chat.id) sign_info = await self.sign_service.get_by_chat_id(chat.id)
if sign_info: if sign_info:
text += f"自动签到推送人数:<code>{len(sign_info)}</code>\n" text += f"自动签到推送人数:<code>{len(sign_info)}</code>\n"
if chat.description: if chat.description:
text += f"群简介:<code>{chat.description}</code>\n" text += f"群简介:<code>{html.escape(chat.description)}</code>\n"
if admins: if admins:
for admin in admins: for admin in admins:
text += f'<a href="tg://user?id={admin.user.id}">{admin.user.full_name}</a> ' text += f'<a href="tg://user?id={admin.user.id}">{html.escape(admin.user.full_name)}</a> '
if isinstance(admin, ChatMemberAdministrator): if isinstance(admin, ChatMemberAdministrator):
text += "C" if admin.can_change_info else "_" text += "C" if admin.can_change_info else "_"
text += "D" if admin.can_delete_messages else "_" text += "D" if admin.can_delete_messages else "_"
@ -82,7 +83,7 @@ class GetChat(Plugin):
await get_genshin_client(chat.id) await get_genshin_client(chat.id)
except CookiesNotFoundError: except CookiesNotFoundError:
temp = "UID 绑定" temp = "UID 绑定"
text += f"<code>{temp}</code>\n" f"游戏 ID<code>{uid}</code>" text += f"<code>{temp}</code>\n游戏 ID<code>{uid}</code>"
sign_info = await self.sign_service.get_by_user_id(chat.id) sign_info = await self.sign_service.get_by_user_id(chat.id)
if sign_info is not None: if sign_info is not None:
text += ( text += (
@ -109,7 +110,7 @@ class GetChat(Plugin):
@bot_admins_rights_check @bot_admins_rights_check
async def get_chat(self, update: Update, context: CallbackContext): async def get_chat(self, update: Update, context: CallbackContext):
user = update.effective_user user = update.effective_user
logger.info(f"用户 {user.full_name}[{user.id}] get_chat 命令请求") logger.info("用户 %s[%s] get_chat 命令请求", user.full_name, user.id)
message = update.effective_message message = update.effective_message
args = get_args(context) args = get_args(context)
if not args: if not args:
@ -129,5 +130,4 @@ class GetChat(Plugin):
text = await self.parse_private_chat(chat) text = await self.parse_private_chat(chat)
await message.reply_text(text, parse_mode="HTML") await message.reply_text(text, parse_mode="HTML")
except (BadRequest, Forbidden) as exc: except (BadRequest, Forbidden) as exc:
await message.reply_text(f"通过 id 获取会话信息失败API 返回:{exc}") await message.reply_text(f"通过 id 获取会话信息失败API 返回:{exc.message}")
return