【问题标题】:How to receive file_id through python-telegram-bot?如何通过 python-telegram-bot 接收 file_id?
【发布时间】:2016-11-02 15:49:30
【问题描述】:

我正在使用python-telegram-bot 制作一个 Telegram 机器人,我需要一些方法来接收语音消息。为此,我需要下载它们,并且要做到那个,我必须得到他们的file_ids。然而,MessageHandler 处理...嗯,消息,Handler 给了我一个NotImplementedError。有没有办法获得file_id

【问题讨论】:

    标签: python telegram python-telegram-bot


    【解决方案1】:

    我知道这个问题很老,但我在最新版本 (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')
    

    您可以在Transition-guide-to-Version-12.0阅读更多内容

    【讨论】:

      【解决方案2】:

      下载语音消息的最简单方法是使用语音过滤器注册 MessageHandler。文档提供了有关 Filtersvoice 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))
      

      【讨论】:

        【解决方案3】:

        在 13+ 版本中,您需要使用 update.message.voice.file_id 而不是 update.message.audio.file_id。所以代码将是:

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

        【讨论】:

          【解决方案4】:

          我将向您展示一个带有照片文件的示例,但它适用于任何文件(您只需要更改参数)

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多