-
Notifications
You must be signed in to change notification settings - Fork 143
/
bot.py
123 lines (113 loc) · 3.94 KB
/
bot.py
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
import os, logging, asyncio
from telethon import Button
from telethon import TelegramClient, events
from telethon.tl.types import ChannelParticipantAdmin
from telethon.tl.types import ChannelParticipantCreator
from telethon.tl.functions.channels import GetParticipantRequest
from telethon.errors import UserNotParticipantError
logging.basicConfig(
level=logging.INFO,
format='%(name)s - [%(levelname)s] - %(message)s'
)
LOGGER = logging.getLogger(__name__)
api_id = int(os.environ.get("APP_ID"))
api_hash = os.environ.get("API_HASH")
bot_token = os.environ.get("TOKEN")
client = TelegramClient('client', api_id, api_hash).start(bot_token=bot_token)
spam_chats = []
@client.on(events.NewMessage(pattern="^/start$"))
async def start(event):
await event.reply(
"__**I'm MentionAll Bot**, I can mention almost all members in group or channel 👻\nClick **/help** for more information__\n\n Follow [@AnjanaMadu](https://github.com/AnjanaMadu) on Github",
link_preview=False,
buttons=(
[
Button.url('📣 Channel', 'https://t.me/harp_tech'),
Button.url('📦 Source', 'https://github.com/AnjanaMadu/MentionAllBot')
]
)
)
@client.on(events.NewMessage(pattern="^/help$"))
async def help(event):
helptext = "**Help Menu of MentionAllBot**\n\nCommand: /mentionall\n__You can use this command with text what you want to mention others.__\n`Example: /mentionall Good Morning!`\n__You can you this command as a reply to any message. Bot will tag users to that replied messsage__.\n\nFollow [@AnjanaMadu](https://github.com/AnjanaMadu) on Github"
await event.reply(
helptext,
link_preview=False,
buttons=(
[
Button.url('📣 Channel', 'https://t.me/harp_tech'),
Button.url('📦 Source', 'https://github.com/AnjanaMadu/MentionAllBot')
]
)
)
@client.on(events.NewMessage(pattern="^/mentionall ?(.*)"))
async def mentionall(event):
chat_id = event.chat_id
if event.is_private:
return await event.respond("__This command can be use in groups and channels!__")
is_admin = False
try:
partici_ = await client(GetParticipantRequest(
event.chat_id,
event.sender_id
))
except UserNotParticipantError:
is_admin = False
else:
if (
isinstance(
partici_.participant,
(
ChannelParticipantAdmin,
ChannelParticipantCreator
)
)
):
is_admin = True
if not is_admin:
return await event.respond("__Only admins can mention all!__")
if event.pattern_match.group(1) and event.is_reply:
return await event.respond("__Give me one argument!__")
elif event.pattern_match.group(1):
mode = "text_on_cmd"
msg = event.pattern_match.group(1)
elif event.is_reply:
mode = "text_on_reply"
msg = await event.get_reply_message()
if msg == None:
return await event.respond("__I can't mention members for older messages! (messages which are sent before I'm added to group)__")
else:
return await event.respond("__Reply to a message or give me some text to mention others!__")
spam_chats.append(chat_id)
usrnum = 0
usrtxt = ''
async for usr in client.iter_participants(chat_id):
if not chat_id in spam_chats:
break
usrnum += 1
usrtxt += f"[{usr.first_name}](tg://user?id={usr.id}) "
if usrnum == 5:
if mode == "text_on_cmd":
txt = f"{usrtxt}\n\n{msg}"
await client.send_message(chat_id, txt)
elif mode == "text_on_reply":
await msg.reply(usrtxt)
await asyncio.sleep(2)
usrnum = 0
usrtxt = ''
try:
spam_chats.remove(chat_id)
except:
pass
@client.on(events.NewMessage(pattern="^/cancel$"))
async def cancel_spam(event):
if not event.chat_id in spam_chats:
return await event.respond('__There is no proccess on going...__')
else:
try:
spam_chats.remove(event.chat_id)
except:
pass
return await event.respond('__Stopped.__')
print(">> BOT STARTED <<")
client.run_until_disconnected()