-
-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathbot.py
208 lines (190 loc) · 6.82 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
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
# This file is part of the ForveSub distribution (https://github.com/xditya/ForceSub).
# Copyright (c) 2021 Adiya
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# License can be found in < https://github.com/xditya/ForceSub/blob/main/License> .
import logging
from telethon.utils import get_display_name
import re
from telethon import TelegramClient, events, Button
from decouple import config
from telethon.tl.functions.users import GetFullUserRequest
from telethon.errors.rpcerrorlist import UserNotParticipantError
from telethon.tl.functions.channels import GetParticipantRequest
logging.basicConfig(
format="[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s", level=logging.INFO
)
log = logging.getLogger("BotzHub")
# start the bot
log.info("Starting...")
try:
bottoken = config("BOT_TOKEN")
xchannel = config("CHANNEL")
welcome_msg = config("WELCOME_MSG")
welcome_not_joined = config("WELCOME_NOT_JOINED")
on_join = config("ON_JOIN", cast=bool)
on_new_msg = config("ON_NEW_MSG", cast=bool)
except Exception as e:
log.error(e)
log.info("Bot is quiting...")
exit()
try:
BotzHub = TelegramClient("BotzHub", 6, "eb06d4abfb49dc3eeb1aeb98ae0f581e").start(
bot_token=bottoken
)
except Exception as e:
log.error(f"ERROR!\n{str(e)}")
log.error("Bot is quiting...")
exit()
channel = xchannel.replace("@", "")
bot_self = BotzHub.loop.run_until_complete(BotzHub.get_me())
# join check
async def get_user_join(id):
ok = True
try:
await BotzHub(GetParticipantRequest(channel=channel, participant=id))
ok = True
except UserNotParticipantError:
ok = False
return ok
@BotzHub.on(events.ChatAction)
async def _(event):
if on_join is False:
return
if not event.is_group:
return
if event.action_message:
return
if event.user_joined or event.user_added:
user = await event.get_user()
chat = await event.get_chat()
title = chat.title or "this chat"
pp = await BotzHub.get_participants(chat)
count = len(pp)
mention = f"[{get_display_name(user)}](tg://user?id={user.id})"
name = user.first_name
last = user.last_name
fullname = f"{name} {last}" if last else name
username = f"@{uu}" if (uu := user.username) else mention
x = await get_user_join(user.id)
if x is True:
msg = welcome_msg.format(
mention=mention,
title=title,
fullname=fullname,
username=username,
name=name,
last=last,
channel=f"@{channel}",
count=count,
)
butt = [Button.url("Channel", url=f"https://t.me/{channel}")]
else:
msg = welcome_not_joined.format(
mention=mention,
title=title,
fullname=fullname,
username=username,
name=name,
last=last,
channel=f"@{channel}",
count=count,
)
butt = [
Button.url("Channel", url=f"https://t.me/{channel}"),
Button.inline("UnMute Me", data=f"unmute_{user.id}"),
]
await BotzHub.edit_permissions(
event.chat.id, user.id, until_date=None, send_messages=False
)
await event.reply(msg, buttons=butt)
@BotzHub.on(events.NewMessage(incoming=True))
async def mute_on_msg(event):
if event.is_private:
return
if on_new_msg is False:
return
x = await get_user_join(event.sender_id)
temp = await BotzHub.get_entity(event.sender_id)
if x is False:
if temp.bot:
return
nm = temp.first_name
try:
await BotzHub.edit_permissions(
event.chat.id, event.sender_id, until_date=None, send_messages=False
)
except Exception as e:
log.error(e)
return
user = await event.get_sender()
chat = await event.get_chat()
title = chat.title or "this chat"
pp = await BotzHub.get_participants(chat)
count = len(pp)
mention = f"[{get_display_name(user)}](tg://user?id={user.id})"
name = user.first_name
last = user.last_name
fullname = f"{name} {last}" if last else name
username = f"@{uu}" if (uu := user.username) else mention
reply_msg = welcome_not_joined.format(
mention=mention,
title=title,
fullname=fullname,
username=username,
name=name,
last=last,
channel=f"@{channel}",
count=count,
)
butt = [
Button.url("Channel", url=f"https://t.me/{channel}"),
Button.inline("UnMute Me", data=f"unmute_{event.sender_id}"),
]
await event.reply(reply_msg, buttons=butt)
@BotzHub.on(events.callbackquery.CallbackQuery(data=re.compile(b"unmute_(.*)")))
async def _(event):
uid = int(event.data_match.group(1).decode("UTF-8"))
if uid == event.sender_id:
x = await get_user_join(uid)
nm = event.sender.first_name
if x is False:
await event.answer(
f"You haven't joined @{channel} yet!", cache_time=0, alert=True
)
elif x is True:
try:
await BotzHub.edit_permissions(
event.chat.id, uid, until_date=None, send_messages=True
)
except Exception as e:
log.error(e)
return
msg = f"Welcome to {(await event.get_chat()).title}, {nm}!\nGood to see you here!"
butt = [Button.url("Channel", url=f"https://t.me/{channel}")]
await event.edit(msg, buttons=butt)
else:
await event.answer(
"You are an old member and can speak freely! This isn't for you!",
cache_time=0,
alert=True,
)
@BotzHub.on(events.NewMessage(pattern="^/start$"))
async def strt(event):
await event.reply(
f"Hi. I'm a force subscribe bot made specially for @{channel}!\n\nCheckout @BotzHub :)",
buttons=[
Button.url("Channel", url=f"https://t.me/{channel}"),
Button.url("Repository", url="https://github.com/xditya/ForceSub"),
],
)
log.info("ForceSub Bot has started as @%s.\nDo visit @BotzHub!", bot_self.username)
BotzHub.run_until_disconnected()