Automated messages to Microsoft teams with Python.

Shelwyn Corte
5 min readMar 7, 2022

--

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:

url = "https://<Your webhook URL>"

IssueType="CPU USAGE"
Severity=1
ServerName="SERVER-007"

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)

SevImage={
1: "https://icons.iconarchive.com/icons/custom-icon-design/flatastic-10/128/Trafficlight-red-icon.png",
2: "https://icons.iconarchive.com/icons/hopstarter/soft-scraps/128/Button-Blank-Yellow-icon.png",
3: "https://icons.iconarchive.com/icons/custom-icon-design/flatastic-10/128/Trafficlight-green-icon.png"
}

AssignTo={
"CPU USAGE": "john.doe@outlook.com",
"MEM USAGE": "john.smith@outlook.com",
"DISK USAGE": "jack.daniel@outlook.com"
}

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={
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": "A9CCE3",
"summary": "Server Alert",
"sections": [{
"activityTitle": "New task reported for: %s" %IssueType ,
"activitySubtitle": "Server Monitoring",
"activityImage": SevImage[1],
"facts": [{
"name": "Assigned to",
"value": AssignTo[IssueType]
}, {
"name": "Severity",
"value": Severity
}, {
"name": "Server name",
"value": ServerName
}, {
"name": "Issue Type",
"value": IssueType
}]
}],
"potentialAction": [{
"@type": "OpenUri",
"name": "View details on console",
"targets": [{
"os": "default",
"uri": "https://<LINK-TO-SERVER-LOG-CONSOLE>"
}]
},{
"@type": "OpenUri",
"name": "Update Service Incident",
"targets": [{
"os": "default",
"uri": "https://<LINK-TO-INCIDENT-MANAGEMENT-TOOL>"
}]
}]
}

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.

headers = {'Content-Type': 'application/json','Accept': 'application/json'}

json_payload = json.dumps(payload)
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:

import requests
import json

url = "https://<Your webhook URL>"

IssueType="CPU USAGE"
Severity=1
ServerName="SERVER-007"

SevImage={
1: "https://icons.iconarchive.com/icons/custom-icon-design/flatastic-10/128/Trafficlight-red-icon.png",
2: "https://icons.iconarchive.com/icons/hopstarter/soft-scraps/128/Button-Blank-Yellow-icon.png",
3: "https://icons.iconarchive.com/icons/custom-icon-design/flatastic-10/128/Trafficlight-green-icon.png"
}

AssignTo={
"CPU USAGE": "john.doe@outlook.com",
"MEM USAGE": "john.smith@outlook.com",
"DISK USAGE": "jack.daniel@outlook.com"
}

payload={
"@type": "MessageCard",
"@context": "http://schema.org/extensions",
"themeColor": "A9CCE3",
"summary": "Server Alert",
"sections": [{
"activityTitle": "New task reported for: %s" %IssueType ,
"activitySubtitle": "Server Monitoring",
"activityImage": SevImage[1],
"facts": [{
"name": "Assigned to",
"value": AssignTo[IssueType]
}, {
"name": "Severity",
"value": Severity
}, {
"name": "Server name",
"value": ServerName
}, {
"name": "Issue Type",
"value": IssueType
}]
}],
"potentialAction": [{
"@type": "OpenUri",
"name": "View details on console",
"targets": [{
"os": "default",
"uri": "https://<LINK-TO-SERVER-LOG-CONSOLE>"
}]
},{
"@type": "OpenUri",
"name": "Update Service Incident",
"targets": [{
"os": "default",
"uri": "https://<LINK-TO-INCIDENT-MANAGEMENT-TOOL>"
}]
}]
}

headers = {'Content-Type': 'application/json','Accept': 'application/json'}

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

--

--

Shelwyn Corte
Shelwyn Corte

Written by Shelwyn Corte

Automation Engineer | https://shelwyn.in/ | Currently in Bengaluru

No responses yet