从Microsoft Team Channel中的本地系统发送文件/图像作为消息

时间:2020-09-25 05:05:25

标签: microsoft-graph-api microsoft-teams

如何使用Microsoft Graph API的发送消息在Microsoft Teams Channel中发送本地文件/图像?

在创建发送团队频道的消息时,我遵循文档https://docs.microsoft.com/en-us/graph/api/resources/chatmessageattachment?view=graph-rest-1.0附加图像。我正在尝试发送图像的base64格式。但是我遇到了错误。

{
    "error": {
        "code": "InternalServerError",
        "message": "Failed to process request.",
        "innerError": {
            "date": "2020-09-25T11:43:02",
            "request-id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
            "client-request-id": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
        }
    }
}

1 个答案:

答案 0 :(得分:0)

似乎没有合适的API可让您直接将本地文件作为消息发送。

Creating chatMessage in a channel仅支持发送SharePoint中已存在的文件。因此,有两个步骤发送本地文件:首先,将文件上传到SharePoint,然后创建chatMessage。


1。将文件上传到SharePoint:

小于4MB的文件:

PUT https://graph.microsoft.com/v1.0/drives/{{drive-id}}/items/root:/{{file-name}}:/content
Header:
"Authorization" : "Bearer <access-token>"
Body: binary (select binary option in body in postman)
Upload a file using select file option
file-name: is file name along with extension example: test.txt

文件大于4MB:

GET: https://graph.microsoft.com/v1.0/drives/{{drive-id}}/items/root:/{{file-name}}:/createUploadSession
Header:
"Authorization" : "Bearer <access-token>"

“获取”请求将返回上传网址。

PUT <upload url>
Header:
"Authorization" : "Bearer <access-token>"
Body: binary (select binary option in body in postman)

有关更多详细信息,请参见here

2。创建chatMessage:

POST https://graph.microsoft.com/v1.0/teams/{id}/channels/{id}/messages
Content-type: application/json

{
    "body": {
        "contentType": "html",
        "content": "Here's the latest budget. <attachment id=\"153fa47d-18c9-4179-be08-9879815a9f90\"></attachment>"
    },
    "attachments": [
        {
            "id": "153fa47d-18c9-4179-be08-9879815a9f90",
            "contentType": "reference",
            "contentUrl": "https://m365x987948.sharepoint.com/sites/test/Shared%20Documents/General/test%20doc.docx",
            "name": "Budget.docx"
        }
    ]
}