예제 #1
0
        public async Task OnReceiveActivityAsync(IBotContext context)
        {
            bool handled     = false;
            var  activeTopic = context.State.ConversationProperties["ActiveTopic"] as ITopic;

            if (activeTopic == null)
            {
                // use default topic
                activeTopic = new DefaultTopic();
                context.State.ConversationProperties["ActiveTopic"] = activeTopic;
                handled = await activeTopic.StartTopic(context);
            }
            else
            {
                // continue to use the active topic
                handled = await activeTopic.ContinueTopic(context);
            }

            // the bot only needs to transition from defaultTopic -> other topics and back, so
            // if activeTopic's result is false and the activeToic is NOT the default topic,
            // we switch back to default topic
            if (handled == false && !(context.State.ConversationProperties["ActiveTopic"] is DefaultTopic))
            {
                // resume default topic
                activeTopic = new DefaultTopic();
                context.State.ConversationProperties["ActiveTopic"] = activeTopic;
                handled = await activeTopic.ResumeTopic(context);
            }
        }
        private async Task BotReceiveHandler(IBotContext context)
        {
            // --- Bot logic 
            bool handled = false;
            // Get the current ActiveTopic from my conversation state
            var activeTopic = context.State.Conversation[ConversationProperties.ACTIVETOPIC] as ITopic;

            // if there isn't one 
            if (activeTopic == null)
            {
                // use default topic
                activeTopic = new DefaultTopic();
                context.State.Conversation[ConversationProperties.ACTIVETOPIC] = activeTopic;
                handled = await activeTopic.StartTopic(context);
            }
            else
            {
                // continue to use the active topic
                handled = await activeTopic.ContinueTopic(context);
            }

            // AlarmBot only needs to transition from defaultTopic -> subTopic and back, so 
            // if activeTopic's result is false and the activeToic is NOT the default topic, we switch back to default topic
            if (handled == false && !(context.State.Conversation[ConversationProperties.ACTIVETOPIC] is DefaultTopic))
            {
                // resume default topic
                activeTopic = new DefaultTopic();
                context.State.Conversation[ConversationProperties.ACTIVETOPIC] = activeTopic;
                handled = await activeTopic.ResumeTopic(context);
            }
        }
        public MessagesController(IConfiguration configuration)
        {
            if (activityAdapter == null)
            {
                string appId       = configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppIdKey)?.Value;
                string appPassword = configuration.GetSection(MicrosoftAppCredentials.MicrosoftAppPasswordKey)?.Value;

                // Create the activity adapter to send/receive Activity objects
                activityAdapter = new BotFrameworkAdapter(appId, appPassword);
                bot             = new Bot(activityAdapter)
                                  .Use(new MemoryStorage())
                                  .Use(new BotStateManager())
                                  .Use(new DefaultTopicView())
                                  .Use(new RegExpRecognizerMiddleware()
                                       .AddIntent("takeTurn", new Regex("take turn(.*)", RegexOptions.IgnoreCase))
                                       .AddIntent("standardAction", new Regex("attack(.*)|standard(.*)", RegexOptions.IgnoreCase))
                                       .AddIntent("moveAction", new Regex("move(.*)", RegexOptions.IgnoreCase))
                                       .AddIntent("swiftAction", new Regex("swift(.*)", RegexOptions.IgnoreCase))
                                       .AddIntent("doubleMoveAction", new Regex("double move(.*)", RegexOptions.IgnoreCase))
                                       .AddIntent("endTurn", new Regex("end turn(.*)", RegexOptions.IgnoreCase))
                                       .AddIntent("confirmYes", new Regex("(yes|yep)", RegexOptions.IgnoreCase))
                                       .AddIntent("confirmNo", new Regex("(no|nope)", RegexOptions.IgnoreCase)))
                                  .OnReceive(async(context) =>
                {
                    // Bot logic
                    bool handled = false;

                    // Get the current active topic from my conversation state
                    var activeTopic = context.State.Conversation[ConversationProperties.ACTIVETOPIC] as ITopic;

                    // If there isn't an active topic, create a default topic.
                    if (activeTopic == null)
                    {
                        activeTopic = new DefaultTopic();
                        context.State.Conversation[ConversationProperties.ACTIVETOPIC] = activeTopic;
                        handled = await activeTopic.StartTopic(context);
                    }
                    else
                    {
                        // If there is an active topic, continue to use the active topic.
                        handled = await activeTopic.ContinueTopic(context);
                    }

                    if (handled == false && !(context.State.Conversation[ConversationProperties.ACTIVETOPIC] is DefaultTopic))
                    {
                        // Resume default topic if no other topic was started.
                        activeTopic = new DefaultTopic();
                        context.State.Conversation[ConversationProperties.ACTIVETOPIC] = activeTopic;
                        handled = await activeTopic.ResumeTopic(context);
                    }
                });
            }
        }