Let's say you have your MongoDB databases on MongoDB Atlas (Ideally hosted on Microsoft Azure, but that's not important now).
Let's also say you are using Microsoft Teams and you are trying to use it for as many scenarios as possible.
My initial approach would be checking if MongoDB comes with a connector for Teams to integrate it seamlessly. As per today, I can let you know this feature doesn't exist (they already have a feature request for it).
My second approach would be, using MongoDb's webhooks integrations to send the notifications to Teams, but no, it doesn't work either. This functionality is sending a custom payload which Teams doesn't understand.
As there is no OOTB solution, I'm proposing is simple one: Let's build a gateway to transform that custom payload into a Message Card that Teams can understand. The process will be:
- MongoDb sends the notification via webhook integration to our gateway.
- Our gateway will read the custom payload and transform it into a Message card object.
- Our gateway will post the new Message Card to our Teams' Webhook.
- Teams will display the notification on our channel.
Let's get started!
Setting up a custom incoming webhook in Microsoft Teams
This step enables your channel for receiving external messages.
- In Microsoft Teams, choose More options (⋯) next to the channel name and then choose Connectors.
- Scroll through the list of Connectors to Incoming Webhook, and choose Add.
- Enter a name for the webhook, upload an image to associate with data from the webhook, and choose "Create".
- Copy the webhook to the clipboard and save it. You'll need the webhook URL for sending information to Microsoft Teams.
- Choose Done.
Creating the gateway between MongoDB and Microsoft Teams
We are going to use an Azure Function to do the job. There is plenty of documentation about creating Azure functions out there. I'll give you some links to the official documentation:
- Create a function app from the Azure portal. This will give you an Internet-facing endpoint accessible for MongoDB. It will also give you the capabilities to implement our transformation and call back to Microsoft Teams. Also, creating a consumption base app service plan when following the guide, should result in an almost free service, considering the number of free resources provided by Azure and the expected limited number of alerts triggered from MongoDB.
- Create an HTTP triggered function. With this step, you'll have an actual URL to be used by MongoDB and a method in a class to implement our logic. Copy the function URL as described in the "Test the function step", as we'll need it later.
- Replace the code of the function, by the one provided by the following Github Gist (YOu can find it also at the bottom of the page)
- Type the URL generated with Teams, to the variable "uri" (line 26). With these steps we are telling the function app where must it call to send us our notifications.
Configuring MongoDB Atlas to send the notifications to the gateway (our function app on Azure)
- Login into your project and go to Integrations (Left-hand column)
- From the list of possible integrations, choose Webhook Settings and click "Configure".
- On the pop-up, paste the URL of the function app you saved on the previous step. The URL already contains the secret, so leave the "Webhook Secret" field empty.
Now MongoDB is able to communicate with our function app, but by default all the alerts are configured to send them by email only. So let's change it.
- Under your project, go to "Alerts", and here click on the tab "Alert settings".
- For each alert, click on the 3 dots on the right and click "Edit".
- At the bottom, there is an "Add" dropdown. Click on it and choose "Webhook".This step will add the previously configured webhook to the list of targets to send the notification to.
- You can remove the previously default target "email" if you don't want to receive the notifications by email.
And that's it, now MongoDB will call your Azure function whenever an alert is triggered, this function will get the data sent by MongoDB, and push it to your Microsoft teams channel to get a message like the image below:
Of course, this is a "quick and dirty" example to give you a PoC.