如何使用 Graph API 在 MS Teams 中发布消息

Pra*_*ash 3 c# microsoft-teams microsoft-graph-api

我正在尝试使用以下代码使用 Graph API 向 MS Teams 发送消息。

 List<Recipient> recipients = new List<Recipient>();
        recipients.Add(new Recipient
        {
            EmailAddress = new EmailAddress
            {
                Address = "user1@abctest.onmicrosoft.com"
            }
        });

        // Create the message.
        Message msg = new Message
        {
            Body = new ItemBody
            {
                Content = "Test message3",
                ContentType = BodyType.Text,
            },
            ToRecipients = recipients
        };

        _graphServiceClient.Users["fe0bb333-3334c49-a3eb-25af61fed1db"].SendMail(msg, true).Request().PostAsync().Wait();
Run Code Online (Sandbox Code Playgroud)

此代码不会在 MS Team 中发送消息,而是在电子邮件中发送该消息。

我正在遵循文档https://learn.microsoft.com/en-us/graph/api/message-send?view=graph-rest-1.0并尝试 Graph Explorer 发送消息但无法正常工作。

帖子:https://graph.microsoft.com/v1.0/users/fe0bb333-3335-4c49-a3eb-25af61fed1db/messages/22229b36-a7cb-4a33-a9f9-dd75742bf067/send

请求正文

{“正文”:“你好世界”}

但是,我从 Graph Explorer 收到以下错误:

{
    "error": {
        "code": "ErrorInvalidIdMalformed",
        "message": "Id is malformed.",
        "innerError": {
            "request-id": "9cddabed-f886-4c89-be8b-7b5735ad957f",
            "date": "2019-04-21T05:37:11"
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 5

Teams API 允许创建新聊天,以及向现有聊天发送消息。

创建一个新的聊天对象

向现有聊天发送消息

列出聊天以检索现有聊天的 ID

其他选项

如果您不知道如何向用户发送消息,您可能会对发送主动消息机器人主动消息传递感兴趣。