async def inline_test(event): query = event.pattern_match.group(1) variables = {'search': query} json = requests.post(GRAPHQL, json={'query': anime_query, 'variables': variables}).json()[ 'data'].get('Media', None) if json: msg, info, trailer, image = format_results(json) results=[InputBotInlineResult( id=event.id, type='photo', send_message=InputBotInlineMessageMediaAuto(msg), title=json['title']['romaji'], description='Nothin', url=info, thumb=InputWebDocument( url=image, size=42, mime_type='image/png', attributes=[DocumentAttributeImageSize(w=42, h=42)] ), content=InputWebDocument( url=image, size=42, mime_type='image/png', attributes=[DocumentAttributeImageSize(w=42, h=42)] ) )] await AnimeBot(SetInlineBotResultsRequest(event.id, results=results, cache_time=0))
async def _handle_matrix_file(self, sender_id: TelegramID, event_id: EventID, space: TelegramID, client: 'MautrixTelegramClient', content: MediaMessageEventContent, reply_to: TelegramID) -> None: file = await self.main_intent.download_media(content.url) mime = content.info.mimetype w, h = content.info.width, content.info.height if content.msgtype == MessageType.STICKER: if mime != "image/gif": mime, file, w, h = util.convert_image(file, source_mime=mime, target_type="webp") else: # Remove sticker description content[ "net.maunium.telegram.internal.filename"] = "sticker.gif" content.body = "" file_name = self._get_file_meta( content["net.maunium.telegram.internal.filename"], mime) attributes = [DocumentAttributeFilename(file_name=file_name)] if w and h: attributes.append(DocumentAttributeImageSize(w, h)) caption = content.body if content.body.lower() != file_name.lower( ) else None media = await client.upload_file_direct( file, mime, attributes, file_name, max_image_size=config["bridge.image_as_file_size"] * 1000**2) async with self.send_lock(sender_id): if await self._matrix_document_edit(client, content, space, caption, media, event_id): return try: response = await client.send_media(self.peer, media, reply_to=reply_to, caption=caption) except (PhotoInvalidDimensionsError, PhotoSaveFileInvalidError, PhotoExtInvalidError): media = InputMediaUploadedDocument(file=media.file, mime_type=mime, attributes=attributes) response = await client.send_media(self.peer, media, reply_to=reply_to, caption=caption) self._add_telegram_message_to_db(event_id, space, 0, response)
async def inline_test2(event): query = event.pattern_match.group(1) variables = {'search': query} json = requests.post(GRAPHQL, json={'query': anime_query, 'variables': variables}).json()[ 'data'].get('Media', None) if json: msg, info, trailer, image = format_results(json) if trailer: buttons =[ [ Button.url("More Info", url=info), Button.url("Trailer 🎬", url=trailer) ] ] else: buttons =[ [ Button.url("More Info", url=info) ] ] builder = event.builder size = DocumentAttributeImageSize(w=42, h=42) results = [] results.append(builder.article( title=json['title']['romaji'], description=f"{json['format']} | {json.get('episodes', 'N/A')} Episodes", url=info, thumb=InputWebDocument( url=image, size=42, attributes=size, mime_type="image/png" ), content=InputWebDocument( url=image, size=42, attributes=size, mime_type="image/png" ), text=msg, buttons=buttons )) await event.answer(results if results else None)
import logging from telethon.sync import TelegramClient from telethon.tl.types import DocumentAttributeImageSize from uniconfig import * from globalconfig import get from _disconnector import disconnect # === Inline settings ========================================================= _THUMB_CONFIG = { 'common': { 'size': 300, 'mime_type': 'image/jpeg', 'attributes': DocumentAttributeImageSize(48, 48) } } COMMON_THUMB_CONFIG = _THUMB_CONFIG['common'] # === Logging and bot settings ================================================ # Set logging logformat = '[%(levelname) 5s/%(asctime)s] %(name)s: %(message)s' logging.basicConfig(format=logformat, level=logging.WARNING) # Set bot configurations: session name, api_id and api_hash session_name = 'translator-bot' if not TEST_MODE else 'translator-bot-test' api_id = eval(get('API_ID')) api_hash = get('API_HASH')
async def _handle_matrix_file( self, sender_id: TelegramID, event_id: EventID, space: TelegramID, client: 'MautrixTelegramClient', content: MediaMessageEventContent, reply_to: TelegramID, caption: TextMessageEventContent = None) -> None: mime = content.info.mimetype w, h = content.info.width, content.info.height file_name = content["net.maunium.telegram.internal.filename"] max_image_size = config["bridge.image_as_file_size"] * 1000**2 if config["bridge.parallel_file_transfer"] and content.url: file_handle, file_size = await parallel_transfer_to_telegram( client, self.main_intent, content.url, sender_id) else: if content.file: if not decrypt_attachment: self.log.warning( f"Can't bridge encrypted media event {event_id}:" " matrix-nio not installed") return file = await self.main_intent.download_media(content.file.url) file = decrypt_attachment(file, content.file.key.key, content.file.hashes.get("sha256"), content.file.iv) else: file = await self.main_intent.download_media(content.url) if content.msgtype == MessageType.STICKER: if mime != "image/gif": mime, file, w, h = util.convert_image(file, source_mime=mime, target_type="webp") else: # Remove sticker description file_name = "sticker.gif" file_handle = await client.upload_file(file) file_size = len(file) file_handle.name = file_name attributes = [DocumentAttributeFilename(file_name=file_name)] if w and h: attributes.append(DocumentAttributeImageSize(w, h)) if (mime == "image/png" or mime == "image/jpeg") and file_size < max_image_size: media = InputMediaUploadedPhoto(file_handle) else: media = InputMediaUploadedDocument(file=file_handle, attributes=attributes, mime_type=mime or "application/octet-stream") caption, entities = self._matrix_event_to_entities( caption) if caption else (None, None) async with self.send_lock(sender_id): if await self._matrix_document_edit(client, content, space, caption, media, event_id): return try: response = await client.send_media(self.peer, media, reply_to=reply_to, caption=caption, entities=entities) except (PhotoInvalidDimensionsError, PhotoSaveFileInvalidError, PhotoExtInvalidError): media = InputMediaUploadedDocument(file=media.file, mime_type=mime, attributes=attributes) response = await client.send_media(self.peer, media, reply_to=reply_to, caption=caption, entities=entities) self._add_telegram_message_to_db(event_id, space, 0, response) await self._send_delivery_receipt(event_id)