Search code examples
dialogflow-esdialogflow-es-fulfillmentdialogflow-cx

How to send audio back from Dialogflow CX webhook?


I am using this code to send back text response from Node.JS Dialogflow CX webhook. I want to play an audio as fulfillment so I want to send link to that audio back.

How can I send back audio file 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);
});

Solution

  • To utilize your audio URI, you must add it to your ‘jsonResponse’. You will need to layer it to your webhookResponse in via the following fields: jsonResponse.fulfillment_response.messages.play_audio

    ‘play_audio’ will specify the audio clip URI that will be played. This URI must be a public URI.

    For example (PlayAudio):

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

    Please note that these audio responses currently only work with Dialogflow’s Telephony integrations such as Avaya or AudioCodes, or with a custom integration you can create yourself using the APIs and Client Libraries. Not all audio files work for Avaya and AudioCodes, so I recommend using .WAV files as they work with both.

    If you are using a custom integration, you can create your own implementation to support your preferred audio format.