How can I use python to send a message to a team's user ?

Michael Wilcox 26 Reputation points
2024-11-13T22:46:36.1866667+00:00

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"))

Microsoft Security Microsoft Graph
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2024-11-14T01:32:06.0833333+00:00

    Hello Michael Wilcox,

    Thank you for reaching out to Microsoft Support!

    In the Create chat, the value of "******@odata.bind" should be "https://graph.microsoft.com/v1.0/users (user_id/user_principal_name)", Please amend your first member the value of the "https://graph.microsoft.com/v1.0/me".

    Hope this helps.

    If the answer is helpful, please click Accept Answer and kindly upvote it. If you have any further questions about this answer, please click Comment.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.