2022-09-08 01:08:37 +00:00
|
|
|
|
import json
|
2022-07-26 10:07:31 +00:00
|
|
|
|
from functools import wraps
|
2022-11-24 01:47:29 +00:00
|
|
|
|
from typing import Callable, cast, Optional
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
|
|
|
|
from aiohttp import ClientConnectorError
|
2022-09-20 06:42:12 +00:00
|
|
|
|
from genshin import InvalidCookies, GenshinException, TooManyRequests, DataNotPublic
|
2022-07-26 10:07:31 +00:00
|
|
|
|
from httpx import ConnectTimeout
|
2022-11-24 01:47:29 +00:00
|
|
|
|
from telegram import Update, ReplyKeyboardRemove, InlineKeyboardButton, InlineKeyboardMarkup, Message
|
2022-07-26 10:07:31 +00:00
|
|
|
|
from telegram.error import BadRequest, TimedOut, Forbidden
|
2022-11-24 01:47:29 +00:00
|
|
|
|
from telegram.ext import CallbackContext, ConversationHandler, filters
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
2022-11-24 01:47:29 +00:00
|
|
|
|
from core.baseplugin import add_delete_message_job
|
2022-11-12 15:08:25 +00:00
|
|
|
|
from modules.apihelper.error import APIHelperException, ReturnCodeError, APIHelperTimedOut, ResponseException
|
2022-09-01 01:51:01 +00:00
|
|
|
|
from utils.error import UrlResourcesNotFoundError
|
2022-09-08 01:08:37 +00:00
|
|
|
|
from utils.log import logger
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
|
|
|
|
|
2022-11-24 01:47:29 +00:00
|
|
|
|
async def send_user_notification(update: Update, context: CallbackContext, text: str) -> Optional[Message]:
|
2022-11-19 08:43:50 +00:00
|
|
|
|
if not isinstance(update, Update):
|
|
|
|
|
logger.warning("错误的消息类型 %s", repr(update))
|
2022-11-24 01:47:29 +00:00
|
|
|
|
return None
|
2022-10-17 10:17:46 +00:00
|
|
|
|
if update.inline_query is not None: # 忽略 inline_query
|
2022-11-24 01:47:29 +00:00
|
|
|
|
return None
|
2022-11-12 12:59:42 +00:00
|
|
|
|
if "重新绑定" in text:
|
|
|
|
|
buttons = InlineKeyboardMarkup(
|
|
|
|
|
[[InlineKeyboardButton("点我重新绑定", url=f"https://t.me/{context.bot.username}?start=set_cookie")]]
|
|
|
|
|
)
|
|
|
|
|
elif "通过验证" in text:
|
|
|
|
|
buttons = InlineKeyboardMarkup(
|
|
|
|
|
[[InlineKeyboardButton("点我通过验证", url=f"https://t.me/{context.bot.username}?start=verify_verification")]]
|
|
|
|
|
)
|
|
|
|
|
else:
|
|
|
|
|
buttons = ReplyKeyboardRemove()
|
2022-10-21 07:06:24 +00:00
|
|
|
|
user = update.effective_user
|
2022-09-08 01:08:37 +00:00
|
|
|
|
message = update.effective_message
|
2022-10-21 07:06:24 +00:00
|
|
|
|
chat = update.effective_chat
|
2022-07-26 10:07:31 +00:00
|
|
|
|
if message is None:
|
|
|
|
|
update_str = update.to_dict() if isinstance(update, Update) else str(update)
|
2022-11-17 12:36:25 +00:00
|
|
|
|
logger.warning("错误的消息类型\n %s", json.dumps(update_str, indent=2, ensure_ascii=False))
|
2022-11-24 01:47:29 +00:00
|
|
|
|
return None
|
2022-11-17 12:36:25 +00:00
|
|
|
|
logger.info("尝试通知用户 %s[%s] 在 %s[%s] 的错误信息[%s]", user.full_name, user.id, chat.full_name, chat.id, text)
|
2022-07-26 10:07:31 +00:00
|
|
|
|
try:
|
2022-11-24 01:47:29 +00:00
|
|
|
|
return await message.reply_text(text, reply_markup=buttons, allow_sending_without_reply=True)
|
2022-11-17 12:36:25 +00:00
|
|
|
|
except ConnectTimeout:
|
|
|
|
|
logger.error("httpx 模块连接服务器 ConnectTimeout 发送 update_id[%s] 错误信息失败", update.update_id)
|
|
|
|
|
except BadRequest as exc:
|
|
|
|
|
logger.error("发送 update_id[%s] 错误信息失败 错误信息为 [%s]", update.update_id, exc.message)
|
|
|
|
|
except Forbidden as exc:
|
|
|
|
|
logger.error("发送 update_id[%s] 错误信息失败 错误信息为 [%s]", update.update_id, exc.message)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
logger.error("发送 update_id[%s] 错误信息失败 错误信息为 [%s]", update.update_id, repr(exc))
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.exception(exc)
|
2022-11-24 01:47:29 +00:00
|
|
|
|
return None
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
|
|
|
|
|
2022-10-21 07:06:24 +00:00
|
|
|
|
def telegram_warning(update: Update, text: str):
|
|
|
|
|
user = update.effective_user
|
|
|
|
|
message = update.effective_message
|
|
|
|
|
chat = update.effective_chat
|
2022-11-19 08:43:50 +00:00
|
|
|
|
msg = f"{text}\n user_id[{user.id}] chat_id[{chat.id}] message_id[{message.id}]"
|
2022-10-21 07:06:24 +00:00
|
|
|
|
logger.warning(msg)
|
|
|
|
|
|
|
|
|
|
|
2022-07-26 10:07:31 +00:00
|
|
|
|
def error_callable(func: Callable) -> Callable:
|
|
|
|
|
"""Plugins 错误处理修饰器
|
|
|
|
|
|
|
|
|
|
非常感谢 @Bibo-Joshi 提出的建议
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
@wraps(func)
|
|
|
|
|
async def decorator(*args, **kwargs):
|
|
|
|
|
if len(args) == 3:
|
|
|
|
|
# self update context
|
|
|
|
|
_, update, context = args
|
|
|
|
|
elif len(args) == 2:
|
|
|
|
|
# update context
|
|
|
|
|
update, context = args
|
|
|
|
|
else:
|
|
|
|
|
return await func(*args, **kwargs)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
update = cast(Update, update)
|
|
|
|
|
context = cast(CallbackContext, context)
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text: str = ""
|
2022-07-26 10:07:31 +00:00
|
|
|
|
try:
|
|
|
|
|
return await func(*args, **kwargs)
|
|
|
|
|
except ClientConnectorError:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.error("aiohttp 模块连接服务器 ClientConnectorError")
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ 请稍后再试"
|
2022-07-26 10:07:31 +00:00
|
|
|
|
except ConnectTimeout:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.error("httpx 模块连接服务器 ConnectTimeout")
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ 请稍后再试"
|
2022-07-26 10:07:31 +00:00
|
|
|
|
except TimedOut:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.error("python-telegram-bot 模块连接服务器 TimedOut")
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ 请稍后再试"
|
2022-09-01 01:51:01 +00:00
|
|
|
|
except UrlResourcesNotFoundError as exc:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.error("URL数据资源未找到")
|
|
|
|
|
logger.exception(exc)
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = "出错了呜呜呜 ~ 资源未找到 ~ "
|
2022-07-26 10:07:31 +00:00
|
|
|
|
except InvalidCookies as exc:
|
2022-10-08 03:14:03 +00:00
|
|
|
|
if exc.retcode in (10001, -100):
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = "出错了呜呜呜 ~ Cookie 无效,请尝试重新绑定"
|
2022-10-08 01:01:51 +00:00
|
|
|
|
elif exc.retcode == 10103:
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = "出错了呜呜呜 ~ Cookie 有效,但没有绑定到游戏帐户,请尝试重新绑定"
|
2022-07-26 10:07:31 +00:00
|
|
|
|
else:
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.warning("Cookie错误")
|
|
|
|
|
logger.exception(exc)
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = f"出错了呜呜呜 ~ Cookie 无效 错误信息为 {exc.original} 请尝试重新绑定"
|
2022-07-26 10:07:31 +00:00
|
|
|
|
except TooManyRequests as exc:
|
2022-11-14 13:09:10 +00:00
|
|
|
|
logger.warning("查询次数太多(操作频繁) %s", exc)
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = "出错了呜呜呜 ~ 当天查询次数已经超过30次,请次日再进行查询"
|
2022-09-20 06:42:12 +00:00
|
|
|
|
except DataNotPublic:
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = "出错了呜呜呜 ~ 查询的用户数据未公开"
|
2022-07-26 10:07:31 +00:00
|
|
|
|
except GenshinException as exc:
|
2022-10-08 01:01:51 +00:00
|
|
|
|
if exc.retcode == -130:
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = "出错了呜呜呜 ~ 未设置默认角色,请尝试重新绑定"
|
2022-11-05 15:28:36 +00:00
|
|
|
|
elif exc.retcode == 1034:
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = "出错了呜呜呜 ~ 服务器检测到该账号可能存在异常,请求被拒绝,请尝试通过验证"
|
2022-11-14 13:09:10 +00:00
|
|
|
|
elif exc.retcode == -500001:
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = "出错了呜呜呜 ~ 网络出小差了,请稍后重试~"
|
2022-11-14 13:09:10 +00:00
|
|
|
|
elif exc.retcode == -1:
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = "出错了呜呜呜 ~ 系统发生错误,请稍后重试~"
|
2022-11-14 13:09:10 +00:00
|
|
|
|
elif exc.retcode == -10001: # 参数异常 应该抛出错误
|
|
|
|
|
raise exc
|
2022-11-05 15:28:36 +00:00
|
|
|
|
else:
|
|
|
|
|
logger.error("GenshinException")
|
|
|
|
|
logger.exception(exc)
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = f"出错了呜呜呜 ~ 获取账号信息发生错误 错误信息为 {exc.original if exc.original else exc.retcode} ~ 请稍后再试"
|
2022-10-08 00:59:08 +00:00
|
|
|
|
except ReturnCodeError as exc:
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = f"出错了呜呜呜 ~ API请求错误 错误信息为 {exc.message} ~ 请稍后再试"
|
2022-10-21 07:06:24 +00:00
|
|
|
|
except APIHelperTimedOut:
|
|
|
|
|
logger.warning("APIHelperException")
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = "出错了呜呜呜 ~ API请求超时 ~ 请稍后再试"
|
2022-11-12 15:08:25 +00:00
|
|
|
|
except ResponseException as exc:
|
|
|
|
|
logger.error("APIHelperException [%s]%s", exc.code, exc.message)
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = f"出错了呜呜呜 ~ API请求错误 错误信息为 {exc.message if exc.message else exc.code} ~ 请稍后再试"
|
2022-10-08 00:59:08 +00:00
|
|
|
|
except APIHelperException as exc:
|
|
|
|
|
logger.error("APIHelperException")
|
|
|
|
|
logger.exception(exc)
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = "出错了呜呜呜 ~ API请求错误 ~ 请稍后再试"
|
2022-07-26 10:07:31 +00:00
|
|
|
|
except BadRequest as exc:
|
2022-10-21 07:06:24 +00:00
|
|
|
|
if "Replied message not found" in exc.message:
|
|
|
|
|
telegram_warning(update, exc.message)
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = "气死我了!怎么有人喜欢发一个命令就秒删了!"
|
|
|
|
|
elif "Message is not modified" in exc.message:
|
2022-10-21 07:06:24 +00:00
|
|
|
|
telegram_warning(update, exc.message)
|
2022-11-24 01:47:29 +00:00
|
|
|
|
else:
|
|
|
|
|
logger.error("python-telegram-bot 请求错误")
|
|
|
|
|
logger.exception(exc)
|
|
|
|
|
text = "出错了呜呜呜 ~ telegram-bot-api请求错误 ~ 请稍后再试"
|
2022-07-26 10:07:31 +00:00
|
|
|
|
except Forbidden as exc:
|
2022-10-22 16:40:30 +00:00
|
|
|
|
logger.error("python-telegram-bot 返回 Forbidden")
|
2022-09-08 01:08:37 +00:00
|
|
|
|
logger.exception(exc)
|
2022-11-24 01:47:29 +00:00
|
|
|
|
text = "出错了呜呜呜 ~ telegram-bot-api请求错误 ~ 请稍后再试"
|
|
|
|
|
if text:
|
|
|
|
|
notice_message = await send_user_notification(update, context, text)
|
|
|
|
|
message = update.effective_message
|
|
|
|
|
if message:
|
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
|
|
|
|
if notice_message:
|
|
|
|
|
add_delete_message_job(context, notice_message.chat_id, notice_message.message_id, 60)
|
|
|
|
|
add_delete_message_job(context, message.chat_id, message.message_id, 60)
|
|
|
|
|
else:
|
|
|
|
|
user = update.effective_user
|
|
|
|
|
chat = update.effective_chat
|
|
|
|
|
logger.error("发送 %s[%s] 在 %s[%s] 的通知出现问题 通知文本不存在", user.full_name, user.id, chat.full_name, chat.id)
|
|
|
|
|
return ConversationHandler.END
|
2022-07-26 10:07:31 +00:00
|
|
|
|
|
|
|
|
|
return decorator
|