如何从 Dialogflow CX webhook 发回音频?

How to send audio back from Dialogflow CX webhook?

我正在使用此代码从 Node.JS Dialogflow CX webhook 发回文本响应。我想播放音频作为实现,所以我想将 link 发送回该音频。

如何发回音频文件link?

const express = require("express");
const app = express();
const bodyParser = require("body-parser");

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));

app.post("/webhook", (request, response) => {
    let tag = request.body.fulfillmentInfo.tag;
    let jsonResponse = {};
    if (tag == "hello") {
        //fulfillment response to be sent to the agent if the request tag is equal to "welcome tag"
        jsonResponse = {
            fulfillment_response: {
                messages: [{
                    text: {
                        //fulfillment text response to be sent to the agent
                        text: ["Hi! This is a webhook response"]
                    }
                }]
            }
        };
    } else {
        jsonResponse = {
            //fulfillment text response to be sent to the agent if there are no defined responses for the specified tag
            fulfillment_response: {
                messages: [{
                    text: {
                        ////fulfillment text response to be sent to the agent
                        text: [
                            `There are no fulfillment responses defined for "${tag}"" tag`
                        ]
                    }
                }]
            }
        };
    }
    response.json(jsonResponse);
});

const listener = app.listen(3000, () => {
    console.log("Your app is listening on port " + listener.address().port);
});

要使用您的音频 URI,您必须将其添加到您的“jsonResponse”中。您需要通过以下字段将其分层到您的 webhookResponse 中:jsonResponse.fulfillment_response.messages.play_audio

‘play_audio’将指定将要播放的音频剪辑 URI。此 URI 必须是 public URI。

例如 (PlayAudio):

jsonResponse = { 
    fulfillment_response: { 
        messages: [{ 
            play_audio: { 
                audio_uri: `https://URI.EXAMPLE.WAV`
            } 
        }] 
    } 
};

请注意,这些音频响应目前仅适用于 Dialogflow 的 Telephony integrations such as Avaya or AudioCodes, or with a custom integration you can create yourself using the APIs and Client Libraries。并非所有音频文件都适用于 Avaya 和 AudioCodes,因此我建议使用 .WAV 文件,因为它们适用于两者。

如果您使用的是自定义集成,则可以创建自己的实现来支持您首选的音频格式。