How can I use python to send a message to a team's user ?
The python code says @odata.bind is missing yet it it is the json ? Here is the Error 400 {'error': {'code': 'BadRequest', 'message': "'@odata.bind' field is missing in the request.", 'innerError': {'date': '2024-11-13T22:40:19', 'request-id': '194448f7-3dc0-4cef-914c-bdacb2331fb5', 'client-request-id': '1
import requests
from msal import ConfidentialClientApplication
# Replace these values with your app's details
#client_id = 'YOUR_CLIENT_ID'
#client_secret = 'YOUR_CLIENT_SECRET'
#tenant_id = 'YOUR_TENANT_ID'
#user_email = 'USER_EMAIL' # Replace with the recipient's email address
scope = ['https://graph.microsoft.com/.default']
message = "Hello from Python!"
# Initialize the MSAL confidential client
app = ConfidentialClientApplication(client_id, authority=f'https://login.microsoftonline.com/{tenant_id}', client_credential=client_secret)
# Acquire a token
result = app.acquire_token_for_client(scopes=scope)
if 'access_token' in result:
access_token = result['access_token']
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
# Create a new chat
chat_payload = {
"chatType": "oneOnOne",
"members": [
{
"@odata.type": "#microsoft.graph.aadUserConversationMember",
"roles": ["owner"],
"******@odata.bind": "https://graph.microsoft.com/v1.0/me"
},
{
"@odata.type": "#microsoft.graph.aadUserConversationMember",
"roles": ["owner"],
"******@odata.bind": f"https://graph.microsoft.com/v1.0/users{user_email}"
}
]
}
# print("headers", headers, "chat payload",chat_payload)
response = requests.post('https://graph.microsoft.com/v1.0/chats', headers=headers, json=chat_payload )
if response.status_code == 201:
chat_id = response.json()['id']
print (id)
# Send a message to the new chat
message_payload = {
"body": {
"content": "Hello, this is a test message!"
}
}
message_response = requests.post(f'https://graph.microsoft.com/v1.0/chats/{chat_id}/messages', headers=headers, json=message_payload)
# 19:******@unq.gbl.spaces/messages
print("LINE 55: ", message_response.status_code, message_response.json())
else:
print("LINE 57: " , response.status_code, response.json())
else:
print("Failed to acquire token:", result.get("error"), result.get("error_description"))