简体   繁体   中英

how to set a dialog as the welcome message in bot framework v4

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.

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:

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

Check the ConversationUpdate activity:

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


            }
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM