-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTEMPMAIL.py
More file actions
172 lines (152 loc) · 6.74 KB
/
Copy pathTEMPMAIL.py
File metadata and controls
172 lines (152 loc) · 6.74 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import os
import logging
from pyrogram import Client, filters
from pyrogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from datetime import datetime
# Configure logging
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)
# Bot configuration
API_ID = int(os.environ.get("API_ID"))
API_HASH = os.environ.get("API_HASH")
BOT_TOKEN = os.environ.get("BOT_TOKEN")
REQUEST_CHANNEL = os.environ.get("REQUEST_CHANNEL") # Remove default value to ensure proper setup
ADMIN_ID = int(os.environ.get("ADMIN_ID", "123456789")) # Your Telegram ID
# Initialize the bot
app = Client(
"RequestPickerBot",
api_id=API_ID,
api_hash=API_HASH,
bot_token=BOT_TOKEN
)
# Start command handler
@app.on_message(filters.command("start"))
async def start(client, message):
await message.reply_text(
f"Hello ⏤•S𝙷𝚁𝙴𝚈𝙰𝙽𝚂𝙷 〄! 👋\n\n"
f"I'm a Request Picker Bot. You can make requests in our group using:\n"
f"/r Movie/Show Name YYYY or\n"
f"/request Movie/Show Name YYYY\n\n"
f"You'll receive notifications here when your requests are processed.",
reply_markup=InlineKeyboardMarkup([
[InlineKeyboardButton("Support Group", url="https://t.me/yourgroup")]
])
)
# Request command handler
@app.on_message(filters.command(["r", "request"]))
async def handle_request(client, message):
try:
# Check if REQUEST_CHANNEL is properly set
if not REQUEST_CHANNEL:
await message.reply_text("⚠️ Bot configuration error. Admin has been notified.")
await client.send_message(
chat_id=ADMIN_ID,
text="❌ ERROR: REQUEST_CHANNEL environment variable is not set!"
)
return
# Extract the request text
if len(message.command) < 2:
await message.reply_text("Please provide the name of the movie/show you're requesting.")
return
request_text = " ".join(message.command[1:])
user = message.from_user
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Format the request message
request_msg = (
f"📌 **New Request**\n\n"
f"🎬 **Title:** {request_text}\n"
f"👤 **User:** {user.mention}\n"
f"🆔 **User ID:** `{user.id}`\n"
f"⏰ **Time:** {timestamp}\n\n"
f"#Request"
)
# Create buttons for admin actions
buttons = InlineKeyboardMarkup([
[
InlineKeyboardButton("✅ Request Received", callback_data=f"req_received:{user.id}:{request_text}"),
InlineKeyboardButton("✅ Upload Done", callback_data=f"req_uploaded:{user.id}:{request_text}")
],
[
InlineKeyboardButton("⚠️ Already Available", callback_data=f"req_available:{user.id}:{request_text}"),
InlineKeyboardButton("❌ Reject Request", callback_data=f"req_rejected:{user.id}:{request_text}")
]
])
try:
# Forward the request to the channel
await client.send_message(
chat_id=REQUEST_CHANNEL,
text=request_msg,
reply_markup=buttons
)
# Confirm to the user
await message.reply_text(
f"✅ Your request for **{request_text}** has been submitted!\n\n"
f"You'll be notified here when it's processed.",
reply_to_message_id=message.id
)
except Exception as channel_error:
logger.error(f"Channel access error: {channel_error}")
# Notify admin about the error
await client.send_message(
chat_id=ADMIN_ID,
text=f"❌ BOT ERROR when accessing channel:\n"
f"Error: {channel_error}\n"
f"Channel ID: {REQUEST_CHANNEL}\n"
f"Please ensure:\n"
f"1. Bot is added to the channel\n"
f"2. Channel ID is correct\n"
f"3. Bot has admin rights in channel"
)
await message.reply_text(
"⚠️ Our system is currently experiencing issues. The admin has been notified."
)
except Exception as e:
logger.error(f"Error handling request: {e}")
await message.reply_text("An error occurred while processing your request. Please try again.")
# Callback query handler
@app.on_callback_query()
async def handle_callback(client, callback_query):
try:
data = callback_query.data
action, user_id, request_text = data.split(":")
user_id = int(user_id)
# Define responses for different actions
responses = {
"req_received": f"📥 Your request for **{request_text}** has been received and will be processed soon!",
"req_uploaded": f"🎉 Good news! Your requested content **{request_text}** is now available!",
"req_available": f"ℹ️ Your requested content **{request_text}** is already available in our database!",
"req_rejected": f"❌ Sorry, your request for **{request_text}** couldn't be fulfilled at this time."
}
try:
# Send notification to user
await client.send_message(
chat_id=user_id,
text=responses.get(action, "Your request status has been updated.")
)
except Exception as user_msg_error:
logger.error(f"Failed to message user {user_id}: {user_msg_error}")
await callback_query.answer("Failed to notify user. They may have blocked the bot.")
return
# Update the original message in the channel
try:
await callback_query.message.edit_text(
f"{callback_query.message.text}\n\n"
f"🔄 **Status:** {action.replace('_', ' ').title()}",
reply_markup=None
)
except Exception as edit_error:
logger.error(f"Failed to edit message: {edit_error}")
await callback_query.answer("Action processed but failed to update channel message.")
return
# Answer the callback query
await callback_query.answer(f"User notified about {action.replace('_', ' ')}")
except Exception as e:
logger.error(f"Error handling callback: {e}")
await callback_query.answer("An error occurred while processing this action.")
# Start the bot
if __name__ == "__main__":
logger.info("Starting Request Picker Bot...")
app.run()