예제 #1
0
        /// <summary>
        /// Called when user clicks on the add-in menu
        /// </summary>
        /// <param name="e">The context of the VS tools and metadata</param>
        public override void OnClick(AddinEventArgs e)
        {
            try
            {
                DadJoke           joke  = DadJoke.construct(Request.dadJoke());
                MotivationalQuote quote = MotivationalQuote.construct(Request.motivationalQuote());

                string message = string.Empty;

                if (joke.Id != null)
                {
                    //message += string.Format("Dad joke:\n{0}", joke.Joke);
                    message += joke.Joke;
                }

                //if (quote.Id != 0)
                //{
                //    message += "\n\n";
                //    message += string.Format("Motivational quote:\n{0}{1}", HttpUtility.HtmlDecode(quote.Content), quote.Title.ToUpper());
                //}

                CoreUtility.DisplayInfo(message);
            }
            catch (Exception ex)
            {
                CoreUtility.HandleExceptionWithErrorMessage(ex);
            }
        }
        private async void DiscoverMotivationals(object state)
        {
            _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);

            var users = await _userService.GetAllUsers();

            foreach (var user in users)
            {
                MotivationalQuote randomQuote = _motivationalQuotesService.GetRandomQuote().Result;

                var now = DateTime.Now;

                bool mustSend = false;

                if (user.Frequency.Frequency == MessageFrequency.Day)
                {
                    if (now.Subtract(user.Frequency.LastMessaged) > TimeSpan.FromDays(-1))
                    {
                        mustSend = true;
                    }
                }
                else if (user.Frequency.Frequency == MessageFrequency.Hour)
                {
                    if (now.Subtract(user.Frequency.LastMessaged) > TimeSpan.FromHours(-1))
                    {
                        mustSend = true;
                    }
                }
                else if (user.Frequency.Frequency == MessageFrequency.Minute)
                {
                    if (now.Subtract(user.Frequency.LastMessaged) > TimeSpan.FromMinutes(-1))
                    {
                        mustSend = true;
                    }
                }

                else if (user.Frequency.Frequency == MessageFrequency.Never)
                {
                    mustSend = false;
                }

                if (mustSend)
                {
                    _messageBroadcastService.SendMessage(user.Name, user.Number, $"{randomQuote.Quote}. To stop receiving these messages reply STOP.");
                }

                var userFrequency = new UserFrequency
                {
                    LastMessaged = now,
                    Frequency    = user.Frequency.Frequency
                };

                user.Frequency = userFrequency;

                await _userService.UpdateUser(user.Id, user);
            }
        }