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

假如想象 提交于 2020-05-13 14:28:06

问题


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

回答1:


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


            }
        }
    }


来源:https://stackoverflow.com/questions/58487605/how-to-set-a-dialog-as-the-welcome-message-in-bot-framework-v4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!