Notify to Teams on Firebase events
When creating a new project you usually want to know if something is happening in it after you released in production. You can do that on one hand by manually hitting “F5” in your tools and constantly query your database for changes or you can automate it.
In this small tutorial I will show you how to send notifications to Microsoft Teams from Google Firebase Functions. In this special case user create events, since I want to know when a new user is starting to use my application.
This is working either with the business or free version of Microsoft Teams, but it is required to upgrade your Google Firebase plan to “Blaze” (Pay as you go) to have “Functions” available.
Microsoft Teams
The setup in Microsoft Teams is rather simple.
Within one of your teams specify a channel which should receive the notifications or create a new one.
For that channel we open the “Connectors” menu for adding a new Webhook connector.
Select “Configure” for the “Incoming Webhook” connector to add a new one to the channel.
Give the Webhook a reasonable name and maybe a nice logo
After clicking “Create” the Webhook will be added and the unique URL is presented in the following screen. Save the URL. We will need it later in our script (The URL will not be lost after closing the window, you can access it at any time again)
The Microsoft Teams part is finished by that.
Google Firebase
In Google Firebase we will utilize the “Cloud Functions” feature to send the notifications.
I assume that you have already setup your project to use functions by following the official documentation: https://firebase.google.com/docs/functions/get-started
Since we have to perform a http call we install “Axios” as helper library (You can also use the standard “http” module)
npm install --save axios
and import it in the “index.js” file of our functions structure
const axios = require(‘axios’);
Then we create a function which will be triggered on the users “onCreate” event
exports.notifyNewUser = functions.auth.user().onCreate((user) => {};
I will also use the current date & time and of course the webhook URL we got from Teams, so I define them as variables
const webHook = "https://outlook.office.com/webhook/....";const curDate = new Date();
Also I define the content which we want to send to Teams. I will use an “AdaptiveCard” (https://adaptivecards.io/), since that will be the common format for all Microsoft tools in near future.
var card = {
"type": "message",
"attachments": [
{
"contentType": "application/vnd.microsoft.card.adaptive",
"contentUrl": null,
"content": {
"type": "AdaptiveCard",
"body": [
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "New User Created"
},
{
"type": "FactSet",
"facts": [
{
"title": "Create Date",
"value": curDate.toISOString()
},
{
"title": "UUID",
"value": user.uid
}
]
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.3"
}
}
]
};
You can use the Designer to create the card body which is used as attachment to be send to Teams: https://adaptivecards.io/designer/
All variables of the Firebase Functions environment are available to enrich your message, but you should never send your users personal data, like e-mail address or name, to respect data privacy. Due to that I use the “uid” in this example.
As final step we send our message to the Webhook using a simple http POST
axios.post(webHook, card)
.catch(error => {
console.error(error)
});
Deploy the whole thing to Firebase and start testing
firebase deploy --only functions
You can find the full example in the following GIT repository: https://gitlab.com/bomba19881/firebase-functions-teams-demo
Test
Testing is very easy.
Access our projects Firebase Console and navigate to “Authentication”. Assuming that you have already configured and enabled Authentication you can now simply start adding a user there.
Once everything is working as expected you should directly get the notification in Teams
Self-evident you can use any trigger available in Firebase to send notifications, dependent on your use-case.
Example Code
Documentations + Tools
- https://firebase.google.com/docs/functions/get-started
- https://firebase.google.com/docs/functions/auth-events
- https://nodejs.dev/learn/make-an-http-post-request-using-nodejs
- https://docs.microsoft.com/en-us/microsoftteams/platform/webhooks-and-connectors/how-to/connectors-using#send-adaptive-cards-using-an-incoming-webhook
- https://adaptivecards.io/
- https://adaptivecards.io/designer/