-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
executable file
·98 lines (78 loc) · 4.87 KB
/
bot.py
File metadata and controls
executable file
·98 lines (78 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import os
import asyncio
from main import download_video, convert_to_mp3, transcribe_audio, get_summary
from telegram import Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import ApplicationBuilder, ContextTypes, CommandHandler, CallbackQueryHandler, MessageHandler, filters
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
print(f"用户 {update.effective_user.id} 请求开始处理")
await context.bot.send_message(chat_id=update.effective_chat.id, text="请发送YouTube视频链接")
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
url = update.message.text
print(f"用户 {update.effective_user.id} 发送链接: {url}")
if not url.startswith(('http://', 'https://')):
print(f"用户 {update.effective_user.id} 发送了无效链接")
await context.bot.send_message(chat_id=update.effective_chat.id, text="请发送有效的视频链接")
return
print(f"开始处理用户 {update.effective_user.id} 的视频")
await context.bot.send_message(chat_id=update.effective_chat.id, text="收到命令,开始下载...")
try:
# 下载视频
print(f"开始下载用户 {update.effective_user.id} 的视频")
filepath, filename = download_video(url)
print(f"用户 {update.effective_user.id} 的视频下载完成: {filepath}")
await context.bot.send_message(chat_id=update.effective_chat.id, text="下载完成,转音频中...")
# 转换音频
print(f"开始转换用户 {update.effective_user.id} 的音频")
mp3_path = convert_to_mp3(filepath, filename)
print(f"用户 {update.effective_user.id} 的音频转换完成: {mp3_path}")
await context.bot.send_message(chat_id=update.effective_chat.id, text="转音频完成,语音转文字中...")
# 转文字
print(f"开始转录用户 {update.effective_user.id} 的音频")
text, txt_path = transcribe_audio(mp3_path)
print(f"用户 {update.effective_user.id} 的音频转录完成: {txt_path}")
await context.bot.send_message(chat_id=update.effective_chat.id, text="转文字完成,摘要总结中...")
# 生成摘要
print(f"开始生成用户 {update.effective_user.id} 的摘要")
summary = get_summary(text)
print(f"用户 {update.effective_user.id} 的摘要生成完成")
# 发送支付图片
print(f"向用户 {update.effective_user.id} 发送支付图片")
img_url = './pay.jpg'
await context.bot.send_photo(chat_id=update.effective_chat.id, photo=f"{img_url}")
# 创建支付按钮
keyboard = [
[InlineKeyboardButton("已支付", callback_data='button1')],
[InlineKeyboardButton("放弃支付", callback_data='button2')],
]
reply_markup = InlineKeyboardMarkup(keyboard)
await update.message.reply_text(text="摘要总结完毕,请支付", reply_markup=reply_markup)
# 保存摘要到上下文,以便后续使用
context.user_data['summary'] = summary
except Exception as e:
print(f"处理用户 {update.effective_user.id} 的请求时出错: {str(e)}")
await context.bot.send_message(chat_id=update.effective_chat.id, text=f"处理过程中出现错误: {str(e)}")
async def button_callback(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
print(f"用户 {update.effective_user.id} 点击了按钮: {query.data}")
if query.data == 'button1':
await query.edit_message_text("感谢您的支付,正在处理您的请求...")
# 发送保存的摘要内容
summary = context.user_data.get('summary', '未找到摘要内容')
print(f"向用户 {update.effective_user.id} 发送摘要内容")
await context.bot.send_message(chat_id=update.effective_chat.id, text=summary)
elif query.data == 'button2':
print(f"用户 {update.effective_user.id} 取消支付")
await query.edit_message_text("您已取消支付,感谢使用我们的服务")
async def post_init(application):
print("Bot启动成功,正在等待消息...")
start_handler = CommandHandler('download', start)
message_handler = MessageHandler(filters.TEXT & ~filters.COMMAND, handle_message)
button_handler = CallbackQueryHandler(button_callback)
TOKEN='8189753094:AAEjgDzt58g4jcTdRLjuLbdGwRrO3dqzA1I'
application = ApplicationBuilder().token(TOKEN).proxy("socks5://127.0.0.1:7890").get_updates_proxy("socks5://127.0.0.1:7890").post_init(post_init).build()
application.add_handler(start_handler)
application.add_handler(message_handler)
application.add_handler(button_handler)
print("正在启动Bot...")
application.run_polling()