C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c# – Bot框架:检测用户上传文件的时间大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试在我的机器人中创建一个表单,用户必须上传文件和其他文本输入.为此,我做了一个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(),CancellationToken.NonE);

                await context.PostAsync($"you sent a file");
            }
            else
            {
                await context.PostAsync($"No File received");
            }
            context.Wait(messageReceivedAsync);
        }
    }
}

大佬总结

以上是大佬教程为你收集整理的c# – Bot框架:检测用户上传文件的时间全部内容,希望文章能够帮你解决c# – Bot框架:检测用户上传文件的时间所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。