【问题标题】:Microsoft Bot builder : add pdf attachement and open it in web browserMicrosoft Bot builder:添加 pdf 附件并在 Web 浏览器中打开它
【发布时间】:2018-05-03 14:58:40
【问题描述】:

我正在尝试创建一个机器人,我需要在其中添加一个 pdf 文件作为附件,当用户单击它时,它应该在网络浏览器中打开或下载。 我已经尝试过这样做,但正如您在enter image description here 中看到的那样,当我点击它时没有任何反应。

添加pdf附件的代码是:

  private static Attachment GetInlineAttachment()
            {
                var imagePath = System.Web.HttpContext.Current.Server.MapPath("~/Files/mutuelle.pdf");

              //  var imageData = Convert.ToBase64String(File.ReadAllBytes(imagePath));

                return new Attachment
                {
                    Name = "mutuelle.pdf",
                    ContentType = "application/pdf",
                    ContentUrl = imagePath
                };

            }
[LuisIntent("Mutuelle")]
        public async Task MutuelleIntent(IDialogContext context, LuisResult result)
        {


            var replyMessage = context.MakeMessage();
            try
            {

                Attachment attachment = GetInlineAttachment();
                replyMessage.Attachments = new List<Attachment> { attachment };
                await context.PostAsync(" Vous retrouverez dans le document ci-dessous toutes les prestations prise en charge par notre mutuelle.");

                await context.PostAsync(replyMessage);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }

            context.Wait(MessageReceived);
        }

有人可以帮帮我吗?

【问题讨论】:

  • 你想使用什么渠道?
  • 我目前正在使用模拟器,但我的目标是在 Azure 上使用(我认为)网络聊天频道

标签: .net pdf botframework azure-language-understanding


【解决方案1】:

需要添加一个pdf文件作为附件,当用户点击它时,它应该在网络浏览器中打开或下载。

您似乎想将存储在您的应用程序文件夹中的 PDF 文件作为附件发送给用户。如果可能,您可以尝试将这些 PDF 文件存储在 Azure Blob 存储中(而不是应用程序文件夹中),可以通过 HTTP 或 HTTPS 从世界任何地方访问。然后您可以将文件作为附件发送给用户,如下所示:

var replyMessage = context.MakeMessage();

replyMessage.Attachments = new List<Attachment>
{
    new Attachment
    {
        Name = "mutuelle.pdf",
        ContentType = "application/pdf",
        ContentUrl = "https://{storageaccount}.blob.core.windows.net/files/mutuelle.pdf?st=2018-05-04T08%3A03%3A29Z&se=2018-05-05T08%3A03%3A29Z&sp=rl&sv=2017-04-17&sr=b&sig=sWYrbwPhgGeOEVNAAoRZXiIC%2B0sNtOMvokeRGkorDhM%3D"
    }
};

await context.PostAsync(" Vous retrouverez dans le document ci-dessous toutes les prestations prise en charge par notre mutuelle.");
await context.PostAsync(replyMessage);

此外,某些渠道可能不允许漫游器将某些类型的文件(例如 .pdf)作为附件发送。在这种情况下,如果将文件存储在 Azure Blob 存储中,则可以将该文件的 URL 作为纯文本消息共享给用户,然后用户可以复制该 URL 并在浏览器中打开或下载它。如果你想限制对存储在 Azure Blob 存储中的文件的访问,Azure Blob 存储提供了SAS,可用于授予对存储资源的有限访问权限。

注意:issues are reported in github关于部分渠道不支持发送pdf附件

【讨论】:

    【解决方案2】:

    我可以通过使用下面的代码来实现这一点,看起来使用英雄卡在这里真的很有帮助。因为有一次我知道模拟器中有一个错误,如果机器人共享一个链接,它是不可点击的,因为点击事件注册在聊天气泡上,而不是链接本身。不知道这是否得到了解决。

    private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
    {
            var reply2 = await result as Activity;
            Activity reply = reply2.CreateReply();
            string image64;
            try
            {
                string base64String;
                var image = Convert.ToBase64String(File.ReadAllBytes(
                    @"C:\Users\v-jassow\Documents\Visual Studio 2017\Projects\Bot Application6\Bot Application6\nachos.pdf"));
                image64 = "data:application/pdf;base64," + image; 
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
    
            HeroCard heroCard = new HeroCard()
            {
                Text = "Click me",
                Tap = new CardAction()
                {
                    Value = @"http://localhost:3979/nachos.pdf",
                    Type = "openUrl",
                }
            };
    
            reply.Attachments = new List<Attachment>
            {
                heroCard.ToAttachment()
            };
            await context.PostAsync(reply);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-12
      • 2012-08-18
      • 2021-03-15
      相关资源
      最近更新 更多