簡體   English   中英

如何在 python-telegram-bot 中獲取 file_id(語音)?

[英]How can take file_id (voice) in python-telegram-bot?

我對 python 電報機器人中的語音消息有疑問。 如何從用戶那里獲取聲音並在 python-telegram-bot 中處理這個聲音?

為語音文件處理程序嘗試這樣的事情

def voice_handler(update, context):
    bot = context.bot
    file = bot.getFile(update.message.voice.file_id)
    file.download('voice.mp3')

並在主函數中設置 MessageHandler 如下:

def main():
    updater = Updater(token=lashi_bot,use_context=True)
    updater.dispatcher.add_handler(MessageHandler(Filters.voice, voice_handler))

正如您在此示例中所看到的,您可以從中獲取 file_id(voice):

update.message.voice.file_id

您需要的一切都在更新中,您可以在您的函數中打印更新,如下所示:

def voice_handler(update, context):
    bot = context.bot
    file = bot.getFile(update.message.voice.file_id)
    print(update)
    file.download('voice.mp3')

你看看里面有什么,它很有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM