Firebase Cloud Functions + Slack WebHooks = đ
The shortest possible tutorial.
Context
You have built a webapp for your startup using Firebase for the backend.
Specifically, youâre taking advantage of their Authentication service to sign up and log in your users.
Youâre obviously using Slack, because nobody takes emojis more seriously than these guys. âĄ
User Story
As a member of my beloved Startup,
I want to be notified when a new user signs up.
It will boost my motivation and as a result I will treat my dog đ¶ better.
1. Be Notified
How to receive something in Slack? â Incoming WebHooks!
Go to Slack and click on Apps & integrations
Select Build
Create your App by choosing a name and your team
Select and activate the Incoming Webhooks functionality
Create a Webhook and select the channel you want to post to
(EDIT: please make sure that the channel you select is a private channel including only people who have a good business reason to see the email address of your new users â thanks Ăamonn O'B-S)
Copy the sample curl request given
Paste it in your terminal
Press enter
VoilĂ ! A POST
request to an url and we have a notification in our Slack.
đ«đ«đ« #SORTED
2. Listen to new signups
Hello Cloud Functions!
Go to your Firebase console and select the Functions section
Install firebase-tools
if you have to
Go to an empty folder in your terminal and type:
firebase init
Select the Functions feature and your Firebase project you want to deploy to
Open the folder in an editor
Add the function that listens to onCreate
events (from the Firebase documentation) and posts a json
payload to your Slack hook url: Thatâs the step connecting the Cloud Functions to your Slack incoming Webhooks!
You notice the use of the request
library, so add it to your package.json
by typing:
npm i request --save
One last thing to do is to deploy the function to Firebase. Type the following:
firebase deploy --only functions
You can go back to your Firebase Functions section and check that it has been uploaded.
3. Demo!
đ„đ„đ„đ„đ„đ„
Try and create a new user on Firebase, you should then see on your Slack something like:
Summary
You created an app on Slack and activated the Incoming Webhook feature to get an url you can post your messages to.
You created a Cloud Function on Firebase that listens to new signup and posts to your Webhook url a json payload including the email of the new user.
Your user story is satisfied and itâs worth mentioning that you didnât have to touch your code base at all!
Short & Sweet đ
PS: You probably heard scary things about Firebase billing recently but it should be all good from now on.