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

仅用Dialogflow CX实现通话音频播放可行吗?Flask代码求助

仅通过Dialogflow CX能否在电话通话中播放预录音频?

我想确认是否仅依赖Dialogflow CX就能实现电话通话中播放预录音频。我尝试过多种方式流式传输音频URL来播放,但均未成功。我用Flask搭建了webhook,先后测试了两种代码实现方案——基于telephonySynthesizeSpeech的SSML标签格式,以及audio payload格式,但都没能实现预录音频的播放功能。

第一种代码实现:

@app.route('/webhook', methods=['POST'])
def handle():
    req = request.get_json(force=True)
    print(f"Request data: {req}")  # 打印请求数据

    user_text = req.get('transcript')
    tag = req["fulfillmentInfo"]["tag"]

    print(f"用户文本: {user_text}, 标签: {tag}")  # 打印用户文本和标签

    response = ""  # 初始化响应

    if tag == 'dialogflowRES':
        if user_text is not None:
            response = "Hello this is an example"
        else:
            response = ""
    
    print(f"响应内容: {response}")  # 打印响应内容

    if "Bye" in response:        
        return jsonify({
            'fulfillment_response': {
                'messages': [{
                    'text': {
                        'text': [response]
                    }
                }]
            },
            'end_interaction': True
        })
    else:
        # 替换为你的音频文件公开URL
        audio_public_url = 'https://storage.googleapis.com/twiliobucket/output.wav'
        return jsonify({
            'fulfillment_response': {
                'messages': [{
                    'payload': {
                        'telephonySynthesizeSpeech': {
                            'text': response,
                            'ssml': f'<speak><audio src="{audio_public_url}"/></speak>'
                        }
                    }
                }]
            }
        })

我还尝试了另一种语法,但仍然无效:

return jsonify({
    'fulfillment_response': {
        'messages': [{
            'payload': {
                'audio': {
                    'uri': audio_public_url,
                    'config': {
                        'audioEncoding': 'AUDIO_ENCODING_LINEAR_16',
                        'sampleRateHertz': 8000
                    }
                }
            }
        }]
    }
})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 20:42:36