private void SendMessageTo(User user)
 {
     var subject = "EXERCISE TIME";
     var exercise = user.GetNewExercise();
     var body = exercise.ToCommand();
     _mailGunClient.SendMessage(user, subject, body);
     _textBeltClient.SendMessage(user, body);
     _slackClient.SendMessage(user, body);
     user.WriteToDatabase(_store);
 }
 public void SendMessage(User user, string body)
 {
     if (string.IsNullOrEmpty(user.PhoneNumber)) return;
     var values = new Dictionary<string, string>()
     {
         {"number", user.PhoneNumber},
         {"message", body},
     };
     _client.PostAsync("/text", new FormUrlEncodedContent(values));
     Console.WriteLine($"Sent text to {user.Name} at {user.PhoneNumber}");
 }
 public void SendMessage(User user, string subject, string body)
 {
     if (string.IsNullOrEmpty(user.EmailAddress)) return;
     var values = new Dictionary<string, string>()
     {
         {"domain", _mailGunEndpoint},
         {"from", $"EXTREME FITNESS COACH <postmaster@{_mailGunEndpoint}>"},
         {"to", $"{user.Name} <{user.EmailAddress}>"},
         {"subject", subject},
         {"text", body}
     };
     _client.PostAsync($"/v3/{_mailGunEndpoint}", new FormUrlEncodedContent(values));
     Console.WriteLine($"Sent email to {user.Name} at {user.EmailAddress}");
 }
 private async void SendContinuousMessagesTo(User user)
 {
     try
     {
         while (true)
         {
             await Task.Delay(user.GetNewDuration());
             var date = DateTime.Now;
             var hour = date.Hour;
             var day = (int) date.DayOfWeek;
             if (user.IsActive && 9 <= hour && hour < 17 && 1 <= day && day <= 5)
             {
                 SendMessageTo(user);
             }
         }
     }
     catch (Exception e)
     {
         Console.WriteLine("Error while sending message.\n" + e.StackTrace);
     }
 }
 public void SendMessage(User user, string body)
 {
     if (string.IsNullOrEmpty(user.SlackName)) return;
     SendMessage($"@{user.SlackName} {body}");
     Console.WriteLine($"Sent Slack message to {user.Name} at @{user.SlackName}");
 }
 public void SendMessage(User user, string body)
 {
     SendMessage(user, "Exercise Assignment", body);
 }
        private string ProcessSlackCommand(string triggerWord, User user, string formText)
        {
            var responseBody = "";
            var text = formText.Split('|');
            switch (triggerWord)
            {
                case "progress":
                    responseBody = user.GetLatestProgress();
                    break;
                case "ping":
                    responseBody = "The Coach is in the house!";
                    break;
                case "help":
                    responseBody =
                        @"*Help*: Gets help. All commands are case-insensitive.
*Ping*: Pings to see whether _The Coach_ is operational.
*Progress*: Prints out your cumulative progress.
*My settings*: Prints out your settings (max reps, list of exercises, etc).
*Take a break*: Makes the coach ignore you.
*My body is ready*: Makes the coach stop ignoring you.
*Update|MinReps|123*: Changes MinReps to 123. Can also use MaxReps, MinDuration, or MaxDuration.
*Add exercise|Cows eaten|EAT {0} COWS*: Adds a new exercise to _your_ list of possible exercises. The `{0}` gets replaced by a random between your MinReps/MaxReps.
*Add exercise|Cows eaten|EAT {0} COWS|5|23*: Same, but {0} gets replaced by a random between 5 and 23. This lets you override your MinReps/MaxReps on a per-exercise basis.
*Remove exercise|Cows eaten*: Removes the exercise with Name ""Cows eaten"". If you don't know the Name, run My settings to see your list.";
                    break;
                case "take a break":
                    user.IsActive = false;
                    responseBody = $"Exercise assignments are now off for {user.Name}.";
                    break;
                case "my settings":
                    responseBody = $"Settings for {user.Name}:\n```{user}```";
                    break;
                case "update":
                    int num;
                    if (text.Length == 3 && int.TryParse(text[2], out num))
                    {
                        responseBody = $"{user.Name}'s {text[1]} now set to {text[2]}.";
                        var variableName = text[1].ToLower();
                        switch (variableName)
                        {
                            case "minreps":
                                if (num >= 0 && num <= user.MaxReps)
                                {
                                    user.MinReps = num;
                                }
                                break;
                            case "maxreps":
                                if (num >= 0 && num >= user.MinReps)
                                {
                                    user.MaxReps = num;
                                }
                                break;
                            case "minduration":
                                if (num >= 0 && num <= user.MaxDuration)
                                {
                                    user.MinDuration = num;
                                }
                                break;
                            case "maxduration":
                                if (num >= 10000 && num >= user.MinDuration)
                                {
                                    user.MaxDuration = num;
                                }
                                break;
                            default:
                                responseBody = "Syntax error.";
                                break;
                        }
                    }
                    break;
                case "my body is ready":
                    user.IsActive = true;
                    responseBody = $"Exercise assignments are now on for {user.Name}!";
                    break;
                case "add exercise":
                    if (text.Length >= 3)
                    {
                        var exercise = new Exercise(text[1], text[2], 0);
                        if (text.Length == 5)
                        {
                            int min;
                            int max;
                            var b1 = int.TryParse(text[3], out min);
                            var b2 = int.TryParse(text[4], out max);
                            if (b1 && b2)
                            {
                                exercise.MinReps = min;
                                exercise.MaxReps = max;
                            }
                        }
                        user.ExerciseList.Add(exercise);
                        responseBody = $"Added the following exercise for {user.Name}:\n```{exercise}```";
                    }
                    break;
                case "remove exercise":
                    if (text.Length == 2)
                    {
                        var index = user.ExerciseList.FindIndex(e => e.Name == text[1]);
                        if (index > -1)
                        {
                            var exercise = user.ExerciseList[index];
                            user.ExerciseList.RemoveAt(index);
                            responseBody = $"Removed the following exercise from {user.Name}:\n```{exercise}```";
                        }
                    }
                    break;
            }
            user.WriteToDatabase(_store);
            return responseBody;
        }