bot commited on
Commit ·
c58486c
1
Parent(s): 0affc64
Test
Browse files
main.py
CHANGED
|
@@ -233,6 +233,112 @@ async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
|
|
| 233 |
#################### 文件操作 #############################
|
| 234 |
|
| 235 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 236 |
#################### 离线任务处理 ##########################
|
| 237 |
# 确认操作的回调
|
| 238 |
async def handle_task_confirmation(update: Update, context: CallbackContext):
|
|
@@ -427,11 +533,27 @@ async def init_client():
|
|
| 427 |
TG_BOT_APPLICATION.add_handler(
|
| 428 |
CallbackQueryHandler(handle_task_confirmation, pattern="^confirm_task")
|
| 429 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 430 |
TG_BOT_APPLICATION.add_handler(CommandHandler("start", start))
|
| 431 |
TG_BOT_APPLICATION.add_handler(CommandHandler("help", help))
|
| 432 |
TG_BOT_APPLICATION.add_handler(CommandHandler("quota", quota))
|
| 433 |
TG_BOT_APPLICATION.add_handler(CommandHandler("emptytrash", tg_emptytrash))
|
| 434 |
TG_BOT_APPLICATION.add_handler(CommandHandler("tasks", tg_show_task))
|
|
|
|
| 435 |
# Message 消息处理相关的命令!
|
| 436 |
TG_BOT_APPLICATION.add_handler(MessageHandler(filters.TEXT, handle_message))
|
| 437 |
|
|
|
|
| 233 |
#################### 文件操作 #############################
|
| 234 |
|
| 235 |
|
| 236 |
+
async def tg_show_files(update: Update, context: CallbackContext):
|
| 237 |
+
if "file_id" not in context.user_data:
|
| 238 |
+
context.user_data["file_id"] = ""
|
| 239 |
+
files = await THUNDERX_CLIENT.file_list(
|
| 240 |
+
100, context.user_data["file_id"], "", {}, {}
|
| 241 |
+
)
|
| 242 |
+
keyboard = []
|
| 243 |
+
|
| 244 |
+
if files["files"] is None:
|
| 245 |
+
await update.message.reply_text("❌未找到文件!!")
|
| 246 |
+
else:
|
| 247 |
+
# 为每个文件创建按钮和操作选项
|
| 248 |
+
for file in files["files"]:
|
| 249 |
+
if file["kind"].lower() == "drive#folder":
|
| 250 |
+
keyboard.append(
|
| 251 |
+
[
|
| 252 |
+
InlineKeyboardButton(
|
| 253 |
+
f"查看📁: {file['name']}",
|
| 254 |
+
callback_data=f"list_file:{file['id']}",
|
| 255 |
+
),
|
| 256 |
+
InlineKeyboardButton(
|
| 257 |
+
f"删除",
|
| 258 |
+
callback_data=f"delete_file:{file['id']}",
|
| 259 |
+
),
|
| 260 |
+
]
|
| 261 |
+
)
|
| 262 |
+
else:
|
| 263 |
+
keyboard.append(
|
| 264 |
+
[
|
| 265 |
+
InlineKeyboardButton(
|
| 266 |
+
f"下载📄: {file['name']}",
|
| 267 |
+
callback_data=f"download_file:{file['id']}",
|
| 268 |
+
),
|
| 269 |
+
InlineKeyboardButton(
|
| 270 |
+
f"删除",
|
| 271 |
+
callback_data=f"delete_file:{file['id']}",
|
| 272 |
+
),
|
| 273 |
+
]
|
| 274 |
+
)
|
| 275 |
+
|
| 276 |
+
reply_markup = InlineKeyboardMarkup(keyboard)
|
| 277 |
+
await update.message.reply_text(f"📋文件列表:", reply_markup=reply_markup)
|
| 278 |
+
|
| 279 |
+
|
| 280 |
+
async def handle_file_confirmation(update: Update, context: CallbackContext):
|
| 281 |
+
query = update.callback_query
|
| 282 |
+
await query.answer()
|
| 283 |
+
|
| 284 |
+
# 获取确认操作的类型和文件 ID
|
| 285 |
+
action, file_id = query.data.split(":")[0], query.data.split(":")[1]
|
| 286 |
+
|
| 287 |
+
if action == "confirm_file_delete_file":
|
| 288 |
+
await THUNDERX_CLIENT.delete_forever([file_id])
|
| 289 |
+
await query.edit_message_text(f"✅文件 {file_id} 已删除。")
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
async def handle_file_cancel(update: Update, context: CallbackContext):
|
| 293 |
+
query = update.callback_query
|
| 294 |
+
await query.answer()
|
| 295 |
+
# 获取取消操作的类型和文件 ID
|
| 296 |
+
action, file_id = query.data.split(":")[0], query.data.split(":")[1]
|
| 297 |
+
# 返回文件夹列表
|
| 298 |
+
await query.edit_message_text(f"操作已取消")
|
| 299 |
+
|
| 300 |
+
|
| 301 |
+
# 处理任务操作的回调
|
| 302 |
+
async def handle_file_operation(update: Update, context: CallbackContext):
|
| 303 |
+
query = update.callback_query
|
| 304 |
+
await query.answer()
|
| 305 |
+
|
| 306 |
+
# 获取操作类型和文件 ID
|
| 307 |
+
action, file_id = query.data.split(":")
|
| 308 |
+
|
| 309 |
+
# 需要确认的操作
|
| 310 |
+
if action in ["delete_file"]:
|
| 311 |
+
# 生成确认消息
|
| 312 |
+
keyboard = [
|
| 313 |
+
[
|
| 314 |
+
InlineKeyboardButton(
|
| 315 |
+
"确认", callback_data=f"confirm_file_{action}:{file_id}"
|
| 316 |
+
)
|
| 317 |
+
],
|
| 318 |
+
[
|
| 319 |
+
InlineKeyboardButton(
|
| 320 |
+
"取消", callback_data=f"cancel_file_{action}:{file_id}"
|
| 321 |
+
)
|
| 322 |
+
],
|
| 323 |
+
]
|
| 324 |
+
reply_markup = InlineKeyboardMarkup(keyboard)
|
| 325 |
+
await query.edit_message_text(
|
| 326 |
+
f"你确定要{action}文件 {file_id} 吗?", reply_markup=reply_markup
|
| 327 |
+
)
|
| 328 |
+
else:
|
| 329 |
+
# 不需要确认的操作,直接处理
|
| 330 |
+
await perform_file_action(update, context, action, file_id)
|
| 331 |
+
|
| 332 |
+
|
| 333 |
+
async def perform_file_action(
|
| 334 |
+
update: Update, context: CallbackContext, action: str, file_id: str
|
| 335 |
+
):
|
| 336 |
+
if action == "list_file":
|
| 337 |
+
context.user_data["file_id"] = file_id
|
| 338 |
+
await tg_show_files(update, context)
|
| 339 |
+
# await update.callback_query.edit_message_text(f"你选择了取消任务:{file_id}")
|
| 340 |
+
|
| 341 |
+
|
| 342 |
#################### 离线任务处理 ##########################
|
| 343 |
# 确认操作的回调
|
| 344 |
async def handle_task_confirmation(update: Update, context: CallbackContext):
|
|
|
|
| 533 |
TG_BOT_APPLICATION.add_handler(
|
| 534 |
CallbackQueryHandler(handle_task_confirmation, pattern="^confirm_task")
|
| 535 |
)
|
| 536 |
+
|
| 537 |
+
########## 文件操作 ###############
|
| 538 |
+
|
| 539 |
+
TG_BOT_APPLICATION.add_handler(
|
| 540 |
+
CallbackQueryHandler(handle_file_operation, pattern="^delete_file:")
|
| 541 |
+
)
|
| 542 |
+
# 处理取消任务操作
|
| 543 |
+
TG_BOT_APPLICATION.add_handler(
|
| 544 |
+
CallbackQueryHandler(handle_file_cancel, pattern="^cancel_file")
|
| 545 |
+
)
|
| 546 |
+
# 处理确认操作(确认删除、复制等)
|
| 547 |
+
TG_BOT_APPLICATION.add_handler(
|
| 548 |
+
CallbackQueryHandler(handle_file_confirmation, pattern="^confirm_file")
|
| 549 |
+
)
|
| 550 |
+
|
| 551 |
TG_BOT_APPLICATION.add_handler(CommandHandler("start", start))
|
| 552 |
TG_BOT_APPLICATION.add_handler(CommandHandler("help", help))
|
| 553 |
TG_BOT_APPLICATION.add_handler(CommandHandler("quota", quota))
|
| 554 |
TG_BOT_APPLICATION.add_handler(CommandHandler("emptytrash", tg_emptytrash))
|
| 555 |
TG_BOT_APPLICATION.add_handler(CommandHandler("tasks", tg_show_task))
|
| 556 |
+
TG_BOT_APPLICATION.add_handler(CommandHandler("files", tg_show_files))
|
| 557 |
# Message 消息处理相关的命令!
|
| 558 |
TG_BOT_APPLICATION.add_handler(MessageHandler(filters.TEXT, handle_message))
|
| 559 |
|