[Python] Is there a single way to store different media types in Telegram bot?

Discussão em 'Python' iniciado por Stack, Abril 2, 2025.

  1. Stack

    Stack Membro Participativo

    I'm doing different Telegram bots, that can send media to their users.
    For video files I used this code to store video uploaded by admin:

    caption = event.message.text or ""
    file_id = event.message.file.id
    data['file_id'] = file_id
    data['caption'] = caption
    save_data(data)


    And this to restore it and send to a user:

    data = load_data()
    file_id = data['file_id'],
    caption = data['caption']
    bot.send_video(message.chat.id, file_id ], caption=caption)


    To save and restore images I didn't find file_id usage, but this with PKL extension:

    async with client.conversation(user_id) as conv:
    await conv.send_message("Upload a photo")
    photo_event = await conv.get_response()
    save_price_pic(photo_event.photo)

    def save_price_pic(photo):
    with open('price.pkl', 'wb') as file:
    pickle.dump(photo, file)


    And restore:

    def load_price_pic():
    with open('price.pkl', 'rb') as file:
    return pickle.load(file)
    photo = load_price_pic()
    client.send_file(user_id,
    photo,
    caption = "my caption")


    Is there any similar method to work with this media? I'd prefer to use file_id in both cases.

    Continue reading...

Compartilhe esta Página