Beispiel #1
0
        public async Task <IActionResult> Subscribe(SubscribeViewModel model)
        {
            if (string.IsNullOrEmpty(model.Email) || !(await IsValidAsync(model.Email)))
            {
                return(Json(new { result = false, Error = "Email field is empty" }));
            }

            var dbSet = _context.Set <Subscriber>();
            var entry = await dbSet.FirstOrDefaultAsync(s => s.Email == model.Email);

            if (entry == null)
            {
                entry = (await dbSet.AddAsync(new Subscriber
                {
                    Name = model.Email,
                    Email = model.Email,
                    EmailSend = true,
                    DateCreated = DateTime.Now,
                    Culture = CultureInfo.CurrentUICulture.ToString()
                })).Entity;
            }
            else
            {
                if (entry.Unsubscribe)
                {
                    entry.Unsubscribe = false;
                }
                else
                {
                    return(Json(new { result = false, Error = "User is already subscribed to the newsletter" }));
                }
            }

            await _context.SaveChangesAsync();

            _slack.Post(new SlackMessage
            {
                Attachments = new List <SlackAttachment> {
                    new SlackAttachment
                    {
                        Color     = "#120a8f",
                        Title     = $"Подписка пользователя {entry.Name}",
                        TitleLink = $"mailto:{entry.Email}",
                        Fields    = new List <SlackField>
                        {
                            new SlackField {
                                Title = $"Подписка: {(entry.Unsubscribe ? "Нет":"Да")}",
                                Value = $"Email: {entry.Email}",
                                Short = false
                            }
                        },
                        Pretext = $"Дата регистрации: {entry.DateCreated.ToString("dd:MM:yyyy HH:mm")}"
                    }
                }
            });
            //var sendResult = await _emailSender.SendEmailSubscription(model.Email, model.Email);

            return(Json(new { result = true /*sendResult*/, Error = "" }));
        }
Beispiel #2
0
        public Task VacancyReviewCreated(VacancyReview vacancyReview)
        {
            var messageBody = $"Vacancy VAC{vacancyReview.VacancyReference} is ready for review";

            if (vacancyReview.SubmissionCount > 1)
            {
                messageBody += $" ({GetNumericSuffix(vacancyReview.SubmissionCount)} submission)";
            }

            var message = new SlackMessage {
                Text = messageBody
            };

            return(_slackClient.Post(message, Emojis.New));
        }
Beispiel #3
0
        public bool ReportException(Exception ex, WebHookOptions options)
        {
            if (options == null)
            {
                throw new NullReferenceException(
                          "An instance of WebHookOptions must be provided as it contains the details for connecting to the Slack web hook.");
            }
            if (string.IsNullOrEmpty(options.WebhookUrl))
            {
                throw new ArgumentException(
                          "WebHookOptions.WebhookUrl must contain a value. Please provide the URL to your Slack team webhook.");
            }

            var message = new WebHooks.SlackMessage();

            if (!string.IsNullOrEmpty(options.ChannelName))
            {
                message.Channel = options.ChannelName;
            }
            if (!string.IsNullOrEmpty(options.UserName))
            {
                message.Username = options.UserName;
            }

            message.IconEmoji = options.IconEmoji ?? WebHooks.Emoji.HeavyExclamationMark;

            message.Text = options.Text ?? DEFAULT_TEXT;

            var attachment = new WebHooks.SlackAttachment();

            // simple message for unformatted and notification views
            attachment.Fallback = string.Format("Web app exception: {0}", ex.Message);
            attachment.Color    = options.AttachmentColor ?? DEFAULT_COLOR;

            if (!string.IsNullOrEmpty(options.AttachmentTitle))
            {
                attachment.Title = options.AttachmentTitle;
            }
            if (!string.IsNullOrEmpty(options.AttachmentTitleLink))
            {
                attachment.TitleLink = options.AttachmentTitleLink;
            }
            attachment.MrkdwnIn = new List <string> {
                "text"
            };
            var textFormat = !string.IsNullOrWhiteSpace(options.ExceptionTextFormat) ? options.ExceptionTextFormat : DEFAULT_FORMAT;

            var requestUrl = HttpContext.Current != null?HttpContext.Current.Request.Url.ToString() : "[no HttpContext available]";

            attachment.Text = textFormat
                              .Replace("%%url%%", requestUrl)
                              .Replace("%%hostname%%", Environment.MachineName)
                              .Replace("%%ex:type%%", ex.GetType().ToString())
                              .Replace("%%ex:message%%", ex.Message)
                              .Replace("%%ex:site%%", ex.TargetSite != null ? ex.TargetSite.ToString() : "")
                              .Replace("%%ex:stackTrace%%", ex.StackTrace);

            message.Attachments = new List <WebHooks.SlackAttachment>();
            message.Attachments.Add(attachment);

            return(_client.Post(message));
        }