-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot.py
More file actions
306 lines (260 loc) · 9.77 KB
/
bot.py
File metadata and controls
306 lines (260 loc) · 9.77 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
296
297
298
299
300
301
302
303
304
305
306
import contextlib
import re
import logging
from aioredis import Redis
from decouple import config
from telethon import TelegramClient, events, Button, types, functions, errors
logging.basicConfig(
level=logging.INFO, format="[%(levelname)s] %(asctime)s - %(message)s"
)
log = logging.getLogger("ChannelActions")
log.info("\n\nStarting...\n")
try:
bot_token = config("BOT_TOKEN")
REDIS_URI = config("REDIS_URI")
REDIS_PASSWORD = config("REDIS_PASSWORD")
AUTH = [int(i) for i in config("OWNERS").split(" ")]
except Exception as e:
log.exception(e)
exit(1)
# connecting the client
try:
bot = TelegramClient(None, 6, "eb06d4abfb49dc3eeb1aeb98ae0f581e").start(
bot_token=bot_token
)
except Exception as e:
log.exception(e)
exit(1)
REDIS_URI = REDIS_URI.split(":")
db = Redis(
host=REDIS_URI[0],
port=REDIS_URI[1],
password=REDIS_PASSWORD,
decode_responses=True,
)
# users to db
def str_to_list(text): # Returns List
return text.split(" ")
def list_to_str(list): # Returns String # sourcery skip: avoid-builtin-shadow
str = "".join(f"{x} " for x in list)
return str.strip()
async def is_added(var, id): # Take int or str with numbers only , Returns Boolean
if not str(id).isdigit():
return False
users = await get_all(var)
return str(id) in users
async def add_to_db(var, id): # Take int or str with numbers only , Returns Boolean
# sourcery skip: avoid-builtin-shadow
id = str(id)
if not id.isdigit():
return False
try:
users = await get_all(var)
users.append(id)
await db.set(var, list_to_str(users))
return True
except Exception as e:
return False
async def get_all(var): # Returns List
users = await db.get(var)
return [""] if users is None or users == "" else str_to_list(users)
async def get_me():
me = await bot.get_me()
myname = me.username
return f"@{myname}"
bot_username = bot.loop.run_until_complete(get_me())
start_msg = """Hi {user}!
**I'm Channel Actions Bot, a bot mainly focused on working with the new [admin approval invite links](https://t.me/telegram/153).**
**__I can__**:
- __Auto approve new join requests.__
- __Auto Decline New Join Requests.__
`Click the below button to know how to use me!`"""
start_buttons = [
[Button.inline("How to use me ❓", data="helper")]
]
@bot.on(events.NewMessage(incoming=True, pattern=f"^/start({bot_username})?$"))
async def starters(event):
from_ = await bot.get_entity(event.sender_id)
await event.reply(
start_msg.format(user=from_.first_name),
buttons=start_buttons,
link_preview=False,
)
if not (await is_added("BOTUSERS", event.sender_id)):
await add_to_db("BOTUSERS", event.sender_id)
@bot.on(events.CallbackQuery(data="start"))
async def start_in(event):
from_ = await bot.get_entity(event.sender_id)
with contextlib.suppress(errors.rpcerrorlist.MessageNotModifiedError):
await event.edit(
start_msg.format(user=from_.first_name),
buttons=start_buttons,
link_preview=False,
)
@bot.on(events.CallbackQuery(data="helper"))
async def helper(event):
await event.edit(
'**Usage instructions.**\n\nAdd me to your channel, as administrator, with "add users" permission, and forward me a message from that chat to set me up!',
buttons=Button.inline("Main Menu 📭", data="start"),
)
@bot.on(events.NewMessage(incoming=True, func=lambda e: e.is_private and e.fwd_from))
async def settings_selctor(event): # sourcery skip: avoid-builtin-shadow
id = event.fwd_from.from_id
if not isinstance(id, types.PeerChannel):
await event.reply("Looks like this isn't from a channel!")
return
try:
chat = await bot.get_entity(id)
if chat.admin_rights is None:
await event.reply("Seems like I'm not admin in this channel!")
return
except ValueError:
await event.reply("Seems like you haven't added me to your channel!")
return
# check if the guy trying to change settings is an admin
try:
who_u = (
await bot(
functions.channels.GetParticipantRequest(
channel=chat.id,
participant=event.sender_id,
)
)
).participant
except errors.rpcerrorlist.UserNotParticipantError:
await event.reply(
"You are not in the channel, or an admin, to perform this action."
)
return
if not (
isinstance(
who_u, (types.ChannelParticipantCreator, types.ChannelParticipantAdmin)
)
):
await event.reply(
"You are not an admin of this channel and cannot change it's settings!"
)
return
added_chats = await db.get("CHAT_SETTINGS") or "{}"
added_chats = eval(added_chats)
welcome_msg = eval(await db.get("WELCOME_MSG") or "{}")
is_modded = bool(welcome_msg.get(chat.id))
setting = added_chats.get(str(chat.id)) or "Auto-Approve"
await event.reply(
"**Settings for {title}**\n\n__Select what to do on new join requests:__\n**Current setting** - __{set}__\n\n__Set your welcome message:__\nCurrently modified: {is_modded}".format(
title=chat.title, set=setting, is_modded=is_modded
),
buttons=[
[Button.inline("Auto-Approve", data=f"set_ap_{chat.id}")],
[Button.inline("Auto-Disapprove", data=f"set_disap_{chat.id}")],
[Button.inline("Set Welcome Message", data=f"mod_{chat.id}")],
],
)
@bot.on(events.CallbackQuery(data=re.compile("set_(.*)")))
async def settings(event):
args = event.pattern_match.group(1).decode("utf-8")
setting, chat = args.split("_")
added_chats = await db.get("CHAT_SETTINGS") or "{}"
added_chats = eval(added_chats)
if setting == "ap":
op = "Auto-Approve"
added_chats.update({chat: op})
elif setting == "disap":
op = "Auto-Disapprove"
added_chats.update({chat: op})
await db.set("CHAT_SETTINGS", str(added_chats))
await event.edit(
f"Settings updated! New members in the channel `{chat}` will be {op}d!"
)
@bot.on(events.CallbackQuery(data=re.compile("mod_(.*)")))
async def mod_welcome(event):
args = int(event.pattern_match.group(1).decode("utf-8"))
welcome_msg = eval(await db.get("WELCOME_MSG") or "{}")
await event.delete()
async with bot.conversation(event.sender_id) as conv:
await conv.send_message(
"Send the new welcome message you want to be sent to a user when he is approved into your channel.\nAvailable formattings:\n- {name} - users name.\n- {chat} - chat title.",
buttons=Button.force_reply(),
)
msg = await conv.get_reply()
if not msg.text:
await event.reply("You can only set a text message!")
return
msg = msg.text
welcome_msg.update({args: msg})
await db.set("WELCOME_MSG", str(welcome_msg))
chat = await bot.get_entity(args)
await conv.send_message(
f"Welcome message for {chat.title} has been successfully set!"
)
@bot.on(events.Raw(types.UpdateBotChatInviteRequester))
async def approver(event):
chat = event.peer.channel_id
chat_settings = await db.get("CHAT_SETTINGS") or "{}"
chat_settings = eval(chat_settings)
welcome_msg = eval(await db.get("WELCOME_MSG") or "{}")
chat_welcome = (
welcome_msg.get(chat)
or "Hello {name}, your request to join {chat} has been {dn}"
)
chat_welcome += "\nSend /start to know more." # \n\n__**Powered by @M2LINKS**__"
who = await bot.get_entity(event.user_id)
chat_ = await bot.get_entity(chat)
dn = "approved!"
appr = True
if chat_settings.get(str(chat)) == "Auto-Approve":
appr = True
dn = "approved!"
elif chat_settings.get(str(chat)) == "Auto-Disapprove":
appr = False
dn = "disapproved :("
with contextlib.suppress(
errors.rpcerrorlist.UserIsBlockedError, errors.rpcerrorlist.PeerIdInvalidError
):
await bot.send_message(
event.user_id,
chat_welcome.format(name=who.first_name, chat=chat_.title, dn=dn),
buttons=Button.url("Updates", url="https://t.me/m2links"),
)
with contextlib.suppress(errors.rpcerrorlist.UserAlreadyParticipantError):
await bot(
functions.messages.HideChatJoinRequestRequest(
approved=appr, peer=chat, user_id=event.user_id
)
)
@bot.on(events.NewMessage(incoming=True, from_users=AUTH, pattern="^/stats$"))
async def auth_(event):
xx = await event.reply("Calculating...")
t = await db.get("CHAT_SETTINGS") or "{}"
t = eval(t)
await xx.edit(
"**ChannelActionsBot Stats**\n\nUsers: {}\nGroups added (with modified settings): {}".format(
len(await get_all("BOTUSERS")), len(t.keys())
)
)
@bot.on(events.NewMessage(incoming=True, from_users=AUTH, pattern="^/broadcast$"))
async def broad(e):
if not e.reply_to_msg_id:
return await e.reply(
"Please use `/broadcast` as reply to the message you want to broadcast."
)
msg = await e.get_reply_message()
xx = await e.reply("In progress...")
users = await get_all("BOTUSERS")
done = error = 0
for i in users:
try:
await bot.send_message(
int(i),
msg.text,
file=msg.media,
buttons=msg.buttons,
link_preview=False,
)
done += 1
except Exception:
error += 1
await xx.edit("Broadcast completed.\nSuccess: {}\nFailed: {}".format(done, error))
log.info("Started Bot - %s", bot_username)
log.info("@M2LINKS")
bot.run_until_disconnected()