def test_with_animation_file(self, animation_file): # noqa: F811 # fixture found in test_animation input_media_animation = InputMediaAnimation(animation_file, caption="test 2") assert input_media_animation.type == self.type assert isinstance(input_media_animation.media, InputFile) assert input_media_animation.caption == "test 2"
def test_send_media_group_custom_filename( self, bot, chat_id, photo_file, # noqa: F811 animation_file, # noqa: F811 audio_file, # noqa: F811 video_file, # noqa: F811 monkeypatch, ): def make_assertion(url, data, **kwargs): result = all(im.media.filename == 'custom_filename' for im in data['media']) # We are a bit hacky here b/c Bot.send_media_group expects a list of Message-dicts return [Message(0, None, None, text=result).to_dict()] monkeypatch.setattr(bot.request, 'post', make_assertion) media = [ InputMediaAnimation(animation_file, filename='custom_filename'), InputMediaAudio(audio_file, filename='custom_filename'), InputMediaPhoto(photo_file, filename='custom_filename'), InputMediaVideo(video_file, filename='custom_filename'), ] assert bot.send_media_group(chat_id, media)[0].text is True
def test_with_animation(self, animation): # noqa: F811 # fixture found in test_animation input_media_animation = InputMediaAnimation(animation, caption="test 2") assert input_media_animation.type == self.type assert input_media_animation.media == animation.file_id assert input_media_animation.caption == "test 2"
def test_with_local_files(self): input_media_animation = InputMediaAnimation( data_file("telegram.mp4"), thumb=data_file("telegram.jpg")) assert input_media_animation.media == data_file( "telegram.mp4").as_uri() assert input_media_animation.thumb == data_file( "telegram.jpg").as_uri()
def test_with_local_files(self): input_media_animation = InputMediaAnimation( 'tests/data/telegram.mp4', thumb='tests/data/telegram.jpg') assert input_media_animation.media == ( Path.cwd() / 'tests/data/telegram.mp4').as_uri() assert input_media_animation.thumb == ( Path.cwd() / 'tests/data/telegram.jpg').as_uri()
async def test_send_media_group_custom_filename( self, bot, chat_id, photo_file, # noqa: F811 animation_file, # noqa: F811 audio_file, # noqa: F811 video_file, # noqa: F811 monkeypatch, ): async def make_assertion(url, request_data: RequestData, *args, **kwargs): result = all( field_tuple[0] == "custom_filename" for field_tuple in request_data.multipart_data.values()) if result is True: raise Exception("Test was successful") monkeypatch.setattr(bot.request, "post", make_assertion) media = [ InputMediaAnimation(animation_file, filename="custom_filename"), InputMediaAudio(audio_file, filename="custom_filename"), InputMediaPhoto(photo_file, filename="custom_filename"), InputMediaVideo(video_file, filename="custom_filename"), ] with pytest.raises(Exception, match="Test was successful"): await bot.send_media_group(chat_id, media)
def input_media_animation(class_thumb_file): return InputMediaAnimation(media=TestInputMediaAnimation.media, caption=TestInputMediaAnimation.caption, parse_mode=TestInputMediaAnimation.parse_mode, width=TestInputMediaAnimation.width, height=TestInputMediaAnimation.height, thumb=class_thumb_file, duration=TestInputMediaAnimation.duration)
def download_medias(self, medias): input_medias = [] for url in medias: path = self.download(url) with open(path, "rb") as f: if 'gif' in path: input_medias.append(InputMediaAnimation(media=f)) else: input_medias.append(InputMediaPhoto(media=f)) return input_medias
def get_input_media(file_id: str, media_type: str): from telegram.files import photosize, video, videonote, document, animation, sticker from telegram import InputMedia, InputMediaPhoto, InputMediaDocument, InputMediaVideo, InputMediaAnimation, Sticker if media_type == f"{photosize.PhotoSize}": return InputMediaPhoto(file_id) if media_type == f"{video.Video}" or media_type == f"{videonote.VideoNote}": return InputMediaVideo(file_id) if media_type == f"{document.Document}": return InputMediaDocument(file_id) if media_type == f"{animation.Animation}": return InputMediaAnimation(file_id)
def _get_media(message: TMessage, caption: str): if message.animation: return InputMediaAnimation(media=message.animation, caption=caption) if message.audio: return InputMediaAudio(media=message.audio, caption=caption) if message.document: return InputMediaDocument(media=message.document, caption=caption) if message.photo: return InputMediaPhoto(media=message.photo[-1], caption=caption) if message.video: return InputMediaVideo(media=message.video, caption=caption)
def help_button(update, context): query = update.callback_query query.answer() _, hid = query.data.split("_") hid = int(hid) keyboard = [[]] next_button = InlineKeyboardButton("próximo", callback_data=f"help_{hid+1}") prev_button = InlineKeyboardButton("anterior", callback_data=f"help_{hid-1}") media = None if hid == 0: caption = ( "Há três tipos de perguntas no bot, sendo elas:\n\n" "▪️ *múltiplas escolhas*\n▪️ *única escolha*\n▪️ *texto livre*.\n\nTodas " "as respostas _são editáveis antes da finalização_ do questionário." ) media = InputMediaAnimation( "CgACAgEAAxkBAAIDz165k3F3dOyCpA0NzXKTkbk2RT_rAAKfAAPJItBFxyufgHgzykAZBA", caption=caption, parse_mode="markdown") keyboard[0].append(next_button) elif hid == 1: caption = ("As perguntas podem ser obrigatórias ou opcionais, as " "*obrigatórias estão marcadas com estrela*.") media = InputMediaPhoto( "AgACAgEAAxkBAAIDjF65gy5cp6uTnlUBypOFFJ-dDw5mAAJRqDEbySLQRQ8xPAq4fpQp371uBgAEAQADAgADeAADe5kCAAEZBA", caption=caption, parse_mode="markdown") keyboard[0].append(prev_button) keyboard[0].append(next_button) elif hid == 2: media = InputMediaVideo( "BAACAgEAAxkBAAIDfF65f7no-7Wmzex-mYwXmgR-EGuZAAIpAQACvlPJRVufPM2G1aFZGQQ", caption="Vídeo demonstrativo de como utilizar o bot.", parse_mode="markdown") keyboard[0].append(prev_button) if media: context.bot.edit_message_media( query.message.chat.id, query.message.message_id, media=media, reply_markup=InlineKeyboardMarkup(keyboard), )
def build_media(parse_mode, med_type): kwargs = {} if parse_mode != ParseMode.HTML: kwargs["parse_mode"] = parse_mode kwargs["caption"] = markdown_caption else: kwargs["caption"] = html_caption if med_type == "animation": return InputMediaAnimation(animation, **kwargs) if med_type == "document": return InputMediaDocument(document, **kwargs) if med_type == "audio": return InputMediaAudio(audio, **kwargs) if med_type == "photo": return InputMediaPhoto(photo, **kwargs) if med_type == "video": return InputMediaVideo(video, **kwargs)
def build_media(parse_mode, med_type): kwargs = {} if parse_mode != ParseMode.HTML: kwargs['parse_mode'] = parse_mode kwargs['caption'] = markdown_caption else: kwargs['caption'] = html_caption if med_type == 'animation': return InputMediaAnimation(animation, **kwargs) if med_type == 'document': return InputMediaDocument(document, **kwargs) if med_type == 'audio': return InputMediaAudio(audio, **kwargs) if med_type == 'photo': return InputMediaPhoto(photo, **kwargs) if med_type == 'video': return InputMediaVideo(video, **kwargs)