In many companies, Microsoft Teams is at the heart of corporate communications. You can talk with colleagues, plan and assist meetings, create Teams to share information and files within a project. As claimed, it is the hub of Microsoft collaboration.
Today, let’s see how to empower Teams with automation and use it for sending personal or professional notifications. The setup of webhooks is super easy and takes no more than 5 minutes. Incredible!
- You’re training an ML model. It might take hours, and you want to be alerted at the end of the run. Sending yourself an end-of-run signal on your computer and phone is super easy.
- You’re running compute operations of 10 minutes. Too short to do anything else, too long to watch the screen. You might end up on your phone! Receiving a status notification at the end of the run allows to work more efficiently and stop losing time.
- You or your team want to be alerted of some information during a computing process in case of abnormal execution. (Ex: evaluating poor data quality.)
- You want to share daily or monthly data as a report with your team or a wider work audience. Your colleagues will appreciate receiving the data as a dedicated notification in a specific (and organized) channel instead of another email with another joined PowerPoint containing the data.
To send a teams notification, the necessary steps are the following ones
- Having a Teams account and a Team with a Channel to post on (prerequisites)
- Adding the webhook connector to that Team
- Configuring a webhook connector in the Channel
- Creating the content to post
- Requesting the correct URL from any web-connected device (eventually with Python)
Creating the webhook
A webhook is a link that you’ll get and use to send an HTTP
post request, with the desired notification content.
First, you’ll need to decide in which Chanel of which Team you’ll want to post notifications. For this blog post, I created a dedicated Medium Chatbot Team, with a private tutorial channel.
- On the settings (“…”) of the channel, click on Connectors.
2. It will open, all the available connectors. Look up “webhook” in the top right search bar.
3. Click on the “Add” button of the “Incoming Webhook”
4. This opens a new dialog to validate your choice. Once again, click on “Add”
5. The different windows will then close
6. Re-open the connectors of the channel: settings “…” > Connectors. And you’ll see that the first available connector is now the Incoming webhook.
7. Click on the “Configure” button
8. The configuration box opens. And you immediately see how easy the configuration is:
- Give a name to the bot. It will be the name displayed in the channel when the notification arrives. I use to add a robot emoji in the name 🤖 ! Let’s name that one “Your devoted bot 🤖”
- Add a profile picture. Will also be displayed when messages are posted. Since we want to automate it with Python, I’ll add the Python logo as a profile picture. But you can obviously use your company logo, a robot picture, or your Facebook profile picture!
And that’s it! Don’t be afraid, if you’re not sure about your bot’s name, you’ll be able to change this information later without consequences 😉
9. Click on the “Create” button
10. The URL of your devoted bot will appear and you’ll be able to copy it.
It will look like the following one :
11. Save this URL somewhere and we’ll see later how to use it.
12. You can now click on the “Done” button and close the connector’s window.
13. Now, get back to the channel, and you’ll see a message indicating that you configured an Incoming Webhook.
And… That’s pretty much of it! Might take you a few minutes the first time, but in the future, I’m sure you’ll be able to follow the process in less than 45 seconds ⏲
Now that your channel can receive a message from an external source, let’s get deeper and discover how to send that notification.
Sending the notification
There are plenty of ways to create content via the webhook. You’ll find the Microsoft documentation here, but among the many tests that I’ve done, not many worked. Then I’ll show you a basic message that can be interpreted by the webhook.https://towardsdatascience.com/media/e1d209539741008d7b1f7679982d3016
The message must be JSON formatted and contain a minimum of 3 mandatory fields.
- summary: is the text that’ll be displayed on phone notifications.
- activityTitle: is the text that’ll be displayed in the channel on top of your notification
- activitySubtitle: is the content of your notification’s message.
- themeColor (optional): on top of your notification, you’ll see a line appear. This value defines its color with hexadecimal color code.
Sending the message
As mentioned earlier, to send the notification, you’ll need to send a post request with data. Post requests might not be sent from usual browsers, but can easily be managed with Python.
And to send a post request, nothing is more simple than using the requests package, simplifying HTTP for humans.https://towardsdatascience.com/media/9c586f65161100186a72dcab27c38554Sending the request from a Python script
Executing this code will basically send the notification to your channel 😎
No need to say that an internet connection is AB-SO-LU-TE-LY necessary for this step.
You can see the green line that corresponds to the used hexadecimal color value “00FF00”.
Note: instead of using python for the post request, you can also use software such as postman to send notifications.
The result will be very similar (not to say exactly the same!).
It might be useful for testing but no need to say that using python will be easier for further integration!
Formatting the content
The content of your message must be wrapped in a string. Thus, it can be written with basic text such as in this example. But you can also use the Teams markdown solution. But since it uses markers that you can also use in your messages, I’d recommend using standard HTML format which is understood by the webhook.https://towardsdatascience.com/media/cb0e657c6663add0440f99382345f51dHTML formated notification
If you want to use multiline, the
<br> tag will be your best friend. Using triple quotes string in Python allows a visual effect so that you can materialize the “Enter” effect.
If you want to use variables in the message, I suggest using f-strings. And then you can totally create dynamic codehttps://towardsdatascience.com/media/93de14a8294faf335ac995c09279386bCreate HTML content dynamically with Python and f-strings
⚠️ Erasing a message : using webhook url, it is currently not possible to delete a message. So be careful of the data that you send with it.
Receiving the alert
You now know how to post messages on a channel. But since you are not mentioned in it (not possible for now with this basic notification solution), you’ll have to subscribe to the channel notifications.
- Open channel settings “…”
- Click on “ 🔔 Channel notifications”
- Select “Custom”
4. Set the parameters as shown in Figure 14.
5. Click on the “Save” button
Now you can try to resend a message, you’ll receive a real notification from teams. And if you have the app installed on your phone, you’ll get notifications on it.
Losing the bot URL
If you can not remember what the URL of the bot was, don’t panic. It is always possible to find it in the configuration.
- Click on channel settings “…”, open “Connectors”
- On the left part of the window, click on “Configured”
- 🛑 Do NOT click on the “Configure” button
- But kindly click the “1 Configured” link
- Click on the “Manage” button
You’ll then be able to modify the configuration of your bot and copy again the URL to use.
Today we created and configured a Webhook URL in a Teams Channel. Then we saw how to create content to send to this webhook, and most importantly how to proceed to the sending process, with python.
This allows you to include alerting code ATAWAD (AnyTime, AnyWhere, AnyDevice), since you have an internet connection, and to receive these alerts or messages from your mobile phone, for you or your current project’s team.
- you create your bot handler using this code as a starting point: https://github.com/microsoft/BotBuilder-Samples/tree/main/samples/python/02.echo-bot
- you register its URL in MS Bot framerwork here: https://dev.botframework.com/bots/new
- you add MS Teams as a channel