命名空间:microsoft.graph
重要
Microsoft Graph /beta
版本下的 API 可能会发生更改。 不支持在生产应用程序中使用这些 API。 若要确定 API 是否在 v1.0 中可用,请使用 版本 选择器。
在指定的聊天中发送新的 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。
请求
以下示例显示了一个请求。
POST https://graph.microsoft.com/beta/chats/19:2da4c29f6d7041eca70b638b43d45437@thread.v2/messages
Content-type: application/json
{
"body": {
"content": "Hello world"
}
}
// Code snippets are only available for the latest version. Current version is 5.x
// Dependencies
using Microsoft.Graph.Beta.Models;
var requestBody = new ChatMessage
{
Body = new ItemBody
{
Content = "Hello world",
},
};
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=csharp
var result = await graphClient.Chats["{chat-id}"].Messages.PostAsync(requestBody);
mgc-beta chats messages create --chat-id {chat-id} --body '{\
"body": {\
"content": "Hello world"\
}\
}\
'
// Code snippets are only available for the latest major version. Current major version is $v0.*
// Dependencies
import (
"context"
msgraphsdk "github.com/microsoftgraph/msgraph-beta-sdk-go"
graphmodels "github.com/microsoftgraph/msgraph-beta-sdk-go/models"
//other-imports
)
requestBody := graphmodels.NewChatMessage()
body := graphmodels.NewItemBody()
content := "Hello world"
body.SetContent(&content)
requestBody.SetBody(body)
// To initialize your graphClient, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=go
messages, err := graphClient.Chats().ByChatId("chat-id").Messages().Post(context.Background(), requestBody, nil)
// Code snippets are only available for the latest version. Current version is 6.x
GraphServiceClient graphClient = new GraphServiceClient(requestAdapter);
ChatMessage chatMessage = new ChatMessage();
ItemBody body = new ItemBody();
body.setContent("Hello world");
chatMessage.setBody(body);
ChatMessage result = graphClient.chats().byChatId("{chat-id}").messages().post(chatMessage);
const options = {
authProvider,
};
const client = Client.init(options);
const chatMessage = {
body: {
content: 'Hello world'
}
};
await client.api('/chats/19:2da4c29f6d7041eca70b638b43d45437@thread.v2/messages')
.version('beta')
.post(chatMessage);
<?php
use Microsoft\Graph\Beta\GraphServiceClient;
use Microsoft\Graph\Beta\Generated\Models\ChatMessage;
use Microsoft\Graph\Beta\Generated\Models\ItemBody;
$graphServiceClient = new GraphServiceClient($tokenRequestContext, $scopes);
$requestBody = new ChatMessage();
$body = new ItemBody();
$body->setContent('Hello world');
$requestBody->setBody($body);
$result = $graphServiceClient->chats()->byChatId('chat-id')->messages()->post($requestBody)->wait();
Import-Module Microsoft.Graph.Beta.Teams
$params = @{
body = @{
content = "Hello world"
}
}
New-MgBetaChatMessage -ChatId $chatId -BodyParameter $params
# Code snippets are only available for the latest version. Current version is 1.x
from msgraph_beta import GraphServiceClient
from msgraph_beta.generated.models.chat_message import ChatMessage
from msgraph_beta.generated.models.item_body import ItemBody
# To initialize your graph_client, see https://learn.microsoft.com/en-us/graph/sdks/create-client?from=snippets&tabs=python
request_body = ChatMessage(
body = ItemBody(
content = "Hello world",
),
)
result = await graph_client.chats.by_chat_id('chat-id').messages.post(request_body)
响应
以下示例显示了相应的响应。
HTTP/1.1 201 Created
Content-type: application/json
{
"@odata.context": "https://graph.microsoft.com/beta/$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,
"onBehalfOf": 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": []
}