You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从Telegram Bot向wit.ai发送语音遇错误,求技术解决方案

解决Wit.ai处理Telegram语音文件时的调用错误

看起来你在对接Telegram Bot和Wit.ai做语音意图识别时踩了坑,先看看你的代码,然后咱们一步步排查问题:

你的原代码:

def discover(bot, update, user_data):
    voice = bot.getFile(update.message.voice.file_id)
    voice.download('file.ogg')
    client = Wit(wit_access_token)
    with open('file.ogg', 'rb') as f:
        resp = client.speech(f, True, {'Content-Type': 'audio/ogg'})
    print('Yay, got Wit.ai response: ' + str(resp))

最可能的问题:参数顺序错误

Wit Python SDK的speech方法参数顺序和你写的完全不匹配!官方的方法签名是:
client.speech(audio_file, content_type, verbose=False)

你把True(verbose参数)放在了content_type的位置,还把Content-Type包装成了字典,这完全不符合方法要求,这大概率是触发错误的核心原因。

修正后的代码

把参数顺序调整正确,直接传入Content-Type字符串,verbose参数可选:

def discover(bot, update, user_data):
    voice = bot.getFile(update.message.voice.file_id)
    voice.download('file.ogg')
    client = Wit(wit_access_token)
    with open('file.ogg', 'rb') as f:
        # 正确参数顺序:音频文件对象、内容类型字符串、verbose(可选)
        resp = client.speech(f, 'audio/ogg', verbose=True)
    print('Yay, got Wit.ai response: ' + str(resp))

其他可能的排查方向

如果调整参数后还是报错,可以检查这几点:

  • 音频编码兼容性:Telegram的OGG语音默认是Opus编码,而Wit.ai对Vorbis编码的OGG支持更稳定。如果遇到格式不兼容,用ffmpeg转码试试:
    ffmpeg -i file.ogg -acodec libvorbis file_converted.ogg
    
    然后用转后的文件调用API。
  • Token有效性:确认wit_access_token是从Wit.ai控制台复制的有效令牌,没有拼写错误或权限限制。
  • 网络访问:确保你的运行环境能正常访问Wit.ai的API,没有被防火墙、代理拦截。

内容的提问来源于stack exchange,提问作者Francesco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 03:55:29