편집

Send message in a chat

Namespace: microsoft.graph

Send a new chatMessage in the specified chat. This API can't create a new chat; you must use the list chats method to retrieve the ID of an existing chat before you can create a chat message.

Notes:

  • We don't recommend that you use this API for data migration via the standard create message flow. For data migration scenarios, use the import messages flow instead.
  • It's a violation of the terms of use to use Microsoft Teams as a log file. Only send messages that people will read.

This API is available in the following national cloud deployments.

Global service US Government L4 US Government L5 (DOD) China operated by 21Vianet

Permissions

Choose the permission or permissions marked as least privileged for this API. Use a higher privileged permission or permissions only if your app requires it. For details about delegated and application permissions, see Permission types. To learn more about these permissions, see the permissions reference.

Permission type Least privileged permissions Higher privileged permissions
Delegated (work or school account) ChatMessage.Send Chat.ReadWrite, Group.ReadWrite.All
Delegated (personal Microsoft account) Not supported. Not supported.
Application Teamwork.Migrate.All Not available.

HTTP request

POST /chats/{chat-id}/messages

Request headers

Name Description
Authorization Bearer {code}. Required.

Request body

In the request body, supply a JSON representation of a chatMessage object.

Response

If successful, this method returns a 201 Created response code and a new chatMessage object in the response body.

Examples

For a more comprehensive list of examples, see Create chatMessage in a channel or a chat.

Example 1: Create a chatMessage

Request

The following example shows a request.

POST https://graph.microsoft.com/v1.0/chats/19:2da4c29f6d7041eca70b638b43d45437@thread.v2/messages
Content-type: application/json

{
  "body": {
     "content": "Hello world"
  }
}

Response

The following example shows the response.

HTTP/1.1 201 Created
Content-type: application/json

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#chats('19%3A2da4c29f6d7041eca70b638b43d45437%40thread.v2')/messages/$entity",
    "id": "1616991463150",
    "replyToId": null,
    "etag": "1616991463150",
    "messageType": "message",
    "createdDateTime": "2021-03-29T04:17:43.15Z",
    "lastModifiedDateTime": "2021-03-29T04:17:43.15Z",
    "lastEditedDateTime": null,
    "deletedDateTime": null,
    "subject": null,
    "summary": null,
    "chatId": "19:2da4c29f6d7041eca70b638b43d45437@thread.v2",
    "importance": "normal",
    "locale": "en-us",
    "webUrl": null,
    "channelIdentity": null,
    "policyViolation": null,
    "eventDetail": null,
    "from": {
        "application": null,
        "device": null,
        "conversation": null,
        "user": {
            "id": "8ea0e38b-efb3-4757-924a-5f94061cf8c2",
            "displayName": "Robin Kline",
            "userIdentityType": "aadUser"
        }
    },
    "body": {
        "contentType": "text",
        "content": "Hello World"
    },
    "attachments": [],
    "mentions": [],
    "reactions": [],
    "messageHistory": []
}

Example 2: Import a message

The following example shows how to import a message. For more information, see Import messages into Microsoft Teams chats and channels using Microsoft Graph.

Note: The permission scope Teamwork.Migrate.All is required for this scenario. The target chat must be in migration mode.

Important

The createdDateTime must be unique down to the millisecond within the target chat. If a message with the same createdDateTime exists, the request fails with 409 Conflict. Adjust the createdDateTime and retry. For more information, see Import messages into Microsoft Teams chats and channels using Microsoft Graph.

Request

The following example shows how to import a message into a chat on behalf of a user using the createdDateTime and from properties in the request body.

POST https://graph.microsoft.com/v1.0/chats/19:4b6bed8d24574f6a9e436813cb2617d8@thread.tacv2/messages

{
   "createdDateTime": "2019-02-04T19:58:15.511Z",
   "from": {
      "user": {
         "id": "8ea0e38b-efb3-4757-924a-5f94061cf8c2",
         "displayName": "Robin Kline",
         "userIdentityType": "aadUser"
      }
   },
   "body": {
      "contentType": "html",
      "content": "Hello World"
   }
}

Response

The following example shows the response.

HTTP/1.1 201 Created

{
    "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#chats('19%3A4b6bed8d24574f6a9e436813cb2617d8%40thread.tacv2')/messages/$entity",
    "id": "1616991463150",
    "replyToId": null,
    "etag": "1616991463150",
    "messageType": "message",
    "createdDateTime": "2019-02-04T19:58:15.511Z",
    "lastModifiedDateTime": null,
    "deletedDateTime": null,
    "subject": null,
    "summary": null,
    "chatId": "19:4b6bed8d24574f6a9e436813cb2617d8@thread.tacv2",
    "importance": "normal",
    "locale": "en-us",
    "webUrl": null,
    "channelIdentity": null,
    "policyViolation": null,
    "eventDetail": null,
    "from": {
        "application": null,
        "device": null,
        "conversation": null,
        "user": {
            "id": "8ea0e38b-efb3-4757-924a-5f94061cf8c2",
            "displayName": "Robin Kline",
            "userIdentityType": "aadUser"
        }
    },
    "body": {
        "contentType": "html",
        "content": "Hello World"
    },
    "attachments": [],
    "mentions": [],
    "reactions": []
}