public IEnumerable <string> GetWebhookUrls(WebhookEvents eventType) { switch (eventType) { case WebhookEvents.Created: return(_options.CreatedUrl); case WebhookEvents.StatusChanged: return(_options.StatusChangedUrl); default: throw new InvalidOperationException("Unkown webhook event"); } }
private async Task FireWebHooksAsync(Initiative initiative, WebhookEvents eventType, Action <IDictionary <string, string> > data = null, CancellationToken cancellationToken = default(CancellationToken)) { using (LogContext.PushProperty("InitiativeId", initiative.Id)) { var hooks = _webhookUrlService.GetWebhookUrls(eventType).ToList(); _logger.Information("Webhooks will post to {Count} webhooks for event {EventType} for initiative {InitiativeId}", hooks.Count, eventType, initiative.Id); if (!hooks.Any()) { return; } var owner = await GetInitiativeOwner(initiative); var createDateAlberta = TimeZoneInfo.ConvertTimeFromUtc(initiative.CreatedDate.DateTime, AlbertaTimeZone); var values = new Dictionary <string, string> { { "ID", initiative.Id.ToString() }, { "Title", initiative.Title }, { "CreatedDate", createDateAlberta.ToString() }, { "Description", initiative.Description }, { "OwnerName", owner?.Name }, { "OwnerEmail", owner?.Email } }; data?.Invoke(values); var content = new StringContent(JsonConvert.SerializeObject(values, Formatting.Indented)); using (var client = new HttpClient()) { foreach (var eventUrl in hooks) { try { var response = await client.PostAsync(eventUrl, content, cancellationToken); var responseString = await response.Content.ReadAsStringAsync(); _logger.Information("Webhook ({EventType}) at url {Url} returned {StatusCode} with message {Message}", eventType, eventUrl, response.StatusCode, responseString); } catch (Exception err) { _logger.Error(err, "Error posting {EventType} to webhook with Url {Url}: {ErrorMessage}", eventType, eventUrl, err.Message); } } } } }
public async Task SendMessageAsync(WebhookEvents webhookEvent, IUserWithEmail?user, string?title = null, string?url = null, string?description = null, Color?color = null) { EmbedBuilder CreateEmbedBuilder(IUserWithEmail?user) { var builder = new EmbedBuilder { Color = color, }; if (user is not null) { var profileUrl = _entryLinkFactory.GetFullEntryUrl(EntryType.User, user.Id); builder .WithAuthor(name: user.Name, iconUrl: _userIconFactory.GetIcons(user, ImageSizes.Thumb).UrlThumb, url: profileUrl) .WithUrl(profileUrl); } return(builder); } var embeds = new[] { CreateEmbedBuilder(user).WithTitle(title).WithUrl(url).WithDescription(description).Build() }; await _repository.HandleTransactionAsync(async ctx => { var webhooks = await ctx.Query <Webhook>().WhereHasWebhookEvent(webhookEvent).ToListAsync(); var tasks = webhooks.Select(async w => { try { using var client = new DiscordWebhookClient(w.Url); await client.SendMessageAsync(embeds: embeds, username: _brandableStrings.SiteName, avatarUrl: _discordWebhookSettings.AvatarUrl); } catch (Exception x) { s_log.Error(x, "Unable to send message"); } }).ToList(); await Task.WhenAll(tasks); }); }
public static IQueryable <Webhook> WhereHasWebhookEvent(this IQueryable <Webhook> query, WebhookEvents webhookEvent) => query.Where(w => (w.WebhookEvents & webhookEvent) != 0);
public void Value(int expected, WebhookEvents actual) { ((int)actual).Should().Be(expected); }
public Task SendMessageAsync(WebhookEvents webhookEvent, IUserWithEmail?user, string?title = null, string?url = null, string?description = null, Color?color = null) { return(Task.CompletedTask); }