🖌️
Firebase+React_Notes
  • Firebase React Notes
  • React + firebase
    • Firebase - Create React app setup
      • Node & nvm
      • Create React App + Firebase
      • Create firebase app
      • Deploying To Firebase Hosting
      • Switching Environments
      • Typescript typings
      • Firebase cloud function local development
      • Resources
    • Firebase React context
      • Motivation
      • Firebase React Context setup
    • Firebase function local dev react
    • React firebase hooks
  • Multiple ENVs
    • Multiple ENVs
    • Manual setup
    • Terraform
  • Firestore
    • Firestore
      • Using a function to check email domain
    • Firestore data model
    • associated Firebase data with Users
    • Firestore write
    • Firestore - read
      • Removing a listener from firestorm
    • Firestore update
    • Persisting data offline
    • Importing json
  • Auth
    • Auth
    • Firebase UI
    • Firebase Auth with React
    • Linking auth accounts
    • Twitter sign in
    • Google sign in
      • Google sign in custom domain
    • Database Auth
      • Custom claims
      • Limit auth to certain domain only
    • Custom tokens
  • Cloud Functions
    • Cloud Functions
    • Set node version
    • Set timeout and memory allocation
    • Call functions via HTTP requests
    • HTTPS Callable
      • HTTPS Callable cloud function auth check email address domain
    • Separate Cloud Function in multiple files
    • Slack integration
    • Twilio firebase functions
    • ffmpeg convert audio
    • ffmpeg transcoding video
  • Storage
    • Security
    • Create
    • Delete
    • Uploading with React to Firebase Storage
    • Getting full path
    • Firebase `getDownloadURL`
    • Saving files to cloud storage from memory
  • Hosting
    • Hosting
    • Hosting + cloud functions
  • Firebase Admin
    • Firebase admin
  • Firebase analytics
    • Firebase analytics
  • Google App Engine
    • Google App Engine
    • GCP App Engine + video transcoding
  • STT
    • STT + Cloud Function + Cloud Task
      • Example implementation
      • `createTranscript`
      • `createHandler`
        • Firebase ENV
    • Other
      • enableWordTimeOffsets
      • STT longRunningRecognize in Cloud function
      • STT + Cloud Function
      • STT + Google App Engine
      • STT via Google Cloud Video intelligence API
  • CI Integration
    • Travis CI integration
    • Github actions integration
  • Visual code
    • Visual code extension
  • Electron
    • Firebase with electron
  • Pricing
    • Pricing
  • Testing
    • Unit testing
  • Privacy and Security
    • Privacy and security
  • Useful resources
    • links
  • Firebase Extensions
    • Firebase extension
  • Chrome Extension
    • Firebase in a chrome extension
  • Cloud Run
    • Cloud Run
Powered by GitBook
On this page

Was this helpful?

  1. STT
  2. STT + Cloud Function + Cloud Task

`createHandler`

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const { CloudTasksClient } = require('@google-cloud/tasks');
const fetch = require('node-fetch');
const gcpToDpe = require('gcp-to-dpe');

const addMinutes = (date, minutes) => {
  return new Date(date.getTime() + minutes * 60000);
};

const getSecondsSinceEpoch = date => {
  return Math.round(date.getTime() / 1000);
};

const getSttOperationUrl = (operationName, firebaseApiKey) => {
  return `https://speech.googleapis.com/v1/operations/${operationName}?key=${firebaseApiKey}`;
};


exports.firestoreCheckSTT = functions.runWith(MAX_RUNTIME_OPTS).https.onRequest(async (req, res) => {
  const payload = req.body;
  console.log('payload', payload);
  console.log('payload typeof', typeof payload);
  // Does this work or does it need some processing like, JSON.parse etc..?
  const { sttOperationName, docPath } = payload;
  console.log('sttOperationName', sttOperationName);
  console.log('docPath', docPath);
  try {
    // await admin.firestore().doc(payload.docPath).delete();
    // TODO: add firebaseApiKey to ENV
    // https://stackoverflow.com/questions/34442739/how-does-one-set-private-environment-variables-on-firebase-hosting
    const firebaseApiKey = functions.config().webapi.key;
    const operationUrlEndPoint = getSttOperationUrl(sttOperationName, firebaseApiKey);
    return fetch(operationUrlEndPoint)
      .then(response => response.json())
      .then(async resp => {
        console.log('resp');
        // console.log(resp);
        if (resp.done && resp.response) {
          // TODO: save data to firestore
          // resp.response.result
          console.log('transcript');
          const transcript = gcpToDpe(resp);
          // console.log('transcript', transcript);
          console.log('transcript gcpToDpe');
          const { paragraphs, words } = transcript;
          console.log('transcript words');
          console.log('docPath', docPath);

          await admin
            .firestore()
            .doc(docPath)
            .set(
              {
                paragraphs,
                words,
                status: 'done',
              },
              {
                merge: true,
              }
            );
          console.log('admin write');
          return res.sendStatus(200);
        } else {
          console.log('else, not ready - trying task again!');
          //TODO: run cloud task
          const project = admin.instanceId().app.options.projectId;
          // https://firebase.google.com/docs/functions/locations
          const location = 'us-central1';
          const queue = 'firestore-stt';
          const tasksClient = new CloudTasksClient();
          const queuePath = tasksClient.queuePath(project, location, queue);
          const url = `https://${location}-${project}.cloudfunctions.net/firestoreCheckSTT`;
          console.log('url firestoreCheckSTT', url);
          //  const payload = { sttOperationName, docPath };
          // time of expiration expressed in epoch seconds
          const now = new Date();
          const timeFromNowWhenToCheckAgainInMinutes = 5;
          const timeFromNowWhenToCheckAgainAsDate = addMinutes(now, timeFromNowWhenToCheckAgainInMinutes);
          // Epoch, also known as Unix timestamps, is the number of seconds (not milliseconds!) that have elapsed since January 1, 1970 at 00:00:00 GMT
          const secondsSinceEpoch = getSecondsSinceEpoch(timeFromNowWhenToCheckAgainAsDate);

          await admin
            .firestore()
            .doc(docPath)
            .set(
              {
                sttOperationName,
                nextSttProgressCheckAt: timeFromNowWhenToCheckAgainAsDate,
              },
              {
                merge: true,
              }
            );

          const task = {
            httpRequest: {
              httpMethod: 'POST',
              url,
              body: Buffer.from(JSON.stringify(payload)).toString('base64'),
              headers: {
                'Content-Type': 'application/json',
              },
            },
            scheduleTime: {
              seconds: secondsSinceEpoch,
            },
          };
          const [response] = await tasksClient.createTask({ parent: queuePath, task });
          console.log(`Created task ${response.name}`);
          return res.sendStatus(200);
        }
      });
  } catch (error) {
    console.error(error);
    return res.sendStatus(500);
  }
  });
Previous`createTranscript`NextFirebase ENV

Last updated 4 years ago

Was this helpful?