Search code examples
firebase-storagedialogflow-es

How to access an audio file from Firebase Storage from DialogFlow webhook


I am able to play a sound from my DialogFlow fulfillment inline editor webhook with the following.

function saySSML (request, response) {
  const app = new DialogflowApp({request: request, response: response});

  const textToSpeech = '<speak>'
    + 'I can play a sound'
    + '<audio src="https://actions.google.com/sounds/v1/animals/animal_bark_and_growl.ogg">a digital watch alarm</audio>'
    + '</speak>';
  app.ask(textToSpeech);
}

However, I can't figure out how to play an audio file in Firebase Storage. I have tried replacing the audio url with the storage location of the file and also the DownloadUrl1 url as shown when clicking on the file in Firebase Storage.

I've also looked at creating a storage reference as per https://firebase.google.com/docs/storage/web/create-reference but when I add the reference to the storage service, I get 'firebase not defined' error.

function saySSML (request, response) {
  const app = new DialogflowApp({request: request, response: response});

  // Get a reference to the storage service, which is used to create references in your storage bucket
  var storage = firebase.storage();

  // Create a storage reference from our storage service
  var storageRef = storage.ref();

  const textToSpeech = '<speak>'
    + 'I can play a sound'
    + '<audio src="https://firebasestorage.googleapis.com/v0/b/andrewdittmer-c301f.appspot.com/o/alarm_clock.ogg?alt=media&token=d66a4055-5365-4061-b50d-0b6a126841f3">a digital watch alarm</audio>'
    + '</speak>';
  app.ask(textToSpeech);
}

Has anyone been able to successfully do this?

Thanks for any help / pointers.


Solution

  • The SSML parser for the Google Assistant is very strict. The & that is in your URL needs to be escaped, so the result should be &amp;. The Line might look something like this:

        + '<audio src="https://firebasestorage.googleapis.com/v0/b/andrewdittmer-c301f.appspot.com/o/alarm_clock.ogg?alt=media&amp;token=d66a4055-5365-4061-b50d-0b6a126841f3">a digital watch alarm</audio>'
    

    Alternately, if you're using Firebase already, and your audio assets are static, you may want to store the audio in Firebase Hosting.