无法通过团队发送附件(图像等)将Webhook发送到Web服务

时间:2020-07-01 08:03:15

标签: post webhooks attachment microsoft-teams

我在团队中使用外发Webhook将文本以及附件/图像发送到自定义Web服务。 我在网络服务中获取文本消息数据,但是从团队发送时(出网挂钩)我没有图像数据或任何附件

是否支持? 如果没有任何解决方法?

1 个答案:

答案 0 :(得分:1)

实际的卡JSON附件未发送到Webhook。这类似于漫游器场景。

对于图像,如果用户附加了图像,则Bot后端将对其进行解析并将其转换为img标签,并且该内容应以HTML形式在附件对象之一中提供。图像本身存储在AMS(Teams后端存储)中,并且源URL也将代表AMS URL,而不是原始URL。 用户发送的示例图像:

img height=\"142\" src=\"https://us-api.asm.skype.com/v1/objects/0-eus-d1-abe032166f0b806fe9cb17411e42678f/views/imgo\" width=\"336\" id=\"x_0-eus-d1-abe032166f0b806fe9cb17411e42678f\" itemscope=\"\" itemtype=\"http://schema.skype.com/AMSImage\" style=\"vertical-align:bottom; width:336px; height:142px\">

您应该能够使用MicrosoftAppCredential下载图像。 示例代码:

using (HttpClient httpClient = new HttpClient())
            {
                // MS Teams attachment URLs are secured by a JwtToken, so we need to pass the token from our bot.
                var token = await new MicrosoftAppCredentials("id", "password").GetTokenAsync();
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                var responseMessage = await httpClient.GetAsync(imageUrl);
                var contentLenghtBytes = responseMessage.Content.Headers.ContentLength;
                // You could not use this response message to fetch the image for further processing.

                if (responseMessage.StatusCode == System.Net.HttpStatusCode.Accepted)
                {
                    Stream attachmentStream = await responseMessage.Content.ReadAsStreamAsync();
                    attachmentStream.Position = 0;
                    System.Drawing.Image image = System.Drawing.Image.FromStream(attachmentStream);
                    image.Save(@"ImageFromUser.png");
                }
            }