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

时间:2019-04-21 04:31:22

标签: c# microsoft-graph microsoft-teams

我正在尝试通过以下代码使用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();

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

我正在遵循https://docs.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

请求正文

  

{        “正文”:“ Hello World”       }

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

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

2 个答案:

答案 0 :(得分:1)

截至2020年10月的更新

Teams API现在支持使用send ChatMessage.

直接向用户发送消息

但是,您无法创建新的聊天,只能在现有的聊天中创建新消息:

此API无法创建新的聊天;您必须先使用list chats方法来检索现有聊天的ID,然后再创建聊天消息。

如果您不知道如何向用户发送消息,则可能对sending proactive messagesproactive messaging using a bot in Microsoft Teams.感兴趣

答案 1 :(得分:0)

如今,图表中Microsoft Teams的API仅支持将消息发送到通道,而不是发送给用户(私人聊天)。如您所见,here仅列出了通道操作。