-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
127 lines (104 loc) · 3.91 KB
/
Copy pathbot.py
File metadata and controls
127 lines (104 loc) · 3.91 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# bot.py
from telegram import BotCommand, Update, InlineKeyboardButton, InlineKeyboardMarkup
from telegram.ext import (
ApplicationBuilder,
CommandHandler,
CallbackQueryHandler,
MessageHandler,
filters,
ContextTypes,
ConversationHandler,
)
import os
import asyncio
from dotenv import load_dotenv
from src.helpers.translates import Translates
load_dotenv()
translate = Translates()
TOKEN = os.getenv("API_TOKEN", "---")
TARGET_GROUP_ID = os.getenv("GROUP_MESSAGE_ID", "----")
# States
CHOOSE_TEST, WAITING_FOR_UPLOAD = range(2)
user_data_store = {}
commands_fa = [
BotCommand("start", translate.translates["start"]),
BotCommand("finish", translate.translates["finish"]),
]
async def start(update: Update, context: ContextTypes.DEFAULT_TYPE):
keyboard = [
[InlineKeyboardButton("Test 1", callback_data="test1")],
[InlineKeyboardButton("Test 2", callback_data="test2")],
]
await update.message.reply_text(
"sdfsdfsd", reply_markup=InlineKeyboardMarkup(keyboard)
)
return CHOOSE_TEST
async def test_choice(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
await query.answer()
test_selected = query.data
user_data_store[query.from_user.id] = {"test": test_selected}
await query.message.reply_text(
f"You selected {test_selected.upper()}. Follow the instructions below:\n\n"
"1. Do the test as required.\n"
"2. Click 'Finish' when done.",
reply_markup=InlineKeyboardMarkup(
[[InlineKeyboardButton("Finish", callback_data="finish")]]
),
)
return WAITING_FOR_UPLOAD
async def finish_test(update: Update, context: ContextTypes.DEFAULT_TYPE):
query = update.callback_query
if query:
await query.answer()
if query.message:
await query.message.reply_text(
"Upload your result (text + screenshot/video):"
)
return WAITING_FOR_UPLOAD
async def handle_upload(update: Update, context: ContextTypes.DEFAULT_TYPE):
user_id = update.effective_user.id
user_info = user_data_store.get(user_id, {})
username = update.effective_user.username or "Unknown"
test = user_info.get("test", "unknown_test")
caption = f"👤 User: @{username}\n🆔 ID: {user_id}\n🧪 Test: {test}\n"
if update.message.caption:
caption += f"📝 Notes: {update.message.caption}"
if update.message.photo:
await context.bot.send_photo(
chat_id=TARGET_GROUP_ID,
photo=update.message.photo[-1].file_id,
caption=caption,
)
elif update.message.video:
await context.bot.send_video(
chat_id=TARGET_GROUP_ID, video=update.message.video.file_id, caption=caption
)
elif update.message.text:
caption += f"\n📄 Text: {update.message.text}"
await context.bot.send_message(chat_id=TARGET_GROUP_ID, text=caption)
else:
await update.message.reply_text(
"Unsupported format. Please send a photo, video, or text."
)
return WAITING_FOR_UPLOAD
await update.message.reply_text("✅ Your result has been submitted. Thank you!")
return ConversationHandler.END
async def cancel(update: Update, context: ContextTypes.DEFAULT_TYPE):
await update.message.reply_text("Operation cancelled.")
return ConversationHandler.END
if __name__ == "__main__":
app = ApplicationBuilder().token(TOKEN).build()
conv = ConversationHandler(
entry_points=[CommandHandler("start", start)],
states={
CHOOSE_TEST: [CallbackQueryHandler(test_choice, pattern="^test1$|^test2$")],
WAITING_FOR_UPLOAD: [
CallbackQueryHandler(finish_test, pattern="^finish$"),
MessageHandler(filters.ALL, handle_upload),
],
},
fallbacks=[CommandHandler("cancel", cancel)],
)
app.add_handler(conv)
app.run_polling()