05 Jan How to send Messages in Microsoft Teams using Incoming Webhook Adaptive Card
Incoming Webhook permits any external apps to share content in Teams channels where the webhooks are majorly for tracking and notifying tools. It provides a unique URL to which you send a JSON payload with a message in card format. Cards are user interface containers that comprise of content and actions related to a single topic. Microsoft Teams use cards to perform the following functions:
-
Bots
-
Messaging extensions
-
Connectors
We will implement an Alert system that acts as a bot for sending messages via Microsoft Teams. We will use an Incoming webhook to demonstrate this.
How to Create Incoming Webhook in Teams
To create a webhook from Microsoft teams, open Microsoft teams app and go to your selected channel
-
Open Microsoft Teams app and go to the channel you would like to add the webhook and select ••• More options from the top navigation bar.
-
Select Connectors from the dropdown menu:
3. Search for Incoming Webhook and select Add.
4. Select Configure, provide a name, and upload an image for your webhook if you want:
5. The dialog window presents a unique URL that maps to the channel. Copy and save the webhook URL, to send information to Microsoft Teams and select Done:
Now, we can use this URL in our script to post messages to teams.
Template for using Adaptive Cards
The JSON format of the payload type we will include in our script when using the request method along with our webhook must be in this format.
{ "type":"message", "attachments":[ { "contentType":"application/vnd.microsoft.card.adaptive", "contentUrl":null, "content":{ "$schema":"http://adaptivecards.io/schemas/adaptive-card.json", "type":"AdaptiveCard", "version":"1.2", "body":[ { "type": "TextBlock", "text": "For Samples and Templates, see [https://adaptivecards.io/samples](https://adaptivecards.io/samples)" }, { "type": "Image", "url": "https://adaptivecards.io/content/cats/1.png" } ] } } ] }
Sending Message to Teams
Here, we will use webhook URL which uses the Adaptive card type to send data to the Teams channel. With this Adaptive card, we can tag different people in the organization to whom we want to show this message. These messages include sending alert messages to team members for a daily standup meeting.
#import libraries here import requests import json #specify the webhook url url = "https://cndrocom.webhook.office.com/webhookb2/xxxxxxxxxxxxxxxxxxxxxx@xxxxxxxxxxxxxxxxxxx/IncomingWebhook/xxxxxxxxxxxxxxxxx/xxxxxxxxxxxxx" #the payload request was passed in here using json dump to wrap it as a string format payload = json.dumps({ "type": "message", "attachments": [ { "contentType": "application/vnd.microsoft.card.adaptive", "content": { "type": "AdaptiveCard", "body": [ { #this is to indicate how we want our text to be formatted(size, weight e.t.c.) "type": "TextBlock", "size": "Medium", "weight": "Bolder", "text": "Daily Standup Bot" }, { "type": "TextBlock", #passing my message here, whereby I specify the names of each team members and indicate the UPN alongside "text": "Hi, what's the update on our board today <at>Jones UPN</at>, <at>Michael UPN</at>, <at>Grace UPN</at>, <at>Paul UPN</at>, <at>Kate UPN</at>, <at>Mark UPN</at>" } ], "$schema": "http://adaptivecards.io/schemas/adaptive-card.json", "version": "1.0", "msteams": { "width": "Full", "entities": [ #Each team members are being tagged under this part { "type": "mention", #the text refer to the name of that team memeber "text": "<at>Jones UPN</at>", #Here, we specify the member email as the id and the name as the username of that user "mentioned": { "id": "Jones@abc.com", "name": "Jones" } }, { "type": "mention", "text": "<at>Michael UPN</at>", "mentioned": { "id": "Michael@abc.com", "name": "Michael" } }, { "type": "mention", "text": "<at>Grace UPN</at>", "mentioned": { "id": "Grace@abc.com", "name": "Grace" } }, { "type": "mention", "text": "<at>Paul UPN</at>", "mentioned": { "id": "Paul@abc.com", "name": "Paul" } }, { "type": "mention", "text": "<at>Kate UPN</at>", "mentioned": { "id": "Kate@abc.com", "name": "Kate" } }, { "type": "mention", "text": "<at>Mark UPN</at>", "mentioned": { "id": "Mark@abc.com", "name": "Mark" } } ] } } } ] }) #we specify the headers to our request here headers = { 'Content-Type': 'application/json' } #the request method will be used in sending a post request method using our webhook url and the payload data response = requests.request("POST", url, headers=headers, data=payload) #we get response to our text here print(response.text)
Automate the Process
We can automate this process so we don’t need to run the code each time instead use Windows Task Scheduler which we can configure for instance to run the script at 12 noon every day to send a message to us on Teams.
No Comments