As you can probably imagine, at DeckDeckGo, we do not have any collaborators who checks that the publicly, published, slides have descent content. Neither have we have implemented a machine learning robot which would do so, yet.

I am taking care of such a task manually. I have to add, it makes me happy to do so. All the presentations published so far are always interesting.

Nevertheless, I have to be informed when decks are published. That’s why I have implemented a Firebase Cloud Function to send my self an email with all the information I need to quickly review the new content.


Setup A New Cloud Function

I assume you already have a Firebase project and, also have already created some functions. If not, you can follow the following guide to get started.

Moreover, note that I am using TypeScript.


Let’s Get Started

A function needs a trigger, that’s why we are registering a function in index.ts on a collection called, for example, demo (of course your collection can have a different name).

import * as functions from 'firebase-functions';

export const watchCreate =
       functions.
       firestore.
       document('demo/{demoId}').onCreate(onCreateSendEmail);

We can use any other triggers or lifecycle, not necessary the create one.

To respond to the trigger’s execution, we declare a new function which retrieve the newly created value (const demo = snap.data() ) and are adding, for now on, a TODO which should be replaced with an effective method to send email.

import { EventContext } from "firebase-functions";
import { DocumentSnapshot } from "firebase-functions/lib/providers/firestore";
interface Demo {
  content: string;
}

async function onCreateSendEmail(
                 snap: DocumentSnapshot, 
                 _context: EventContext) {
  const demo: Demo = snap.data() as Demo;

  try {
    // TODO: send email
  } catch (err) {
    console.error(err);
  }
}

#firebase #programming #javascript #web-development #firebase-cloud-functions

Send Email From Firebase Cloud Functions
1.25 GEEK