【问题标题】:Microsoft Graph - Teams API works on Graph Explorer but not via codeMicrosoft Graph - Teams API 在 Graph Explorer 上工作,但不是通过代码
【发布时间】:2020-12-11 09:02:31
【问题描述】:

我需要在 Teams 频道上自动发布消息并提及该频道。不幸的是,通过 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}",
                    "conversationIdentityType@odata.type": "#Microsoft.Teams.GraphSvc.conversationIdentityType",
                    "conversationIdentityType": "channel"
                }
            }
        }
    ]
}

运行查询被按下时,消息发布成功,频道被提及。然后,我从图形资源管理器中检索了 C# 代码的代码 sn-p,这导致了以下代码

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}\",\"conversationIdentityType@odata.type\":\"#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

【问题讨论】:

  • 嗨,马克,如果发布的答案解决了您的问题,请点赞或单击复选标记将其标记为答案。这样做可以帮助其他人找到他们问题的答案。见meta.stackexchange.com/questions/5234/…
  • 您在应用中使用的是哪个 Microsoft 图形版本 dll?
  • @Trinetra-MSFT 我正在使用 Microsoft.Graph.Beta 版本 0.35.0-preview。但是 Shiva - 下面的 MSFT Identity 答案运行良好

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


【解决方案1】:

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

Identity A = JsonConvert.DeserializeObject<Identity>("{\"id\":\"{channel id}\",\"displayName\":\"{channel name}\",\"conversationIdentityType@odata.type\":\"#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:342b9f379eb340048b16d9859d9e3712@thread.tacv2"].Messages
                .Request()
                .AddAsync(chatMessage);

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

它会起作用的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    相关资源
    最近更新 更多