【问题标题】:How to receive an image send by users?如何接收用户发送的图片?
【发布时间】:2019-09-04 13:21:32
【问题描述】:

我正在编写一个聊天机器人,我希望用户上传一张图片,以便机器人可以接收它并将图片保存到用户配置文件数据中。但是我在 c# 中很新,我有点迷茫......

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

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

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 类。我也不知道附件提示是如何将图像发送到机器人的,所以在 IncidentFile 中,我为照片输入了“公共字符串”,但我不知道它应该是字节数组还是路径图像位置或其他。 但无论如何,在我测试它并上传照片后,机器人回复说出了点问题......

感谢您的宝贵时间!

【问题讨论】:

  • 你看过微软记录的例子吗?
  • 是的,但没有什么能真正帮助我......
  • 我同意。今天看了下,没啥意思。 “上传”的文件是项目中的一个资源。不知道是谁构成了上传文件。

标签: c# botframework bots


【解决方案1】:

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

如您所见,类型为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);

结果:

参考文献

  • C# Samples。我会链接到特定的,但我们将删除一些并重新组织这个 repo。但是,请看:
    • Basic Bot(即将成为 Core Bot)- 有助于弄清楚如何使用用户配置文件
    • 处理附件 - 一般附件处理
  • Attachment Class
  • AttachmentPrompt Class

【讨论】:

    猜你喜欢
    • 2016-11-29
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 2019-06-01
    • 2014-03-20
    相关资源
    最近更新 更多