-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.py
More file actions
295 lines (248 loc) · 10.5 KB
/
app.py
File metadata and controls
295 lines (248 loc) · 10.5 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import os
import random
import asyncio
import logging
import time
from typing import Optional, Dict
from pyrogram import Client, filters
from pyrogram.types import Message
from pyrogram.errors import FloodWait, ReactionInvalid, MessageNotModified, PeerIdInvalid
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
app = Client(
"my_userbot",
api_id=int(os.environ.get("API_ID", "YOUR-ID")),
api_hash=os.environ.get("API_HASH", "YOUR-HASH"),
session_string=os.environ.get("SESSION_STRING", "YOUR-STRING"),
in_memory=True
)
# Default set of emojis to pick from
VALID_EMOJIS = [
"☃️", "🎄", "🎅", "🎃", "🎉", "🏆", "🆒", "💯", "💋", "💊",
"💘", "💔", "💩", "❤️", "❤️🔥", "🕊", "🙈", "🙉", "🙊", "🙏",
"🤌", "🤝", "🤗", "🤘", "🤩", "🤪", "🤬", "🤓", "🤡", "🤣", "🤯",
"🤷", "🤷♂️", "🤷♀️", "🖕", "🗿", "😁", "😇", "😈", "😍", "😎",
"😡", "😨", "😱", "😭", "🥰", "🥱", "🥴", "😐", "😴", "🙂", "🤔",
"🤨", "😢", "👌", "👍", "👎", "👏", "👀", "👻", "👾", "👨💻",
"💅", "✍", "🫡", "🔥", "⚡", "🦄", "🐳", "🌚", "🌭", "🍾", "🍓",
"🍌", "😘"
]
# Per-chat ON/OFF state (default True)
react_status: Dict[int, bool] = {}
# Per-chat reaction limits (per 60 seconds). None -> unlimited
react_limits: Dict[int, Optional[int]] = {}
# Per-chat counters for rate limiting: { chat_id: {"count": int, "reset": timestamp} }
react_counters: Dict[int, Dict[str, float]] = {}
alive_sent = False
async def send_alive():
"""
Sends a single "alive" message to Saved Messages (me) once on startup.
"""
global alive_sent
if alive_sent:
return
try:
await app.send_message(
"me",
"**🚀 Auto React Userbot FULLY ACTIVE!**\n\n"
"✅ Reacts in **Private, Groups, Channels** by default\n"
"✅ Skips **edited** messages (but now reacts to replies and own messages)\n"
"⚙️ Use `!react on` / `!react off` → per-chat control\n"
"🧾 Use `!reactlimit <n>` → set per-chat reactions per minute (empty/unlimited by default)\n"
"🟢 **Status: ONLINE & REACTING EVERYWHERE (unless disabled)**",
disable_web_page_preview=True
)
alive_sent = True
logger.info("Alive message sent.")
except Exception as e:
logger.error(f"Failed to send alive: {e}")
def is_admin_or_me(message: Message, me_id: int) -> bool:
"""
Returns True if the sender is the bot user (me) or is an admin in the chat.
- For private chats: allow the sender (chat partner).
- For groups/channels: only allow admins or the user themselves to change settings.
"""
# Allow own messages always
if message.from_user and message.from_user.id == me_id:
return True
# Private chats -> allow
if message.chat.type == "private":
return True
# For groups/channels, check admin status
try:
member = asyncio.get_event_loop().run_until_complete(app.get_chat_member(message.chat.id, message.from_user.id))
status = member.status # "creator", "administrator", "member", ...
return status in ("creator", "administrator")
except Exception:
# If we cannot determine, be conservative and disallow
return False
@app.on_message(filters.command("react", prefixes=["/", "!"]) & (filters.group | filters.channel | filters.private))
async def cmd_react_toggle(client: Client, message: Message):
"""
Toggle reactions for the current chat:
!react on -> enable reacting in this chat
!react off -> disable reacting in this chat
!react status -> show status
Only admins (or the user themself in private) can change the setting.
"""
me = await app.get_me()
sender_ok = is_admin_or_me(message, me.id)
if not sender_ok:
await message.reply_text("❌ You must be an admin (or me) to change react settings in this chat.")
return
args = message.text.split()
if len(args) < 2:
await message.reply_text("Usage: `!react on` or `!react off` or `!react status`")
return
cmd = args[1].lower()
chat_id = message.chat.id
if cmd in ("on", "enable"):
react_status[chat_id] = True
await message.reply_text("🟢 Auto reactions ENABLED for this chat.")
elif cmd in ("off", "disable"):
react_status[chat_id] = False
await message.reply_text("🔴 Auto reactions DISABLED for this chat.")
elif cmd in ("status",):
status = "ON" if react_status.get(chat_id, True) else "OFF"
limit = react_limits.get(chat_id, None)
limit_text = "unlimited" if limit is None else f"{limit} per minute"
await message.reply_text(f"🤖 Auto React status: `{status}`\n⏱️ Limit: `{limit_text}`")
else:
await message.reply_text("Unknown option. Use `!react on`, `!react off` or `!react status`.")
@app.on_message(filters.command("reactlimit", prefixes=["/", "!"]) & (filters.group | filters.channel | filters.private))
async def cmd_react_limit(client: Client, message: Message):
"""
Set per-chat reaction limit (per minute).
!reactlimit 10 -> up to 10 reactions per minute in this chat
!reactlimit 0 -> disable reactions entirely in this chat (equivalent to !react off)
!reactlimit unset -> remove limit (unlimited)
!reactlimit status -> show current
Only admins (or the user themself in private) can change the setting.
"""
me = await app.get_me()
sender_ok = is_admin_or_me(message, me.id)
if not sender_ok:
await message.reply_text("❌ You must be an admin (or me) to change react limits in this chat.")
return
args = message.text.split()
chat_id = message.chat.id
if len(args) < 2:
limit = react_limits.get(chat_id, None)
limit_text = "unlimited" if limit is None else f"{limit} per minute"
await message.reply_text(f"Current limit: `{limit_text}`")
return
val = args[1].lower()
if val in ("unset", "none", "unlimited"):
react_limits[chat_id] = None
await message.reply_text("✅ Reaction limit removed — unlimited reactions allowed in this chat.")
elif val in ("status",):
limit = react_limits.get(chat_id, None)
limit_text = "unlimited" if limit is None else f"{limit} per minute"
await message.reply_text(f"Current limit: `{limit_text}`")
else:
try:
n = int(val)
if n < 0:
raise ValueError()
if n == 0:
# Equivalent to disabling all reactions in this chat
react_limits[chat_id] = 0
react_status[chat_id] = False
await message.reply_text("🔴 Reaction limit set to 0 — reactions disabled in this chat.")
else:
react_limits[chat_id] = n
# If it was disabled, don't auto-enable; just set limit
await message.reply_text(f"✅ Reaction limit set to `{n}` per minute for this chat.")
except ValueError:
await message.reply_text("Please provide a non-negative integer, or `unset` for unlimited.")
@app.on_message(
(filters.private | filters.group | filters.channel) &
(filters.incoming | filters.me) & # <-- include both incoming and your own outgoing messages
~filters.command(["react", "reactlimit"], prefixes=["/", "!"])
)
async def auto_react(client: Client, message: Message):
"""
Main auto react handler:
- skips edited messages
- now reacts to replies too
- includes your own messages (filters.me)
- obeys per-chat ON/OFF (default ON)
- obeys per-chat limits (per-minute)
"""
# Skip edited messages
if message.edit_date:
return
chat_id = message.chat.id
# Check ON/OFF state (default ON)
if not react_status.get(chat_id, True):
return
# If the chat has a limit of 0, treat as disabled
limit = react_limits.get(chat_id, None)
if limit == 0:
return
# Ensure message has an id (should always be true)
if not message.id:
return
# Rate limiting per chat (sliding simple window: reset every 60s)
now = time.time()
counter = react_counters.get(chat_id)
if not counter or now > counter.get("reset", 0):
# reset window
react_counters[chat_id] = {"count": 0, "reset": now + 60}
counter = react_counters[chat_id]
# If limit is set and we've reached it, skip reacting
if limit is not None:
if counter["count"] >= limit:
# Reached limit for current minute
logger.debug(f"Reaction limit reached for chat {chat_id}: {counter['count']}/{limit}")
return
emoji = random.choice(VALID_EMOJIS)
try:
await message.react(emoji=emoji)
logger.info(f"Reacted {emoji} → {chat_id} | Msg ID: {message.id}")
# increase counter
react_counters[chat_id]["count"] += 1
except ReactionInvalid:
# emoji not allowed in this chat or other reaction-related problem
pass
except FloodWait as e:
logger.warning(f"FloodWait: sleeping {e.value}s")
await asyncio.sleep(e.value)
except PeerIdInvalid:
logger.warning(f"PeerIdInvalid skipped: {chat_id}")
# Try to auto-resolve by fetching chat
try:
await app.get_chat(chat_id)
except Exception:
pass
except Exception as e:
error = str(e)
if "MESSAGE_ID_INVALID" in error or "REACTION_INVALID" in error:
# benign, skip
pass
else:
logger.error(f"React failed: {error}")
@app.on_message(filters.private & filters.me)
async def auto_start_trigger(client: Client, message: Message):
# Allow manual trigger from Saved Messages (me) to resend alive status
await send_alive()
async def main():
try:
await app.start()
me = await app.get_me()
logger.info(f"Userbot started as @{me.username or me.first_name}")
await send_alive()
# Keep alive forever
await asyncio.Event().wait()
except Exception as e:
logger.critical(f"Startup failed: {e}")
await asyncio.sleep(5)
os._exit(1)
if __name__ == "__main__":
try:
app.run(main())
except KeyboardInterrupt:
logger.info("Userbot stopped by user.")
except Exception as e:
logger.critical(f"Critical error: {e}")
os._exit(1)