我试图添加一种功能来接收用户的输入附件,基本上是试图合并bot框架中的handling-attachments bot示例和我的自定义瀑布对话框。
但是您如何在瀑布对话框中访问iturncontext函数? 。下面是我的代码的说明。
我的瀑布步骤之一:
private async Task<DialogTurnResult> DescStepAsync2(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
stepContext.Values["Title"] = (string)stepContext.Result;
await stepContext.Context.SendActivityAsync(MessageFactory.Text("upload a image"), cancellationToken);
var activity = stepContext.Context.Activity;
if (activity.Attachments != null && activity.Attachments.Any())
{
Activity reply = (Activity)HandleIncomingAttachment(stepContext.Context.Activity);
return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = reply }, cancellationToken);
}
else
{
var reply = MessageFactory.Text("else image condition thrown");
// reply.Attachments.Add(Cards.GetHeroCard().ToAttachment());
return await stepContext.PromptAsync(nameof(TextPrompt), new PromptOptions { Prompt = reply }, cancellationToken);
}
}
[这里是HandleIncomingAttachment函数,我从上面链接的机器人构建器样本中借用了。
private static IMessageActivity HandleIncomingAttachment(IMessageActivity activity)
{
string replyText = string.Empty;
foreach (var file in activity.Attachments)
{
// Determine where the file is hosted.
var remoteFileUrl = file.ContentUrl;
// Save the attachment to the system temp directory.
var localFileName = Path.Combine(Path.GetTempPath(), file.Name);
// Download the actual attachment
using (var webClient = new WebClient())
{
webClient.DownloadFile(remoteFileUrl, localFileName);
}
replyText += $"Attachment \"{file.Name}\"" +
$" has been received and saved to \"{localFileName}\"\r\n";
}
return MessageFactory.Text(replyText);
}
以下是对话的笔录:
编辑:我已经为此编辑了代码,它仍然不等我上传附件。只需完成此步骤即可。
private async Task<DialogTurnResult> DescStepAsync2(WaterfallStepContext stepContext, CancellationToken cancellationToken)
{
stepContext.Values["Desc"] = (string)stepContext.Result;
var reply = (Activity)ProcessInput(stepContext.Context);
return await stepContext.PromptAsync(nameof(AttachmentPrompt), new PromptOptions { Prompt = reply }, cancellationToken);
}
过程输入功能:
private static IMessageActivity ProcessInput(ITurnContext turnContext)
{
var activity = turnContext.Activity;
IMessageActivity reply = null;
if (activity.Attachments != null && activity.Attachments.Any())
{
// We know the user is sending an attachment as there is at least one item .
// in the Attachments list.
reply = HandleIncomingAttachment(activity);
}
else
{
reply = MessageFactory.Text("No attachement detected ");
// Send at attachment to the user.
}
return reply;
}
A WaterfallStepContext
继承自WaterfallStepContext
,因此可以通过其DialogContext
属性访问ITurnContext
。您发布的瀑布步骤代码在使用Context
或Context
时已经执行了此操作。