1000 Python Questions
Get 1 Python question daily. Join this telegram channel https://t.me/python1000questions
Advertise with us
requests   0   2505
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   2505

Related Articles:
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....
Get latest Bitcoin and other crypto-currencies rates using python Django
Getting and displaying the latest bitcoin and other crypto currencies prices using python Django. Complete step to step guide to develop a django project to fetch bitcoin prices. Bitcoin prices in python....
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...
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,...
SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.

© 2017-2020 Python Circle   Contact   Sponsor   Archive   Sitemap