简体   繁体   English

如何通过python-telegram-bot接收file_id?

[英]How to receive file_id through python-telegram-bot?

I'm making a Telegram bot using python-telegram-bot , and I need some way to receive voice messages.我正在使用python-telegram-bot制作电报机器人,我需要某种方式来接收语音消息。 For that, I need to download them, and to do that , I have to get their file_id s.为此,我需要下载它们,为此,我必须获取它们的file_id However, the MessageHandler handles... well, messages, and Handler gives me a NotImplementedError .但是, MessageHandler处理......好吧,消息,而Handler给了我一个NotImplementedError Is there a way to get the file_id ?有没有办法获得file_id

I know this question is old but I was facing a problem with this in the latest version (12+)我知道这个问题很老,但我在最新版本 (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.因此,回调函数中的 botpass_user_data 似乎弃用,从现在开始您应该使用基于上下文的回调。

CallbackContext is an object that contains all the extra context information regarding an Update, error or Job. CallbackContext 是一个对象,它包含有关更新、错误或作业的所有额外上下文信息。

to the new style using CallbackContext:使用 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您可以在Transition-guide-to-Version-12.0 中阅读更多内容

The easiest way to download voice messages is to register a MessageHandler with a voice filter.下载语音消息的最简单方法是使用语音过滤器注册 MessageHandler。 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))

In version 13+, you need to use update.message.在 13+ 版本中,您需要使用 update.message。 voice .file_id instead of update.message. voice .file_id 而不是 update.message。 audio .file_id.音频.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')

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM