fix: telegram service message

This commit is contained in:
xtaodada 2023-09-11 20:48:52 +08:00
parent 0ee2aabaaa
commit 4ff489cf95
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
6 changed files with 58 additions and 53 deletions

View File

@ -1,45 +1,70 @@
import contextlib import contextlib
from datetime import datetime, timedelta from datetime import datetime, timedelta
from pyrogram.enums import MessageServiceType, ChatMemberStatus from cashews import cache
from pyrogram import filters from pyrogram import filters
from pyrogram.enums import ChatMemberStatus
from sticker.scheduler import add_delete_message_job from pyrogram.types import ChatMemberUpdated
from sticker.single_utils import Client, Message
from sticker import bot, log
from pyromod.utils.errors import TimeoutConversationError from pyromod.utils.errors import TimeoutConversationError
from sticker.scheduler import add_delete_message_job
from sticker.single_utils import Client
from sticker import bot, log
MSG_PUBLIC = """您好,我发现此群组为公开群组,您需要联系创建者打开 `管理员批准后才能入群` 功能,我就能更好地工作。"""
MSG_SUCCESS = """验证成功,您已经成为群组的一员了!"""
MSG_FAILURE = """验证失败,请重试。"""
MSG = """您好 %s ,当前群组开启了验证功能。 MSG = """您好 %s ,当前群组开启了验证功能。
您需要在 30 秒内发送任意一个 贴纸 来完成验证""" 您需要在 30 秒内发送任意一个 贴纸 来完成验证"""
ADMIN_MSG = """管理员邀请,自动放行。""" ADMIN_MSG = """管理员邀请,自动放行。"""
@bot.on_message(filters.service) @bot.on_chat_member_updated()
async def chat_members_handle(client: Client, message: Message): async def invite(client: Client, chat_member_updated: ChatMemberUpdated):
if message.service != MessageServiceType.NEW_CHAT_MEMBERS: chat = chat_member_updated.chat
old_chat_member = chat_member_updated.old_chat_member
if user := chat_member_updated.new_chat_member and not old_chat_member:
if not user.user:
return return
chat = message.chat if user.user.is_self:
if message.new_chat_members: with contextlib.suppress(Exception):
for i in message.new_chat_members: await log(chat, user.invited_by, "NEW_GROUP")
if i.is_self: if chat.username:
with contextlib.suppress(Exception):
await client.send_message(chat.id, MSG_PUBLIC)
if await cache.get(f"cid:{chat.id}"):
return return
user = message.new_chat_members[0] if message.new_chat_members else message.from_user member = chat_member_updated.new_chat_member
old_member = chat_member_updated.old_chat_member
if not member:
return
if not member.user:
return
user = member.user
old_user = old_member.user if old_member else None
if user.is_self or user.is_verified or user.is_bot or user.is_deleted or user.is_support: if user.is_self or user.is_verified or user.is_bot or user.is_deleted or user.is_support:
return return
if message.new_chat_members and message.from_user and ( if member.status not in {ChatMemberStatus.MEMBER}:
await bot.get_chat_member(chat.id, message.from_user.id) return
if old_user and old_user.id == user.id and old_user.status in {
ChatMemberStatus.ADMINISTRATOR,
ChatMemberStatus.OWNER,
ChatMemberStatus.MEMBER,
ChatMemberStatus.RESTRICTED,
}:
return
if user and chat_member_updated.from_user and (
await bot.get_chat_member(chat.id, chat_member_updated.from_user.id)
).status in {ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.OWNER}: ).status in {ChatMemberStatus.ADMINISTRATOR, ChatMemberStatus.OWNER}:
try: try:
msg = await message.reply(ADMIN_MSG) msg = await client.send_message(chat.id, ADMIN_MSG)
except Exception: except Exception:
return return
add_delete_message_job(msg) add_delete_message_job(msg)
return return
try: try:
msg = await message.reply(MSG % user.mention) msg = await client.send_message(chat.id, MSG % user.mention)
except Exception: except Exception:
return return
try: try:
@ -49,8 +74,6 @@ async def chat_members_handle(client: Client, message: Message):
with contextlib.suppress(Exception): with contextlib.suppress(Exception):
await msg.delete() await msg.delete()
if not msg_.sticker: if not msg_.sticker:
with contextlib.suppress(Exception):
await message.delete()
with contextlib.suppress(Exception): with contextlib.suppress(Exception):
await bot.ban_chat_member(chat.id, user.id, datetime.now() + timedelta(minutes=5)) await bot.ban_chat_member(chat.id, user.id, datetime.now() + timedelta(minutes=5))
with contextlib.suppress(Exception): with contextlib.suppress(Exception):
@ -63,8 +86,6 @@ async def chat_members_handle(client: Client, message: Message):
except TimeoutConversationError: except TimeoutConversationError:
with contextlib.suppress(Exception): with contextlib.suppress(Exception):
await msg.delete() await msg.delete()
with contextlib.suppress(Exception):
await message.delete()
with contextlib.suppress(Exception): with contextlib.suppress(Exception):
await bot.ban_chat_member(chat.id, user.id, datetime.now() + timedelta(minutes=5)) await bot.ban_chat_member(chat.id, user.id, datetime.now() + timedelta(minutes=5))
with contextlib.suppress(Exception): with contextlib.suppress(Exception):

View File

@ -1,22 +0,0 @@
import contextlib
from pyrogram.types import ChatMemberUpdated
from sticker.single_utils import Client
from sticker import bot, log
MSG_PUBLIC = """您好,我发现此群组为公开群组,您需要联系创建者打开 `管理员批准后才能入群` 功能,我就能更好地工作。"""
MSG_SUCCESS = """验证成功,您已经成为群组的一员了!"""
MSG_FAILURE = """验证失败,请重试。"""
@bot.on_chat_member_updated()
async def invite(client: Client, chat_member_updated: ChatMemberUpdated):
chat = chat_member_updated.chat
if user := chat_member_updated.new_chat_member:
if user.user.is_self:
with contextlib.suppress(Exception):
await log(chat, user.invited_by, "NEW_GROUP")
if user.user.is_self and chat.username:
with contextlib.suppress(Exception):
await client.send_message(chat.id, MSG_PUBLIC)

View File

@ -1,5 +1,6 @@
import contextlib import contextlib
from cashews import cache
from pyrogram.types import ChatJoinRequest from pyrogram.types import ChatJoinRequest
from pyrogram import filters from pyrogram import filters
@ -20,6 +21,7 @@ MSG_FAILURE = """验证失败,请重试。"""
@bot.on_chat_join_request() @bot.on_chat_join_request()
async def new_member(client: Client, chat_join_request: ChatJoinRequest): async def new_member(client: Client, chat_join_request: ChatJoinRequest):
chat = chat_join_request.chat chat = chat_join_request.chat
await cache.set(f"cid:{chat.id}", "True", expire=3600, exist=True)
user = chat_join_request.from_user user = chat_join_request.from_user
add_decline_request_job(chat_join_request) add_decline_request_job(chat_join_request)
try: try:

View File

@ -38,7 +38,7 @@ async def re_verify(client: Client, message: Message):
await message.delete() await message.delete()
try: try:
msg = await message.reply_to_message.reply(MSG % user.mention) msg = await message.reply_to_message.reply(MSG % user.mention)
except Exception as e: except Exception as _:
return return
try: try:
msg_ = await client.listen(chat.id, filters=filters.user(user.id), timeout=30) msg_ = await client.listen(chat.id, filters=filters.user(user.id), timeout=30)

View File

@ -1,7 +1,8 @@
pyrogram==2.0.106 pyrogram==2.0.106
TgCrypto>=1.2.3 TgCrypto>=1.2.3
PyYAML>=6.0 PyYAML==6.0.1
coloredlogs>=15.0.1 coloredlogs>=15.0.1
sqlitedict==2.0.0 sqlitedict==2.1.0
apscheduler==3.9.1 apscheduler==3.10.4
cashews==6.2.0
pytz pytz

View File

@ -1,14 +1,17 @@
import contextlib import contextlib
from coloredlogs import ColoredFormatter import sys
from cashews import cache
from datetime import datetime, timezone from datetime import datetime, timezone
from logging import getLogger, StreamHandler, CRITICAL, INFO, basicConfig, DEBUG from logging import getLogger, StreamHandler, CRITICAL, INFO, basicConfig, DEBUG
from sticker.config import Config from coloredlogs import ColoredFormatter
from sticker.scheduler import scheduler
import pyromod.listen import pyromod.listen
from pyrogram import Client from pyrogram import Client
import sys
from sticker.config import Config
from sticker.scheduler import scheduler
cache.setup("mem://")
logs = getLogger(__name__) logs = getLogger(__name__)
logging_format = "%(levelname)s [%(asctime)s] [%(name)s] %(message)s" logging_format = "%(levelname)s [%(asctime)s] [%(name)s] %(message)s"
logging_handler = StreamHandler() logging_handler = StreamHandler()