如何在Bot Framework Composer中实现文件保存至Azure File Share
1. 提取原有SDK代码核心逻辑
先把你之前用Bot Framework SDK实现的文件解析+Azure File Share保存核心代码抽离出来,保留最关键的业务逻辑:
- 从对话活动中读取用户上传的附件
- 下载附件内容流
- 连接Azure File Share并完成文件上传
示例核心代码片段:
using Azure.Storage.Files.Shares; using Microsoft.Bot.Schema; using System.Net.Http; using System.IO; public async Task SaveAttachmentToFileShare(Attachment attachment, string connectionString, string shareName) { // 下载附件内容 var httpClient = new HttpClient(); var response = await httpClient.GetAsync(attachment.ContentUrl); var contentStream = await response.Content.ReadAsStreamAsync(); // 连接Azure File Share并创建目录(如果不存在) var shareClient = new ShareClient(connectionString, shareName); await shareClient.CreateIfNotExistsAsync(); var directoryClient = shareClient.GetDirectoryClient("user-uploads"); await directoryClient.CreateIfNotExistsAsync(); // 上传文件到指定目录 var fileClient = directoryClient.GetFileClient(attachment.Name); await fileClient.UploadAsync(contentStream, overwrite: true); }
2. 在Composer机器人中添加自定义代码
Composer基于Bot Framework SDK构建,需在机器人解决方案中添加自定义逻辑:
- 直接在机器人的Runtime项目中新建类文件,将上述核心代码放入
- 安装必要的NuGet包:
Azure.Storage.Files.Shares、Microsoft.Bot.Builder
3. 创建自定义Dialog(可被Composer调用的Action)
为让Composer能在对话流中触发保存操作,需封装自定义Dialog:
using Microsoft.Bot.Builder.Dialogs; using System.Threading; using System.Threading.Tasks; public class SaveToFileShareDialog : Dialog { private readonly string _storageConnectionString; private readonly string _fileShareName; public SaveToFileShareDialog(string dialogId, string connectionString, string shareName) : base(dialogId) { _storageConnectionString = connectionString; _fileShareName = shareName; } public override async Task<DialogTurnResult> BeginDialogAsync(DialogContext dc, object options = null, CancellationToken cancellationToken = default) { // 获取当前对话中的附件 var attachments = dc.Context.Activity.Attachments; if (attachments != null && attachments.Count > 0) { foreach (var attachment in attachments) { await SaveAttachmentToFileShare(attachment, _storageConnectionString, _fileShareName); } await dc.Context.SendActivityAsync("文件已成功保存至Azure File Share"); } else { await dc.Context.SendActivityAsync("未检测到上传文件,请重新上传"); } return await dc.EndDialogAsync(cancellationToken: cancellationToken); } // 复用之前的核心保存方法 private async Task SaveAttachmentToFileShare(Attachment attachment, string connectionString, string shareName) { // 核心保存逻辑代码 } }
4. 注册自定义Dialog到Composer机器人
在Runtime项目的Startup.cs中,将自定义Dialog注入到服务容器:
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Configuration; // 在ConfigureServices方法中添加 services.AddSingleton<SaveToFileShareDialog>(sp => { var config = sp.GetRequiredService<IConfiguration>(); var connectionString = config["AzureFileShareConnectionString"]; var shareName = config["AzureFileShareName"]; return new SaveToFileShareDialog("SaveToFileShare", connectionString, shareName); });
同时在Composer的Bot Settings中添加两个配置项:
AzureFileShareConnectionString:你的Azure存储账户连接字符串AzureFileShareName:目标文件共享名称
5. 在Composer对话流中调用自定义Action
打开Composer的对话设计界面:
- 在需要处理文件上传的节点,添加Run a custom action步骤
- 选择已注册的
SaveToFileShareDialog - 设置触发条件:在触发器中选择“Message with attachments”,确保用户上传文件时自动触发
6. 验证功能
启动Composer机器人,上传测试文件,检查Azure File Share的目标目录下是否成功保存文件;若出现问题,可在自定义代码中添加日志排查连接权限、文件流读取等问题。
内容的提问来源于stack exchange,提问作者joegreen
相关产品推荐
相关产品推荐

