当前位置 : 主页 > 编程语言 > c语言 >

c# – Bot框架:检测用户上传文件的时间

来源:互联网 收集:自由互联 发布时间:2021-06-25
我正在尝试在我的机器人中创建一个表单,用户必须上传文件和其他文本输入.为此,我做了一个ReceiveAttachmentDialog,它将验证用户是否已经上传了一个文件,问题是我无法确定如何检测用户何
我正在尝试在我的机器人中创建一个表单,用户必须上传文件和其他文本输入.为此,我做了一个ReceiveAttachmentDialog,它将验证用户是否已经上传了一个文件,问题是我无法确定如何检测用户何时上传文件并因此在ReceiveAttachmentDialo中午餐.

forDialog是这样的:

[Serializable]
    public class FraisDialog : IDialog<object>
    {

        public async Task StartAsync(IDialogContext context)
        {
            // Root dialog initiates and waits for the next message from the user. 
            // When a message arrives, call MessageReceivedAsync.

            var replyMessage = context.MakeMessage();


            replyMessage.Attachments = new List<Connector.Attachment> { CreateAdaptiveCardwithEntry() };
            await context.PostAsync("Veuillez compléter le formulaire ci-dessous");
            await context.PostAsync(replyMessage, CancellationToken.None);
            context.Wait(this.MessageReceivedAsync);
        }


        public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
        {

            context.Wait(MessageReceivedAsync);
            var message = await result;

            if (message.Value != null)
            {
                // Got an Action Submit
                dynamic value = message.Value;
                string submitType = value.Type.ToString();
                switch (submitType)
                {
                    case "SaveFunction":
                        if (value.titre == "")
                        {
                            await context.PostAsync("Veuillez compléter tous les paramètres du formulaire \n");
                        }
                        else
                        {
                            await context.PostAsync($"Vous avez saisie les paramètres suivants : \n titre :  {value.titre} \n date : {value.date} \n montant :  {value.montant}");

                           context.Done<string>(null);
                       }
                       return;
                }
            }
       }

        public Connector.Attachment CreateAdaptiveCardwithEntry()
        {
            var submitActionData = JObject.Parse("{ \"Type\": \"SaveFunction\" }");
            var card = new AdaptiveCard()
            {

                Body = new List<CardElement>()
                {  
                    // Hotels Search form  

                    new TextBlock() { Text = "Titre de la note des frais" },
                    new TextInput()
                    {
                        Id = "titre",
                        Speak = "<s>Veuillez saisir le titre</s>",
                        Placeholder = "Veuillez saisir le titre",
                        Style = TextInputStyle.Text
                    },
                    new TextBlock() { Text = "Date de la note des frais" },
                    new DateInput()
                    {
                        Id = "date",
                        Placeholder ="Veuillez saisir la Date de la note des frais"
                    },

                    new TextBlock() { Text = "Montant en euros de la note de frais" },
                    new NumberInput()
                    {
                        Id = "montant",
                        Speak = "<s>Veuillez saisir le Montant en euros de la note de frais</s>",
                        Placeholder = "Veuillez saisir le Montant de la note de frais",

                    },

                },

                Actions = new List<ActionBase>()
                {
                    new SubmitAction()
                    {
                       Title = "Envoyer",
                       Speak = "<s>Envoyer</s>",
                       DataJson = submitActionData.ToString()

                    }
                }
            };

            Connector.Attachment attachment = new Connector.Attachment()
            {
                ContentType = AdaptiveCard.ContentType,
                Content = card
            };
            return attachment;
        }
    }

我的问题是我如何检测用户何时上传文件?我应该在哪里放置await context.Forward(new ReceiveAttachmentDialog(),this.ResumeAfterRecieveDialog,context.Activity,CancellationToken.None);

正如@Ali Heikal所说,formflow对你来说是一个完全有效的选择.不使用表单流的另一个选项是检查从用户收到的机器人的活动是否有任何附件. Activity.Attachments是IList< Attachment>它将包含用户上传到机器人的任何文件.因此,只需检查Activity.Attachments.Any()就会给你一个布尔值,告诉你用户是否上传了一个文件.

这是RootDialog.cs中的一个简单示例:

using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Connector;
using System;
using System.Linq;
using System.Net;
using System.Threading.Tasks;

namespace Bot_Application1.Dialogs
{
    [Serializable]
    public class RootDialog : IDialog<object>
    {
        public Task StartAsync(IDialogContext context)
        {
            context.Wait(MessageReceivedAsync);

            return Task.CompletedTask;
        }

        private async Task MessageReceivedAsync(IDialogContext context, IAwaitable<object> result)
        {
            var activity = await result as Activity;

            if (activity.Attachments.Any())
            {
                //do something with the file
                //it looks like this is where you would put your 
                //context.Forward() 
                await context.Forward(new ReceiveAttachmentDialog(), this.ResumeAfterRecieveDialog, context.Activity, CancellationToken.None);

                await context.PostAsync($"you sent a file");
            }
            else
            {
                await context.PostAsync($"No File received");
            }
            context.Wait(MessageReceivedAsync);
        }
    }
}
网友评论