python - 如何在 python-telegram-bot 中获取 file_id(语音)?
问题描述
我对 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')
你会看到里面有什么,它很有用
推荐阅读
- javascript - 在while循环中选择HTML
- python-3.x - pyqt5 广泛使用的多线程或并发处理?
- coldfusion - 图像写入时 Lucee 的问题
- r - (MANOVA.RM) RM 返回错误:找不到主题变量
- reactjs - 通过删除 map 函数进行一次性 API 调用
- chown - 将 /root 的所有权意外更改为 uid 1000(在:安装在 USB pendrive 上的 parted magic live 系统;添加自己的程序时会发生这种情况)
- android - Kotlin 在 Android 上使用 Dialogflow 和 Fuel 实现简单的聊天机器人
- ios - 配置文件导致“iPhone 已拒绝启动请求”。
- powershell - 用于读取 OneDrive 上的文本文件的 Powershell 脚本
- django - Okta 的 django-allauth openid 实现