在聊天中发送消息

命名空间:microsoft.graph

在指定的聊天中发送新的 chatMessage。 此 API 无法创建新聊天;必须先使用 列表聊天 方法检索现有聊天的 ID,然后才能创建聊天消息。

注意:

  • 建议不要通过标准创建消息流使用此 API 进行数据迁移。 对于数据迁移方案,请改用 导入消息 流。
  • 将 Microsoft Teams 用作日志文件违反了 使用条款 。 仅发送人们将阅读的消息。

此 API 可用于以下国家级云部署。

全局服务 美国政府 L4 美国政府 L5 (DOD) 由世纪互联运营的中国
✅ ✅ ✅ ✅

权限

为此 API 选择标记为最低特权的权限。 只有在应用需要它时,才使用更高的特权权限。 有关委派权限和应用程序权限的详细信息,请参阅权限类型。 要了解有关这些权限的详细信息,请参阅 权限参考。

权限类型 最低特权权限 更高特权权限
委派(工作或学校帐户) ChatMessage.Send Chat.ReadWrite、Group.ReadWrite.All
委派(个人 Microsoft 帐户) 不支持。 不支持。
应用程序 Teamwork.Migrate.All 不可用。

HTTP 请求

POST /chats/{chat-id}/messages

请求标头

名称 说明
Authorization Bearer {code}。 必需。

请求正文

在请求正文中,提供 chatMessage 对象的 JSON 表示形式。

响应

如果成功,此方法在响应正文中返回响应 201 Created 代码和新的 chatMessage 对象。

示例

有关更全面的示例列表,请参阅 在频道或聊天中创建 chatMessage。

示例 1:创建 chatMessage

请求

以下示例显示了一个请求。

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

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

响应

以下示例显示了相应的响应。

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": []
}

示例 2:导入邮件

以下示例演示如何导入消息。 有关详细信息,请参阅 使用 Microsoft Graph 将消息导入Microsoft Teams 聊天和频道。

注意:此方案需要权限范围 Teamwork.Migrate.All 。 目标聊天必须处于迁移模式。

重要

createdDateTime 在目标聊天中必须唯一到毫秒。 如果存在具有相同 createdDateTime 的消息,则请求失败并出现 409 Conflict。 调整 createdDateTime 并重试。 有关详细信息,请参阅 使用 Microsoft Graph 将消息导入Microsoft Teams 聊天和频道。

请求

以下示例演示如何使用 createdDateTime 和 请求 正文中的属性代表用户将消息导入聊天。

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

响应

以下示例显示了相应的响应。

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": []
}