简体   繁体   English

接受瀑布对话框上的附件并将它们本地存储在机器人框架 v4 中

[英]Accepting attachments on a waterfall dialog and storing them locally in bot framework v4

I was trying to add a functionality to take input-attachments from the user, basically trying to merge the handling-attachments bot sample from bot framework and my custom waterfall dialog.我试图添加一个功能来从用户那里获取输入附件,基本上是试图合并来自机器人框架的处理附件机器人示例和我的自定义瀑布对话框。

But how do you access iturncontext functions in the waterfall dialog?但是如何在瀑布对话框中访问 iturncontext 函数呢? . . Below is a explanation of my code.下面是我的代码的解释。

One of my waterfall step:我的瀑布步骤之一:

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);
    }
}

Here is the HandleIncomingAttachment function which i borrowed from bot builder samples linked above.这是我从上面链接的机器人构建器示例中借来的 HandleIncomingAttachment function。

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);
}

Here is the transcript of the conversation:以下是谈话记录:转录机器人框架

EDIT: i have edited my code to this, it still doesnt wait for me to upload a attachment.just finishes the step.编辑:我已经编辑了我的代码,它仍然不等我上传附件。只需完成这一步。

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);
}

process input function:过程输入 function:

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 inherits from DialogContext and therefore the ITurnContext can be accessed through its Context property. WaterfallStepContext继承自DialogContext ,因此可以通过其Context属性访问ITurnContext The waterfall step code you posted already does this when it uses stepContext.Context.SendActivityAsync or stepContext.Context.Activity .您发布的瀑布步骤代码在使用stepContext.Context.SendActivityAsyncstepContext.Context.Activity时已经执行此操作。

so i figured this out, thanks to this post: github.com/microsoft/botframework-sdk/issues/5312所以我想通了,感谢这篇文章:github.com/microsoft/botframework-sdk/issues/5312

how my code looks now:我的代码现在看起来如何:

declaring a attachment prompt:声明附件提示:

 public class CancelDialog : ComponentDialog
{

    private static string attachmentPromptId = $"{nameof(CancelDialog)}_attachmentPrompt";
    public CancelDialog()
        : base(nameof(CancelDialog))
    {

        // This array defines how the Waterfall will execute.
        var waterfallSteps = new WaterfallStep[]
        {
            TitleStepAsync,
            DescStepAsync,
          //  AskForAttachmentStepAsync,
            UploadAttachmentAsync,
            UploadCodeAsync,
            SummaryStepAsync,

        };

        // Add named dialogs to the DialogSet. These names are saved in the dialog state.
        AddDialog(new WaterfallDialog(nameof(WaterfallDialog), waterfallSteps));
        AddDialog(new TextPrompt(nameof(TextPrompt)));
        AddDialog(new NumberPrompt<int>(nameof(NumberPrompt<int>), AgePromptValidatorAsync));
        AddDialog(new ChoicePrompt(nameof(ChoicePrompt)));
        AddDialog(new ConfirmPrompt(nameof(ConfirmPrompt)));
        AddDialog(new AttachmentPrompt(attachmentPromptId));

ask for attachment prompt in the waterfall:在瀑布中询问附件提示:

private async Task<DialogTurnResult> UploadAttachmentAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {

        stepContext.Values["desc"] = (string)stepContext.Result;
     //   if ((bool)stepContext.Result)
        {
            return await stepContext.PromptAsync(
        attachmentPromptId,
        new PromptOptions
        {
            Prompt = MessageFactory.Text($"Can you upload a file?"),
        });
        }
        //else
        //{
        //    return await stepContext.NextAsync(-1, cancellationToken);
        //}

    }

processing the file and storing it:处理文件并存储它:

private async Task<DialogTurnResult> UploadCodeAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken)
    {
        List<Attachment> attachments = (List<Attachment>)stepContext.Result;
        string replyText = string.Empty;
        foreach (var file in 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";
        }}

hope you get a idea.希望你有一个想法。 thank you @Kyle and @Michael谢谢@Kyle 和@Michael

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

相关问题 在瀑布对话框中使用 luis 验证提示(Bot 框架 V4) - Validate prompt with luis in waterfall dialog (Bot framework V4) 通过跳过bot框架v4中第一个对话的第一步,将第一个对话的瀑布步骤调用到另一个对话中 - Invoke the steps of waterfall of first dialog into other dialog by skipping the first step of a first dialog in bot framework v4 具有复杂对话流的顺序瀑布模型Bot Framework C#v4 - Sequential Waterfall Models with Complex Dialog flows Bot Framework C# v4 如何从机器人框架 v4 中的 ActivityHandler.OnMessageActivityAsync 启动瀑布对话框 - How to start a waterfall dialog from ActivityHandler.OnMessageActivityAsync in bot framework v4 如何在C#[Bot Framework v4]中从QnaBot(qna制造商api)调用瀑布对话框? - How to call waterfall dialog from QnaBot (qna maker api) in C# [Bot Framework v4]? 瀑布对话框中的C#Bot V4文本提示 - C# bot V4 Text Prompt inside a waterfall dialog Bot Framework v4圆形对话框参考 - Bot framework v4 circular dialog reference Botframework v4:如何简化这个瀑布式对话框? - Botframework v4: How to simplify this waterfall dialog? 从bot框架v4中的当前步骤转到下一个瀑布步骤 - Go to next Waterfall step from current step in bot framework v4 如何验证自适应卡片机器人框架 v4(瀑布模型)c# 中的输入字段 - How to validate input fields in adaptive card bot framework v4 (waterfall model) c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM