@@ -45,7 +45,7 @@ def send_animation(self,
4545 "pyrogram.ReplyKeyboardRemove" ,
4646 "pyrogram.ForceReply" ] = None ,
4747 progress : callable = None ,
48- progress_args : tuple = ()) -> "pyrogram.Message" :
48+ progress_args : tuple = ()) -> Union [ "pyrogram.Message" , None ] :
4949 """Use this method to send animation files (animation or H.264/MPEG-4 AVC video without sound).
5050
5151 Args:
@@ -119,79 +119,83 @@ def send_animation(self,
119119
120120 Returns:
121121 On success, the sent :obj:`Message <pyrogram.Message>` is returned.
122+ In case the upload is deliberately stopped with :meth:`stop_transmission`, None is returned instead.
122123
123124 Raises:
124125 :class:`Error <pyrogram.Error>` in case of a Telegram RPC error.
125126 """
126127 file = None
127128 style = self .html if parse_mode .lower () == "html" else self .markdown
128129
129- if os .path .exists (animation ):
130- thumb = None if thumb is None else self .save_file (thumb )
131- file = self .save_file (animation , progress = progress , progress_args = progress_args )
132- media = types .InputMediaUploadedDocument (
133- mime_type = mimetypes .types_map [".mp4" ],
134- file = file ,
135- thumb = thumb ,
136- attributes = [
137- types .DocumentAttributeVideo (
138- supports_streaming = True ,
139- duration = duration ,
140- w = width ,
141- h = height
142- ),
143- types .DocumentAttributeFilename (os .path .basename (animation )),
144- types .DocumentAttributeAnimated ()
145- ]
146- )
147- elif animation .startswith ("http" ):
148- media = types .InputMediaDocumentExternal (
149- url = animation
150- )
151- else :
152- try :
153- decoded = utils .decode (animation )
154- fmt = "<iiqqqqi" if len (decoded ) > 24 else "<iiqq"
155- unpacked = struct .unpack (fmt , decoded )
156- except (AssertionError , binascii .Error , struct .error ):
157- raise FileIdInvalid from None
158- else :
159- if unpacked [0 ] != 10 :
160- media_type = BaseClient .MEDIA_TYPE_ID .get (unpacked [0 ], None )
161-
162- if media_type :
163- raise FileIdInvalid ("The file_id belongs to a {}" .format (media_type ))
164- else :
165- raise FileIdInvalid ("Unknown media type: {}" .format (unpacked [0 ]))
166-
167- media = types .InputMediaDocument (
168- id = types .InputDocument (
169- id = unpacked [2 ],
170- access_hash = unpacked [3 ],
171- file_reference = b""
172- )
130+ try :
131+ if os .path .exists (animation ):
132+ thumb = None if thumb is None else self .save_file (thumb )
133+ file = self .save_file (animation , progress = progress , progress_args = progress_args )
134+ media = types .InputMediaUploadedDocument (
135+ mime_type = mimetypes .types_map [".mp4" ],
136+ file = file ,
137+ thumb = thumb ,
138+ attributes = [
139+ types .DocumentAttributeVideo (
140+ supports_streaming = True ,
141+ duration = duration ,
142+ w = width ,
143+ h = height
144+ ),
145+ types .DocumentAttributeFilename (os .path .basename (animation )),
146+ types .DocumentAttributeAnimated ()
147+ ]
173148 )
174-
175- while True :
176- try :
177- r = self .send (
178- functions .messages .SendMedia (
179- peer = self .resolve_peer (chat_id ),
180- media = media ,
181- silent = disable_notification or None ,
182- reply_to_msg_id = reply_to_message_id ,
183- random_id = self .rnd_id (),
184- reply_markup = reply_markup .write () if reply_markup else None ,
185- ** style .parse (caption )
186- )
149+ elif animation .startswith ("http" ):
150+ media = types .InputMediaDocumentExternal (
151+ url = animation
187152 )
188- except FilePartMissing as e :
189- self .save_file (animation , file_id = file .id , file_part = e .x )
190153 else :
191- for i in r .updates :
192- if isinstance (i , (types .UpdateNewMessage , types .UpdateNewChannelMessage )):
193- return pyrogram .Message ._parse (
194- self , i .message ,
195- {i .id : i for i in r .users },
196- {i .id : i for i in r .chats }
154+ try :
155+ decoded = utils .decode (animation )
156+ fmt = "<iiqqqqi" if len (decoded ) > 24 else "<iiqq"
157+ unpacked = struct .unpack (fmt , decoded )
158+ except (AssertionError , binascii .Error , struct .error ):
159+ raise FileIdInvalid from None
160+ else :
161+ if unpacked [0 ] != 10 :
162+ media_type = BaseClient .MEDIA_TYPE_ID .get (unpacked [0 ], None )
163+
164+ if media_type :
165+ raise FileIdInvalid ("The file_id belongs to a {}" .format (media_type ))
166+ else :
167+ raise FileIdInvalid ("Unknown media type: {}" .format (unpacked [0 ]))
168+
169+ media = types .InputMediaDocument (
170+ id = types .InputDocument (
171+ id = unpacked [2 ],
172+ access_hash = unpacked [3 ],
173+ file_reference = b""
197174 )
175+ )
176+
177+ while True :
178+ try :
179+ r = self .send (
180+ functions .messages .SendMedia (
181+ peer = self .resolve_peer (chat_id ),
182+ media = media ,
183+ silent = disable_notification or None ,
184+ reply_to_msg_id = reply_to_message_id ,
185+ random_id = self .rnd_id (),
186+ reply_markup = reply_markup .write () if reply_markup else None ,
187+ ** style .parse (caption )
188+ )
189+ )
190+ except FilePartMissing as e :
191+ self .save_file (animation , file_id = file .id , file_part = e .x )
192+ else :
193+ for i in r .updates :
194+ if isinstance (i , (types .UpdateNewMessage , types .UpdateNewChannelMessage )):
195+ return pyrogram .Message ._parse (
196+ self , i .message ,
197+ {i .id : i for i in r .users },
198+ {i .id : i for i in r .chats }
199+ )
200+ except BaseClient .StopTransmission :
201+ return None
0 commit comments