gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
python script send adaptive card user to user in ms teams use graph api
- User
python script send adaptive card user to user in ms teams use graph api
- Assistant
python script send adaptive card user to user in ms teams use graph api
To send an Adaptive Card to a user in Microsoft Teams using the Graph API in Python, you can follow these steps:
Make sure you have the required permissions. You need to have the delegated permission User.Read.All and ChannelMessage.Send for the Microsoft Graph API.
Install the required Python packages: requests and adal.
Import the necessary modules:
import requests
import json
from adal import AuthenticationContext
tenant_id = '<Your Tenant ID>'
client_id = '<Your Client ID>'
client_secret = '<Your Client Secret>'
user_id = '<User ID of the recipient>'
card = {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.3",
"body": [
{
"type": "TextBlock",
"text": "Hello!",
"size": "large",
"weight": "bolder",
"color": "accent"
},
{
"type": "TextBlock",
"text": "This is an Adaptive Card sent via Graph API from Python!"
}
],
"actions": [
{
"type": "Action.OpenUrl",
"title": "Learn more",
"url": "https://adaptivecards.io/"
}
]
}
adal library:authority_url = f'https://login.microsoftonline.com/{tenant_id}'
resource_url = 'https://graph.microsoft.com'
context = AuthenticationContext(authority_url)
token = context.acquire_token_with_client_credentials(resource_url, client_id, client_secret)
access_token = token['accessToken']
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {access_token}'
}
body = {
'message': {
'subject': 'Adaptive Card from Python',
'body': {
'contentType': 'html',
'content': json.dumps(card)
},
'toRecipients': [
{
'emailAddress': {
'address': user_id
}
}
]
},
'saveToSentItems': 'false'
}
response = requests.post('https://graph.microsoft.com/v1.0/me/sendMail', headers=headers, data=json.dumps(body))
if response.status_code == 202:
print('Adaptive Card sent successfully!')
else:
print('Failed to send Adaptive Card.')
print(response.text)
Make sure to replace the placeholders <Your Tenant ID>, <Your Client ID>, <Your Client Secret>, and <User ID of the recipient> with your actual values.
This will send an Adaptive Card as an email to the specified user in Microsoft Teams.
Not yet included in a published release.