Skip to content

Commit 3d33809

Browse files
authored
Stabilize Leaving of Disallowed Chats & Delete Privacy Command in Groups (#143)
* Stabilize Leaving of Disallowed Chats * Delete Privacy Command in Groups
1 parent a555e0b commit 3d33809

File tree

3 files changed

+27
-14
lines changed

3 files changed

+27
-14
lines changed

components/callbacks.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import asyncio
2+
import contextlib
23
import logging
34
import random
45
import re
@@ -18,11 +19,14 @@
1819
User,
1920
)
2021
from telegram.constants import ChatAction, MessageLimit
22+
from telegram.error import BadRequest
2123
from telegram.ext import Application, ApplicationHandlerStop, ContextTypes, Job, JobQueue
2224
from telegram.helpers import escape_markdown
2325

2426
from components import const
2527
from components.const import (
28+
ALLOWED_CHAT_IDS,
29+
ALLOWED_USERNAMES,
2630
BUY_TEXT,
2731
DEFAULT_REPO_NAME,
2832
DEFAULT_REPO_OWNER,
@@ -290,8 +294,18 @@ async def delete_message(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
290294
await try_to_delete(cast(Message, update.effective_message))
291295

292296

293-
async def leave_chat(update: Update, context: ContextTypes.DEFAULT_TYPE) -> None:
294-
context.application.create_task(cast(Chat, update.effective_chat).leave(), update=update)
297+
async def leave_chat(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
298+
if (
299+
not (chat := update.effective_chat)
300+
or chat.type == chat.PRIVATE
301+
or chat.username in ALLOWED_USERNAMES
302+
or chat.id in ALLOWED_CHAT_IDS
303+
):
304+
return
305+
306+
with contextlib.suppress(BadRequest):
307+
await chat.leave()
308+
295309
raise ApplicationHandlerStop
296310

297311

@@ -585,5 +599,7 @@ async def privacy(update: Update, _: ContextTypes.DEFAULT_TYPE) -> None:
585599
"""Reply with the privacy policy"""
586600
message = cast(Message, update.effective_message)
587601
await message.reply_text(
588-
f"Please read my privacy policy in <a href='{PRIVACY_POLICY}'>here</a>."
602+
f"Please read my privacy policy in <a href='{PRIVACY_POLICY}'>here</a>.", quote=False
589603
)
604+
if message.chat.type != message.chat.PRIVATE:
605+
await try_to_delete(message)

components/const.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
OFFTOPIC_USERNAME = "pythontelegrambottalk"
2020
ONTOPIC_USERNAME = "pythontelegrambotgroup"
2121
DEV_GROUP_USERNAME = "pythontelegrambotdev"
22+
CHANNEL_USERNAME = "pythontelegrambotchannel"
23+
CHANNEL_CHAT_ID = "@" + CHANNEL_USERNAME
2224
OFFTOPIC_CHAT_ID = "@" + OFFTOPIC_USERNAME
2325
ONTOPIC_CHAT_ID = "@" + ONTOPIC_USERNAME
2426
ERROR_CHANNEL_CHAT_ID = -1001397960657
@@ -34,7 +36,7 @@
3436
WIKI_FAQ_URL = urljoin(WIKI_URL, "Frequently-Asked-Questions")
3537
WIKI_FRDP_URL = urljoin(WIKI_URL, "Frequently-requested-design-patterns")
3638
EXAMPLES_URL = urljoin(DOCS_URL, "/examples.html")
37-
ALLOWED_USERNAMES = (OFFTOPIC_USERNAME, ONTOPIC_USERNAME, DEV_GROUP_USERNAME)
39+
ALLOWED_USERNAMES = (OFFTOPIC_USERNAME, ONTOPIC_USERNAME, DEV_GROUP_USERNAME, CHANNEL_USERNAME)
3840
ALLOWED_CHAT_IDS = (
3941
ERROR_CHANNEL_CHAT_ID,
4042
-1001494805131, # dev chat

rules_bot.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
Defaults,
2121
InlineQueryHandler,
2222
MessageHandler,
23+
TypeHandler,
2324
filters,
2425
)
2526

@@ -45,8 +46,6 @@
4546
tag_hint,
4647
)
4748
from components.const import (
48-
ALLOWED_CHAT_IDS,
49-
ALLOWED_USERNAMES,
5049
COMPAT_ERRORS,
5150
DESCRIPTION,
5251
ERROR_CHANNEL_CHAT_ID,
@@ -135,11 +134,9 @@ def main() -> None:
135134
)
136135
# Leave groups that are not maintained by PTB
137136
application.add_handler(
138-
MessageHandler(
139-
filters.ChatType.GROUPS
140-
& ~filters.StatusUpdate.LEFT_CHAT_MEMBER
141-
& ~(filters.Chat(username=ALLOWED_USERNAMES) | filters.Chat(chat_id=ALLOWED_CHAT_IDS)),
142-
leave_chat,
137+
TypeHandler(
138+
type=Update,
139+
callback=leave_chat,
143140
),
144141
group=-2,
145142
)
@@ -153,9 +150,7 @@ def main() -> None:
153150

154151
application.add_handler(
155152
MessageHandler(
156-
filters.SenderChat.CHANNEL
157-
& ~filters.IS_AUTOMATIC_FORWARD
158-
& ~filters.Chat(chat_id=ERROR_CHANNEL_CHAT_ID),
153+
filters.SenderChat.CHANNEL & ~filters.ChatType.CHANNEL & ~filters.IS_AUTOMATIC_FORWARD,
159154
ban_sender_channels,
160155
block=False,
161156
)

0 commit comments

Comments
 (0)