Dan*_*a02 3 python telegram python-telegram-bot
我正在使用 制作 Telegram 机器人python-telegram-bot,我需要某种方式来接收语音消息。为此,我需要下载它们,为此,我必须获取它们的file_ids。但是,MessageHandler句柄......好吧,消息,并Handler给我一个NotImplementedError. 有没有办法获得file_id?
我知道这个问题很老,但我在最新版本 (12+) 中遇到了这个问题
因此,回调函数中的 botpass_user_data 似乎已被弃用,从现在开始您应该使用基于上下文的回调。
CallbackContext 是一个对象,它包含有关更新、错误或作业的所有额外上下文信息。
使用 CallbackContext 的新样式:
def voice_handler(update: Update, context: CallbackContext):
file = context.bot.getFile(update.message.audio.file_id)
file.download('./voice.ogg')
Run Code Online (Sandbox Code Playgroud)
您可以在Transition-guide-to-Version-12.0 中阅读更多内容
下载语音消息的最简单方法是向语音过滤器注册 MessageHandler。文档提供了有关过滤器和语音模块的更多信息。
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))
Run Code Online (Sandbox Code Playgroud)