mirror of
https://github.com/PaiGramTeam/PamGram.git
synced 2024-11-17 20:33:41 +00:00
059bcd5e70
* 🔧 使用 dotenv 重构 config
默认配置从 config.json 移动到 config.py 中。如果要覆盖默认配置,在根目录创建
.env 文件按照 .env.example 的例子编辑。
这个方案的优点是:
* 支持写注释
* 以后如果新增配置项,如果用默认值就可以,不需要修改 .env 文件
* 如果通过 serverless、docker 或者 k8s 部署,方便不用修改文件,直接注入环境变量
修改配置
53 lines
2.1 KiB
Python
53 lines
2.1 KiB
Python
from telegram import Update
|
||
from telegram.constants import ChatAction
|
||
from telegram.error import BadRequest
|
||
from telegram.ext import CommandHandler, CallbackContext
|
||
|
||
from config import config
|
||
from core.template.services import TemplateService
|
||
from logger import Log
|
||
from utils.decorators.error import error_callable
|
||
from utils.decorators.restricts import restricts
|
||
from utils.plugins.manager import listener_plugins_class
|
||
from utils.service.inject import inject
|
||
|
||
|
||
@listener_plugins_class()
|
||
class Help:
|
||
"""帮助菜单"""
|
||
|
||
@inject
|
||
def __init__(self, template_service: TemplateService):
|
||
self.template_service = template_service
|
||
self.help_png = None
|
||
self.file_id = None
|
||
|
||
@classmethod
|
||
def create_handlers(cls) -> list:
|
||
_help = cls()
|
||
return [
|
||
CommandHandler("help", _help.command_start, block=False),
|
||
]
|
||
|
||
@error_callable
|
||
@restricts()
|
||
async def command_start(self, update: Update, _: CallbackContext) -> None:
|
||
message = update.message
|
||
user = update.effective_user
|
||
Log.info(f"用户 {user.full_name}[{user.id}] 发出help命令")
|
||
if self.file_id is None or config.debug:
|
||
await message.reply_chat_action(ChatAction.TYPING)
|
||
help_png = await self.template_service.render('bot/help', "help.html", {}, {"width": 768, "height": 768})
|
||
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
||
reply_photo = await message.reply_photo(help_png, filename="help.png", allow_sending_without_reply=True)
|
||
photo = reply_photo.photo[0]
|
||
self.file_id = photo.file_id
|
||
else:
|
||
try:
|
||
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
||
await message.reply_photo(self.file_id, allow_sending_without_reply=True)
|
||
except BadRequest as error:
|
||
self.file_id = None
|
||
Log.error("发送图片失败,尝试清空已经保存的file_id,错误信息为", error)
|
||
await message.reply_text("发送图片失败", allow_sending_without_reply=True)
|