Exemplo n.º 1
0
        /// <summary>
        /// Continue the topic, method which is routed to while this topic is active
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Task <bool> ContinueTopic(IBotContext context)
        {
            var activeTopic = (ITopic)context.State.ConversationProperties[ConversationProperties.ACTIVETOPIC];

            switch (context.Request.Type)
            {
            case ActivityTypes.Message:
                switch ((string)context.TopIntent?.Name)
                {
                case "addAlarm":
                    // switch to addAlarm topic
                    activeTopic = new AddAlarmTopic();
                    context.State.ConversationProperties[ConversationProperties.ACTIVETOPIC] = activeTopic;
                    return(activeTopic.StartTopic(context));

                case "showAlarms":
                    // switch to show alarms topic
                    activeTopic = new ShowAlarmsTopic();
                    context.State.ConversationProperties[ConversationProperties.ACTIVETOPIC] = activeTopic;
                    return(activeTopic.StartTopic(context));

                case "deleteAlarm":
                    // switch to delete alarm topic
                    activeTopic = new DeleteAlarmTopic();
                    context.State.ConversationProperties[ConversationProperties.ACTIVETOPIC] = activeTopic;
                    return(activeTopic.StartTopic(context));

                case "help":
                    // show help
                    DefaultTopicResponses.ReplyWithHelp(context);
                    return(Task.FromResult(true));

                default:
                    // show our confusion
                    DefaultTopicResponses.ReplyWithConfused(context);
                    return(Task.FromResult(true));
                }

            default:
                break;
            }
            return(Task.FromResult(true));
        }
Exemplo n.º 2
0
        public RootTopic(IBotContext context) : base(context)
        {
            // User state initialization should be done once in the welcome
            //  new user feature. Placing it here until that feature is added.
            if (context.State.UserProperties[USER_STATE_ALARMS] == null)
            {
                context.State.UserProperties[USER_STATE_ALARMS] = new List <Alarm>();
            }

            this.SubTopics.Add(ADD_ALARM_TOPIC, () =>
            {
                var addAlarmTopic = new AddAlarmTopic();

                addAlarmTopic.Set
                .OnSuccess((ctx, alarm) =>
                {
                    this.ClearActiveTopic();

                    ((List <Alarm>)ctx.State.UserProperties[USER_STATE_ALARMS]).Add(alarm);

                    context.Reply($"Added alarm named '{ alarm.Title }' set for '{ alarm.Time }'.");
                })
                .OnFailure((ctx, reason) =>
                {
                    this.ClearActiveTopic();

                    context.Reply("Let's try something else.");

                    this.ShowDefaultMessage(ctx);
                });

                return(addAlarmTopic);
            });

            this.SubTopics.Add(DELETE_ALARM_TOPIC, () =>
            {
                var deleteAlarmTopic = new DeleteAlarmTopic(context.State.UserProperties[USER_STATE_ALARMS]);

                deleteAlarmTopic.Set
                .OnSuccess((ctx, value) =>
                {
                    this.ClearActiveTopic();

                    if (!value.DeleteConfirmed)
                    {
                        context.Reply($"Ok, I won't delete alarm '{ value.Alarm.Title }'.");
                        return;
                    }

                    ((List <Alarm>)ctx.State.UserProperties[USER_STATE_ALARMS]).RemoveAt(value.AlarmIndex);

                    context.Reply($"Done. I've deleted alarm '{ value.Alarm.Title }'.");
                })
                .OnFailure((ctx, reason) =>
                {
                    this.ClearActiveTopic();

                    context.Reply("Let's try something else.");

                    this.ShowDefaultMessage(context);
                });

                return(deleteAlarmTopic);
            });
        }
Exemplo n.º 3
0
        public RootTopic(ITurnContext turnContext) : base(turnContext)
        {
            // User state initialization should be done once in the welcome
            //  new user feature. Placing it here until that feature is added.
            if (turnContext.GetUserState <BotUserState>().Alarms == null)
            {
                turnContext.GetUserState <BotUserState>().Alarms = new List <Alarm>();
            }

            this.SubTopics.Add(ADD_ALARM_TOPIC, (object[] args) =>
            {
                var addAlarmTopic = new AddAlarmTopic();

                addAlarmTopic.Set
                .OnSuccess((turn, alarm) =>
                {
                    ClearActiveTopic();

                    turn.GetUserState <BotUserState>().Alarms.Add(alarm);

                    turn.SendActivity($"Added alarm named '{ alarm.Title }' set for '{ alarm.Time }'.");
                })
                .OnFailure((turn, reason) =>
                {
                    ClearActiveTopic();

                    turn.SendActivity("Let's try something else.");

                    ShowDefaultMessage(turnContext);
                });

                return(addAlarmTopic);
            });

            this.SubTopics.Add(DELETE_ALARM_TOPIC, (object[] args) =>
            {
                var alarms = (args.Length > 0) ? (List <Alarm>)args[0] : null;

                var deleteAlarmTopic = new DeleteAlarmTopic(alarms);

                deleteAlarmTopic.Set
                .OnSuccess((turn, value) =>
                {
                    this.ClearActiveTopic();

                    if (!value.DeleteConfirmed)
                    {
                        turn.SendActivity($"Ok, I won't delete alarm '{ value.Alarm.Title }'.");
                        return;
                    }

                    turn.GetUserState <BotUserState>().Alarms.RemoveAt(value.AlarmIndex);

                    turn.SendActivity($"Done. I've deleted alarm '{ value.Alarm.Title }'.");
                })
                .OnFailure((turn, reason) =>
                {
                    ClearActiveTopic();

                    turn.SendActivity("Let's try something else.");

                    ShowDefaultMessage(turn);
                });

                return(deleteAlarmTopic);
            });
        }