示例#1
0
        protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            // Save context variable
            if (context == null)
            {
                context = turnContext;
            }

            // Register user
            var id = turnContext.Activity.Conversation.Id;

            if (!db.IsUserAdded(id))
            {
                db.TryToAddUser(new TUser()
                {
                    Id = id
                });
                db.AddUserForVisit(id, turnContext.Activity.From.Name);
            }

            // Count visit
            db.AddVisit(id);

            var commandHandler = new CommandsHandler(cacheDictionary, commands);

            commandHandler.Execute(id, turnContext.Activity.Text);
            //// Execute previous command (if exists)
            //if (cacheDictionary.ContainsKey(id) && cacheDictionary.GetValue(id).ContainsKey("next"))
            //{
            //    foreach (var command in commands)
            //    {
            //        if (command.Contains(cacheDictionary.GetValue(id)["next"]))
            //        {
            //            var message = command.Execute(id, turnContext.Activity.Text);
            //            if (message != null)
            //                await turnContext.SendActivityAsync(MessageFactory.Text(message), cancellationToken);
            //            return;
            //        }
            //    }
            //}

            //// Execute command by user message (if previous command is null)
            //foreach (var command in commands)
            //{
            //    if (command.Contains(turnContext.Activity.Text))
            //    {
            //        var message = command.Execute(id, turnContext.Activity.Text);
            //        if (message != null)
            //            await turnContext.SendActivityAsync(MessageFactory.Text(message), cancellationToken);
            //        return;
            //    }
            //}

            // Write all commands (if previous command is null and user message is not command)
            foreach (var command in commands)
            {
                await turnContext.SendActivityAsync(MessageFactory.Text($"{command.Name} ({command.GetDescription()}) \n"), cancellationToken);
            }
        }