如何接收用户发送的图像?

问题描述 投票:0回答:1

我正在编写一个聊天机器人,我希望用户上传一个图像,以便机器人可以接收它并将图像保存到用户profil数据中。但是我在c#中很新,而且我有点失落......

我正在使用MS Bot框架。要构建对话框,我使用瀑布步骤并捕获用户回复,我使用提示。为了接收附件,我在MS文档上看到它存在一个AttachmentPrompt类。但我对如何使用它以及如何在用户profil中保存文件感到有点困惑。

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

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类。我也不知道Attachment提示如何将图像发送到bot,所以在IncidentFile中,我为Photo设置了“public string”,但我不知道它应该是字节数组,还是路径图像位置或其他。但无论如何,在我测试它并上传照片之后,机器人回答说出了问题......

感谢您的时间!

c# botframework bots
1个回答
3
投票

你真是太近了!用户上传照片后,您可以使用stepContext.Result在下一个瀑布步骤中访问它:

enter image description here

如你所见,类型是Microsoft.Bot.Schema.Attachment,所以将你的IncidentFile改为:

using Microsoft.Bot.Schema;

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

您可以在上传步骤之后的步骤中保存该信息,例如:

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

结果:

enter image description here

References

  • C# Samples。我将链接到特定的,但我们即将删除一些并重新组织此回购。但是,看看: Basic Bot(很快将成为Core Bot) - 有助于弄清楚如何使用用户配置文件 处理附件 - 一般附件处理
  • Attachment Class
  • AttachmentPrompt Class
© www.soinside.com 2019 - 2024. All rights reserved.