简体   繁体   English

在 Microsoft Bot framework V4 中从 Bot 向用户发送 Excel 文件

[英]Sending an Excel file from Bot to User in Microsoft Bot framework V4

I am developing a Bot application using MS bot framework V4.我正在使用 MS bot 框架 V4 开发一个 Bot 应用程序。 I want to send Excel file (.xlsx) from Bot to the user, it is a template which user later fills in and send back to Bot.我想将 Excel 文件(.xlsx)从 Bot 发送给用户,这是用户稍后填写并发送回 Bot 的模板。 But with the following code, I can see the file in the chat window but user is not able to download the file.但是使用以下代码,我可以在聊天 window 中看到该文件,但用户无法下载该文件。 How can I achieve this functionality where user can download it.如何在用户可以下载的地方实现此功能。 Note: Please see the ELSE part below where user is supposed to download the Excel template.注意:请参阅下面的ELSE部分,用户应该下载 Excel 模板。

 private async Task<DialogTurnResult> UploadFileTemplateCheckStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        string choice = ((FoundChoice)stepContext.Result).Value;
        if (choice.ToLower().Equals("yes"))
        {
            stepContext.Values["IsFileTemplate"] = (string)stepContext.Result;
            return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions
            { Prompt = MessageFactory.Text("Please upload the file.") }, cancellationToken);
        }
        else
        {
            //Provide the template.
            var ImagePath = Path.Combine(_env.WebRootPath, "TeeUpTemplate.xlsx");
            var ImageData = Convert.ToBase64String(File.ReadAllBytes(ImagePath));
            Attachment attachment = new Attachment()
            {
                Name = "TeeUpTemplate.xlsx", ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                ThumbnailUrl = Path.Combine(_env.WebRootPath, "Excel.png"),
                ContentUrl = $"data:application/vnd.ms-excel;base64,{ImageData}"
            };

            IMessageActivity reply = MessageFactory.Text("Use this template to fill the file.");
            reply.Attachments = new List<Attachment>();
            reply.Attachments.Add(attachment);
            reply.From = stepContext.Context.Activity.From;
            await stepContext.Context.SendActivityAsync(reply, cancellationToken);

            return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions
            { Prompt = MessageFactory.Text("Upload the file") }, cancellationToken);
        }
    }

Here is the solution which I implemented.这是我实施的解决方案。 If your bot works with attachments then one of the method is One Drive subscription or sharepoint subscription.如果您的机器人使用附件,则其中一种方法是 One Drive 订阅或 sharepoint 订阅。 It is a mandate.这是一项任务。 I used One Drive subscription.我使用了 One Drive 订阅。 All the attachment first gets uploaded or downloaded to your One Drive Account from where it goes to the bot.所有附件首先会上传或下载到您的 One Drive 帐户,然后再发送到机器人。

The major drawback for developers is you can not test this in Bot Emulator, it has to be tested directly in Teams as Content Type = application/vnd.microsoft.teams.file.download.info only has support in Teams.开发人员的主要缺点是您无法在 Bot Emulator 中对此进行测试,它必须直接在 Teams 中进行测试,因为Content Type = application/vnd.microsoft.teams.file.download.info仅在 Teams 中具有支持。

One more point is you will have to upload Manifest file with file with还有一点是您必须上传清单文件和文件

"supportsFiles": true,

You may refer this sample for C# to get more insights:您可以参考 C# 的此示例以获得更多见解:

https://github.com/microsoft/BotBuilder-Samples/tree/main/samples/csharp_dotnetcore/56.teams-file-upload https://github.com/microsoft/BotBuilder-Samples/tree/main/samples/csharp_dotnetcore/56.teams-file-upload

Thank you.谢谢你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM