如何在bot框架v4中将对话框设置为欢迎消息 [英] how to set a dialog as the welcome message in bot framework v4

查看:56
本文介绍了如何在bot框架v4中将对话框设置为欢迎消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

欢迎消息很酷,但是您不一定需要它们.在我的情况下,我有多个对话框,其中有一个MainDialog假定可以触发其他对话框.

Welcome messages are cool , but you don't necessarily need them . In my case i have multiple dialog's , with a MainDialog which is suppose to trigger other dialog's .

我遇到的问题是(使用bot模拟器)用户必须在触发主对话框之前键入一些内容.

The issue i am having is ( using bot emulator) that the user has to type something before the main dialog is triggered .

不能在添加成员时触发对话框吗? 感谢您阅读:).这是我的整个课堂:

cant we trigger a dialog when members are added ? Thanks for reading through :).Here is my entire class :

此机器人代码,源自richcardsbot示例

this bot code , derived from the richcardsbot sample here :

  using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using HackBot.Dialogs;
using Microsoft.Bot.Builder;
using Microsoft.Bot.Schema;
using Microsoft.BotBuilderSamples.Bots;
using Microsoft.Extensions.Logging;

namespace HackBot.Bots
{
    public class RichCardsBot : DialogBot<MainDialog>
    {
        public RichCardsBot(ConversationState conversationState, UserState userState, MainDialog dialog, ILogger<DialogBot<MainDilaog>> logger)
            : base(conversationState, userState, dialog, logger)
        {

        }

        protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
        {
            foreach (var member in membersAdded)
            {
                var attachments = new List<Attachment>();
                // Greet anyone that was not the target (recipient) of this message.
                // To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details.
                if (member.Id != turnContext.Activity.Recipient.Id)
                {
                    var reply = MessageFactory.Attachment(attachments);
                    //var reply= MessageFactory.Text("The following flight has been cancelled ."
                    //    + " You have a Hotel booking for the Destination."
                    //    + "what would you like to do with the booking ?.");
                    reply.Attachments.Add(Cards.FirstCard().ToAttachment());

                    await turnContext.SendActivityAsync(reply, cancellationToken);
                }
            }
        }
    }
}

推荐答案

检查 ConversationUpdate 活动:

 // innderDc is the **DialogContext**
 var activity = innerDc.Context.Activity;

// Check activity type
switch (activity.Type) {
 case ActivityTypes.ConversationUpdate:
     {
         if (activity.MembersAdded ? .Count > 0) {
             foreach(var member in activity.MembersAdded) {

               // do logic
                await innerDc.BeginDialogAsync(nameof("yourDialog"));

             }
         }
         break;
     }

更新: 请尝试以下操作:

UPDATE: Try the following:

 protected override async Task OnMembersAddedAsync(IList<ChannelAccount> membersAdded, ITurnContext<IConversationUpdateActivity> turnContext, CancellationToken cancellationToken)
    {
        foreach (var member in membersAdded)
        {
            // Greet anyone that was not the target (recipient) of this message.
            // To learn more about Adaptive Cards, see https://aka.ms/msbot-adaptivecards for more details.
            if (member.Id != turnContext.Activity.Recipient.Id)
            {
                //var reply = MessageFactory.Text("Welcome to CardBot."
                //    + " This bot will show you different types of Rich Cards."
                //    + " Please type anything to get started.");

                //await turnContext.SendActivityAsync(reply, cancellationToken);
                await Dialog.RunAsync(turnContext, ConversationState.CreateProperty<DialogState>(nameof(DialogState)), cancellationToken);


            }
        }
    }

这篇关于如何在bot框架v4中将对话框设置为欢迎消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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