From 19ce22088074a197254ae9c4a6fdd07914c9e435 Mon Sep 17 00:00:00 2001
From: anonymousx97 <88324835+anonymousx97@users.noreply.github.com>
Date: Sun, 15 Dec 2024 16:16:13 +0530
Subject: [PATCH 01/10] feat: use lib side check of valid lengths and prevent
invalid requests being sent.
New Checks in:
send_message
edit_message text/caption
set_administrator_title
update_profile (bio)
---
.../methods/chats/set_administrator_title.py | 4 +++-
.../methods/messages/edit_inline_caption.py | 5 ++++-
pyrogram/methods/messages/edit_inline_text.py | 3 +++
.../methods/messages/edit_message_caption.py | 12 +++++++++++-
pyrogram/methods/messages/edit_message_text.py | 3 +++
pyrogram/methods/messages/send_message.py | 3 +++
pyrogram/methods/users/update_profile.py | 15 +++++++++------
pyrogram/utils.py | 17 +++++++++++++++++
8 files changed, 53 insertions(+), 9 deletions(-)
diff --git a/pyrogram/methods/chats/set_administrator_title.py b/pyrogram/methods/chats/set_administrator_title.py
index 2c77066ed..e1334bb10 100644
--- a/pyrogram/methods/chats/set_administrator_title.py
+++ b/pyrogram/methods/chats/set_administrator_title.py
@@ -19,7 +19,7 @@
from typing import Union
import pyrogram
-from pyrogram import raw
+from pyrogram import raw, utils
class SetAdministratorTitle:
@@ -56,6 +56,8 @@ async def set_administrator_title(
await app.set_administrator_title(chat_id, user_id, "Admin Title")
"""
+ utils.check_valid_length(text=title, arg_type="title", max_length=utils.MAX_ADMIN_RANK_LEN)
+
chat_id = await self.resolve_peer(chat_id)
user_id = await self.resolve_peer(user_id)
diff --git a/pyrogram/methods/messages/edit_inline_caption.py b/pyrogram/methods/messages/edit_inline_caption.py
index fd2f9442c..15f2c5abe 100644
--- a/pyrogram/methods/messages/edit_inline_caption.py
+++ b/pyrogram/methods/messages/edit_inline_caption.py
@@ -19,7 +19,7 @@
from typing import Optional, List
import pyrogram
-from pyrogram import types, enums
+from pyrogram import types, enums, utils
class EditInlineCaption:
@@ -70,6 +70,9 @@ async def edit_inline_caption(
link_preview_options = types.LinkPreviewOptions(
show_above_text=show_caption_above_media
)
+
+ utils.check_valid_length(text=caption, arg_type="caption", max_length=utils.MAX_CAPTION_LEN)
+
return await self.edit_inline_text(
inline_message_id=inline_message_id,
text=caption,
diff --git a/pyrogram/methods/messages/edit_inline_text.py b/pyrogram/methods/messages/edit_inline_text.py
index 2f0dd30ef..2f13a6bf6 100644
--- a/pyrogram/methods/messages/edit_inline_text.py
+++ b/pyrogram/methods/messages/edit_inline_text.py
@@ -80,6 +80,9 @@ async def edit_inline_text(
)
)
"""
+
+ utils.check_valid_length(text=text, arg_type="text", max_length=utils.MAX_MESSAGE_TEXT_LEN)
+
if disable_web_page_preview and link_preview_options:
raise ValueError(
"Parameters `disable_web_page_preview` and `link_preview_options` are mutually "
diff --git a/pyrogram/methods/messages/edit_message_caption.py b/pyrogram/methods/messages/edit_message_caption.py
index 6752d0025..1cbe4f714 100644
--- a/pyrogram/methods/messages/edit_message_caption.py
+++ b/pyrogram/methods/messages/edit_message_caption.py
@@ -20,7 +20,7 @@
from typing import Union, List, Optional
import pyrogram
-from pyrogram import types, enums
+from pyrogram import types, enums, utils
class EditMessageCaption:
@@ -85,6 +85,16 @@ async def edit_message_caption(
show_above_text=show_caption_above_media
)
+ pyrogram.utils.check_valid_length(
+ text=caption,
+ arg_type="caption",
+ max_length=(
+ utils.MAX_PREMIUM_CAPTION_LEN
+ if self.me.is_premium
+ else utils.MAX_CAPTION_LEN
+ ),
+ )
+
return await self.edit_message_text(
chat_id=chat_id,
message_id=message_id,
diff --git a/pyrogram/methods/messages/edit_message_text.py b/pyrogram/methods/messages/edit_message_text.py
index 62ad47f50..6ce4cf8bd 100644
--- a/pyrogram/methods/messages/edit_message_text.py
+++ b/pyrogram/methods/messages/edit_message_text.py
@@ -94,6 +94,9 @@ async def edit_message_text(
)
)
"""
+
+ utils.check_valid_length(text=text, arg_type="text", max_length=utils.MAX_MESSAGE_TEXT_LEN)
+
if disable_web_page_preview and link_preview_options:
raise ValueError(
"Parameters `disable_web_page_preview` and `link_preview_options` are mutually "
diff --git a/pyrogram/methods/messages/send_message.py b/pyrogram/methods/messages/send_message.py
index 21044d614..a28913ac8 100644
--- a/pyrogram/methods/messages/send_message.py
+++ b/pyrogram/methods/messages/send_message.py
@@ -153,6 +153,9 @@ async def send_message(
[InlineKeyboardButton("Docs", url="https://docs.pyrogram.org")]
]))
"""
+
+ utils.check_valid_length(text=text, arg_type="text", max_length=utils.MAX_MESSAGE_TEXT_LEN)
+
if disable_web_page_preview and link_preview_options:
raise ValueError(
"Parameters `disable_web_page_preview` and `link_preview_options` are mutually "
diff --git a/pyrogram/methods/users/update_profile.py b/pyrogram/methods/users/update_profile.py
index 0ec7ee969..2f4b4aeba 100644
--- a/pyrogram/methods/users/update_profile.py
+++ b/pyrogram/methods/users/update_profile.py
@@ -17,16 +17,16 @@
# along with Pyrogram. If not, see .
import pyrogram
-from pyrogram import raw
+from pyrogram import raw, utils
class UpdateProfile:
async def update_profile(
- self: "pyrogram.Client",
- *,
- first_name: str = None,
- last_name: str = None,
- bio: str = None
+ self: "pyrogram.Client",
+ *,
+ first_name: str = None,
+ last_name: str = None,
+ bio: str = None
) -> bool:
"""Update your profile details such as first name, last name and bio.
@@ -62,6 +62,9 @@ async def update_profile(
# Remove the last name
await app.update_profile(last_name="")
"""
+ if bio:
+ utils.check_valid_length(text=bio, arg_type="bio",
+ max_length=utils.MAX_PREMIUM_USER_BIO_LEN if self.me.is_premium else utils.MAX_USER_BIO_LEN)
return bool(
await self.invoke(
diff --git a/pyrogram/utils.py b/pyrogram/utils.py
index 52a62d8aa..711932ab5 100644
--- a/pyrogram/utils.py
+++ b/pyrogram/utils.py
@@ -245,6 +245,23 @@ def unpack_inline_message_id(inline_message_id: str) -> "raw.base.InputBotInline
MAX_USER_ID_OLD = 2147483647
MAX_USER_ID = 999999999999
+MAX_CAPTION_LEN = 1024
+MAX_PREMIUM_CAPTION_LEN = 4096
+MAX_USER_BIO_LEN = 70
+MAX_PREMIUM_USER_BIO_LEN = 140
+MAX_MESSAGE_TEXT_LEN = 4096
+MAX_ADMIN_RANK_LEN = 16
+
+
+def check_valid_length(text: str, arg_type: str, max_length: int):
+ if not isinstance(text, str):
+ raise ValueError(f"Argument {arg_type} must be a str")
+
+ text_length = len(text)
+
+ assert bool(0 < text_length <= max_length), \
+ f"Invalid length of {text_length} for arg {arg_type}\nValid Lengths: 1-{max_length}"
+
def get_raw_peer_id(peer: raw.base.Peer) -> Optional[int]:
"""Get the raw peer id from a Peer object"""
From 11de34919e5ceb181522882c9e5fb770d55bcad9 Mon Sep 17 00:00:00 2001
From: Shrimadhav U K
Date: Mon, 16 Dec 2024 10:59:23 +0530
Subject: [PATCH 02/10] minor change in Constants
---
.../methods/chats/set_administrator_title.py | 9 ++--
.../methods/messages/edit_inline_caption.py | 2 -
pyrogram/methods/messages/edit_inline_text.py | 10 ++++-
.../methods/messages/edit_message_caption.py | 10 -----
.../methods/messages/edit_message_text.py | 10 ++++-
pyrogram/methods/messages/send_message.py | 6 ++-
pyrogram/methods/users/update_profile.py | 12 +++++-
pyrogram/utils.py | 43 +++++++++++--------
8 files changed, 62 insertions(+), 40 deletions(-)
diff --git a/pyrogram/methods/chats/set_administrator_title.py b/pyrogram/methods/chats/set_administrator_title.py
index e1334bb10..422f4036b 100644
--- a/pyrogram/methods/chats/set_administrator_title.py
+++ b/pyrogram/methods/chats/set_administrator_title.py
@@ -16,7 +16,7 @@
# You should have received a copy of the GNU Lesser General Public License
# along with Pyrogram. If not, see .
-from typing import Union
+from typing import Optional, Union
import pyrogram
from pyrogram import raw, utils
@@ -27,7 +27,7 @@ async def set_administrator_title(
self: "pyrogram.Client",
chat_id: Union[int, str],
user_id: Union[int, str],
- title: str,
+ title: Optional[str] = None,
) -> bool:
"""Set a custom title (rank) to an administrator of a supergroup.
@@ -56,7 +56,10 @@ async def set_administrator_title(
await app.set_administrator_title(chat_id, user_id, "Admin Title")
"""
- utils.check_valid_length(text=title, arg_type="title", max_length=utils.MAX_ADMIN_RANK_LEN)
+
+ if title:
+ cc = utils.Constants()
+ cc.check_valid_length(text=title, arg_type="title", max_length="MAX_ADMIN_RANK_LEN")
chat_id = await self.resolve_peer(chat_id)
user_id = await self.resolve_peer(user_id)
diff --git a/pyrogram/methods/messages/edit_inline_caption.py b/pyrogram/methods/messages/edit_inline_caption.py
index 15f2c5abe..55dff0741 100644
--- a/pyrogram/methods/messages/edit_inline_caption.py
+++ b/pyrogram/methods/messages/edit_inline_caption.py
@@ -71,8 +71,6 @@ async def edit_inline_caption(
show_above_text=show_caption_above_media
)
- utils.check_valid_length(text=caption, arg_type="caption", max_length=utils.MAX_CAPTION_LEN)
-
return await self.edit_inline_text(
inline_message_id=inline_message_id,
text=caption,
diff --git a/pyrogram/methods/messages/edit_inline_text.py b/pyrogram/methods/messages/edit_inline_text.py
index 2f13a6bf6..3b2bf6762 100644
--- a/pyrogram/methods/messages/edit_inline_text.py
+++ b/pyrogram/methods/messages/edit_inline_text.py
@@ -81,7 +81,12 @@ async def edit_inline_text(
)
"""
- utils.check_valid_length(text=text, arg_type="text", max_length=utils.MAX_MESSAGE_TEXT_LEN)
+ message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values()
+
+ if message:
+ cc = utils.Constants()
+ # TODO
+ cc.check_valid_length(text=message, arg_type="text", max_length="MAX_MESSAGE_TEXT_LEN")
if disable_web_page_preview and link_preview_options:
raise ValueError(
@@ -109,7 +114,8 @@ async def edit_inline_text(
no_webpage=link_preview_options.is_disabled if link_preview_options else None,
invert_media=link_preview_options.show_above_text if link_preview_options else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
- **await utils.parse_text_entities(self, text, parse_mode, entities)
+ message=message,
+ entities=entities
),
sleep_threshold=self.sleep_threshold
)
diff --git a/pyrogram/methods/messages/edit_message_caption.py b/pyrogram/methods/messages/edit_message_caption.py
index 1cbe4f714..bf04392a6 100644
--- a/pyrogram/methods/messages/edit_message_caption.py
+++ b/pyrogram/methods/messages/edit_message_caption.py
@@ -85,16 +85,6 @@ async def edit_message_caption(
show_above_text=show_caption_above_media
)
- pyrogram.utils.check_valid_length(
- text=caption,
- arg_type="caption",
- max_length=(
- utils.MAX_PREMIUM_CAPTION_LEN
- if self.me.is_premium
- else utils.MAX_CAPTION_LEN
- ),
- )
-
return await self.edit_message_text(
chat_id=chat_id,
message_id=message_id,
diff --git a/pyrogram/methods/messages/edit_message_text.py b/pyrogram/methods/messages/edit_message_text.py
index 6ce4cf8bd..f520f9147 100644
--- a/pyrogram/methods/messages/edit_message_text.py
+++ b/pyrogram/methods/messages/edit_message_text.py
@@ -95,7 +95,12 @@ async def edit_message_text(
)
"""
- utils.check_valid_length(text=text, arg_type="text", max_length=utils.MAX_MESSAGE_TEXT_LEN)
+ message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values()
+
+ if message:
+ cc = utils.Constants()
+ # TODO
+ cc.check_valid_length(text=message, arg_type="text", max_length="MAX_MESSAGE_TEXT_LEN")
if disable_web_page_preview and link_preview_options:
raise ValueError(
@@ -132,7 +137,8 @@ async def edit_message_text(
media=media,
reply_markup=await reply_markup.write(self) if reply_markup else None,
schedule_date=utils.datetime_to_timestamp(schedule_date),
- **await utils.parse_text_entities(self, text, parse_mode, entities)
+ message=message,
+ entities=entities
)
session = None
business_connection = None
diff --git a/pyrogram/methods/messages/send_message.py b/pyrogram/methods/messages/send_message.py
index a28913ac8..d556fd7b5 100644
--- a/pyrogram/methods/messages/send_message.py
+++ b/pyrogram/methods/messages/send_message.py
@@ -154,8 +154,6 @@ async def send_message(
]))
"""
- utils.check_valid_length(text=text, arg_type="text", max_length=utils.MAX_MESSAGE_TEXT_LEN)
-
if disable_web_page_preview and link_preview_options:
raise ValueError(
"Parameters `disable_web_page_preview` and `link_preview_options` are mutually "
@@ -191,6 +189,10 @@ async def send_message(
)
message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values()
+ if message:
+ cc = utils.Constants()
+ cc.check_valid_length(text=message, arg_type="text", max_length="MAX_MESSAGE_TEXT_LEN")
+
session = None
business_connection = None
if business_connection_id:
diff --git a/pyrogram/methods/users/update_profile.py b/pyrogram/methods/users/update_profile.py
index 2f4b4aeba..dd5430502 100644
--- a/pyrogram/methods/users/update_profile.py
+++ b/pyrogram/methods/users/update_profile.py
@@ -63,8 +63,16 @@ async def update_profile(
await app.update_profile(last_name="")
"""
if bio:
- utils.check_valid_length(text=bio, arg_type="bio",
- max_length=utils.MAX_PREMIUM_USER_BIO_LEN if self.me.is_premium else utils.MAX_USER_BIO_LEN)
+ cc = utils.Constants()
+ cc.check_valid_length(
+ text=bio,
+ arg_type="bio",
+ max_length=(
+ "MAX_PREMIUM_USER_BIO_LEN"
+ if self.me and self.me.is_premium
+ else "MAX_USER_BIO_LEN"
+ )
+ )
return bool(
await self.invoke(
diff --git a/pyrogram/utils.py b/pyrogram/utils.py
index 711932ab5..9ef71fb19 100644
--- a/pyrogram/utils.py
+++ b/pyrogram/utils.py
@@ -245,23 +245,6 @@ def unpack_inline_message_id(inline_message_id: str) -> "raw.base.InputBotInline
MAX_USER_ID_OLD = 2147483647
MAX_USER_ID = 999999999999
-MAX_CAPTION_LEN = 1024
-MAX_PREMIUM_CAPTION_LEN = 4096
-MAX_USER_BIO_LEN = 70
-MAX_PREMIUM_USER_BIO_LEN = 140
-MAX_MESSAGE_TEXT_LEN = 4096
-MAX_ADMIN_RANK_LEN = 16
-
-
-def check_valid_length(text: str, arg_type: str, max_length: int):
- if not isinstance(text, str):
- raise ValueError(f"Argument {arg_type} must be a str")
-
- text_length = len(text)
-
- assert bool(0 < text_length <= max_length), \
- f"Invalid length of {text_length} for arg {arg_type}\nValid Lengths: 1-{max_length}"
-
def get_raw_peer_id(peer: raw.base.Peer) -> Optional[int]:
"""Get the raw peer id from a Peer object"""
@@ -609,3 +592,29 @@ def fix_up_voice_audio_uri(
mime_type = "audio/ogg"
# BEWARE: https://t.me/c/1279877202/74
return mime_type
+
+
+class Constants:
+ # TODO: https://github.com/pyrogram/pyrogram/pull/323#issuecomment-629629992
+ # Text of the message to be sent, 1-4096 characters
+ MAX_MESSAGE_TEXT_LEN = 4096
+
+ # Caption for the animation, audio, document, photo, video or voice, 0-1024 characters
+ MAX_CAPTION_LEN = 1024
+ MAX_PREMIUM_CAPTION_LEN = 2048
+
+ MAX_USER_BIO_LEN = 70
+ MAX_PREMIUM_USER_BIO_LEN = 140
+
+ MAX_ADMIN_RANK_LEN = 16
+
+
+ def check_valid_length(self, text: str, arg_type: str, max_length_tye: str):
+ if not isinstance(text, str):
+ raise ValueError(f"Argument {arg_type} must be a str")
+
+ text_length = len(text)
+ max_length = getattr(self, max_length_tye, 0) # TODO
+
+ assert bool(0 < text_length <= max_length), \
+ f"Invalid length of {text_length} for arg {arg_type}\nValid Lengths: 1-{max_length}"
From 722a2f619d3c94612d7e6de9cdfa8a30f1bb8119 Mon Sep 17 00:00:00 2001
From: Shrimadhav U K
Date: Mon, 23 Dec 2024 10:04:25 +0530
Subject: [PATCH 03/10] another trying attempt
---
pyrogram/session/session.py | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py
index 036d6a7a8..82a06bfcb 100644
--- a/pyrogram/session/session.py
+++ b/pyrogram/session/session.py
@@ -125,7 +125,7 @@ async def start(self):
await self.send(raw.functions.Ping(ping_id=0), timeout=self.START_TIMEOUT)
if not self.is_cdn:
- await self.send(
+ cfg = await self.send(
raw.functions.InvokeWithLayer(
layer=layer,
query=raw.functions.InitConnection(
@@ -146,6 +146,9 @@ async def start(self):
),
timeout=self.START_TIMEOUT
)
+ if isinstance(cfg, raw.types.Config): # TODO
+ pyrogram.utils.Constants.MAX_MESSAGE_TEXT_LEN = cfg.message_length_max
+ pyrogram.utils.Constants.MAX_CAPTION_LEN = cfg.caption_length_max
self.ping_task = self.loop.create_task(self.ping_worker())
From 5e9accffc6d3047bf3ab49802a17cf190d196203 Mon Sep 17 00:00:00 2001
From: Shrimadhav U K
Date: Tue, 24 Dec 2024 09:17:57 +0530
Subject: [PATCH 04/10] Testing Changes ?
make Client attribute instead of Global constant
---
pyrogram/client.py | 30 +++++++++++++++++++
.../methods/chats/set_administrator_title.py | 3 +-
pyrogram/methods/messages/edit_inline_text.py | 4 +--
.../methods/messages/edit_message_text.py | 4 +--
pyrogram/methods/messages/send_message.py | 3 +-
pyrogram/methods/users/update_profile.py | 7 ++---
pyrogram/session/session.py | 4 +--
pyrogram/utils.py | 26 ----------------
8 files changed, 39 insertions(+), 42 deletions(-)
diff --git a/pyrogram/client.py b/pyrogram/client.py
index 7d39a8eb3..e4ce7f925 100644
--- a/pyrogram/client.py
+++ b/pyrogram/client.py
@@ -362,6 +362,8 @@ def __init__(
self.message_cache = Cache(self.max_message_cache_size)
self.business_user_connection_cache = Cache(self.max_business_user_connection_cache_size)
+ self.app_constant = Constant()
+
# Sometimes, for some reason, the server will stop sending updates and will only respond to pings.
# This watchdog will invoke updates.GetState in order to wake up the server and enable it sending updates again
# after some idle time has been detected.
@@ -1240,3 +1242,31 @@ def __setitem__(self, key, value):
if len(self.store) > self.capacity:
for _ in range(self.capacity // 2 + 1):
del self.store[next(iter(self.store))]
+
+
+class Constant:
+ # Text of the message to be sent, 1-4096 characters
+ MAX_MESSAGE_LENGTH = (1, 4096)
+
+ # Caption for the animation, audio, document, photo, video or voice, 0-1024 characters
+ MAX_CAPTION_LENGTH = (0, 1024)
+
+ MAX_USERBIO_LENGTH = (0, 70)
+ MAX_PREMIUM_USERBIO_LENGTH = (0, 140)
+
+ MAX_ADMIN_RANK_LENGTH = (0, 16)
+
+
+ def __init__(self):
+ super().__init__()
+
+
+ def check_valid_length(self, text: str, arg_type: str, max_length_tye: str):
+ if not isinstance(text, str):
+ raise ValueError(f"Argument {arg_type} must be a str")
+
+ text_length = len(text)
+ max_length = getattr(self, max_length_tye, (0, 0))
+
+ assert bool(max_length[0] < text_length <= max_length[1]), \
+ f"Invalid length of {text_length} for arg {arg_type}\nValid Lengths: {max_length[0]}-{max_length[1]}"
diff --git a/pyrogram/methods/chats/set_administrator_title.py b/pyrogram/methods/chats/set_administrator_title.py
index 422f4036b..c42f1d403 100644
--- a/pyrogram/methods/chats/set_administrator_title.py
+++ b/pyrogram/methods/chats/set_administrator_title.py
@@ -58,8 +58,7 @@ async def set_administrator_title(
"""
if title:
- cc = utils.Constants()
- cc.check_valid_length(text=title, arg_type="title", max_length="MAX_ADMIN_RANK_LEN")
+ self.app_constant.check_valid_length(text=title, arg_type="title", max_length="MAX_ADMIN_RANK_LENGTH")
chat_id = await self.resolve_peer(chat_id)
user_id = await self.resolve_peer(user_id)
diff --git a/pyrogram/methods/messages/edit_inline_text.py b/pyrogram/methods/messages/edit_inline_text.py
index 3b2bf6762..b00d4e0f9 100644
--- a/pyrogram/methods/messages/edit_inline_text.py
+++ b/pyrogram/methods/messages/edit_inline_text.py
@@ -84,9 +84,7 @@ async def edit_inline_text(
message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values()
if message:
- cc = utils.Constants()
- # TODO
- cc.check_valid_length(text=message, arg_type="text", max_length="MAX_MESSAGE_TEXT_LEN")
+ self.app_constant.check_valid_length(text=message, arg_type="text", max_length="MAX_MESSAGE_LENGTH")
if disable_web_page_preview and link_preview_options:
raise ValueError(
diff --git a/pyrogram/methods/messages/edit_message_text.py b/pyrogram/methods/messages/edit_message_text.py
index f520f9147..5ec8821f3 100644
--- a/pyrogram/methods/messages/edit_message_text.py
+++ b/pyrogram/methods/messages/edit_message_text.py
@@ -98,9 +98,7 @@ async def edit_message_text(
message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values()
if message:
- cc = utils.Constants()
- # TODO
- cc.check_valid_length(text=message, arg_type="text", max_length="MAX_MESSAGE_TEXT_LEN")
+ self.app_constant.check_valid_length(text=message, arg_type="text", max_length="MAX_MESSAGE_LENGTH")
if disable_web_page_preview and link_preview_options:
raise ValueError(
diff --git a/pyrogram/methods/messages/send_message.py b/pyrogram/methods/messages/send_message.py
index d556fd7b5..6fbcf6ff2 100644
--- a/pyrogram/methods/messages/send_message.py
+++ b/pyrogram/methods/messages/send_message.py
@@ -190,8 +190,7 @@ async def send_message(
message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values()
if message:
- cc = utils.Constants()
- cc.check_valid_length(text=message, arg_type="text", max_length="MAX_MESSAGE_TEXT_LEN")
+ self.app_constant.check_valid_length(text=message, arg_type="text", max_length="MAX_MESSAGE_LENGTH")
session = None
business_connection = None
diff --git a/pyrogram/methods/users/update_profile.py b/pyrogram/methods/users/update_profile.py
index dd5430502..ef777f824 100644
--- a/pyrogram/methods/users/update_profile.py
+++ b/pyrogram/methods/users/update_profile.py
@@ -63,14 +63,13 @@ async def update_profile(
await app.update_profile(last_name="")
"""
if bio:
- cc = utils.Constants()
- cc.check_valid_length(
+ self.app_constant.check_valid_length(
text=bio,
arg_type="bio",
max_length=(
- "MAX_PREMIUM_USER_BIO_LEN"
+ "MAX_PREMIUM_USERBIO_LENGTH"
if self.me and self.me.is_premium
- else "MAX_USER_BIO_LEN"
+ else "MAX_USERBIO_LENGTH"
)
)
diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py
index 82a06bfcb..9e73fafb8 100644
--- a/pyrogram/session/session.py
+++ b/pyrogram/session/session.py
@@ -147,8 +147,8 @@ async def start(self):
timeout=self.START_TIMEOUT
)
if isinstance(cfg, raw.types.Config): # TODO
- pyrogram.utils.Constants.MAX_MESSAGE_TEXT_LEN = cfg.message_length_max
- pyrogram.utils.Constants.MAX_CAPTION_LEN = cfg.caption_length_max
+ self.client.app_constant.MAX_MESSAGE_LENGTH[1] = cfg.message_length_max
+ self.client.app_constant.MAX_CAPTION_LENGTH[1] = cfg.caption_length_max
self.ping_task = self.loop.create_task(self.ping_worker())
diff --git a/pyrogram/utils.py b/pyrogram/utils.py
index 9ef71fb19..52a62d8aa 100644
--- a/pyrogram/utils.py
+++ b/pyrogram/utils.py
@@ -592,29 +592,3 @@ def fix_up_voice_audio_uri(
mime_type = "audio/ogg"
# BEWARE: https://t.me/c/1279877202/74
return mime_type
-
-
-class Constants:
- # TODO: https://github.com/pyrogram/pyrogram/pull/323#issuecomment-629629992
- # Text of the message to be sent, 1-4096 characters
- MAX_MESSAGE_TEXT_LEN = 4096
-
- # Caption for the animation, audio, document, photo, video or voice, 0-1024 characters
- MAX_CAPTION_LEN = 1024
- MAX_PREMIUM_CAPTION_LEN = 2048
-
- MAX_USER_BIO_LEN = 70
- MAX_PREMIUM_USER_BIO_LEN = 140
-
- MAX_ADMIN_RANK_LEN = 16
-
-
- def check_valid_length(self, text: str, arg_type: str, max_length_tye: str):
- if not isinstance(text, str):
- raise ValueError(f"Argument {arg_type} must be a str")
-
- text_length = len(text)
- max_length = getattr(self, max_length_tye, 0) # TODO
-
- assert bool(0 < text_length <= max_length), \
- f"Invalid length of {text_length} for arg {arg_type}\nValid Lengths: 1-{max_length}"
From 0eb8ee5325aa751c307531e8ad666b1dbfb1d5a6 Mon Sep 17 00:00:00 2001
From: shriMADhav U k
Date: Tue, 24 Dec 2024 14:32:57 +0100
Subject: [PATCH 05/10] Try to fix some more conditions
---
pyrogram/methods/chats/set_administrator_title.py | 3 ++-
pyrogram/methods/messages/edit_inline_text.py | 2 +-
pyrogram/methods/messages/edit_message_text.py | 2 +-
pyrogram/methods/messages/send_message.py | 2 +-
pyrogram/methods/users/update_profile.py | 2 +-
pyrogram/session/session.py | 4 ++--
6 files changed, 8 insertions(+), 7 deletions(-)
diff --git a/pyrogram/methods/chats/set_administrator_title.py b/pyrogram/methods/chats/set_administrator_title.py
index c42f1d403..bb98a8b9d 100644
--- a/pyrogram/methods/chats/set_administrator_title.py
+++ b/pyrogram/methods/chats/set_administrator_title.py
@@ -45,6 +45,7 @@ async def set_administrator_title(
For a contact that exists in your Telegram address book you can use his phone number (str).
title (``str``, *optional*):
+ New custom title for the administrator; 0-16 characters, emoji are not allowed.
A custom title that will be shown to all members instead of "Owner" or "Admin".
Pass None or "" (empty string) to remove the custom title.
@@ -58,7 +59,7 @@ async def set_administrator_title(
"""
if title:
- self.app_constant.check_valid_length(text=title, arg_type="title", max_length="MAX_ADMIN_RANK_LENGTH")
+ self.app_constant.check_valid_length(text=title, arg_type="title", max_length_tye="MAX_ADMIN_RANK_LENGTH")
chat_id = await self.resolve_peer(chat_id)
user_id = await self.resolve_peer(user_id)
diff --git a/pyrogram/methods/messages/edit_inline_text.py b/pyrogram/methods/messages/edit_inline_text.py
index b00d4e0f9..984505d21 100644
--- a/pyrogram/methods/messages/edit_inline_text.py
+++ b/pyrogram/methods/messages/edit_inline_text.py
@@ -84,7 +84,7 @@ async def edit_inline_text(
message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values()
if message:
- self.app_constant.check_valid_length(text=message, arg_type="text", max_length="MAX_MESSAGE_LENGTH")
+ self.app_constant.check_valid_length(text=message, arg_type="text", max_length_tye="MAX_MESSAGE_LENGTH")
if disable_web_page_preview and link_preview_options:
raise ValueError(
diff --git a/pyrogram/methods/messages/edit_message_text.py b/pyrogram/methods/messages/edit_message_text.py
index 5ec8821f3..016587843 100644
--- a/pyrogram/methods/messages/edit_message_text.py
+++ b/pyrogram/methods/messages/edit_message_text.py
@@ -98,7 +98,7 @@ async def edit_message_text(
message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values()
if message:
- self.app_constant.check_valid_length(text=message, arg_type="text", max_length="MAX_MESSAGE_LENGTH")
+ self.app_constant.check_valid_length(text=message, arg_type="text", max_length_tye="MAX_MESSAGE_LENGTH")
if disable_web_page_preview and link_preview_options:
raise ValueError(
diff --git a/pyrogram/methods/messages/send_message.py b/pyrogram/methods/messages/send_message.py
index 6fbcf6ff2..cacf2d012 100644
--- a/pyrogram/methods/messages/send_message.py
+++ b/pyrogram/methods/messages/send_message.py
@@ -190,7 +190,7 @@ async def send_message(
message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values()
if message:
- self.app_constant.check_valid_length(text=message, arg_type="text", max_length="MAX_MESSAGE_LENGTH")
+ self.app_constant.check_valid_length(text=message, arg_type="text", max_length_tye="MAX_MESSAGE_LENGTH")
session = None
business_connection = None
diff --git a/pyrogram/methods/users/update_profile.py b/pyrogram/methods/users/update_profile.py
index ef777f824..946ab5972 100644
--- a/pyrogram/methods/users/update_profile.py
+++ b/pyrogram/methods/users/update_profile.py
@@ -66,7 +66,7 @@ async def update_profile(
self.app_constant.check_valid_length(
text=bio,
arg_type="bio",
- max_length=(
+ max_length_tye=(
"MAX_PREMIUM_USERBIO_LENGTH"
if self.me and self.me.is_premium
else "MAX_USERBIO_LENGTH"
diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py
index 9e73fafb8..c592d3673 100644
--- a/pyrogram/session/session.py
+++ b/pyrogram/session/session.py
@@ -147,8 +147,8 @@ async def start(self):
timeout=self.START_TIMEOUT
)
if isinstance(cfg, raw.types.Config): # TODO
- self.client.app_constant.MAX_MESSAGE_LENGTH[1] = cfg.message_length_max
- self.client.app_constant.MAX_CAPTION_LENGTH[1] = cfg.caption_length_max
+ self.client.app_constant.MAX_MESSAGE_LENGTH = (1, cfg.message_length_max)
+ self.client.app_constant.MAX_CAPTION_LENGTH = (0, cfg.caption_length_max)
self.ping_task = self.loop.create_task(self.ping_worker())
From 0596ef43673fc26c17f3386f1bf9744f820aa0a3 Mon Sep 17 00:00:00 2001
From: shriMADhav U k
Date: Tue, 24 Dec 2024 14:45:42 +0100
Subject: [PATCH 06/10] add check for answer_inline_query
---
pyrogram/client.py | 26 ++++++++++++++++----
pyrogram/methods/bots/answer_inline_query.py | 2 ++
2 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/pyrogram/client.py b/pyrogram/client.py
index e4ce7f925..616d51eeb 100644
--- a/pyrogram/client.py
+++ b/pyrogram/client.py
@@ -1256,17 +1256,33 @@ class Constant:
MAX_ADMIN_RANK_LENGTH = (0, 16)
+ # Use the InlineQuery.answer() method. No more than 50 results per query are allowed.
+ MAX_INLINE_QUERY_RESULTS = (0, 50)
def __init__(self):
super().__init__()
- def check_valid_length(self, text: str, arg_type: str, max_length_tye: str):
- if not isinstance(text, str):
- raise ValueError(f"Argument {arg_type} must be a str")
+ def check_valid_length(
+ self,
+ text: Union[List, str],
+ arg_type: str,
+ max_length_tye: str
+ ):
+ if not (
+ isinstance(text, str) or
+ isinstance(text, list)
+ ):
+ raise ValueError(f"Argument {arg_type} must be a str | list")
text_length = len(text)
max_length = getattr(self, max_length_tye, (0, 0))
- assert bool(max_length[0] < text_length <= max_length[1]), \
- f"Invalid length of {text_length} for arg {arg_type}\nValid Lengths: {max_length[0]}-{max_length[1]}"
+ assert (
+ bool(max_length[0] < text_length <= max_length[1]),
+ (
+ f"Invalid length of {text_length} for arg {arg_type}\n"
+ f"Valid Lengths: {max_length[0]}-{max_length[1]}"
+ )
+ )
+
diff --git a/pyrogram/methods/bots/answer_inline_query.py b/pyrogram/methods/bots/answer_inline_query.py
index c3a450a01..1f15a889c 100644
--- a/pyrogram/methods/bots/answer_inline_query.py
+++ b/pyrogram/methods/bots/answer_inline_query.py
@@ -96,6 +96,8 @@ async def answer_inline_query(
InputTextMessageContent("Message content"))])
"""
+ self.app_constant.check_valid_length(text=results, arg_type="results", max_length_tye="MAX_INLINE_QUERY_RESULTS")
+
return await self.invoke(
raw.functions.messages.SetInlineBotResults(
query_id=int(inline_query_id),
From 132b7ddf1aa75bfae124b5ff1abf8724e828fc42 Mon Sep 17 00:00:00 2001
From: shriMADhav U k
Date: Tue, 24 Dec 2024 14:58:02 +0100
Subject: [PATCH 07/10] Add condition check in update_profile
first_name and last_name
---
pyrogram/client.py | 2 ++
.../methods/chats/set_administrator_title.py | 2 +-
.../methods/messages/edit_inline_caption.py | 2 +-
pyrogram/methods/messages/edit_inline_text.py | 8 +-----
.../methods/messages/edit_message_caption.py | 2 +-
.../methods/messages/edit_message_text.py | 8 +-----
pyrogram/methods/users/update_profile.py | 25 +++++++++++++++----
7 files changed, 27 insertions(+), 22 deletions(-)
diff --git a/pyrogram/client.py b/pyrogram/client.py
index 616d51eeb..6a984abe2 100644
--- a/pyrogram/client.py
+++ b/pyrogram/client.py
@@ -1251,6 +1251,8 @@ class Constant:
# Caption for the animation, audio, document, photo, video or voice, 0-1024 characters
MAX_CAPTION_LENGTH = (0, 1024)
+ MAX_USER_FIRSTNAME_LENGTH = (1, 64)
+ MAX_USER_LASTNAME_LENGTH = (0, 64)
MAX_USERBIO_LENGTH = (0, 70)
MAX_PREMIUM_USERBIO_LENGTH = (0, 140)
diff --git a/pyrogram/methods/chats/set_administrator_title.py b/pyrogram/methods/chats/set_administrator_title.py
index bb98a8b9d..aeb7d5d13 100644
--- a/pyrogram/methods/chats/set_administrator_title.py
+++ b/pyrogram/methods/chats/set_administrator_title.py
@@ -19,7 +19,7 @@
from typing import Optional, Union
import pyrogram
-from pyrogram import raw, utils
+from pyrogram import raw
class SetAdministratorTitle:
diff --git a/pyrogram/methods/messages/edit_inline_caption.py b/pyrogram/methods/messages/edit_inline_caption.py
index 55dff0741..9dd5aa2c4 100644
--- a/pyrogram/methods/messages/edit_inline_caption.py
+++ b/pyrogram/methods/messages/edit_inline_caption.py
@@ -19,7 +19,7 @@
from typing import Optional, List
import pyrogram
-from pyrogram import types, enums, utils
+from pyrogram import types, enums
class EditInlineCaption:
diff --git a/pyrogram/methods/messages/edit_inline_text.py b/pyrogram/methods/messages/edit_inline_text.py
index 984505d21..28f3c1ced 100644
--- a/pyrogram/methods/messages/edit_inline_text.py
+++ b/pyrogram/methods/messages/edit_inline_text.py
@@ -81,11 +81,6 @@ async def edit_inline_text(
)
"""
- message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values()
-
- if message:
- self.app_constant.check_valid_length(text=message, arg_type="text", max_length_tye="MAX_MESSAGE_LENGTH")
-
if disable_web_page_preview and link_preview_options:
raise ValueError(
"Parameters `disable_web_page_preview` and `link_preview_options` are mutually "
@@ -112,8 +107,7 @@ async def edit_inline_text(
no_webpage=link_preview_options.is_disabled if link_preview_options else None,
invert_media=link_preview_options.show_above_text if link_preview_options else None,
reply_markup=await reply_markup.write(self) if reply_markup else None,
- message=message,
- entities=entities
+ **await utils.parse_text_entities(self, text, parse_mode, entities)
),
sleep_threshold=self.sleep_threshold
)
diff --git a/pyrogram/methods/messages/edit_message_caption.py b/pyrogram/methods/messages/edit_message_caption.py
index bf04392a6..6752d0025 100644
--- a/pyrogram/methods/messages/edit_message_caption.py
+++ b/pyrogram/methods/messages/edit_message_caption.py
@@ -20,7 +20,7 @@
from typing import Union, List, Optional
import pyrogram
-from pyrogram import types, enums, utils
+from pyrogram import types, enums
class EditMessageCaption:
diff --git a/pyrogram/methods/messages/edit_message_text.py b/pyrogram/methods/messages/edit_message_text.py
index 016587843..57dceab61 100644
--- a/pyrogram/methods/messages/edit_message_text.py
+++ b/pyrogram/methods/messages/edit_message_text.py
@@ -95,11 +95,6 @@ async def edit_message_text(
)
"""
- message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values()
-
- if message:
- self.app_constant.check_valid_length(text=message, arg_type="text", max_length_tye="MAX_MESSAGE_LENGTH")
-
if disable_web_page_preview and link_preview_options:
raise ValueError(
"Parameters `disable_web_page_preview` and `link_preview_options` are mutually "
@@ -135,8 +130,7 @@ async def edit_message_text(
media=media,
reply_markup=await reply_markup.write(self) if reply_markup else None,
schedule_date=utils.datetime_to_timestamp(schedule_date),
- message=message,
- entities=entities
+ **await utils.parse_text_entities(self, text, parse_mode, entities)
)
session = None
business_connection = None
diff --git a/pyrogram/methods/users/update_profile.py b/pyrogram/methods/users/update_profile.py
index 946ab5972..f3a1cf3db 100644
--- a/pyrogram/methods/users/update_profile.py
+++ b/pyrogram/methods/users/update_profile.py
@@ -22,11 +22,11 @@
class UpdateProfile:
async def update_profile(
- self: "pyrogram.Client",
- *,
- first_name: str = None,
- last_name: str = None,
- bio: str = None
+ self: "pyrogram.Client",
+ *,
+ first_name: str = None,
+ last_name: str = None,
+ bio: str = None
) -> bool:
"""Update your profile details such as first name, last name and bio.
@@ -62,6 +62,21 @@ async def update_profile(
# Remove the last name
await app.update_profile(last_name="")
"""
+
+ if first_name:
+ self.app_constant.check_valid_length(
+ text=first_name,
+ arg_type="first_name",
+ max_length_tye="MAX_USER_FIRSTNAME_LENGTH"
+ )
+
+ if last_name:
+ self.app_constant.check_valid_length(
+ text=last_name,
+ arg_type="last_name",
+ max_length_tye="MAX_USER_LASTNAME_LENGTH"
+ )
+
if bio:
self.app_constant.check_valid_length(
text=bio,
From 3d342f9b832d571f0fb7b2a9b6809b4916a087b1 Mon Sep 17 00:00:00 2001
From: shriMADhav U k
Date: Tue, 24 Dec 2024 15:00:24 +0100
Subject: [PATCH 08/10] minor fixes
---
pyrogram/methods/messages/edit_inline_caption.py | 1 -
pyrogram/methods/messages/edit_inline_text.py | 1 -
pyrogram/methods/users/update_profile.py | 2 +-
3 files changed, 1 insertion(+), 3 deletions(-)
diff --git a/pyrogram/methods/messages/edit_inline_caption.py b/pyrogram/methods/messages/edit_inline_caption.py
index 9dd5aa2c4..fd2f9442c 100644
--- a/pyrogram/methods/messages/edit_inline_caption.py
+++ b/pyrogram/methods/messages/edit_inline_caption.py
@@ -70,7 +70,6 @@ async def edit_inline_caption(
link_preview_options = types.LinkPreviewOptions(
show_above_text=show_caption_above_media
)
-
return await self.edit_inline_text(
inline_message_id=inline_message_id,
text=caption,
diff --git a/pyrogram/methods/messages/edit_inline_text.py b/pyrogram/methods/messages/edit_inline_text.py
index 28f3c1ced..2f0dd30ef 100644
--- a/pyrogram/methods/messages/edit_inline_text.py
+++ b/pyrogram/methods/messages/edit_inline_text.py
@@ -80,7 +80,6 @@ async def edit_inline_text(
)
)
"""
-
if disable_web_page_preview and link_preview_options:
raise ValueError(
"Parameters `disable_web_page_preview` and `link_preview_options` are mutually "
diff --git a/pyrogram/methods/users/update_profile.py b/pyrogram/methods/users/update_profile.py
index f3a1cf3db..3398b78fe 100644
--- a/pyrogram/methods/users/update_profile.py
+++ b/pyrogram/methods/users/update_profile.py
@@ -17,7 +17,7 @@
# along with Pyrogram. If not, see .
import pyrogram
-from pyrogram import raw, utils
+from pyrogram import raw
class UpdateProfile:
From 0b193c1d785ea4ff69f76551fa1ea696ac861791 Mon Sep 17 00:00:00 2001
From: anonymousx97 <88324835+anonymousx97@users.noreply.github.com>
Date: Wed, 25 Dec 2024 12:21:13 +0530
Subject: [PATCH 09/10] use arg_type to fetch values and use a single
is_premium check
---
pyrogram/client.py | 58 +++++++++----------
pyrogram/methods/bots/answer_inline_query.py | 2 +-
.../methods/chats/set_administrator_title.py | 2 +-
pyrogram/methods/messages/send_message.py | 2 +-
pyrogram/methods/users/update_profile.py | 27 +--------
5 files changed, 34 insertions(+), 57 deletions(-)
diff --git a/pyrogram/client.py b/pyrogram/client.py
index 6a984abe2..cb3a9eb74 100644
--- a/pyrogram/client.py
+++ b/pyrogram/client.py
@@ -362,7 +362,7 @@ def __init__(
self.message_cache = Cache(self.max_message_cache_size)
self.business_user_connection_cache = Cache(self.max_business_user_connection_cache_size)
- self.app_constant = Constant()
+ self.app_constant = Constant(client_instance=self)
# Sometimes, for some reason, the server will stop sending updates and will only respond to pings.
# This watchdog will invoke updates.GetState in order to wake up the server and enable it sending updates again
@@ -1245,46 +1245,44 @@ def __setitem__(self, key, value):
class Constant:
+ """Tuple of min, max, premium max lengths"""
+
# Text of the message to be sent, 1-4096 characters
- MAX_MESSAGE_LENGTH = (1, 4096)
+ _MAX_TEXT_LENGTH = (1, 4096, 0)
# Caption for the animation, audio, document, photo, video or voice, 0-1024 characters
- MAX_CAPTION_LENGTH = (0, 1024)
+ _MAX_CAPTION_LENGTH = (0, 1024, 0)
- MAX_USER_FIRSTNAME_LENGTH = (1, 64)
- MAX_USER_LASTNAME_LENGTH = (0, 64)
- MAX_USERBIO_LENGTH = (0, 70)
- MAX_PREMIUM_USERBIO_LENGTH = (0, 140)
+ _MAX_FIRST_NAME_LENGTH = (1, 64, 0)
+ _MAX_LAST_NAME_LENGTH = (0, 64, 0)
- MAX_ADMIN_RANK_LENGTH = (0, 16)
+ # USER's BIO
+ _MAX_BIO_LENGTH = (0, 70, 140)
- # Use the InlineQuery.answer() method. No more than 50 results per query are allowed.
- MAX_INLINE_QUERY_RESULTS = (0, 50)
+ # ADMIN TITLE
+ _MAX_TITLE_LENGTH = (0, 16, 0)
- def __init__(self):
- super().__init__()
+ # Use the InlineQuery.answer() method. No more than 50 results per query are allowed.
+ _MAX_RESULTS_LENGTH = (0, 50, 0)
+ def __init__(self, client_instance: Client):
+ self.is_premium = client_instance.me.is_premium
- def check_valid_length(
- self,
- text: Union[List, str],
- arg_type: str,
- max_length_tye: str
- ):
- if not (
- isinstance(text, str) or
- isinstance(text, list)
- ):
+ def check_valid_length(self, text: Union[List, str], arg_type: str, ):
+ if not isinstance(text, (str, list)):
raise ValueError(f"Argument {arg_type} must be a str | list")
text_length = len(text)
- max_length = getattr(self, max_length_tye, (0, 0))
- assert (
- bool(max_length[0] < text_length <= max_length[1]),
- (
- f"Invalid length of {text_length} for arg {arg_type}\n"
- f"Valid Lengths: {max_length[0]}-{max_length[1]}"
- )
- )
+ attr_str = f"_max{arg_type}_length".upper()
+
+ # MIN, MAX, PREM-MAX
+ lengths: tuple[int, int, int] = getattr(self, attr_str, (0, 0, 0))
+
+ min_length = lengths[0]
+ # Check if client is premium and the premium value isn't 0
+ max_length = lengths[2] if self.is_premium and lengths[2] != 0 else lengths[1]
+
+ error_info = f"\nInvalid length of {text_length} for arg {arg_type}\nValid Lengths: {min_length}-{max_length}"
+ assert bool(min_length < text_length <= max_length), error_info
diff --git a/pyrogram/methods/bots/answer_inline_query.py b/pyrogram/methods/bots/answer_inline_query.py
index 1f15a889c..31f524555 100644
--- a/pyrogram/methods/bots/answer_inline_query.py
+++ b/pyrogram/methods/bots/answer_inline_query.py
@@ -96,7 +96,7 @@ async def answer_inline_query(
InputTextMessageContent("Message content"))])
"""
- self.app_constant.check_valid_length(text=results, arg_type="results", max_length_tye="MAX_INLINE_QUERY_RESULTS")
+ self.app_constant.check_valid_length(text=results, arg_type="results")
return await self.invoke(
raw.functions.messages.SetInlineBotResults(
diff --git a/pyrogram/methods/chats/set_administrator_title.py b/pyrogram/methods/chats/set_administrator_title.py
index aeb7d5d13..767f6537d 100644
--- a/pyrogram/methods/chats/set_administrator_title.py
+++ b/pyrogram/methods/chats/set_administrator_title.py
@@ -59,7 +59,7 @@ async def set_administrator_title(
"""
if title:
- self.app_constant.check_valid_length(text=title, arg_type="title", max_length_tye="MAX_ADMIN_RANK_LENGTH")
+ self.app_constant.check_valid_length(text=title, arg_type="title")
chat_id = await self.resolve_peer(chat_id)
user_id = await self.resolve_peer(user_id)
diff --git a/pyrogram/methods/messages/send_message.py b/pyrogram/methods/messages/send_message.py
index cacf2d012..5b012c8a9 100644
--- a/pyrogram/methods/messages/send_message.py
+++ b/pyrogram/methods/messages/send_message.py
@@ -190,7 +190,7 @@ async def send_message(
message, entities = (await utils.parse_text_entities(self, text, parse_mode, entities)).values()
if message:
- self.app_constant.check_valid_length(text=message, arg_type="text", max_length_tye="MAX_MESSAGE_LENGTH")
+ self.app_constant.check_valid_length(text=message, arg_type="text")
session = None
business_connection = None
diff --git a/pyrogram/methods/users/update_profile.py b/pyrogram/methods/users/update_profile.py
index 3398b78fe..c49ac3901 100644
--- a/pyrogram/methods/users/update_profile.py
+++ b/pyrogram/methods/users/update_profile.py
@@ -63,30 +63,9 @@ async def update_profile(
await app.update_profile(last_name="")
"""
- if first_name:
- self.app_constant.check_valid_length(
- text=first_name,
- arg_type="first_name",
- max_length_tye="MAX_USER_FIRSTNAME_LENGTH"
- )
-
- if last_name:
- self.app_constant.check_valid_length(
- text=last_name,
- arg_type="last_name",
- max_length_tye="MAX_USER_LASTNAME_LENGTH"
- )
-
- if bio:
- self.app_constant.check_valid_length(
- text=bio,
- arg_type="bio",
- max_length_tye=(
- "MAX_PREMIUM_USERBIO_LENGTH"
- if self.me and self.me.is_premium
- else "MAX_USERBIO_LENGTH"
- )
- )
+ for var_name in ("first_name", "last_name", "bio"):
+ if var_value := locals()[var_name]:
+ self.app_constant.check_valid_length(text=var_value, arg_type=var_name)
return bool(
await self.invoke(
From b2fde7653feb4106ed2af3ffd6daadd7220e25f2 Mon Sep 17 00:00:00 2001
From: anonymousx97 <88324835+anonymousx97@users.noreply.github.com>
Date: Wed, 25 Dec 2024 12:36:58 +0530
Subject: [PATCH 10/10] adjustments to the new logic
---
pyrogram/client.py | 16 ++++++++--------
pyrogram/session/session.py | 4 ++--
2 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/pyrogram/client.py b/pyrogram/client.py
index cb3a9eb74..711edf04e 100644
--- a/pyrogram/client.py
+++ b/pyrogram/client.py
@@ -1248,22 +1248,22 @@ class Constant:
"""Tuple of min, max, premium max lengths"""
# Text of the message to be sent, 1-4096 characters
- _MAX_TEXT_LENGTH = (1, 4096, 0)
+ MAX_TEXT_LENGTH = (1, 4096, 0)
# Caption for the animation, audio, document, photo, video or voice, 0-1024 characters
- _MAX_CAPTION_LENGTH = (0, 1024, 0)
+ MAX_CAPTION_LENGTH = (0, 1024, 0)
- _MAX_FIRST_NAME_LENGTH = (1, 64, 0)
- _MAX_LAST_NAME_LENGTH = (0, 64, 0)
+ MAX_FIRST_NAME_LENGTH = (1, 64, 0)
+ MAX_LAST_NAME_LENGTH = (0, 64, 0)
# USER's BIO
- _MAX_BIO_LENGTH = (0, 70, 140)
+ MAX_BIO_LENGTH = (0, 70, 140)
# ADMIN TITLE
- _MAX_TITLE_LENGTH = (0, 16, 0)
+ MAX_TITLE_LENGTH = (0, 16, 0)
# Use the InlineQuery.answer() method. No more than 50 results per query are allowed.
- _MAX_RESULTS_LENGTH = (0, 50, 0)
+ MAX_RESULTS_LENGTH = (0, 50, 0)
def __init__(self, client_instance: Client):
self.is_premium = client_instance.me.is_premium
@@ -1274,7 +1274,7 @@ def check_valid_length(self, text: Union[List, str], arg_type: str, ):
text_length = len(text)
- attr_str = f"_max{arg_type}_length".upper()
+ attr_str = f"max_{arg_type}_length".upper()
# MIN, MAX, PREM-MAX
lengths: tuple[int, int, int] = getattr(self, attr_str, (0, 0, 0))
diff --git a/pyrogram/session/session.py b/pyrogram/session/session.py
index c592d3673..4234552f2 100644
--- a/pyrogram/session/session.py
+++ b/pyrogram/session/session.py
@@ -147,8 +147,8 @@ async def start(self):
timeout=self.START_TIMEOUT
)
if isinstance(cfg, raw.types.Config): # TODO
- self.client.app_constant.MAX_MESSAGE_LENGTH = (1, cfg.message_length_max)
- self.client.app_constant.MAX_CAPTION_LENGTH = (0, cfg.caption_length_max)
+ self.client.app_constant.MAX_TEXT_LENGTH = (1, cfg.message_length_max, cfg.message_length_max)
+ self.client.app_constant.MAX_CAPTION_LENGTH = (0, cfg.caption_length_max, cfg.caption_length_max)
self.ping_task = self.loop.create_task(self.ping_worker())