【问题标题】:How to post message in MS Teams using Graph API如何使用 Graph API 在 MS Teams 中发布消息
【发布时间】:2021-08-07 07:12:32
【问题描述】:

我正在尝试使用以下代码使用 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

请求正文

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

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

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

【问题讨论】:

    标签: c# microsoft-graph-api microsoft-teams


    【解决方案1】:

    20201 年 8 月更新

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

    Create a new chat object

    Send messages to existing chats

    List chats to retrieve the ID of an existing chat

    其他选项

    如果您不知道如何向用户发送消息,您可能会对sending proactive messages 和proactive messaging for bots. 感兴趣

    【讨论】:

    • 根据文档,用户现在可以create chats
    • 谢谢@Luv2Learn,我已经更新了答案以反映新方法。
    【解决方案2】:

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

    【讨论】:

    • 感谢@baywet 的更新。是的。我也在Graph Explorer工具中进行了测试,它可以在通道中发送消息。任何解决方法或调整或任何其他 API,如 EWS API 将有助于在私人消息(用户到用户)中发送消息?
    • 微软团队没有其他公共 API。我知道团队在一些事情上依赖于 Skype 的业务基础设施,不确定私人消息是否是其中之一。但你可以看看 ucwa,看看它是否会在某个时候重新出现在团队中。
    • 另一种方法可能是利用机器人来处理用户通信,但不确定它是否适合您的场景
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-10
    • 2021-03-22
    • 2022-01-02
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多