🐛 Fix wrong file identifier specified

When encountering an error file ID, it should be removed from the cache.
This commit is contained in:
洛水居室 2023-03-15 16:20:01 +08:00
parent e086f011a1
commit 7bca698c18
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC
3 changed files with 27 additions and 4 deletions

View File

@ -4,7 +4,6 @@ from hashlib import sha256
from typing import Any, Optional from typing import Any, Optional
from core.base_service import BaseService from core.base_service import BaseService
from core.dependence.redisdb import RedisDB from core.dependence.redisdb import RedisDB
__all__ = ["TemplatePreviewCache", "HtmlToFileIdCache"] __all__ = ["TemplatePreviewCache", "HtmlToFileIdCache"]
@ -51,6 +50,9 @@ class HtmlToFileIdCache(BaseService.Component):
if ttl != -1: if ttl != -1:
await self.client.expire(ck, ttl) await self.client.expire(ck, ttl)
async def delete_data(self, html: str, file_type: str) -> bool:
return await self.client.delete(self.cache_key(html, file_type))
def cache_key(self, html: str, file_type: str) -> str: def cache_key(self, html: str, file_type: str) -> str:
key = sha256(html.encode()).hexdigest() key = sha256(html.encode()).hexdigest()
return f"{self.qname}:{file_type}:{key}" return f"{self.qname}:{file_type}:{key}"

View File

@ -2,6 +2,7 @@ from enum import Enum
from typing import List, Optional, Union from typing import List, Optional, Union
from telegram import InputMediaDocument, InputMediaPhoto, Message from telegram import InputMediaDocument, InputMediaPhoto, Message
from telegram.error import BadRequest
from core.services.template.cache import HtmlToFileIdCache from core.services.template.cache import HtmlToFileIdCache
from core.services.template.error import ErrorFileType, FileIdNotFound from core.services.template.error import ErrorFileType, FileIdNotFound
@ -55,7 +56,13 @@ class RenderResult:
if self.file_type != FileType.PHOTO: if self.file_type != FileType.PHOTO:
raise ErrorFileType raise ErrorFileType
reply = await message.reply_photo(photo=self.photo, *args, **kwargs) try:
reply = await message.reply_photo(photo=self.photo, *args, **kwargs)
except BadRequest as exc:
if "Wrong file identifier" in exc.message and isinstance(self.photo, str):
await self._cache.delete_data(self.html, self.file_type.name)
raise BadRequest(message="Wrong file identifier specified")
raise exc
await self.cache_file_id(reply) await self.cache_file_id(reply)
@ -66,7 +73,13 @@ class RenderResult:
if self.file_type != FileType.DOCUMENT: if self.file_type != FileType.DOCUMENT:
raise ErrorFileType raise ErrorFileType
reply = await message.reply_document(document=self.photo, *args, **kwargs) try:
reply = await message.reply_document(document=self.photo, *args, **kwargs)
except BadRequest as exc:
if "Wrong file identifier" in exc.message and isinstance(self.photo, str):
await self._cache.delete_data(self.html, self.file_type.name)
raise BadRequest(message="Wrong file identifier specified")
raise exc
await self.cache_file_id(reply) await self.cache_file_id(reply)
@ -81,7 +94,13 @@ class RenderResult:
media=self.photo, caption=self.caption, parse_mode=self.parse_mode, filename=self.filename media=self.photo, caption=self.caption, parse_mode=self.parse_mode, filename=self.filename
) )
edit_media = await message.edit_media(media, *args, **kwargs) try:
edit_media = await message.edit_media(media, *args, **kwargs)
except BadRequest as exc:
if "Wrong file identifier" in exc.message and isinstance(self.photo, str):
await self._cache.delete_data(self.html, self.file_type.name)
raise BadRequest(message="Wrong file identifier specified")
raise exc
await self.cache_file_id(edit_media) await self.cache_file_id(edit_media)

View File

@ -148,6 +148,8 @@ class ErrorHandler(Plugin):
raise ApplicationHandlerStop raise ApplicationHandlerStop
elif "Not enough rights" in context.error.message: elif "Not enough rights" in context.error.message:
notice = self.ERROR_MSG_PREFIX + "权限不足,请检查对应权限是否开启" notice = self.ERROR_MSG_PREFIX + "权限不足,请检查对应权限是否开启"
elif "Wrong file identifier specified" in context.error.message:
notice = self.ERROR_MSG_PREFIX + "文件标识符未找到 ~ 请稍后再试"
else: else:
logger.error("python-telegram-bot 请求错误", exc_info=context.error) logger.error("python-telegram-bot 请求错误", exc_info=context.error)
notice = self.ERROR_MSG_PREFIX + "telegram-bot-api请求错误 ~ 请稍后再试" notice = self.ERROR_MSG_PREFIX + "telegram-bot-api请求错误 ~ 请稍后再试"