Vitamin C
Vitamin C

Reputation: 312

Microsoft Graph API how to send an attachment in a chat

I want to use the Microsoft Graph API to send messages with attachments to chats or channels. https://learn.microsoft.com/en-us/graph/api/chatmessage-post?view=graph-rest-1.0&tabs=http#example-4-send-a-message-with-file-attachment-in-it

I can send normal messages already just fine like this:

def post_message(chat_id: str, subject: str = "", content_type: str = "text", content: str = "") -> None:
    url = f"https://graph.microsoft.com/v1.0/chats/{chat_id}/messages"

    json = {
        "subject": subject,
        "body": {
            "contentType": content_type,
            "content": content
        }
    }

    res = requests.post(url, headers=header, json=json)

I try to copy the body from the example in the link above, substitute for my values and swap json variable for this one:

attachment_id = '7QW90B10D7-B5AK-420A-AC78-1156324A54F2' # not real, only to show how it looks like
json = {
    "body": {
        "contentType": 'html',
        "content": f'i dunno what i\'m doing. <attachment id="{attachment_id}"></attachment>'
    },
    'attachments': [
        {
            'id': attachment_id,
            'contentType': 'reference',
            'contentUrl': 'https://foo.sharepoint.com/sites/bar/User%20documentation/Databricks/Databricks%20guide.pptx',
            'name': 'Databricks guide.pptx'
        }
    ]
}

I get requests.exceptions.HTTPError: 400 Client Error: Bad Request for url

What's wrong with the code? How to get attachment id from a file correctly because I am not sure I got the right value?

Upvotes: 0

Views: 1598

Answers (1)

scottwtang
scottwtang

Reputation: 2040

I was able to get it working with your code, (using both formats <attachment id=\"\"> and <attachment id="">), so it seems the error is probably with your attachment_id.

I retrieved the driveItem ID by following this answer, where the driveItem ID is the GUID value in the eTag property in the response.

You could get the file by the path: https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root:/{item-path}

For example: https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root:/test.docx

If the file is in a folder, it would be like this: https://graph.microsoft.com/v1.0/sites/{site-id}/drive/root:/folder1/test.docx

Sample request after obtaining the ID

access_token = ""

attachment_name = "file.txt"
attachment_path = "https://domain.sharepoint.com/Shared%20Documents"
attachment_id = "12345678-1234-1234-1234-123456789123"
attachment_url = f"{attachment_path}/{attachment_name}"

chat_id = ""

req_url = f"https://graph.microsoft.com/v1.0/chats/{chat_id}/messages"

req_headers = {
        "Authorization": "Bearer " + access_token,
        "Content-Type": "application/json"
    }

json = {
    "body": {
        "contentType": "html",
        "content": f"Test message <attachment id=\"{attachment_id}\"></attachment>"
    },
    "attachments": [
        {
            "id": attachment_id,
            "contentType": "reference",
            "contentUrl": attachment_url,
            "name": attachment_name
        }
    ]
}

result = requests.post(url = req_url, headers = req_headers, json = json)

Upvotes: 1

Related Questions