如何在bot框架v4中将对话框设置为欢迎消息

时间:2019-10-21 13:42:37

标签: c# botframework

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

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

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

此机器人代码(源自richcardsbot示例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);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

检查 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;
     }

更新: 请尝试以下操作:

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


            }
        }
    }