2022-04-14 07:18:45 +00:00
|
|
|
|
import html
|
|
|
|
|
import traceback
|
2022-06-28 12:23:03 +00:00
|
|
|
|
from functools import wraps
|
2022-06-02 17:37:34 +00:00
|
|
|
|
from typing import Callable, Optional
|
|
|
|
|
|
2022-04-14 07:18:45 +00:00
|
|
|
|
import ujson
|
2022-06-02 17:37:34 +00:00
|
|
|
|
from aiohttp import ClientConnectorError
|
2022-06-03 07:54:56 +00:00
|
|
|
|
from genshin import InvalidCookies, GenshinException, TooManyRequests
|
2022-06-02 17:37:34 +00:00
|
|
|
|
from httpx import ConnectTimeout
|
2022-07-01 07:22:40 +00:00
|
|
|
|
from telegram import Update, ReplyKeyboardRemove, Message
|
2022-04-14 07:18:45 +00:00
|
|
|
|
from telegram.constants import ParseMode
|
2022-06-28 15:44:10 +00:00
|
|
|
|
from telegram.error import BadRequest, TimedOut, Forbidden
|
2022-06-03 07:58:51 +00:00
|
|
|
|
from telegram.ext import CallbackContext, ConversationHandler
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
|
|
from config import config
|
2022-06-09 07:12:06 +00:00
|
|
|
|
from logger import Log
|
2022-04-14 07:18:45 +00:00
|
|
|
|
|
|
|
|
|
try:
|
2022-05-28 07:55:33 +00:00
|
|
|
|
notice_chat_id = config.TELEGRAM["notice"]["ERROR"]["chat_id"]
|
2022-05-19 14:04:27 +00:00
|
|
|
|
except KeyError as error:
|
|
|
|
|
Log.warning("错误通知Chat_id获取失败或未配置,BOT发生致命错误时不会收到通知 错误信息为\n", error)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
notice_chat_id = None
|
|
|
|
|
|
|
|
|
|
|
2022-06-03 07:54:56 +00:00
|
|
|
|
async def send_user_notification(update: Update, _: CallbackContext, text: str):
|
2022-06-10 08:47:32 +00:00
|
|
|
|
effective_user = update.effective_user
|
2022-07-01 07:22:40 +00:00
|
|
|
|
message: Optional[Message] = None
|
|
|
|
|
if update.callback_query is not None:
|
2022-06-03 07:54:56 +00:00
|
|
|
|
message = update.callback_query.message
|
2022-07-01 07:22:40 +00:00
|
|
|
|
if update.message is not None:
|
|
|
|
|
message = update.message
|
|
|
|
|
if update.edited_message is not None:
|
|
|
|
|
message = update.edited_message
|
|
|
|
|
if message is None:
|
|
|
|
|
update_str = update.to_dict() if isinstance(update, Update) else str(update)
|
|
|
|
|
Log.warning("错误的消息类型\n" + ujson.dumps(update_str, indent=2, ensure_ascii=False))
|
|
|
|
|
return
|
2022-06-10 08:47:32 +00:00
|
|
|
|
chat = message.chat
|
|
|
|
|
Log.info(f"尝试通知用户 {effective_user.full_name}[{effective_user.id}] "
|
|
|
|
|
f"在 {chat.full_name}[{chat.id}]"
|
|
|
|
|
f"的 错误信息[{text}]")
|
2022-06-02 17:37:34 +00:00
|
|
|
|
try:
|
2022-06-03 07:54:56 +00:00
|
|
|
|
await message.reply_text(text, reply_markup=ReplyKeyboardRemove(), allow_sending_without_reply=True)
|
2022-06-02 17:37:34 +00:00
|
|
|
|
except BadRequest as exc:
|
2022-06-03 07:54:56 +00:00
|
|
|
|
Log.error(f"发送 update_id[{update.update_id}] 错误信息失败 错误信息为 {str(exc)}")
|
2022-06-26 12:16:34 +00:00
|
|
|
|
except Forbidden as exc:
|
|
|
|
|
Log.error(f"发送 update_id[{update.update_id}] 错误信息失败 错误信息为 {str(exc)}")
|
|
|
|
|
except BaseException as exc:
|
|
|
|
|
Log.error(f"发送 update_id[{update.update_id}] 错误信息失败 错误信息为 {str(exc)}")
|
2022-06-02 17:37:34 +00:00
|
|
|
|
finally:
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def conversation_error_handler(func: Callable) -> Callable:
|
2022-06-28 12:23:03 +00:00
|
|
|
|
"""Conversation的错误处理修饰器
|
2022-06-28 15:44:10 +00:00
|
|
|
|
|
2022-06-09 12:52:59 +00:00
|
|
|
|
非常感谢 @Bibo-Joshi 提出的建议
|
|
|
|
|
"""
|
|
|
|
|
|
2022-06-28 12:23:03 +00:00
|
|
|
|
@wraps(func)
|
2022-06-02 17:37:34 +00:00
|
|
|
|
async def decorator(*args, **kwargs):
|
|
|
|
|
update: Optional[Update] = None
|
|
|
|
|
context: Optional[CallbackContext] = None
|
2022-06-26 12:16:34 +00:00
|
|
|
|
if len(args) == 3:
|
|
|
|
|
# self update context
|
|
|
|
|
_, update, context = args
|
|
|
|
|
elif len(args) == 2:
|
|
|
|
|
# update context
|
|
|
|
|
update, context = args
|
|
|
|
|
else:
|
2022-06-02 17:37:34 +00:00
|
|
|
|
return await func(*args, **kwargs)
|
|
|
|
|
try:
|
|
|
|
|
return await func(*args, **kwargs)
|
2022-06-28 12:23:03 +00:00
|
|
|
|
except ClientConnectorError:
|
|
|
|
|
Log.error("aiohttp模块连接服务器ClientConnectorError")
|
2022-06-03 07:54:56 +00:00
|
|
|
|
await send_user_notification(update, context, "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ ")
|
|
|
|
|
return ConversationHandler.END
|
2022-06-28 12:23:03 +00:00
|
|
|
|
except ConnectTimeout:
|
|
|
|
|
Log.error("httpx模块连接服务器ConnectTimeout")
|
2022-06-03 07:54:56 +00:00
|
|
|
|
await send_user_notification(update, context, "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ ")
|
2022-06-07 13:33:05 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-06-28 12:23:03 +00:00
|
|
|
|
except TimedOut:
|
|
|
|
|
Log.error("python-telegram-TimedOut模块连接服务器TimedOut")
|
2022-06-07 13:33:05 +00:00
|
|
|
|
await send_user_notification(update, context, "出错了呜呜呜 ~ 服务器连接超时 服务器熟啦 ~ ")
|
2022-06-03 07:54:56 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-06-02 17:37:34 +00:00
|
|
|
|
except InvalidCookies as exc:
|
2022-06-10 08:47:32 +00:00
|
|
|
|
Log.warning("Cookie错误", exc)
|
2022-06-14 16:31:08 +00:00
|
|
|
|
if "10001" in str(exc):
|
|
|
|
|
await send_user_notification(update, context, "Cookies无效,请尝试重新绑定账户")
|
|
|
|
|
elif "10103" in str(exc):
|
2022-06-26 07:49:17 +00:00
|
|
|
|
await send_user_notification(update, context, "Cookie有效,但没有绑定到游戏帐户,请尝试重新绑定邮游戏账户")
|
2022-06-14 16:31:08 +00:00
|
|
|
|
else:
|
|
|
|
|
await send_user_notification(update, context, "Cookies无效,具体原因未知")
|
2022-06-03 07:54:56 +00:00
|
|
|
|
return ConversationHandler.END
|
|
|
|
|
except TooManyRequests as exc:
|
2022-06-10 08:51:23 +00:00
|
|
|
|
Log.warning("查询次数太多(操作频繁)", exc)
|
|
|
|
|
await send_user_notification(update, context, "当天查询次数已经超过30次,请次日再进行查询")
|
2022-06-03 07:54:56 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-06-02 17:37:34 +00:00
|
|
|
|
except GenshinException as exc:
|
2022-06-26 07:48:25 +00:00
|
|
|
|
if "[-130]" in str(exc):
|
|
|
|
|
await send_user_notification(update, context,
|
|
|
|
|
f"未设置默认角色,请尝试重新绑定默认角色")
|
|
|
|
|
return ConversationHandler.END
|
2022-06-10 08:47:32 +00:00
|
|
|
|
Log.warning("GenshinException", exc)
|
2022-06-03 07:54:56 +00:00
|
|
|
|
await send_user_notification(update, context,
|
2022-06-19 15:15:43 +00:00
|
|
|
|
f"获取账号信息发生错误,错误信息为 {str(exc)}")
|
2022-06-03 07:54:56 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-06-10 09:06:58 +00:00
|
|
|
|
except BadRequest as exc:
|
|
|
|
|
Log.warning("python-telegram-bot请求错误", exc)
|
2022-06-12 07:29:16 +00:00
|
|
|
|
await send_user_notification(update, context, f"telegram-bot-api请求错误 错误信息为 {str(exc)}")
|
2022-06-10 09:06:58 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-06-26 12:16:34 +00:00
|
|
|
|
except Forbidden as exc:
|
|
|
|
|
Log.warning("python-telegram-bot 返回 Forbidden", exc)
|
2022-07-01 07:22:40 +00:00
|
|
|
|
await send_user_notification(update, context, f"telegram-bot-api请求错误")
|
2022-06-26 12:16:34 +00:00
|
|
|
|
return ConversationHandler.END
|
2022-06-02 17:37:34 +00:00
|
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
|
|
2022-04-14 07:18:45 +00:00
|
|
|
|
async def error_handler(update: object, context: CallbackContext) -> None:
|
|
|
|
|
"""
|
|
|
|
|
记录错误并发送消息通知开发人员。
|
|
|
|
|
Log the error and send a telegram message to notify the developer.
|
|
|
|
|
"""
|
|
|
|
|
Log.error(msg="处理函数时发生异常:", exc_info=context.error)
|
|
|
|
|
|
|
|
|
|
if notice_chat_id is None:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
tb_list = traceback.format_exception(None, context.error, context.error.__traceback__)
|
|
|
|
|
tb_string = ''.join(tb_list)
|
|
|
|
|
|
|
|
|
|
update_str = update.to_dict() if isinstance(update, Update) else str(update)
|
2022-05-26 12:07:05 +00:00
|
|
|
|
text_1 = (
|
2022-04-14 07:18:45 +00:00
|
|
|
|
f'<b>处理函数时发生异常</b> \n'
|
|
|
|
|
f'Exception while handling an update \n'
|
|
|
|
|
f'<pre>update = {html.escape(ujson.dumps(update_str, indent=2, ensure_ascii=False))}'
|
|
|
|
|
'</pre>\n\n'
|
|
|
|
|
f'<pre>context.chat_data = {html.escape(str(context.chat_data))}</pre>\n\n'
|
|
|
|
|
f'<pre>context.user_data = {html.escape(str(context.user_data))}</pre>\n\n'
|
|
|
|
|
)
|
2022-05-26 12:07:05 +00:00
|
|
|
|
text_2 = (
|
2022-04-14 07:18:45 +00:00
|
|
|
|
f'<pre>{html.escape(tb_string)}</pre>'
|
|
|
|
|
)
|
|
|
|
|
try:
|
|
|
|
|
if 'make sure that only one bot instance is running' in tb_string:
|
|
|
|
|
Log.error("其他机器人在运行,请停止!")
|
|
|
|
|
return
|
2022-05-26 12:07:05 +00:00
|
|
|
|
await context.bot.send_message(notice_chat_id, text_1, parse_mode=ParseMode.HTML)
|
|
|
|
|
await context.bot.send_message(notice_chat_id, text_2, parse_mode=ParseMode.HTML)
|
2022-04-14 07:18:45 +00:00
|
|
|
|
except BadRequest as exc:
|
|
|
|
|
if 'too long' in str(exc):
|
2022-05-26 12:07:05 +00:00
|
|
|
|
text = (
|
2022-04-14 07:18:45 +00:00
|
|
|
|
f'<b>处理函数时发生异常,traceback太长导致无法发送,但已写入日志</b> \n'
|
|
|
|
|
f'<code>{html.escape(str(context.error))}</code>'
|
|
|
|
|
)
|
2022-05-19 14:04:27 +00:00
|
|
|
|
try:
|
2022-05-26 12:07:05 +00:00
|
|
|
|
await context.bot.send_message(notice_chat_id, text, parse_mode=ParseMode.HTML)
|
2022-05-19 14:04:27 +00:00
|
|
|
|
except BadRequest:
|
2022-05-26 12:07:05 +00:00
|
|
|
|
text = (
|
2022-05-28 07:27:22 +00:00
|
|
|
|
'<b>处理函数时发生异常,traceback太长导致无法发送,但已写入日志</b> \n')
|
2022-05-19 14:04:27 +00:00
|
|
|
|
try:
|
2022-05-26 12:07:05 +00:00
|
|
|
|
await context.bot.send_message(notice_chat_id, text, parse_mode=ParseMode.HTML)
|
2022-06-07 16:00:35 +00:00
|
|
|
|
except BadRequest as exc_1:
|
2022-06-10 08:47:32 +00:00
|
|
|
|
Log.error("处理函数时发生异常", exc_1)
|
2022-05-26 12:07:05 +00:00
|
|
|
|
effective_user = update.effective_user
|
2022-05-19 14:04:27 +00:00
|
|
|
|
try:
|
2022-07-01 07:22:40 +00:00
|
|
|
|
message: Optional[Message] = None
|
|
|
|
|
if update.callback_query is not None:
|
|
|
|
|
message = update.callback_query.message
|
|
|
|
|
if update.message is not None:
|
|
|
|
|
message = update.message
|
|
|
|
|
if update.edited_message is not None:
|
|
|
|
|
message = update.edited_message
|
2022-05-19 14:04:27 +00:00
|
|
|
|
if message is not None:
|
2022-05-26 12:07:05 +00:00
|
|
|
|
chat = message.chat
|
|
|
|
|
Log.info(f"尝试通知用户 {effective_user.full_name}[{effective_user.id}] "
|
2022-05-26 17:03:22 +00:00
|
|
|
|
f"在 {chat.full_name}[{chat.id}]"
|
2022-05-26 12:07:05 +00:00
|
|
|
|
f"的 update_id[{update.update_id}] 错误信息")
|
2022-05-26 11:51:06 +00:00
|
|
|
|
text = f"派蒙这边发生了点问题无法处理!\n" \
|
2022-05-26 12:07:05 +00:00
|
|
|
|
f"如果当前有对话请发送 /cancel 退出对话。\n" \
|
2022-05-26 13:48:00 +00:00
|
|
|
|
f"错误信息为 <code>{html.escape(str(context.error))}</code>"
|
2022-05-26 12:07:05 +00:00
|
|
|
|
await context.bot.send_message(message.chat_id, text, reply_markup=ReplyKeyboardRemove(),
|
|
|
|
|
parse_mode=ParseMode.HTML)
|
|
|
|
|
except BadRequest as exc:
|
|
|
|
|
Log.error(f"发送 update_id[{update.update_id}] 错误信息失败 错误信息为 {str(exc)}")
|
2022-05-19 14:04:27 +00:00
|
|
|
|
pass
|