bot
commited on
Commit
·
44537f3
1
Parent(s):
fae2328
添加查看存储使用信息
Browse files
main.py
CHANGED
@@ -94,6 +94,24 @@ async def verify_token(
|
|
94 |
)
|
95 |
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
app = FastAPI()
|
98 |
|
99 |
|
@@ -126,13 +144,55 @@ TG_BASE_URL = "https://tg.alist.dpdns.org/bot"
|
|
126 |
# 定义命令处理函数
|
127 |
async def start(update: Update, context):
|
128 |
commands = (
|
129 |
-
"欢迎使用我的机器人!\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
)
|
131 |
await update.message.reply_text(commands)
|
132 |
|
133 |
|
134 |
async def help(update: Update, context):
|
135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
136 |
|
137 |
|
138 |
@app.on_event("startup")
|
@@ -177,6 +237,7 @@ async def init_client():
|
|
177 |
# 将命令处理函数添加到 dispatcher
|
178 |
TG_BOT_APPLICATION.add_handler(CommandHandler("start", start))
|
179 |
TG_BOT_APPLICATION.add_handler(CommandHandler("help", help))
|
|
|
180 |
|
181 |
|
182 |
# FastAPI 路由:接收来自 Telegram 的 Webhook 回调
|
|
|
94 |
)
|
95 |
|
96 |
|
97 |
+
def format_bytes(size: int) -> str:
|
98 |
+
# 预设单位
|
99 |
+
units = ["B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"]
|
100 |
+
|
101 |
+
# 确保字节数是正数
|
102 |
+
if size < 0:
|
103 |
+
raise ValueError("字节大小不能为负数")
|
104 |
+
|
105 |
+
# 选择合适的单位
|
106 |
+
unit_index = 0
|
107 |
+
while size >= 1024 and unit_index < len(units) - 1:
|
108 |
+
size /= 1024.0
|
109 |
+
unit_index += 1
|
110 |
+
|
111 |
+
# 格式化输出,保留两位小数
|
112 |
+
return f"{size:.2f} {units[unit_index]}"
|
113 |
+
|
114 |
+
|
115 |
app = FastAPI()
|
116 |
|
117 |
|
|
|
144 |
# 定义命令处理函数
|
145 |
async def start(update: Update, context):
|
146 |
commands = (
|
147 |
+
"欢迎使用我的机器人!\n\n"
|
148 |
+
"以下是您可以使用的命令:\n"
|
149 |
+
"/tasks - 查看下载任务\n"
|
150 |
+
"/files - 查看文件列表\n"
|
151 |
+
"/quota - 查看存储空间\n"
|
152 |
+
"/emptytrash - 清空回收站\n"
|
153 |
+
"/help - 获取帮助信息\n"
|
154 |
)
|
155 |
await update.message.reply_text(commands)
|
156 |
|
157 |
|
158 |
async def help(update: Update, context):
|
159 |
+
commands = (
|
160 |
+
"欢迎使用我的机器人!\n\n"
|
161 |
+
"以下是您可以使用的命令:\n"
|
162 |
+
"/tasks - 查看下载任务\n"
|
163 |
+
"/files - 查看文件列表\n"
|
164 |
+
"/quota - 查看存储空间\n"
|
165 |
+
"/emptytrash - 清空回收站\n"
|
166 |
+
"/help - 获取帮助信息\n"
|
167 |
+
)
|
168 |
+
await update.message.reply_text(commands)
|
169 |
+
|
170 |
+
|
171 |
+
async def quota(update: Update, context):
|
172 |
+
"""
|
173 |
+
返回信息
|
174 |
+
{
|
175 |
+
"kind": "drive#about",
|
176 |
+
"quota": {
|
177 |
+
"kind": "drive#quota",
|
178 |
+
"limit": "72057604737418240",
|
179 |
+
"usage": "18700975438",
|
180 |
+
"usage_in_trash": "0",
|
181 |
+
"play_times_limit": "2",
|
182 |
+
"play_times_usage": "0",
|
183 |
+
"is_unlimited": true
|
184 |
+
},
|
185 |
+
"expires_at": "2026-04-08T21:47:59.000+08:00",
|
186 |
+
"quotas": {}
|
187 |
+
}
|
188 |
+
"""
|
189 |
+
quota_info = await THUNDERX_CLIENT.get_quota_info()
|
190 |
+
if quota_info["usage"] is None:
|
191 |
+
await update.message.reply_text("未找到使用信息,请稍后再试!")
|
192 |
+
else:
|
193 |
+
await update.message.reply_text(
|
194 |
+
f"使用信息:\n{format_bytes(quota_info['limit'])}/{format_bytes(quota_info['usage'])}\n到期时间:\n{quota_info['expires_at']}"
|
195 |
+
)
|
196 |
|
197 |
|
198 |
@app.on_event("startup")
|
|
|
237 |
# 将命令处理函数添加到 dispatcher
|
238 |
TG_BOT_APPLICATION.add_handler(CommandHandler("start", start))
|
239 |
TG_BOT_APPLICATION.add_handler(CommandHandler("help", help))
|
240 |
+
TG_BOT_APPLICATION.add_handler(CommandHandler("quota", quota))
|
241 |
|
242 |
|
243 |
# FastAPI 路由:接收来自 Telegram 的 Webhook 回调
|