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

你好,我正在编写一个聊天机器人,我需要能够在聊天中发送图像。它们只是小图标。 我尝试调整“处理附件”示例(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

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

请参阅此屏幕快照,以解释问题:

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

这是实际的图像:

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

!!

这是我使用的代码:

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#还是相当陌生,而且还对代码进行了一般性的介绍,我非常感谢!谢谢

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

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

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的更多示例。 希望它能有所帮助,并祝您有美好的一天。

本文链接:https://www.f2er.com/3071955.html

大家都在问