如何避免图像MS Bot构建器框架的自动重新封装

问题描述 投票:3回答:1

你好,我正在编写一个聊天机器人,我需要能够在聊天中发送图像。它们只是小图标。我已尝试调整“处理附件”示例(https://github.com/microsoft/BotBuilder-Samples/blob/master/samples/csharp_dotnetcore/15.handling-attachments/Bots/AttachmentsBot.cs

中的代码

以及文档的此页面:(https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-howto-add-media-attachments?view=azure-bot-service-4.0&tabs=csharp

但是它会自动调整小图标的大小以适合较大的框架。我不确定为什么...

请参见下面的屏幕截图,其中说明了问题:enter image description here

这是实际图像:enter image description here !!

这是我使用的代码:

var reply = MessageFactory.Text("This is an inline attachment.");
reply.Attachments = new List<Attachment>() { GetInlineAttachment() };
await stepContext.Context.SendActivityAsync(reply, cancellationToken);
       private static Attachment GetInlineAttachment()
        {
            var imagePath = Path.Combine(Environment.CurrentDirectory, @"Resources\uc2icon.png");
            var imageData = Convert.ToBase64String(File.ReadAllBytes(imagePath));

            return new Attachment
            {
                Name = @"Resources\architecture-resize.png",
                ContentType = "image/png",
                ContentUrl = $"data:image/png;base64,{imageData}",
            };
        }

我对C#还是相当陌生,而且通常也可以进行编码,感谢您的帮助!!谢谢

c# image botframework attachment chatbot
1个回答
0
投票

您可以使用自适应卡附件解决此问题,请尝试以下代码:

protected override async Task OnMessageActivityAsync(ITurnContext<IMessageActivity> turnContext, CancellationToken cancellationToken)
{

    var imagePath = Path.Combine(Environment.CurrentDirectory, @"Resources\uc2icon.png");
    var imageData = Convert.ToBase64String(File.ReadAllBytes(imagePath));
    var url = $"data:image/png;base64,{imageData}";

    var adaptiveJsonString = "{\"$schema\":\"http://adaptivecards.io/schemas/adaptive-card.json\",\"type\":\"AdaptiveCard\",\"version\":\"1.0\",\"body\":[{\"type\":\"ImageSet\",\"imageSize\":\"auto\",\"images\":[{\"type\":\"Image\",\"url\":\""+ url + "\"}]}]}";

    var adaptiveCardAttachment = new Attachment()
    {
        ContentType = "application/vnd.microsoft.card.adaptive",
        Content = JsonConvert.DeserializeObject(adaptiveJsonString),
    };


    await turnContext.SendActivityAsync(MessageFactory.Attachment(adaptiveCardAttachment), cancellationToken);
}

结果:enter image description here

有关机器人自适应卡的更多示例,see here。希望它能有所帮助,并祝您有美好的一天。

© www.soinside.com 2019 - 2024. All rights reserved.