Automated messages to Microsoft teams with Python.

In this article, we will have a look at how to send automated messages or alerts to your team members on a Microsoft teams channel. The idea here is to send a message/alert to the teams channel where all the team members are added. The message will include the alert type, the person whom the alert is assigned to and a couple of buttons where the person can check the alert in detail on the application portal and update the incident raised by the generated alert from the incident management tool portal.

MS Teams already comes with an integration feature called “Connectors”. This feature helps you integrate third-party applications such as (RSS feed, Yammer, Google analytics, Webhooks and many more) with your office ecosystem.

We will be using the webhook API with python to send alerts to our teams channel

Section 1 [MS Teams]

Create a team, add some members and a channel to it (in the below example, we have created a team called “Alerts” and added two channels [Application alerts, Server alerts]).

We will configure the “Server Alerts” channel for alerts, right click on the channel and click connectors

You will see a list of available connectors, since we will be using webhook, select the connector “Incoming Webhook” by clicking on the add button

A new window will popup asking confirmation, click add one more time

You will be taken back to the main screen, where you will have to right click on the channel again and click Connectors, this time to configure the webhook

Click configure and you will asked for the below details.

Give a name to your connector, upload an image if you would like to and click on Create. You will then be presented with the below screen which will have an URL

Copy the URL and save it, click on done and we are good with the connector configuration.

Section 2[Python]

Installation:
— pip install requests

Code:
Let us break down our code:

Lets start by initializing the variables, the URL will hold the value of the webhook url we got when we configured the webhook.
For testing purpose I have given sample values in the IssueType, Severity, and ServerName variables. These values can be derived from a file, an API or any source where you will be getting the details to configure alerts depending on your application. (You can play around with different options in the code to see the way alerts are generated)

The first dictionary contains the severity level and a link of an image (Red for severity 1, yellow for severity 2, green for severity 3) which will be displayed on the message card depending on the severity of the alert.
The second dictionary contains the issue types and the person who will be assigned the alert depending on the issue

Payload to send to the webhook API.

“@type”: “MessageCard” tells the API the type of message we are creating
“summary”: “Server Alert” — summary of the message
“activityTitle”, “activitySubtitle” — Title/subtile to be displayed on the message card
“activityImage” — Image to be displayed on the message card
“facts[]” — In this array we are creating key, pair values to be displayed on the message card
“potentialAction” — In this array we are buttons where the user can click to take actions if required.

response = requests.request("POST", url, headers=headers, data=json_payload)print(response.text)

Headers to be sent to the API are set in the headers variable, the payload variable is converted into string for parsing and stored in the json_payload variable.
We are posting the data to the webhook API and storing the response in the response variable, and finally printing the response received from the API.

Once your code is executed, you should see a message card created in your teams channel

Feel free to play around with IssueType and Severity variables to see different responses being generated.

Full Code:

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store