def test_with_document_file(self, document_file): # noqa: F811 # fixture found in test_document input_media_document = InputMediaDocument(document_file, caption="test 3") assert input_media_document.type == self.type assert isinstance(input_media_document.media, InputFile) assert input_media_document.caption == "test 3"
def show_to_design_article(update: Update, context: CallbackContext): query = update.callback_query context.user_data['CARDMAKER_MENU_MESSAGE_ID'] = query.message.message_id article_list = get_list_to_design(cardmaker=context.user_data['Name']) article_index = int(query.data.split()[1]) - 1 context.user_data['CARDMAKER_ARTICLE_ID'] = article_list[article_index][ 'id'] global IS_CHECKING_TO_DESIGN if not IS_CHECKING_TO_DESIGN: context.user_data['Checking_article_message'] = context.bot.send_document( chat_id=update.effective_message.chat_id, document=article_list[article_index]['file_id'], caption= f'Автор: *{article_list[article_index]["author"]}*\n\nДедлайн: *{article_list[article_index]["deadline"]}*', parse_mode=ParseMode.MARKDOWN, reply_markup=get_cardmaker_ready_article_inline_keyboard(), ) IS_CHECKING_TO_DESIGN = True else: context.bot.edit_message_media( media=InputMediaDocument( media=article_list[article_index]['file_id'], caption=f'Автор: *{article_list[article_index]["author"]}*', parse_mode=ParseMode.MARKDOWN, ), chat_id=update.effective_message.chat_id, message_id=context.user_data['Checking_article_message']. message_id, reply_markup=get_cardmaker_ready_article_inline_keyboard(), )
def test_with_local_files(self): input_media_document = InputMediaDocument( 'tests/data/telegram.mp4', thumb='tests/data/telegram.jpg') assert input_media_document.media == ( Path.cwd() / 'tests/data/telegram.mp4').as_uri() assert input_media_document.thumb == ( Path.cwd() / 'tests/data/telegram.jpg').as_uri()
def show_approval_article(update: Update, context: CallbackContext): query = update.callback_query article_list = get_approval_list() article_index = int(query.data.split()[1]) - 1 context.user_data['Menu_ID'] = query.message.message_id context.user_data['Article_ID'] = article_list[article_index]['id'] context.user_data['Article_index'] = article_index global IS_CHECKING_ARTICLE if not IS_CHECKING_ARTICLE: context.user_data[ 'Checking_article_message'] = context.bot.send_document( chat_id=update.effective_message.chat_id, document=article_list[article_index]['file_id'], caption=f'Автор: *{article_list[article_index]["author"]}*', parse_mode=ParseMode.MARKDOWN, reply_markup=get_article_approve_inline_keyboard(), ) IS_CHECKING_ARTICLE = True else: context.bot.edit_message_media( media=InputMediaDocument( media=article_list[article_index]['file_id'], caption=f'Автор: *{article_list[article_index]["author"]}*', parse_mode=ParseMode.MARKDOWN, ), chat_id=update.effective_message.chat_id, message_id=context.user_data['Checking_article_message']. message_id, reply_markup=get_article_approve_inline_keyboard(), )
def input_media_document(class_thumb_file): return InputMediaDocument( media=TestInputMediaDocument.media, caption=TestInputMediaDocument.caption, thumb=class_thumb_file, parse_mode=TestInputMediaDocument.parse_mode, )
def edited_post(update: Update, context: CallbackContext): message = update.effective_message msg_id = context.bot_data["messages"][message.message_id] bot = context.bot if message.text: bot.edit_message_text(chat_id=GROUP_ID, message_id=msg_id, text=message.text_html) return elif message.effective_attachment: media = None if message.location: bot.edit_message_live_location(chat_id=GROUP_ID, message_id=msg_id, **message.location.to_dict()) return elif message.photo: media = InputMediaPhoto(media=message.photo[-1].file_id, caption=message.caption_html) elif message.video: media = InputMediaVideo(media=message.video.file_id, caption=message.caption_html) if not media: media = InputMediaDocument( media=message.effective_attachment.file_id, caption=message.caption_html) bot.edit_message_media(chat_id=GROUP_ID, message_id=msg_id, media=media)
def input_media_document(class_thumb_file): return InputMediaDocument( media=TestInputMediaDocument.media, caption=TestInputMediaDocument.caption, thumb=class_thumb_file, parse_mode=TestInputMediaDocument.parse_mode, caption_entities=TestInputMediaDocument.caption_entities, disable_content_type_detection=TestInputMediaDocument.disable_content_type_detection, )
async def fetch_queue(urls) -> None: fs = await biliparser(urls) for num, f in enumerate(fs): if isinstance(f, Exception): logger.warning(f"解析错误! {f}") message.reply_text( captions(f), allow_sending_without_reply=True, reply_markup=origin_link(urls[num]), ) continue if f.mediaurls: tasks = [ get_media(f, img, filename=filename, compression=False) for img, filename in zip(f.mediaurls, f.mediafilename) ] medias = await asyncio.gather(*tasks) logger.info(f"上传中: {f.url}") if len(medias) > 1: medias = [InputMediaDocument(media) for media in medias] message.reply_media_group( medias, allow_sending_without_reply=True, ) try: message.reply_text( captions(f), parse_mode=ParseMode.MARKDOWN_V2, allow_sending_without_reply=True, reply_markup=origin_link(f.url), ) except BadRequest as err: logger.exception(err) logger.info(f"{err} -> 去除Markdown: {f.url}") message.reply_text( captions(f, True), allow_sending_without_reply=True, reply_markup=origin_link(f.url), ) else: try: message.reply_document( document=medias[0], caption=captions(f), parse_mode=ParseMode.MARKDOWN_V2, allow_sending_without_reply=True, reply_markup=origin_link(f.url), ) except BadRequest as err: logger.exception(err) logger.info(f"{err} -> 去除Markdown: {f.url}") message.reply_document( document=medias[0], caption=captions(f, True), allow_sending_without_reply=True, reply_markup=origin_link(f.url), )
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 edit_random_post(self, message: Message, subreddit: str) -> None: """ Edit the current Telegram message with another random Reddit post. Parameters ---------- message : Message python-telegram-bot's instance of the Telegram message. subreddit : str Subreddit from which to retrieve the random post. """ msg_is_text = message.caption is None post = reddit.get_post(helpers.get_random_post_url(subreddit)) assert post is not None if ((msg_is_text and message.text_markdown_v2 == post.get_msg()) or message.caption_markdown_v2 == post.get_msg() or (not msg_is_text and post.get_type() in [ContentType.TEXT, ContentType.YOUTUBE])): # if post is the same or message is not text and post is: retry raise PostEqualsMessageError() args = self.get_args({"message_id": message.message_id}) try: if msg_is_text: if post.get_type() == ContentType.YOUTUBE: args["disable_web_page_preview"] = False self.bot.editMessageText(post.get_msg(), **args) else: media: Optional[Media] = None media_args = dict( media=post.media.url, # type: ignore caption=post.get_msg(), parse_mode="MarkdownV2", ) if post.get_type() == ContentType.GIF: media = InputMediaDocument(**media_args) elif post.get_type() == ContentType.VIDEO: media = InputMediaVideo(**media_args) elif post.get_type() == ContentType.PHOTO: media = InputMediaPhoto(**media_args) self.bot.editMessageMedia(media=media, **args) return except Exception as e: raise PostSendError({ "post_url": post.permalink, "media_url": post.media.url } # type: ignore ) from e
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)
def test_with_document(self, document): # noqa: F811 # fixture found in test_document input_media_document = InputMediaDocument(document, caption="test 3") assert input_media_document.type == self.type assert input_media_document.media == document.file_id assert input_media_document.caption == "test 3"
def test_with_local_files(self): input_media_document = InputMediaDocument( data_file("telegram.mp4"), thumb=data_file("telegram.jpg")) assert input_media_document.media == data_file("telegram.mp4").as_uri() assert input_media_document.thumb == data_file("telegram.jpg").as_uri()