mirror of
https://github.com/pyrogram/pyrogram
synced 2025-08-28 12:57:52 +00:00
Enhance send_sticker by accepting file_ids and URLs
This commit is contained in:
parent
8446129003
commit
4f615bd6dc
@ -1508,7 +1508,9 @@ class Client:
|
|||||||
|
|
||||||
sticker (``str``):
|
sticker (``str``):
|
||||||
Sticker to send.
|
Sticker to send.
|
||||||
Pass a file path as string to send a sticker that exists on your local machine.
|
Pass a file_id as string to send a sticker that exists on the Telegram servers,
|
||||||
|
pass an HTTP URL as a string for Telegram to get a .webp sticker file from the Internet, or
|
||||||
|
pass a file path as string to upload a new sticker that exists on your local machine.
|
||||||
|
|
||||||
disable_notification (``bool``, optional):
|
disable_notification (``bool``, optional):
|
||||||
Sends the message silently.
|
Sends the message silently.
|
||||||
@ -1529,25 +1531,55 @@ class Client:
|
|||||||
The size of the file.
|
The size of the file.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
On success, the sent Message is returned.
|
On success, the sent :obj:`Message <pyrogram.Message>` is returned.
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
:class:`Error <pyrogram.Error>`
|
:class:`Error <pyrogram.Error>`
|
||||||
"""
|
"""
|
||||||
file = self.save_file(sticker, progress=progress)
|
file = None
|
||||||
|
|
||||||
|
if os.path.exists(sticker):
|
||||||
|
file = self.save_file(sticker, progress=progress)
|
||||||
|
media = types.InputMediaUploadedDocument(
|
||||||
|
mime_type="image/webp",
|
||||||
|
file=file,
|
||||||
|
attributes=[
|
||||||
|
types.DocumentAttributeFilename(os.path.basename(sticker))
|
||||||
|
]
|
||||||
|
)
|
||||||
|
elif sticker.startswith("http"):
|
||||||
|
media = types.InputMediaDocumentExternal(
|
||||||
|
url=sticker
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
decoded = utils.decode(sticker)
|
||||||
|
fmt = "<iiqqqqi" if len(decoded) > 24 else "<iiqq"
|
||||||
|
unpacked = struct.unpack(fmt, decoded)
|
||||||
|
except (AssertionError, binascii.Error, struct.error):
|
||||||
|
raise FileIdInvalid from None
|
||||||
|
else:
|
||||||
|
if unpacked[0] != 8:
|
||||||
|
media_type = Client.MEDIA_TYPE_ID.get(unpacked[0], None)
|
||||||
|
|
||||||
|
if media_type:
|
||||||
|
raise FileIdInvalid("The file_id belongs to a {}".format(media_type))
|
||||||
|
else:
|
||||||
|
raise FileIdInvalid("Unknown media type: {}".format(unpacked[0]))
|
||||||
|
|
||||||
|
media = types.InputMediaDocument(
|
||||||
|
id=types.InputDocument(
|
||||||
|
id=unpacked[2],
|
||||||
|
access_hash=unpacked[3]
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
r = self.send(
|
r = self.send(
|
||||||
functions.messages.SendMedia(
|
functions.messages.SendMedia(
|
||||||
peer=self.resolve_peer(chat_id),
|
peer=self.resolve_peer(chat_id),
|
||||||
media=types.InputMediaUploadedDocument(
|
media=media,
|
||||||
mime_type="image/webp",
|
|
||||||
file=file,
|
|
||||||
attributes=[
|
|
||||||
types.DocumentAttributeFilename(os.path.basename(sticker))
|
|
||||||
]
|
|
||||||
),
|
|
||||||
silent=disable_notification or None,
|
silent=disable_notification or None,
|
||||||
reply_to_msg_id=reply_to_message_id,
|
reply_to_msg_id=reply_to_message_id,
|
||||||
random_id=self.rnd_id(),
|
random_id=self.rnd_id(),
|
||||||
@ -1557,7 +1589,12 @@ class Client:
|
|||||||
except FilePartMissing as e:
|
except FilePartMissing as e:
|
||||||
self.save_file(sticker, file_id=file.id, file_part=e.x)
|
self.save_file(sticker, file_id=file.id, file_part=e.x)
|
||||||
else:
|
else:
|
||||||
return r
|
for i in r.updates:
|
||||||
|
if isinstance(i, (types.UpdateNewMessage, types.UpdateNewChannelMessage)):
|
||||||
|
users = {i.id: i for i in r.users}
|
||||||
|
chats = {i.id: i for i in r.chats}
|
||||||
|
|
||||||
|
return message_parser.parse_message(self, i.message, users, chats)
|
||||||
|
|
||||||
def send_video(self,
|
def send_video(self,
|
||||||
chat_id: int or str,
|
chat_id: int or str,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user