Пример #1
0
        public async Task <ActionResult> CreateTask(CreateTaskModel data)
        {
            TodoTask todoTask = new TodoTask {
                ListID      = data.ListID,
                TaskName    = data.TaskName,
                Description = data.Description,
                Colour      = "black",
            };

            await _todoTaskRepository.SaveAsync(todoTask);

            return(RedirectToAction("Index", "Home"));
        }
Пример #2
0
        /// <summary>
        /// Accepts a user SMS message, verifies its intent,
        /// performs necessary repository related actions
        /// and parses output message to SMS sender class
        /// </summary>
        /// <param name="number">User's complete phone number</param>
        /// <param name="message">User's SMS message</param>
        /// <returns></returns>
        public override async Task ProcessSMSAsync(string number, string message)
        {
            try {
                // Generates SMS entity for storage
                await _smsRepository.SaveAsync(new Entity.SMS()
                {
                    Message    = message,
                    NumberFrom = number,
                    NumberTo   = ConfigurationManager.AppSettings["Twilio:OutgoingNumber"]
                });

                string          simplifiedNumber = SimplifyNumber(number);
                ApplicationUser user             = await _userManager.FindByNameAsync(simplifiedNumber);

                // Generates new user if message is sent from unknown number
                if (user == null)
                {
                    string         password = Guid.NewGuid().ToString().Substring(0, 6).ToLowerInvariant();
                    IdentityResult ir       = await _userManager.CreateAsync(new ApplicationUser()
                    {
                        UserName             = simplifiedNumber,
                        PhoneNumber          = number,
                        PhoneNumberConfirmed = true,
                        EmailConfirmed       = true,
                        Email         = $"{simplifiedNumber}@foreverly.cloud",
                        SecurityStamp = $"{Guid.NewGuid():N}"
                    }, password);

                    await _smsSender.SendSMSAsync(number,
                                                  $"Your account on foreverly.cloud has been created!\r\nUsername: {simplifiedNumber}\r\nPassword: {password}");

                    user = await _userManager.FindByNameAsync(simplifiedNumber);
                }

                string reminder = null;
                if (message.IsReminderIntent(out reminder))
                {
                    // User created new reminder, generates new list (if required)
                    // and adds reminder to it as a task
                    TodoList tl = user.TodoLists?.FirstOrDefault(x => x.ListName.Equals("Reminders"));

                    if (tl == null)
                    {
                        tl = new TodoList {
                            ListName      = "Reminders",
                            DateCreated   = DateTime.Now,
                            UserID        = user.Id,
                            LeftPositoned = true
                        };
                        await _todoListRepository.SaveAsync(tl);
                    }

                    TodoTask tt = new TodoTask {
                        TaskName    = $"Reminder {(tl.TodoTasks?.Count ?? 0) + 1}",
                        Description = reminder,
                        Colour      = "black",
                        ListID      = tl.ListID
                    };

                    await _todoTaskRepository.SaveAsync(tt);

                    await _smsSender.SendSMSAsync(number, $"We've created your reminder under {tt.TaskName}");
                }
                else if (message.IsLookupIntent())
                {
                    // User wishes to recieve last 5 reminders,
                    // obtains reminders from DB and outputs them to user
                    TodoList tl             = user.TodoLists?.FirstOrDefault(x => x.ListName.Equals("Reminders"));
                    int      remindersCount = tl?.TodoTasks?.Count ?? 0;

                    if (remindersCount == 0)
                    {
                        await _smsSender.SendSMSAsync(number, "You have no reminders");
                    }
                    else
                    {
                        int remindersToSend = Math.Min(remindersCount, 5);

                        if (remindersToSend == 1)
                        {
                            await _smsSender.SendSMSAsync(number, $"Your last reminder was:");
                        }
                        else
                        {
                            await _smsSender.SendSMSAsync(number, $"Your last {remindersToSend} reminders were:");
                        }

                        IEnumerable <TodoTask> localReminders =
                            tl.TodoTasks.OrderByDescending(x => x.TaskID).Take(remindersToSend);

                        foreach (TodoTask tt in localReminders)
                        {
                            await _smsSender.SendSMSAsync(number, $"{tt.TaskName}: {tt.Description}");
                        }
                    }
                }
                else if (message.IsHelpIntent())
                {
                    // User requires usage instructions
                    await _smsSender.SendSMSAsync(number, $"Say \"remind me to ...\" to set a reminder and \"reminders\" to get your last 5 reminders");
                }
                else if (message.IsResetPasswordIntent())
                {
                    // User requires password reset
                    string password = Guid.NewGuid().ToString().Substring(0, 6).ToLowerInvariant();

                    await((ApplicationUserManager)_userManager).ChangePasswordAsync(user.Id, password);

                    await _smsSender.SendSMSAsync(number, $"Your new account details\r\nUsername: {user.UserName}\r\nPassword: {password}");
                }
                else
                {
                    // Recieved invalid message
                    await _smsSender.SendSMSAsync(number, $"I'm sorry, I don't understand that, type \"how ...\" for valid commands");
                }
            } catch {
                // Error occured
                await _smsSender.SendSMSAsync(number, $"Something went wrong, we were unable to complete your action");
            }
        }