【问题标题】:Graph API - BadRequest on sending channel messages with @mentionGraph API - 使用 @mention 发送频道消息时出现 BadRequest
【发布时间】:2021-09-03 11:17:01
【问题描述】:

我们有一些代码在过去 10 个月(自开发以来)一直在工作,今天下午才停止工作。这是一个 WebAPI 代码,用于发送提及机器人和用户的频道消息,现在返回“错误请求。发送了无效的请求正文。”

如果未提供“提及”属性,则调用有效,并且消息在没有@提及的情况下发送。所以,我想知道这个 API 是否发生了重大变化,现在期待“提及”属性采用不同的格式。

按照the Microsoft Graph documentation中的示例代码进行复制非常简单。

我在这里发帖是希望一些开发人员发现一些明显的东西,或者知道使用 API 的另一种方式,它可能会停止抱怨,因为微软需要永远回复。

以下是我们拥有的可以引导我发现问题的代码:

private async Task SendMentionToTheBotAsync(GraphServiceClient onBehalfOfClient, string userName, string teamId, string channelId)
{
        var supportAgentUser = await onBehalfOfClient.Me.Request().GetAsync();

        var chatMessage = new ChatMessage
        {
            Body = new ItemBody
            {
                ContentType = BodyType.Html,
                Content = $"<at id=\"0\">{Configuration["BotName"]}</at>: This is the start of the conversation between {userName} and <at id=\"1\">{supportAgentUser.DisplayName}</at>."
            },
            Mentions = new List<ChatMessageMention>
              {
                new ChatMessageMention
                {
                    Id = 0,
                    MentionText = Configuration["BotName"],
                    Mentioned = new IdentitySet
                    {
                        Application = new Identity
                        {
                            DisplayName = Configuration["BotName"],
                            Id = Configuration["BotAppId"],
                            AdditionalData = new Dictionary<string,object>
                            {
                                {
                                    "applicationIdentityType", "bot"
                                }
                            }
                        }
                    }
                },
                new ChatMessageMention
                {
                    Id = 1,
                    MentionText = supportAgentUser.DisplayName,
                    Mentioned = new IdentitySet
                    {
                        User = new Identity
                        {
                            DisplayName = supportAgentUser.DisplayName,
                            Id = supportAgentUser.Id,
                            AdditionalData = new Dictionary<string,object>
                            {
                                {
                                    "userIdentityType", "aadUser"
                                }
                            }
                        }
                    }
                }
            }
        };

        await onBehalfOfClient.Teams[teamId].Channels[channelId].Messages
                              .Request()
                              .AddAsync(chatMessage);
    }

【问题讨论】:

  • 如果你想让 webapi 方面的专家来看看这个,你应该更加小心你的标签。添加相关的webapi标签,去掉graph标签。

标签: asp.net-web-api graph send channel mention


【解决方案1】:

Microsoft 支持回复:

“感谢您联系 Microsoft 支持。

我了解该问题与向 Teams 发布消息有关。根据屏幕截图,您似乎正在使用提及频道。您可能在请求中使用了键“conversationIdentityType@odata.type”。

您能否尝试从请求正文中删除“conversationIdentityType@odata.type”键,然后重试。它应该工作。这是因为在亚洲地区的部署正在进行中。一旦 100% 推出,此密钥将不会被包含在请求中。”

删除了密钥,它对我有用。

【讨论】:

  • 感谢您在这里分享,迈克尔。由于我使用的是 Graph SDK,因此在处理请求时遇到了问题。我发布的代码组装了 ChatMessage 对象,然后我使用下面的代码发送它。关于如何从请求中删除密钥的任何想法?我已经尝试将聊天消息中的 oData 属性指定为字符串。为空,但它抱怨通知的 oData 无效。等待 onBehalfOfClient.Teams[teamId].Channels[channelId].Messages .Request() .AddAsync(chatMessage);
  • paulo,用截图回应,但我的答案被隐藏了。我的问题是在 Power Automate Flow 中使用经过身份验证的 Graph 查询发布到 Teams。删除上面我的 json 键后是: { "body": { "content": "Power Automate Flow Channel1", "contentType": "html" }, "提及”:[{“id”:0,“mentionText”:“Channel1”,“提及”:{“conversation”:{“id”:“19:1234@thread.tacv2”,“displayName”:“Channel1” , "conversationIdentityType": "频道" } } } ] }
【解决方案2】:

保罗,

不幸的是,我不是程序员。我在 Microsoft 365 Power Automate 工作流中使用 Graph 调用。我有一个应用程序,用于获取授权持有者令牌,然后使用图形 HTTP 操作发布到 Teams 消息。

这是 HTTP 的语法(如果你不熟悉 Flow,紫色项是变量)

click to view image of Power Automate workflow HTTP action

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多