Here's how to send Slack notifications using Firebase Cloud Functions and FireStore


(S/SVR) #1

In our previous article we saw about how to kick start with Firebase’s new add-on FireStore. Now let’s see how we can use firestore along with cloud functions to perform a lot of background operations as well. In our use case we’ll be creating a simple slack notification system when the todos are created and their status are updated.

If you landed here directly I’d strongly recommend you to look at our previous article Getting Started with Cloud Firestore in 5 Minutes to get more information and clarity since we’ll be using the same example.

Right now we’ve todo list where we can create and toggle status of a todo. With this example, we’ll be sending notifications to slack channel when a new todo is created or when a todo is marked as checked/unchecked.

Few Basic Setup.

First you need to get a slack webhook in order to send messages to a channel or a group in your workspace. For creating the webhook follow this link.

Once created the webhook note it and we’ll be using that to configure or firebase’s cloud function.

Firebase Cli

If you haven’t updated the firebase-tools before, update it with the following command.

npm install -g firebase-tools

Now create a firebase project in folder with the following command.

firebase init

Now select firebase functions in the menu popped in terminal, once complete you’ll find a folder called functions and other configuration files.

Writing Cloud Functions

Now open the index.js file inside functions folder. this is the entry point for our cloud functions and we’ll be writing our functions here for this example.

Installing Slack SDK

Also install the slack node sdk in the functions folder.

npm install @slack/client --save

Configure Slack

We’ll be using the webhook url that we got from slack for configuring the sdk.

var IncomingWebhook = require('@slack/client').IncomingWebhook;
var url = "<YOUR--SLACK--WEBHOOK--URL>";
var webhook = new IncomingWebhook(url);

Function to send message to Slack

We wrote a common function that we’ll be using to post messages to slack passing the message string as parameter

function sendMessage(message) {
  webhook.send(message, function(err, header, statusCode, body) {
    if (err) {
      console.log('Error:', err);
    } else {
      console.log('Received', statusCode, 'from Slack');
    }
  });
}

Writing Cloud functions

First, in order to listen to a specific document we’ll be using the following pattern

exports.functionName = functions.firestore.document('todos/{todoId}')

The cloud functions for firestore is very similar to that for realtime databases. We’ll be using two triggers onCreate() and onUpdate() for triggering when new todo is created and when a todo’s status is toggled.

On Create

exports.createTodo = functions.firestore.document('todos/{todoId}').onCreate(event => {
  var newValue = event.data.data();
  var message = "New Todo Added : " + newValue.title;
  sendMessage(message);
  return true;
});

On Update

exports.updateTodo = functions.firestore.document('todos/{todoId}').onUpdate(event => {
  var newValue = event.data.data();
  var message;
  if (newValue.checked) {
    message = newValue.title + " is marked as checked";
  } else {
    message = newValue.title + " is marked as unchecked";
  }
  sendMessage(message);
  console.log("Udpated Todo :", JSON.stringify(newValue));
  return true;
});

Also do checkout our other articles on Firebase here:


This is a companion discussion topic for the original entry at https://www.skcript.com/svr/send-slack-notifications-using-firebase-cloud-functions-firestore/

(Karthik Kamalakannan) #2

Thanks for writing this @varun. This really helps! :slight_smile:


(Duncan McClean) #3

How would I go about submitting a post request to this cloud function?