Microsoft Graph-Teams API可在Graph Explorer上运行,但不能通过代码运行

马克·卡萨尔

我需要自动在团队频道上发布消息并提及该频道。不幸的是,通过MS Flow,没有提及整个频道的选项,但是似乎通过Graph API的beta版,我可以提及整个频道。

我首先尝试通过Graph Explorer将VERB更改为POST并将URL设置为 <https://graph.microsoft.com/beta/teams/{group ID}/channels/{channel id}/messages>

另外添加了以下请求正文

{
    "subject": "@Mention in Teams channel post!",
    "body": {
        "content": "Hello <at id ='0'>{channel name}</at>, Test message on the channel with at mention.",
        "contentType": "html"
    },
    "mentions": [
        {
            "id": 0,
            "mentionText": "{channel name}",
            "mentioned": {
                "conversation": {
                    "id": "{channel id}",
                    "displayName": "{channel name}",
                    "[email protected]": "#Microsoft.Teams.GraphSvc.conversationIdentityType",
                    "conversationIdentityType": "channel"
                }
            }
        }
    ]
}

按下运行查询”时,消息将成功发布并提及该频道。然后,我从图形资源管理器中检索了C#代码的代码段,从而得到了以下代码

GraphServiceClient graphClient = new GraphServiceClient(authProvider);

var chatMessage = new ChatMessage
{
    Subject = "@Mention in Teams channel post!",
    Body = new ItemBody
    {
        Content = "Hello <at id ='0'>{channel name}</at>, Test message on the channel with at mention.",
        ContentType = BodyType.Html
    },
    Mentions = new List<ChatMessageMention>()
    {
        new ChatMessageMention
        {
            Id = 0,
            MentionText = "{channel name}",
            Mentioned = new IdentitySet
            {
                AdditionalData = new Dictionary<string, object>()
                {
                    {"conversation", "{\"id\":\"{channel id}\",\"displayName\":\"{channel name}\",\"[email protected]\":\"#Microsoft.Teams.GraphSvc.conversationIdentityType\",\"conversationIdentityType\":\"channel\"}"}
                }
            }
        }
    }
};

await graphClient.Teams["{group id}"].Channels["{channel id}"].Messages
    .Request()
    .AddAsync(chatMessage);

但是,执行代码时,将显示以下错误:

ServiceException:代码:BadRequest消息:发送了无效的请求正文。

删除提及或更改提及以成功利用用户可以正常工作。另外,请注意,我尝试同时使用Microsoft.Graph和Microsoft.Graph.Beta

Shiva-MSFT身份

我对此进行了长期的研究,发现由于这种方式编写的代码,它在Graph服务器上发生了反序列化问题。主要问题是与提及属性中的对话有关。Graph服务器无法理解序列化的内容,因此请在发送请求之前尝试对其进行反序列化,如下所示。

Identity A = JsonConvert.DeserializeObject<Identity>("{\"id\":\"{channel id}\",\"displayName\":\"{channel name}\",\"[email protected]\":\"#Microsoft.Teams.GraphSvc.conversationIdentityType\",\"conversationIdentityType\":\"channel\"}");
            var chatMessage = new ChatMessage
            {
                Subject = "@Mention in Teams channel post!",
                Body = new ItemBody
                {
                    Content = "Hello <at id ='0'>General</at>, Test message on the channel with at mention.",
                    ContentType = BodyType.Html
                },
                Mentions = new List<ChatMessageMention>()
                {
                    new ChatMessageMention
                    {
                        Id = 0,
                        MentionText = "General",
                        Mentioned = new IdentitySet
                        {
                            AdditionalData = new Dictionary<string, object>()
                            {
                                {"conversation", A}
                            }
                        }
                    }
                }
            };
            
            try
            {
                await graphClient.Teams["d3b31e36-d63d-4bbe-9478-b4cc7cb17a3d"].Channels["19:[email protected]"].Messages
                .Request()
                .AddAsync(chatMessage);

            }
            catch(Exception e)
            {
                Console.WriteLine(e);
            }
        }

会的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Microsoft Graph Explorer / Microsoft Teams - 404 响应

Microsoft Graph API查询可在资源管理器上运行,但不能在Microsoft Graph .NET客户端库中使用

通过Microsoft Graph API访问Microsoft Teams频道消息时出现错误:Proxy_InternalServerError

Microsoft Teams Graph Explorer:如何在“发送频道消息”API 中提及频道成员?

使用 Microsoft Graph API 或 BOT API 发送 MS Teams 消息

Microsoft-Teams通过Graph API获取所有消息通道及其回复

graph.microsoft.com/beta/teams/***/channels/**/messages此api上的POST无法正常工作

如何使用控制台应用程序通过Microsoft Graph API调用Microsoft Teams OnlineMeeting端点?

如何通过Microsoft graph API C#在Microsoft Teams中将消息发送到1:1聊天或群聊

如何通过Graph API在MS Teams中安排会议

Microsoft Teams Graph API-请求中的无效绑定属性名称所有者

Microsoft Graph Api / Teams-无法列出频道中的聊天消息(401/403)

使用 Graph API 响应 Microsoft Teams 中的机器人调用

当ResourceURI包含结尾斜杠时,Microsoft Graph Teams API不起作用

如何使用Microsoft Graph API检索另一个用户的MS Teams聊天?

使用Graph API和委派权限将成员添加到Microsoft Teams

Microsoft Teams:使用 Graph API 跟踪/列出通话中的参与者

如何在 microsoft-teams graph api 中获得聊天消息的“可见”状态?

通过Microsoft graph api检索用户的图像

通过 Microsoft Graph API 创建用户

如何使用 Microsoft Graph 从 Microsoft Teams 机器人发送消息?

Teams Graph API,从每个 Teams 频道检索文件

通过 Microsoft GRAPH 的 OneDrive 通知

我可以使用Microsoft Graph API在我所属的MS Teams中的所有团队/组中搜索文件吗

使用Graph API在Microsoft Teams的机器人通道中以机器人的身份向用户发送消息

使用Graph Api创建团队时无法访问Microsoft Teams频道的电子邮件地址,SharePoint和文件

通过 Microsoft Graph API (Graph Explorer) 获取用户活动报告的问题

通过C#/ Graph禁用MS Teams邀请邮件

从 Microsoft Graph API 接收访问令牌但不接收刷新令牌