示例#1
0
        public Task NotifyAsync(TeamFoundationRequestContext requestContext, INotification notification, BotElement bot, EventRuleElement matchingRule)
        {
            if (!notification.TargetUserNames.Any())
            {
                return(Task.FromResult(0));
            }

            var    config             = TfsNotificationRelaySection.Instance;
            string host               = bot.GetSetting("host", "127.0.0.1");
            int    port               = bot.GetIntSetting("port", 25);
            string fromAddress        = bot.GetSetting("fromAddress");
            string fromName           = bot.GetSetting("fromName");
            string subjectTextId      = bot.GetSetting("subjectTextId", "plaintext");
            bool   isHtml             = bot.GetSetting("isHtml") == "true";
            var    subjectTextElement = config.Texts.FirstOrDefault(t => t.Id == subjectTextId) ?? bot.Text;
            string subject            = notification.ToMessage(bot, subjectTextElement, s => s).First();

            var client = new SmtpClient(host, port);

            var message = new MailMessage();

            message.From            = new MailAddress(fromAddress, fromName, Encoding.UTF8);
            message.SubjectEncoding = Encoding.UTF8;
            message.Subject         = subject;
            message.IsBodyHtml      = isHtml;
            message.BodyEncoding    = Encoding.UTF8;
            message.Body            = string.Join(isHtml ? "<br/>": "\n", notification.ToMessage(bot, s => s));

            var identityService = requestContext.GetService <ITeamFoundationIdentityService>();

            foreach (var username in notification.TargetUserNames)
            {
                var identity = identityService.ReadIdentity(requestContext, IdentitySearchFactor.AccountName, username);
                var email    = identityService.GetPreferredEmailAddress(requestContext, identity.TeamFoundationId);
                if (string.IsNullOrEmpty(email))
                {
                    string errmsg = $"TfsNotificationRelay.Smtp.SmtpNotifier: User {username} doesn't have an email address.";
                    TeamFoundationApplicationCore.Log(requestContext, errmsg, 0, EventLogEntryType.Warning);
                }
                else
                {
                    message.To.Add(email);
                }
            }

            if (message.To.Any())
            {
                requestContext.Trace(0, TraceLevel.Info, Constants.TraceArea, "SmtpNotifier",
                                     string.Format("Sending {0} email notification to: {1}.", notification.GetType(), string.Join(", ", message.To.Select(m => m.Address))));

                return(client.SendMailAsync(message));
            }
            else
            {
                requestContext.Trace(0, TraceLevel.Warning, Constants.TraceArea, "SmtpNotifier",
                                     string.Format("No recipients to send {0} email notification to.", notification.GetType()));

                return(Task.FromResult(0));
            }
        }
        public async Task NotifyAsync(TeamFoundationRequestContext requestContext, INotification notification, BotElement bot, EventRuleElement matchingRule)
        {
            string room = bot.GetSetting("room");
            string baseUrl = bot.GetSetting("apiBaseUrl");
            string authToken = bot.GetSetting("roomNotificationToken");

            var httpClient = new HttpClient();
            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);

            string json = ToJson((dynamic)notification, bot);
            var content = new StringContent(json, Encoding.UTF8, "application/json");
            string url = baseUrl + "/room/" + room + "/notification";
            requestContext.Trace(0, TraceLevel.Verbose, Constants.TraceArea, "HipChatNotifier", "Sending notification to {0}\n{1}", url, json);

            await httpClient.PostAsync(url, content).ContinueWith(t => t.Result.EnsureSuccessStatusCode());
        }
示例#3
0
        public async Task NotifyAsync(TeamFoundationRequestContext requestContext, INotification notification, BotElement bot, EventRuleElement matchingRule)
        {
            string room      = bot.GetSetting("room");
            string baseUrl   = bot.GetSetting("apiBaseUrl");
            string authToken = bot.GetSetting("roomNotificationToken");

            var httpClient = new HttpClient();

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);

            string json    = ToJson((dynamic)notification, bot);
            var    content = new StringContent(json, Encoding.UTF8, "application/json");
            string url     = baseUrl + "/room/" + room + "/notification";

            requestContext.Trace(0, TraceLevel.Verbose, Constants.TraceArea, "HipChatNotifier", "Sending notification to {0}\n{1}", url, json);

            await httpClient.PostAsync(url, content).ContinueWith(t => t.Result.EnsureSuccessStatusCode());
        }
 /// <summary>
 /// Shortcut for tracing - could be refactored further
 /// </summary>
 /// <param name="requestContext"></param>
 /// <param name="message"></param>
 /// <param name="args"></param>
 private void Trace(TeamFoundationRequestContext requestContext, string message, params object[] args)
 {
     requestContext.Trace(0, TraceLevel.Verbose, Constants.TraceArea, this.GetType().Name, message, args);
 }