如何接收用户发送的图像? [英] How to receive an image send by users?

查看:51
本文介绍了如何接收用户发送的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编程一个聊天机器人,我希望用户上传图像,以便该机器人可以接收它并将图像保存到用户配置文件数据中.但是我在C#中还很陌生,我有点迷路了...

I am programming a chatbot and I want the user to upload an image so that the bot can receive it and save the image into the User profil data. But I am quite new in c# and I am a bit lost...

我正在使用MS Bot Framework.要构建对话框,我使用Waterfall步骤,并使用提示来捕获用户答复.为了接收附件,我在MS文档上看到它存在一个AttachmentPrompt类.但是对于如何使用它以及如何将文件保存在用户配置文件中,我有些困惑.

I am using the MS Bot Framework. To build the dialog, I use Waterfall Step and to catch the user reply, I use Prompt. To receive attachment I saw on MS documents that it exists an AttachmentPrompt class. But I am a bit confused on how to use it and how I can save the file in the user profil.

这就是我构建瀑布对话框的方式:

This is how I build the waterfall dialog :

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

    }

然后,这是捕获提示的功能:

Then, here is the function that catch the Prompt:

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

    }

这就是我保存用户数据的方式:

And this is how I save the user data :

public class IncidentFile
{
    public string Name { get; set; }
    public string Photo { get; set; }

}

我不知道我是否正确使用了AttachmentPrompt类.我也不知道附件提示是如何将图像发送到机器人的,所以在IncidentFile中,我为Photo放置了"public string",但是我不知道它是否应该是字节数组或路径图像位置的位置,否则. 但是无论如何,在我对其进行测试并上传了照片之后,该机器人回答说出了点问题...

I don't know if I use correctly the AttachmentPrompt class. I also don't know how the Attachment prompt is sending the image to the bot, so in the IncidentFile, I put "public string" for the Photo, but I don't know if it should be a byte array, or the path of the image location or else. But anyway, after I test it and i upload a photo, the bot reply that something went wrong...

谢谢您的时间!

推荐答案

您是如此亲密!用户上传照片后,您可以使用stepContext.Result在下一步的Waterfall步骤中访问它:

You're so close! Once the user uploads the photo, you can access it in the next Waterfall step with stepContext.Result:

如您所见,类型为Microsoft.Bot.Schema.Attachment,因此将IncidentFile更改为:

As you can see, the type is Microsoft.Bot.Schema.Attachment, so change your IncidentFile to:

using Microsoft.Bot.Schema;

namespace <your-namespace>
{
    public class IncidentFile
    {
        public string Name { get; set; }
        public Attachment Photo { get; set; }
    }
}

您可以在上传步骤之后的步骤中使用以下方式保存该信息:

You save that information in the step following the upload step with something like:

// Load the IncidentFile
var incidentFile = await IncidentFileAccessor.GetAsync(stepContext.Context);
// Save the photo
incidentFile.Photo = ((List<Attachment>)stepContext.Result)[0];
await IncidentFileAccessor.SetAsync(stepContext.Context, incidentFile);

结果:

  • C#示例.我会链接到特定的链接,但是我们将删除其中一些并重新组织此存储库.但是,请看:
    • Basic Bot(即将成为Core Bot)-非常适合弄清楚如何使用用户个人资料
    • 处理附件-常规附件处理
    • C# Samples. I'd link to specific ones, but we're about to delete some and reorganize this repo. However, look at:
      • Basic Bot (soon to be Core Bot) - Good for figuring out how to use user profiles
      • Handling Attachments - General Attachment handling

      这篇关于如何接收用户发送的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆