How to receive file_id through python-telegram-bot?
Asked Answered
R

4

5

I'm making a Telegram bot using python-telegram-bot, and I need some way to receive voice messages. For that, I need to download them, and to do that, I have to get their file_ids. However, the MessageHandler handles... well, messages, and Handler gives me a NotImplementedError. Is there a way to get the file_id?

Ravenous answered 30/6, 2016 at 13:44 Comment(0)
F
4

The easiest way to download voice messages is to register a MessageHandler with a voice filter. The Docs provide more information on Filters and the voice module.

import telegram
from telegram.ext import Updater

def voice_handler(bot, update):
    file = bot.getFile(update.message.voice.file_id)
    print ("file_id: " + str(update.message.voice.file_id))
    file.download('voice.ogg')

updater = Updater(token='TOKEN')
dispatcher = updater.dispatcher
dispatcher.add_handler(MessageHandler(Filters.voice, voice_handler))
Frequentation answered 20/12, 2016 at 23:36 Comment(0)
S
7

I know this question is old but I was facing a problem with this in the latest version (12+)

So it appears that the bot- pass_user_data in the callback function is deprecated and from now on you should use context based callbacks.

CallbackContext is an object that contains all the extra context information regarding an Update, error or Job.

to the new style using CallbackContext:

def voice_handler(update: Update, context: CallbackContext):
    file = context.bot.getFile(update.message.audio.file_id)
    file.download('./voice.ogg')

You can read more in the Transition-guide-to-Version-12.0

Saraann answered 3/12, 2019 at 8:47 Comment(0)
F
4

The easiest way to download voice messages is to register a MessageHandler with a voice filter. The Docs provide more information on Filters and the voice module.

import telegram
from telegram.ext import Updater

def voice_handler(bot, update):
    file = bot.getFile(update.message.voice.file_id)
    print ("file_id: " + str(update.message.voice.file_id))
    file.download('voice.ogg')

updater = Updater(token='TOKEN')
dispatcher = updater.dispatcher
dispatcher.add_handler(MessageHandler(Filters.voice, voice_handler))
Frequentation answered 20/12, 2016 at 23:36 Comment(0)
A
2

In version 13+, you need to use update.message.voice.file_id instead of update.message.audio.file_id. So the code will be:

def voice_handler(update: Update, context: CallbackContext):
    file = context.bot.getFile(update.message.voice.file_id)
    file.download('./voice.ogg')
Abrasion answered 29/3, 2021 at 4:30 Comment(0)
T
2

I'll show you an example with a photo file, but it works for any file (you'll just need to change the parameters)

from telegram.ext import Updater, CommandHandler
from telegram.ext.callbackcontext import CallbackContext
from telegram.update import Update

def start (update: Update, context: CallbackContext):

    # getting chat_id:
    chatID = update.effective_chat.id

    # sending the photo to discover its file_id:
    photo1 = context.bot.send_photo(chat_id=chatID, photo=open('photo1.jpg','rb'))
    photo1_fileID = photo1.photo[-1].file_id
    context.bot.send_message(chat_id=update.effective_chat.id, text=('file_id photo1.jpg = ' + photo1_fileID))

def main():
    updater = Updater(token='TOKEN', use_context=True)
    dispatcher = updater.dispatcher

    dispatcher.add_handler(CommandHandler('start', start))

    updater.start_polling()
    updater.idle()

if __name__ == '__main__':
    main()
Twinge answered 5/11, 2021 at 5:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.