1818
1919import os
2020import re
21+ import asyncio
2122
2223import pyrogram
2324from pyrogram import raw
2425from pyrogram import types
2526from pyrogram import utils
27+ from pyrogram .errors import RPCError , MediaEmpty
2628from pyrogram .file_id import FileType
2729from .inline_session import get_session
2830
2931
3032class EditInlineMedia :
33+ MAX_RETRIES = 3
34+
3135 async def edit_inline_media (
3236 self : "pyrogram.Client" ,
3337 inline_message_id : str ,
@@ -147,7 +151,8 @@ async def edit_inline_media(
147151 file_name = os .path .basename (media .media )
148152 ),
149153 raw .types .DocumentAttributeAnimated ()
150- ]
154+ ],
155+ nosound_video = True
151156 )
152157 elif re .match ("^https?://" , media .media ):
153158 media = raw .types .InputMediaDocumentExternal (
@@ -165,7 +170,8 @@ async def edit_inline_media(
165170 raw .types .DocumentAttributeFilename (
166171 file_name = os .path .basename (media .media )
167172 )
168- ]
173+ ],
174+ force_file = True
169175 )
170176 elif re .match ("^https?://" , media .media ):
171177 media = raw .types .InputMediaDocumentExternal (
@@ -179,12 +185,34 @@ async def edit_inline_media(
179185
180186 session = await get_session (self , dc_id )
181187
182- return await session .invoke (
183- raw .functions .messages .EditInlineBotMessage (
184- id = unpacked ,
185- media = media ,
186- reply_markup = await reply_markup .write (self ) if reply_markup else None ,
187- ** await self .parser .parse (caption , parse_mode )
188- ),
189- sleep_threshold = self .sleep_threshold
188+ actual_media = await self .invoke (
189+ raw .functions .messages .UploadMedia (
190+ peer = raw .types .InputPeerSelf (),
191+ media = media
192+ )
190193 )
194+
195+ for i in range (self .MAX_RETRIES ):
196+ try :
197+ return await session .invoke (
198+ raw .functions .messages .EditInlineBotMessage (
199+ id = unpacked ,
200+ media = raw .types .InputMediaDocument (
201+ id = raw .types .InputDocument (
202+ id = actual_media .document .id ,
203+ access_hash = actual_media .document .access_hash ,
204+ file_reference = actual_media .document .file_reference
205+ )
206+ ),
207+ reply_markup = await reply_markup .write (self ) if reply_markup else None ,
208+ ** await self .parser .parse (caption , parse_mode )
209+ ),
210+ sleep_threshold = self .sleep_threshold
211+ )
212+ except RPCError as e :
213+ if i == self .MAX_RETRIES - 1 :
214+ raise
215+
216+ if isinstance (e , MediaEmpty ):
217+ # Must wait due to a server race condition
218+ await asyncio .sleep (1 )
0 commit comments