Skip to content

Commit 93dcbbe

Browse files
committed
Create method get_media_file_id()
Method is used to get file id of different types of media
1 parent 02ae255 commit 93dcbbe

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

telebot/util.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,40 @@ def validate_web_app_data(token: str, raw_init_data: str):
643643
return hmac.new(secret_key.digest(), data_check_string.encode(), sha256).hexdigest() == init_data_hash
644644

645645

646+
def get_media_file_id(message: types.Message, content_types: Optional[List[str]] = None):
647+
"""
648+
Use this method to get file id of message media if it's present.
649+
650+
:param message: The message to get media from
651+
:type message: :class:`telebot.types.Message`
652+
653+
:param content_types: Types of media to look for, by default all types are considered.
654+
:type content_types: :obj:`list[str]`, optional
655+
656+
:return: file_id or :obj:`None`
657+
:rtype: :obj:`str` | :obj:`None`
658+
"""
659+
if content_types is None:
660+
content_types = content_type_media
661+
662+
if "animation" in content_types and message.animation is not None:
663+
return message.animation.file_id
664+
elif "audio" in content_types and message.audio is not None:
665+
return message.audio.file_id
666+
elif "document" in content_types and message.document is not None:
667+
return message.document.file_id
668+
elif "photo" in content_types and message.photo is not None:
669+
return message.photo[0].file_id
670+
elif "sticker" in content_types and message.sticker is not None:
671+
return message.sticker.file_id
672+
elif "video" in content_types and message.video is not None:
673+
return message.video.file_id
674+
elif "video_note" in content_types and message.video_note is not None:
675+
return message.video_note.file_id
676+
elif "voice" in content_types and message.voice is not None:
677+
return message.voice.file_id
678+
679+
646680
__all__ = (
647681
"content_type_media", "content_type_service", "update_types",
648682
"WorkerThread", "AsyncTask", "CustomRequestResponse",
@@ -651,7 +685,7 @@ def validate_web_app_data(token: str, raw_init_data: str):
651685
"chunks", "generate_random_token", "pil_image_to_file",
652686
"is_command", "extract_command", "extract_arguments",
653687
"split_string", "smart_split", "escape", "user_link", "quick_markup",
654-
"antiflood", "parse_web_app_data", "validate_web_app_data",
688+
"antiflood", "get_media_file_id", "parse_web_app_data", "validate_web_app_data",
655689
"or_set", "or_clear", "orify", "OrEvent", "per_thread",
656690
"webhook_google_functions"
657691
)

0 commit comments

Comments
 (0)