首页 > 解决方案 > 将内联图片作为 MS Team Bot 发送时出现错误请求

问题描述

当我使用以下代码将附件作为 MS Teams Bot 的内联图片包含在内时,我收到了错误的请求(该代码在没有附件的情况下工作):

var message = Activity.CreateMessageActivity();
message.Text = "message here";
message.Attachments = new List<Attachment>();
var webClient = new WebClient();
byte[] imageBytes = webClient.DownloadData("https://img.icons8.com/windows/452/showing-small-size.png");
string url = "data:image/png;base64," + Convert.ToBase64String(imageBytes);

message.Attachments.Add(new Attachment
{
    ContentType = "image/png",
    ContentUrl = url
});

var conversationParameters = new ConversationParameters
{
  IsGroup = true,
  ChannelData = new TeamsChannelData
  {
      Channel = new ChannelInfo(outputChannelId),
  },
  Activity = (Activity)message
};

var response = await connectorClient.Conversations.CreateConversationAsync(conversationParameters);

我收到以下错误:

Microsoft.Bot.Schema.ErrorResponseException: Operation returned an invalid status code 'BadRequest'
   at Microsoft.Bot.Connector.Conversations.CreateConversationWithHttpMessagesAsync

提前致谢!

标签: botframeworkbotsmicrosoft-teamsmicrosoft-graph-teams

解决方案


与其将图像作为附件包含,不如考虑发送自适应卡片,您可以将图像直接显示给用户。在此处查看有关自适应卡片的更多信息。

作为替代方案,如果您不想显示图像但它托管在某处,只需在文本消息中包含指向它的链接,使用 Teams 中常规文本消息支持的降价格式(在此处查看更多信息)。


推荐阅读