【发布时间】:2019-09-04 13:21:32
【问题描述】:
我正在编写一个聊天机器人,我希望用户上传一张图片,以便机器人可以接收它并将图片保存到用户配置文件数据中。但是我在 c# 中很新,我有点迷茫......
我正在使用 MS Bot 框架。为了构建对话框,我使用瀑布步骤并捕捉用户回复,我使用提示。要接收附件,我在 MS 文档上看到它存在一个 AttachmentPrompt 类。但是我对如何使用它以及如何将文件保存在用户配置文件中有点困惑。
这就是我构建瀑布对话框的方式:
public class MainDialog : ComponentDialog
{
// Prompts names
private const string PhotoPrompt = "PhotoPrompt";
// Dialog IDs
private const string ProfileDialog = "profileDialog";
public MainDialog(IStatePropertyAccessor<IncidentFile> IncidentFileStateAccessor, ILoggerFactory loggerFactory)
: base(nameof(MainDialog))
{
IncidentFileAccessor = IncidentFileStateAccessor ?? throw new ArgumentNullException(nameof(IncidentFileStateAccessor));
// Add control flow dialogs
var waterfallSteps = new WaterfallStep[]
{
InitializeStateStepAsync,
PromptForPhotoStepAsync,
DisplayGreetingStateStepAsync,
};
AddDialog(new WaterfallDialog(ProfileDialog, waterfallSteps));
AddDialog(new AttachmentPrompt(PhotoPrompt));
}
那么,这里是捕捉提示的函数:
private async Task<DialogTurnResult> PromptForPhotoStepAsync(WaterfallStepContext stepContext,CancellationToken cancellationToken)
{
var IncidentState = await IncidentFileAccessor.GetAsync(stepContext.Context);
if (string.IsNullOrWhiteSpace(IncidentState.Photo))
{
// prompt for Photo, if missing in User profil
var opts = new PromptOptions
{
Prompt = new Activity
{
Type = ActivityTypes.Message,
Text = "Can you send me a photo please?",
},
};
return await stepContext.PromptAsync(PhotoPrompt, opts);
}
else
{
return await stepContext.NextAsync();
}
}
这就是我保存用户数据的方式:
public class IncidentFile
{
public string Name { get; set; }
public string Photo { get; set; }
}
我不知道我是否正确使用了 AttachmentPrompt 类。我也不知道附件提示是如何将图像发送到机器人的,所以在 IncidentFile 中,我为照片输入了“公共字符串”,但我不知道它应该是字节数组还是路径图像位置或其他。 但无论如何,在我测试它并上传照片后,机器人回复说出了点问题......
感谢您的宝贵时间!
【问题讨论】:
-
你看过微软记录的例子吗?
-
是的,但没有什么能真正帮助我......
-
我同意。今天看了下,没啥意思。 “上传”的文件是项目中的一个资源。不知道是谁构成了上传文件。
标签: c# botframework bots