【问题标题】:Botframework v4: Uploading image to the bot in another classBotframework v4:将图像上传到另一个类中的机器人
【发布时间】:2019-03-15 15:31:07
【问题描述】:

我在主 bot 类中的 OnTurnAsync() 中有此代码,可让用户上传图像并将其保存在本地并且工作正常。 但是,如果我想在 MainDialog 类等不同的类中执行此操作,我该怎么做呢?

更新:我设法通过下面的代码来做到这一点。但它接受一切,我怎么能做一个只接受图像的验证?

更新:在下面回答。

public class MainDialog : ComponentDialog
{
    private const string InitialId = "mainDialog";
    private const string TEXTPROMPT = "textPrompt";
    private const string ATTACHPROMPT = "attachPrompt";

    public MainDialog(string dialogId)
        : base(dialogId)
    {
        WaterfallStep[] waterfallSteps = new WaterfallStep[]
         {
             FirstStepAsync,
             SecondStepAsync,
         };
        AddDialog(new WaterfallDialog(InitialId, waterfallSteps));
        AddDialog(new AttachmentPrompt(ATTACHPROMPT));
    }

    private static async Task<DialogTurnResult> FirstStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
    {
        return await stepContext.PromptAsync(
              ATTACHPROMPT,
              new PromptOptions
              {
                  Prompt = MessageFactory.Text($"upload photo."),
                  RetryPrompt = MessageFactory.Text($"upload photo pls."),
              });
    }

    private static async Task<DialogTurnResult> SecondStepAsync(WaterfallStepContext stepContext, CancellationToken cancellationToken = default(CancellationToken))
    {
        var activity = stepContext.Context.Activity;

        if (activity.Attachments != null && activity.Attachments.Any())
        {
            foreach (var file in stepContext.Context.Activity.Attachments)
            {
                var remoteFileUrl = file.ContentUrl;

                var localFileName = Path.Combine(Path.GetTempPath(), file.Name);

                using (var webClient = new WebClient())
                {
                    webClient.DownloadFile(remoteFileUrl, localFileName);
                }

                await stepContext.Context.SendActivityAsync($"Attachment {stepContext.Context.Activity.Attachments[0].Name} has been received and saved to {localFileName}.");
            }
        }

        return await stepContext.EndDialogAsync();
    }

用这段代码设法做到了。

            var activity = stepContext.Context.Activity;
        if (activity.Attachments != null && activity.Attachments.Any())
        {
            foreach (var file in stepContext.Context.Activity.Attachments)
            {
                if (file.ContentType.EndsWith("/jpg") || file.ContentType.EndsWith("/png") || file.ContentType.EndsWith("/bmp") || file.ContentType.EndsWith("/jpe"))
                {
                    var remoteFileUrl = file.ContentUrl;

                    var localFileName = Path.Combine(Path.GetTempPath(), file.Name);

                    using (var webClient = new WebClient())
                    {
                        webClient.DownloadFile(remoteFileUrl, localFileName);
                    }

                    await stepContext.Context.SendActivityAsync($"Attachment {stepContext.Context.Activity.Attachments[0].Name} has been received and saved to {localFileName}.");
                    return await stepContext.NextAsync();
                }
                else
                {
                    await stepContext.Context.SendActivityAsync($"Attachment {file.Name} is not an image, it is {file.ContentType}.");

                    // restart the dialog from top
                    return await stepContext.ReplaceDialogAsync(InitialId);
                }
            }
        }

【问题讨论】:

  • 你只需要检查附件的contentType

标签: c# botframework messenger


【解决方案1】:

就像 Nicolas R 所说,您可以像这样进行一些基本验证:

foreach (var file in activity.Attachments)
{
    if (file.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase))
    {
        await turnContext.SendActivityAsync($"Attachment {file.Name} is an image of type {file.ContentType}.");
    }
    else
    {
        await turnContext.SendActivityAsync($"Attachment {file.Name} is not an image, it is {file.ContentType}.");
    }
}

这当然允许任何内容类型以“image/”开头的附件。您可能不希望允许任何以“image/”开头的内容类型,在这种情况下,您需要创建一个可接受类型的列表,例如“image/png”和“image/jpeg”。您也可以考虑检查文件扩展名(看看它是“.png”还是“.jpg”等)

【讨论】:

  • 谢谢先生,我设法做到了,但我选择endsWith。我尝试在提示验证器中执行此操作,但它不起作用。这可能与提示验证器有关吗?
  • 先生,您能看看这个信使位置快速回复问题吗?谢谢! stackoverflow.com/questions/55356005/…
  • @user10860402 - 你的意思是你正在使用附件提示?您必须提出一个新问题并发布您的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-03
  • 2023-03-16
  • 2012-02-21
  • 1970-01-01
  • 2017-03-10
  • 2018-12-05
相关资源
最近更新 更多