1818
1919import os
2020import re
21+ import io
2122import asyncio
2223import io
2324
@@ -77,21 +78,41 @@ async def edit_inline_media(
7778 caption = media .caption
7879 parse_mode = media .parse_mode
7980
80- if isinstance (media , types .InputMediaPhoto ):
81- if isinstance (media .media , io .BytesIO ) or os .path .isfile (media .media ):
81+ is_photo = isinstance (media , types .InputMediaPhoto )
82+
83+ is_bytes_io = isinstance (media .media , io .BytesIO )
84+ is_uploaded_file = is_bytes_io or os .path .isfile (media .media )
85+
86+ is_external_url = not is_uploaded_file and re .match ("^https?://" , media .media )
87+
88+ if is_bytes_io and not hasattr (media .media , "name" ):
89+ media .media .name = "media"
90+
91+ if is_uploaded_file :
92+ filename_attribute = [
93+ raw .types .DocumentAttributeFilename (
94+ file_name = media .media .name if is_bytes_io else os .path .basename (media .media )
95+ )
96+ ]
97+ else :
98+ filename_attribute = []
99+
100+
101+ if is_photo :
102+ if is_uploaded_file :
82103 media = raw .types .InputMediaUploadedPhoto (
83104 file = await self .save_file (media .media )
84105 )
85- elif re . match ( "^https?://" , media . media ) :
106+ elif is_external_url :
86107 media = raw .types .InputMediaPhotoExternal (
87108 url = media .media
88109 )
89110 else :
90111 media = utils .get_input_media_from_file_id (media .media , FileType .PHOTO )
91112 elif isinstance (media , types .InputMediaVideo ):
92- if isinstance ( media . media , io . BytesIO ) or os . path . isfile ( media . media ) :
113+ if is_uploaded_file :
93114 media = raw .types .InputMediaUploadedDocument (
94- mime_type = self .guess_mime_type (media .media ) or "video/mp4" ,
115+ mime_type = ( None if is_bytes_io else self .guess_mime_type (media .media ) ) or "video/mp4" ,
95116 thumb = await self .save_file (media .thumb ),
96117 file = await self .save_file (media .media ),
97118 attributes = [
@@ -100,45 +121,39 @@ async def edit_inline_media(
100121 duration = media .duration ,
101122 w = media .width ,
102123 h = media .height
103- ),
104- raw .types .DocumentAttributeFilename (
105- file_name = os .path .basename (media .media )
106124 )
107- ]
125+ ] + filename_attribute
108126 )
109- elif re . match ( "^https?://" , media . media ) :
127+ elif is_external_url :
110128 media = raw .types .InputMediaDocumentExternal (
111129 url = media .media
112130 )
113131 else :
114132 media = utils .get_input_media_from_file_id (media .media , FileType .VIDEO )
115133 elif isinstance (media , types .InputMediaAudio ):
116- if isinstance ( media . media , io . BytesIO ) or os . path . isfile ( media . media ) :
134+ if is_uploaded_file :
117135 media = raw .types .InputMediaUploadedDocument (
118- mime_type = self .guess_mime_type (media .media ) or "audio/mpeg" ,
136+ mime_type = ( None if is_bytes_io else self .guess_mime_type (media .media ) ) or "audio/mpeg" ,
119137 thumb = await self .save_file (media .thumb ),
120138 file = await self .save_file (media .media ),
121139 attributes = [
122140 raw .types .DocumentAttributeAudio (
123141 duration = media .duration ,
124142 performer = media .performer ,
125143 title = media .title
126- ),
127- raw .types .DocumentAttributeFilename (
128- file_name = os .path .basename (media .media )
129144 )
130- ]
145+ ] + filename_attribute
131146 )
132- elif re . match ( "^https?://" , media . media ) :
147+ elif is_external_url :
133148 media = raw .types .InputMediaDocumentExternal (
134149 url = media .media
135150 )
136151 else :
137152 media = utils .get_input_media_from_file_id (media .media , FileType .AUDIO )
138153 elif isinstance (media , types .InputMediaAnimation ):
139- if isinstance ( media . media , io . BytesIO ) or os . path . isfile ( media . media ) :
154+ if is_uploaded_file :
140155 media = raw .types .InputMediaUploadedDocument (
141- mime_type = self .guess_mime_type (media .media ) or "video/mp4" ,
156+ mime_type = ( None if is_bytes_io else self .guess_mime_type (media .media ) ) or "video/mp4" ,
142157 thumb = await self .save_file (media .thumb ),
143158 file = await self .save_file (media .media ),
144159 attributes = [
@@ -148,33 +163,26 @@ async def edit_inline_media(
148163 w = media .width ,
149164 h = media .height
150165 ),
151- raw .types .DocumentAttributeFilename (
152- file_name = os .path .basename (media .media )
153- ),
154166 raw .types .DocumentAttributeAnimated ()
155- ],
167+ ] + filename_attribute ,
156168 nosound_video = True
157169 )
158- elif re . match ( "^https?://" , media . media ) :
170+ elif is_external_url :
159171 media = raw .types .InputMediaDocumentExternal (
160172 url = media .media
161173 )
162174 else :
163175 media = utils .get_input_media_from_file_id (media .media , FileType .ANIMATION )
164176 elif isinstance (media , types .InputMediaDocument ):
165- if isinstance ( media . media , io . BytesIO ) or os . path . isfile ( media . media ) :
177+ if is_uploaded_file :
166178 media = raw .types .InputMediaUploadedDocument (
167- mime_type = self .guess_mime_type (media .media ) or "application/zip" ,
179+ mime_type = ( None if is_bytes_io else self .guess_mime_type (media .media ) ) or "application/zip" ,
168180 thumb = await self .save_file (media .thumb ),
169181 file = await self .save_file (media .media ),
170- attributes = [
171- raw .types .DocumentAttributeFilename (
172- file_name = os .path .basename (media .media )
173- )
174- ],
182+ attributes = filename_attribute ,
175183 force_file = True
176184 )
177- elif re . match ( "^https?://" , media . media ) :
185+ elif is_external_url :
178186 media = raw .types .InputMediaDocumentExternal (
179187 url = media .media
180188 )
@@ -186,25 +194,37 @@ async def edit_inline_media(
186194
187195 session = await get_session (self , dc_id )
188196
189- actual_media = await self .invoke (
190- raw .functions .messages .UploadMedia (
191- peer = raw .types .InputPeerSelf (),
192- media = media
197+
198+ if is_uploaded_file :
199+ uploaded_media = await self .invoke (
200+ raw .functions .messages .UploadMedia (
201+ peer = raw .types .InputPeerSelf (),
202+ media = media
203+ )
204+ )
205+
206+ actual_media = raw .types .InputMediaPhoto (
207+ id = raw .types .InputPhoto (
208+ id = uploaded_media .photo .id ,
209+ access_hash = uploaded_media .photo .access_hash ,
210+ file_reference = uploaded_media .photo .file_reference
211+ )
212+ ) if is_photo else raw .types .InputMediaDocument (
213+ id = raw .types .InputDocument (
214+ id = uploaded_media .document .id ,
215+ access_hash = uploaded_media .document .access_hash ,
216+ file_reference = uploaded_media .document .file_reference
217+ )
193218 )
194- )
219+ else :
220+ actual_media = media
195221
196222 for i in range (self .MAX_RETRIES ):
197223 try :
198224 return await session .invoke (
199225 raw .functions .messages .EditInlineBotMessage (
200226 id = unpacked ,
201- media = raw .types .InputMediaDocument (
202- id = raw .types .InputDocument (
203- id = actual_media .document .id ,
204- access_hash = actual_media .document .access_hash ,
205- file_reference = actual_media .document .file_reference
206- )
207- ),
227+ media = actual_media ,
208228 reply_markup = await reply_markup .write (self ) if reply_markup else None ,
209229 ** await self .parser .parse (caption , parse_mode )
210230 ),
0 commit comments