2022-09-11 13:15:01 +00:00
|
|
|
import os
|
|
|
|
import time
|
2022-07-26 10:07:31 +00:00
|
|
|
import traceback
|
2023-03-14 01:27:22 +00:00
|
|
|
from typing import Optional
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-09-11 13:15:01 +00:00
|
|
|
import aiofiles
|
2023-03-14 01:27:22 +00:00
|
|
|
from aiohttp import ClientError, ClientConnectorError
|
|
|
|
from genshin import DataNotPublic, GenshinException, InvalidCookies, TooManyRequests
|
2023-03-14 02:40:27 +00:00
|
|
|
from httpx import HTTPError, TimeoutException
|
2023-03-14 01:27:22 +00:00
|
|
|
from telegram import ReplyKeyboardRemove, Update, InlineKeyboardMarkup, InlineKeyboardButton
|
2022-07-26 10:07:31 +00:00
|
|
|
from telegram.constants import ParseMode
|
2023-03-14 01:27:22 +00:00
|
|
|
from telegram.error import BadRequest, Forbidden, TelegramError, TimedOut, NetworkError
|
2023-03-14 05:53:05 +00:00
|
|
|
from telegram.ext import CallbackContext, ApplicationHandlerStop, filters
|
2023-03-14 01:27:22 +00:00
|
|
|
from telegram.helpers import create_deep_linked_url
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-11-21 06:57:12 +00:00
|
|
|
from core.config import config
|
2022-10-28 07:11:14 +00:00
|
|
|
from core.plugin import Plugin, error_handler
|
2023-03-14 01:27:22 +00:00
|
|
|
from modules.apihelper.error import APIHelperException, APIHelperTimedOut, ResponseException, ReturnCodeError
|
|
|
|
from modules.errorpush import (
|
|
|
|
PbClient,
|
|
|
|
PbClientException,
|
|
|
|
SentryClient,
|
|
|
|
SentryClientException,
|
|
|
|
)
|
2022-09-08 01:08:37 +00:00
|
|
|
from utils.log import logger
|
2023-03-14 01:27:22 +00:00
|
|
|
from utils.patch.aiohttp import AioHttpTimeoutException
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2023-01-07 08:00:32 +00:00
|
|
|
try:
|
|
|
|
import ujson as jsonlib
|
|
|
|
|
|
|
|
except ImportError:
|
|
|
|
import json as jsonlib
|
|
|
|
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
class ErrorHandler(Plugin):
|
2023-03-14 01:27:22 +00:00
|
|
|
ERROR_MSG_PREFIX = "出错了呜呜呜 ~ "
|
|
|
|
SEND_MSG_ERROR_NOTICE = "发送 update_id[%s] 错误信息失败 错误信息为 [%s]"
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.notice_chat_id = config.error.notification_chat_id
|
|
|
|
current_dir = os.getcwd()
|
|
|
|
logs_dir = os.path.join(current_dir, "logs")
|
|
|
|
if not os.path.exists(logs_dir):
|
|
|
|
os.mkdir(logs_dir)
|
|
|
|
self.report_dir = os.path.join(current_dir, "report")
|
|
|
|
if not os.path.exists(self.report_dir):
|
|
|
|
os.mkdir(self.report_dir)
|
|
|
|
self.pb_client = PbClient(config.error.pb_url, config.error.pb_sunset, config.error.pb_max_lines)
|
|
|
|
self.sentry = SentryClient(config.error.sentry_dsn)
|
|
|
|
|
|
|
|
async def notice_user(self, update: object, context: CallbackContext, content: str):
|
|
|
|
if not isinstance(update, Update):
|
|
|
|
logger.warning("错误的消息类型 %s", repr(update))
|
|
|
|
return None
|
|
|
|
if update.inline_query is not None: # 忽略 inline_query
|
|
|
|
return None
|
|
|
|
|
|
|
|
if "重新绑定" in content:
|
|
|
|
buttons = InlineKeyboardMarkup(
|
|
|
|
[[InlineKeyboardButton("点我重新绑定", url=create_deep_linked_url(context.bot.username, "set_cookie"))]]
|
|
|
|
)
|
|
|
|
elif "通过验证" in content:
|
|
|
|
buttons = InlineKeyboardMarkup(
|
|
|
|
[
|
|
|
|
[
|
|
|
|
InlineKeyboardButton(
|
|
|
|
"点我通过验证", url=create_deep_linked_url(context.bot.username, "verify_verification")
|
|
|
|
)
|
|
|
|
]
|
|
|
|
]
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
buttons = ReplyKeyboardRemove()
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
user = update.effective_user
|
|
|
|
message = update.effective_message
|
|
|
|
chat = update.effective_chat
|
|
|
|
|
|
|
|
if chat.id == user.id:
|
|
|
|
logger.info("尝试通知用户 %s[%s] 错误信息[%s]", user.full_name, user.id, content)
|
|
|
|
else:
|
|
|
|
logger.info("尝试通知用户 %s[%s] 在 %s[%s] 的错误信息[%s]", user.full_name, user.id, chat.title, chat.id, content)
|
|
|
|
try:
|
|
|
|
if update.callback_query:
|
|
|
|
await update.callback_query.answer(content, show_alert=True)
|
2023-04-11 01:44:39 +00:00
|
|
|
return
|
|
|
|
if message:
|
2023-03-17 04:06:58 +00:00
|
|
|
reply_text = await message.reply_text(content, reply_markup=buttons, allow_sending_without_reply=True)
|
|
|
|
if filters.ChatType.GROUPS.filter(reply_text):
|
|
|
|
self.add_delete_message_job(reply_text, context=context)
|
|
|
|
self.add_delete_message_job(message, context=context)
|
2023-04-11 01:44:39 +00:00
|
|
|
return
|
2023-03-14 01:27:22 +00:00
|
|
|
except TelegramError as exc:
|
|
|
|
logger.error(self.SEND_MSG_ERROR_NOTICE, update.update_id, exc.message)
|
|
|
|
except Exception as exc:
|
|
|
|
logger.error(self.SEND_MSG_ERROR_NOTICE, update.update_id, repr(exc), exc_info=exc)
|
|
|
|
|
|
|
|
def create_notice_task(self, update: object, context: CallbackContext, content: str):
|
|
|
|
context.application.create_task(self.notice_user(update, context, content), update)
|
|
|
|
|
|
|
|
@error_handler()
|
|
|
|
async def process_genshin_exception(self, update: object, context: CallbackContext):
|
|
|
|
if not isinstance(context.error, GenshinException) or not isinstance(update, Update):
|
2022-11-24 15:02:27 +00:00
|
|
|
return
|
2023-03-14 01:27:22 +00:00
|
|
|
exc = context.error
|
|
|
|
notice: Optional[str] = None
|
|
|
|
if isinstance(exc, TooManyRequests):
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "Cookie 无效,请尝试重新绑定"
|
|
|
|
elif isinstance(exc, InvalidCookies):
|
|
|
|
if exc.retcode in (10001, -100):
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "Cookie 无效,请尝试重新绑定"
|
|
|
|
elif exc.retcode == 10103:
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "Cookie 有效,但没有绑定到游戏帐户,请尝试登录通行证,在账号管理里面选择账号游戏信息,将原神设置为默认角色。"
|
|
|
|
else:
|
|
|
|
logger.error("未知Cookie错误", exc_info=exc)
|
|
|
|
notice = self.ERROR_MSG_PREFIX + f"Cookie 无效 错误信息为 {exc.original} 请尝试重新绑定"
|
|
|
|
elif isinstance(exc, DataNotPublic):
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "查询的用户数据未公开"
|
|
|
|
else:
|
|
|
|
if exc.retcode == -130:
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "未设置默认角色,请尝试重新绑定"
|
|
|
|
elif exc.retcode == 1034:
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "服务器检测到该账号可能存在异常,请求被拒绝,请尝试通过验证"
|
|
|
|
elif exc.retcode == -500001:
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "网络出小差了,请稍后重试~"
|
|
|
|
elif exc.retcode == -1:
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "系统发生错误,请稍后重试~"
|
|
|
|
elif exc.retcode == -10001: # 参数异常 不应该抛出异常 进入下一步处理
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
logger.error("GenshinException", exc_info=exc)
|
|
|
|
notice = (
|
|
|
|
self.ERROR_MSG_PREFIX + f"获取账号信息发生错误 错误信息为 {exc.original if exc.original else exc.retcode} ~ 请稍后再试"
|
|
|
|
)
|
|
|
|
if notice:
|
|
|
|
self.create_notice_task(update, context, notice)
|
|
|
|
raise ApplicationHandlerStop
|
|
|
|
|
|
|
|
@error_handler()
|
|
|
|
async def process_telegram_exception(self, update: object, context: CallbackContext):
|
|
|
|
if not isinstance(context.error, TelegramError) or not isinstance(update, Update):
|
|
|
|
return
|
|
|
|
notice: Optional[str] = None
|
2022-11-25 08:41:50 +00:00
|
|
|
if isinstance(context.error, TimedOut):
|
2023-03-16 15:18:39 +00:00
|
|
|
# notice = self.ERROR_MSG_PREFIX + " 连接 telegram 服务器超时"
|
|
|
|
logger.error("连接 telegram 服务器超时 [%s]", repr(context.error))
|
2023-03-17 00:31:31 +00:00
|
|
|
raise ApplicationHandlerStop
|
2023-03-27 02:55:12 +00:00
|
|
|
if isinstance(context.error, BadRequest):
|
2023-03-14 01:27:22 +00:00
|
|
|
if "Replied message not found" in context.error.message:
|
|
|
|
notice = "气死我了!怎么有人喜欢发一个命令就秒删了!"
|
|
|
|
elif "Message is not modified" in context.error.message:
|
|
|
|
logger.warning("编辑消息异常")
|
|
|
|
raise ApplicationHandlerStop
|
|
|
|
elif "Not enough rights" in context.error.message:
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "权限不足,请检查对应权限是否开启"
|
2023-03-15 08:20:01 +00:00
|
|
|
elif "Wrong file identifier specified" in context.error.message:
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "文件标识符未找到 ~ 请稍后再试"
|
2023-03-14 01:27:22 +00:00
|
|
|
else:
|
|
|
|
logger.error("python-telegram-bot 请求错误", exc_info=context.error)
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "telegram-bot-api请求错误 ~ 请稍后再试"
|
|
|
|
elif isinstance(context.error, Forbidden):
|
|
|
|
logger.error("python-telegram-bot 返回 Forbidden")
|
|
|
|
notice = self.ERROR_MSG_PREFIX + "telegram-bot-api请求错误 ~ 请稍后再试"
|
|
|
|
if notice:
|
|
|
|
self.create_notice_task(update, context, notice)
|
|
|
|
raise ApplicationHandlerStop
|
|
|
|
|
|
|
|
@error_handler()
|
|
|
|
async def process_telegram_update_exception(self, update: object, context: CallbackContext):
|
|
|
|
if update is None and isinstance(context.error, NetworkError):
|
|
|
|
logger.error("python-telegram-bot NetworkError : %s", context.error.message)
|
|
|
|
raise ApplicationHandlerStop
|
|
|
|
|
|
|
|
@error_handler()
|
|
|
|
async def process_apihelper_exception(self, update: object, context: CallbackContext):
|
|
|
|
if not isinstance(context.error, APIHelperException) or not isinstance(update, Update):
|
|
|
|
return
|
|
|
|
exc = context.error
|
|
|
|
notice: Optional[str] = None
|
|
|
|
if isinstance(exc, APIHelperTimedOut):
|
2023-03-16 15:18:39 +00:00
|
|
|
notice = self.ERROR_MSG_PREFIX + " 服务器熟啦 ~ 请稍后再试"
|
2023-03-14 01:27:22 +00:00
|
|
|
elif isinstance(exc, ReturnCodeError):
|
|
|
|
notice = self.ERROR_MSG_PREFIX + f"API请求错误 错误信息为 {exc.message if exc.message else exc.code} ~ 请稍后再试"
|
|
|
|
elif isinstance(exc, ResponseException):
|
|
|
|
notice = self.ERROR_MSG_PREFIX + f"API请求错误 错误信息为 {exc.message if exc.message else exc.code} ~ 请稍后再试"
|
|
|
|
if notice:
|
|
|
|
self.create_notice_task(update, context, notice)
|
|
|
|
raise ApplicationHandlerStop
|
|
|
|
|
|
|
|
@error_handler()
|
|
|
|
async def process_httpx_exception(self, update: object, context: CallbackContext):
|
|
|
|
if not isinstance(context.error, HTTPError) or not isinstance(update, Update):
|
|
|
|
return
|
|
|
|
exc = context.error
|
|
|
|
notice: Optional[str] = None
|
2023-03-14 02:40:27 +00:00
|
|
|
if isinstance(exc, TimeoutException):
|
2023-03-16 15:18:39 +00:00
|
|
|
notice = self.ERROR_MSG_PREFIX + " 服务器熟啦 ~ 请稍后再试"
|
2023-03-14 02:40:27 +00:00
|
|
|
logger.warning("Httpx exception[%s]", str(exc))
|
2023-03-14 01:27:22 +00:00
|
|
|
if notice:
|
|
|
|
self.create_notice_task(update, context, notice)
|
|
|
|
raise ApplicationHandlerStop
|
|
|
|
|
|
|
|
@error_handler()
|
|
|
|
async def process_aiohttp_exception(self, update: object, context: CallbackContext):
|
|
|
|
if not isinstance(context.error, ClientError) or not isinstance(update, Update):
|
2022-11-24 15:02:27 +00:00
|
|
|
return
|
2023-03-14 01:27:22 +00:00
|
|
|
exc = context.error
|
|
|
|
notice: Optional[str] = None
|
|
|
|
if isinstance(exc, AioHttpTimeoutException):
|
2023-03-16 15:18:39 +00:00
|
|
|
notice = self.ERROR_MSG_PREFIX + " 服务器熟啦 ~ 请稍后再试"
|
2023-03-14 01:27:22 +00:00
|
|
|
elif isinstance(exc, ClientConnectorError):
|
2023-03-16 15:18:39 +00:00
|
|
|
notice = self.ERROR_MSG_PREFIX + " 连接服务器异常"
|
2023-03-14 01:27:22 +00:00
|
|
|
if notice:
|
|
|
|
self.create_notice_task(update, context, notice)
|
|
|
|
raise ApplicationHandlerStop
|
|
|
|
|
|
|
|
@error_handler(block=False)
|
|
|
|
async def process_z_error(self, update: object, context: CallbackContext) -> None:
|
|
|
|
# 必须 `process_` 加上 `z` 保证该函数最后一个注册
|
|
|
|
"""记录错误并发送消息通知开发人员。
|
|
|
|
logger the error and send a telegram message to notify the developer."""
|
2022-11-24 15:02:27 +00:00
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
logger.error("处理函数时发生异常")
|
2022-09-10 04:02:19 +00:00
|
|
|
logger.exception(context.error, exc_info=(type(context.error), context.error, context.error.__traceback__))
|
2022-07-26 10:07:31 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
if not self.notice_chat_id:
|
2022-07-26 10:07:31 +00:00
|
|
|
return
|
2022-09-08 01:08:37 +00:00
|
|
|
|
|
|
|
tb_list = traceback.format_exception(None, context.error, context.error.__traceback__)
|
2022-09-11 13:15:01 +00:00
|
|
|
tb_string = "".join(tb_list)
|
2022-09-08 01:08:37 +00:00
|
|
|
|
|
|
|
update_str = update.to_dict() if isinstance(update, Update) else str(update)
|
2022-09-11 13:15:01 +00:00
|
|
|
|
|
|
|
error_text = (
|
|
|
|
f"-----Exception while handling an update-----\n"
|
2023-01-07 08:00:32 +00:00
|
|
|
f"update = {jsonlib.dumps(update_str, indent=2, ensure_ascii=False)}\n"
|
2022-09-11 13:15:01 +00:00
|
|
|
f"context.chat_data = {str(context.chat_data)}\n"
|
|
|
|
f"context.user_data = {str(context.user_data)}\n"
|
|
|
|
"\n"
|
|
|
|
"-----Traceback info-----\n"
|
|
|
|
f"{tb_string}"
|
2022-09-08 01:08:37 +00:00
|
|
|
)
|
2022-09-11 13:15:01 +00:00
|
|
|
file_name = f"error_{update.update_id if isinstance(update, Update) else int(time.time())}.txt"
|
2023-03-14 01:27:22 +00:00
|
|
|
log_file = os.path.join(self.report_dir, file_name)
|
2022-09-11 13:15:01 +00:00
|
|
|
try:
|
2022-10-10 11:07:28 +00:00
|
|
|
async with aiofiles.open(log_file, mode="w+", encoding="utf-8") as f:
|
2022-09-11 13:15:01 +00:00
|
|
|
await f.write(error_text)
|
2022-10-11 06:45:07 +00:00
|
|
|
except Exception as exc: # pylint: disable=W0703
|
2022-09-11 13:15:01 +00:00
|
|
|
logger.error("保存日记失败")
|
|
|
|
logger.exception(exc)
|
2022-09-08 01:08:37 +00:00
|
|
|
try:
|
2022-10-10 11:07:28 +00:00
|
|
|
if "make sure that only one bot instance is running" in tb_string:
|
2022-09-08 01:08:37 +00:00
|
|
|
logger.error("其他机器人在运行,请停止!")
|
|
|
|
return
|
2022-10-10 11:07:28 +00:00
|
|
|
await context.bot.send_document(
|
2023-03-14 01:27:22 +00:00
|
|
|
chat_id=self.notice_chat_id,
|
2022-10-10 11:07:28 +00:00
|
|
|
document=open(log_file, "rb"),
|
|
|
|
caption=f'Error: "{context.error.__class__.__name__}"',
|
|
|
|
)
|
2023-03-14 01:27:22 +00:00
|
|
|
except NetworkError as exc:
|
2022-09-11 13:15:01 +00:00
|
|
|
logger.error("发送日记失败")
|
|
|
|
logger.exception(exc)
|
|
|
|
except FileNotFoundError:
|
|
|
|
logger.error("发送日记失败 文件不存在")
|
2022-09-08 01:08:37 +00:00
|
|
|
effective_user = update.effective_user
|
|
|
|
effective_message = update.effective_message
|
|
|
|
try:
|
|
|
|
if effective_message is not None:
|
|
|
|
chat = effective_message.chat
|
2022-10-10 11:07:28 +00:00
|
|
|
logger.info(
|
2023-03-14 01:27:22 +00:00
|
|
|
"尝试通知用户 %s[%s] 在 %s[%s] 的 update_id[%s] 错误信息",
|
|
|
|
effective_user.full_name,
|
|
|
|
effective_user.id,
|
|
|
|
chat.full_name,
|
|
|
|
chat.id,
|
|
|
|
update.update_id,
|
2022-10-10 11:07:28 +00:00
|
|
|
)
|
2022-09-08 01:08:37 +00:00
|
|
|
text = "出错了呜呜呜 ~ 派蒙这边发生了点问题无法处理!"
|
2022-10-10 11:07:28 +00:00
|
|
|
await context.bot.send_message(
|
|
|
|
effective_message.chat_id, text, reply_markup=ReplyKeyboardRemove(), parse_mode=ParseMode.HTML
|
|
|
|
)
|
2023-03-14 01:27:22 +00:00
|
|
|
except NetworkError as exc:
|
|
|
|
logger.error("发送 update_id[%s] 错误信息失败 错误信息为 %s", update.update_id, exc.message)
|
|
|
|
if self.pb_client.enabled:
|
2022-11-21 06:57:12 +00:00
|
|
|
logger.info("正在上传日记到 pb")
|
|
|
|
try:
|
2023-03-14 01:27:22 +00:00
|
|
|
pb_url = await self.pb_client.create_pb(error_text)
|
2022-11-21 06:57:12 +00:00
|
|
|
if pb_url:
|
|
|
|
logger.success("上传日记到 pb 成功")
|
|
|
|
await context.bot.send_message(
|
2023-03-14 01:27:22 +00:00
|
|
|
chat_id=self.notice_chat_id,
|
2022-11-21 06:57:12 +00:00
|
|
|
text=f"错误信息已上传至 <a href='{pb_url}'>fars</a> 请查看",
|
|
|
|
parse_mode=ParseMode.HTML,
|
|
|
|
)
|
|
|
|
except PbClientException as exc:
|
|
|
|
logger.warning("上传错误信息至 fars 失败", exc_info=exc)
|
|
|
|
except Exception as exc:
|
|
|
|
logger.error("上传错误信息至 fars 失败")
|
|
|
|
logger.exception(exc)
|
2023-03-14 01:27:22 +00:00
|
|
|
if self.sentry.enabled:
|
2022-11-21 06:57:12 +00:00
|
|
|
logger.info("正在上传日记到 sentry")
|
|
|
|
try:
|
2023-03-14 01:27:22 +00:00
|
|
|
self.sentry.report_error(update, (type(context.error), context.error, context.error.__traceback__))
|
2022-11-21 06:57:12 +00:00
|
|
|
logger.success("上传日记到 sentry 成功")
|
|
|
|
except SentryClientException as exc:
|
|
|
|
logger.warning("上传错误信息至 sentry 失败", exc_info=exc)
|
|
|
|
except Exception as exc:
|
|
|
|
logger.error("上传错误信息至 sentry 失败")
|
|
|
|
logger.exception(exc)
|