Support
Please support this website. Visit the Amazon using this affiliate link. There won't be any difference in your purchage, we will get some commission for every purchase you make.
Advertise with us
requests   0   48263
How to post messages to Microsoft teams channel using Python


To send messages to the Microsoft teams channel, we will use the requests module.


import requests
import json

response = requests.post(url, headers=headers, data=json.dumps(payload))


'url' will be the webhook URL of the channel which can be fetched as below.

In your Teams application, click on the 'Apps'. Search for 'webhook'.


microsoft team wehbook


Click on the 'Incoming Webhook'. On the next screen, click on the 'Add to a team' button.


microsoft team webhook add


On the next screen, search for the team and channel you want to add the webhook app and then click 'Set up a connector' button at the bottom.

Enter the name of the connector and select the icon image and click 'create'. A webhook URL will be generated. Copy this URL.

Webhook URL should be in similar to the below URL:

url = "https://outlook.office.com/webhook/" \
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx@xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx/" \
"IncomingWebhook/xxxxxxxxxxxxxxxxx/" \
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"


Sending a simple text message:

To send a plain text message, create a payload JSON with 'text' as key.

payload = {
"text": "Sample Alert Text"
}


Create headers JSON and add the 'Content-Type' header in it.

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


Putting it all together:

import requests
import json

url = "https://outlook.office.com/webhook/" \
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx@xxxxxx-xxx-xxx-xxx-xxxxx/" \
"IncomingWebhook/xxxxxxxxxxxxxxxx/" \
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxx"
payload = {
"text": "Sample alert text"
}
headers = {
'Content-Type': 'application/json'
}
response = requests.post(url, headers=headers, data=json.dumps(payload))
print(response.text.encode('utf8'))


Make sure the requests module is installed in your virtual environment. Execute the above code.

python ms_teams_alert.py


You should receive an alert on your channel.

j8g+dw4HhYEcQAAAABJRU5ErkJggg==



Host your Django Application for free on PythonAnyWhere. If you want full control of your application and server, you should consider DigitalOcean. Create an account with this link and get $100 credits.


Photo by Mika Baumeister on Unsplash

requests   0   48263
0 comments on 'How To Post Messages To Microsoft Teams Channel Using Python'

Related Articles:
Python Requests Library: Sending HTTP GET and POST requests using Python
Python requests library to send GET and POST requests, Sending query params in Python Requests GET method, Sending JSON object using python requests POST method, checking response headers and response status in python requests library...
Sending post request with different body formats in Django
There are a few different formats of the body we can use while sending post requests. In this article, we tried to send post requests to different endpoints of our hello world Django application from the postman. Let's see what is the difference between discussed body formats....
Getting query params from request in Django
In this article, we will see how to access the query parameters from a request in the Django view, Accessing GET attribute of request, get() vs getlist() method of request in Django, query parameters Django,...
Scraping data of 2019 Indian General Election using Python Request and BeautifulSoup and analyzing it
scraping 2019 election data india, Scraping data of 2019 Indian General Election using Python BeautifulSoup and analyzing it, using beautifulsoup to collection election data, using request...
DigitalOcean Referral Badge

© 2022-2023 Python Circle   Contact   Sponsor   Archive   Sitemap