public static void Run( [QueueTrigger("jsonbundlequeue")] BundleQueue queuedBundle, [Queue("jsonmessagequeue")] ICollector <JsonMessage> messageQueue, [Table("webhookRegistration")] IQueryable <WebhookRegistrationEntity> existingWebhooks, TraceWriter log) { log.Info($"JSON Message generator trigger function processed: {queuedBundle.Bundle.Name}"); var webhooks = GetAllWebhooksForBundleType(existingWebhooks, queuedBundle.Bundle.Type, queuedBundle.IsUpdate); log.Info($"Found {webhooks.Count} webhooks for type {queuedBundle.Bundle.Type}"); foreach (var webhook in webhooks) { if (!string.IsNullOrEmpty(webhook.Partner)) { queuedBundle.Bundle.URL += "?partner=" + webhook.Partner; } messageQueue.Add(new JsonMessage() { WebhookUrl = webhook.GetDecryptedWebhook(), Payload = queuedBundle }); } }
public static void Run( [QueueTrigger("bundlequeue")] BundleQueue queuedBundle, [Queue("discordmessagequeue")] ICollector <DiscordMessage> messageQueue, [Table("webhookRegistration")] CloudTable existingWebhooks, TraceWriter log) { log.Info($"Message generator trigger function processed: {queuedBundle.Bundle.Name}"); var bundle = queuedBundle.Bundle; var webhooks = existingWebhooks.GetAllWebhooksForBundleType(queuedBundle.Bundle.Type, queuedBundle.IsUpdate); int queued = 0; foreach (var webhook in webhooks) { var message = new DiscordWebhookPayload(queuedBundle, bundle); if (!string.IsNullOrEmpty(webhook.Partner)) { message.AddPartnerLink(webhook.Partner); } messageQueue.Add(new DiscordMessage { WebhookUrl = webhook.GetDecryptedWebhook(), Payload = message }); queued++; } log.Info($"Queued {queued} payloads for type {queuedBundle.Bundle.Type}"); }
public static void Run( [QueueTrigger("bundlequeue")] BundleQueue queuedBundle, [Queue("discordmessagequeue")] ICollector <DiscordMessage> messageQueue, [Table("webhookRegistration")] CloudTable existingWebhooks, TraceWriter log) { log.Info($"Message generator trigger function processed: {queuedBundle.Bundle.Name}"); var bundle = queuedBundle.Bundle; var webhooks = existingWebhooks.GetAllWebhooksForBundleType(queuedBundle.Bundle.Type, queuedBundle.IsUpdate).ToList(); if (queuedBundle.Bundle.Type == BundleTypes.BOOKS) { var isComicOrRpg = false; // If the same webhook is registered as RPG / COMIC, remove it unless this is the right type if (bundle.Name.ToLower().Contains("rpg")) { var rpgWebhooks = existingWebhooks.GetAllWebhooksForBundleType(BundleTypes.RPG, queuedBundle.IsUpdate); webhooks.AddRange(rpgWebhooks); isComicOrRpg = true; } if (!bundle.Name.ToLower().Contains("comic")) { var comicWebhooks = existingWebhooks.GetAllWebhooksForBundleType(BundleTypes.COMIC, queuedBundle.IsUpdate); webhooks.AddRange(comicWebhooks); isComicOrRpg = true; } if (!isComicOrRpg) { var bookOtherWebhooks = existingWebhooks.GetAllWebhooksForBundleType(BundleTypes.BOOK_OTHER, queuedBundle.IsUpdate); webhooks.AddRange(bookOtherWebhooks); } } int queued = 0; foreach (var webhook in webhooks) { var message = new DiscordWebhookPayload(queuedBundle, bundle); if (!string.IsNullOrEmpty(webhook.Partner)) { message.AddPartnerLink(webhook.Partner); } messageQueue.Add(new DiscordMessage { WebhookUrl = webhook.GetDecryptedWebhook(), Payload = message }); queued++; } log.Info($"Queued {queued} payloads for type {queuedBundle.Bundle.Type}"); }
public static void Run( [QueueTrigger("bundlequeue")] BundleQueue queuedBundle, [Queue("discordmessagequeue")] ICollector <DiscordMessage> messageQueue, [Table("webhookRegistration")] IQueryable <WebhookRegistrationEntity> existingWebhooks, TraceWriter log) { log.Info($"Message generator trigger function processed: {queuedBundle.Bundle.Name}"); var bundle = queuedBundle.Bundle; var webhooks = GetAllWebhooksForBundleType(existingWebhooks, queuedBundle.Bundle.Type, queuedBundle.IsUpdate); log.Info($"Found {webhooks.Count} webhooks for type {queuedBundle.Bundle.Type}"); var content = "New Bundle: " + bundle.Name; if (queuedBundle.IsUpdate) { content = "Bundle Updated: " + bundle.Name; } var message = new DiscordWebhookPayload { content = content, embeds = new List <DiscordEmbed>() }; message.embeds.Add(new DiscordEmbed() { url = bundle.URL + "?partner=havenbreaker&charity=151160", title = bundle.Description, image = new ImageField() { url = bundle.ImageUrl }, author = new AuthorField() { name = "Humble Bundle", url = bundle.URL + "?partner=havenbreaker&charity=151160" } }); foreach (var section in bundle.Sections) { var embed = new DiscordEmbed { title = section.Title, url = bundle.URL + "?partner=havenbreaker&charity=151160", description = "" }; foreach (var item in section.Items.Take(25)) { embed.description += item.Name + "\n"; } message.embeds.Add(embed); if (section.Items.Count > 25) { var embedContinued = new DiscordEmbed { title = section.Title, url = bundle.URL + "?partner=havenbreaker&charity=151160", description = "" }; foreach (var item in section.Items.Skip(25).Take(25)) { embedContinued.description += item.Name + "\n"; } message.embeds.Add(embedContinued); } } log.Info("Created message " + JsonConvert.SerializeObject(message)); foreach (var webhook in webhooks) { messageQueue.Add(new DiscordMessage { WebhookUrl = webhook, Payload = message }); } }
public static void Run( [QueueTrigger("bundlequeue")] BundleQueue queuedBundle, [Queue("discordmessagequeue")] ICollector <DiscordMessage> messageQueue, [Table("webhookRegistration")] CloudTable existingWebhooks, TraceWriter log) { log.Info($"Message generator trigger function processed: {queuedBundle.Bundle.Name}"); var bundle = queuedBundle.Bundle; var webhooks = existingWebhooks.GetAllWebhooksForBundleType(queuedBundle.Bundle.Type, queuedBundle.IsUpdate); int queued = 0; foreach (var webhook in webhooks) { var content = "New Bundle: " + bundle.Name; if (queuedBundle.IsUpdate) { content = "Bundle Updated: " + bundle.Name; } var message = new DiscordWebhookPayload { content = content, embeds = new List <DiscordEmbed>() }; message.embeds.Add(new DiscordEmbed() { url = bundle.URL, title = bundle.Description, image = new ImageField() { url = bundle.ImageUrl }, author = new AuthorField() { name = "Humble Bundle", url = bundle.URL } }); foreach (var section in bundle.Sections) { var embed = new DiscordEmbed { title = section.Title, url = bundle.URL, description = "" }; var itemsAdded = 0; foreach (var item in section.Items) { embed.description += GetItemName(item, queuedBundle.UpdatedItems); itemsAdded++; // Create a new embed every 25 items if (itemsAdded % 25 == 0) { message.embeds.Add(embed); embed = new DiscordEmbed { title = section.Title + " (Continued)", url = bundle.URL, description = "" }; } } // Add last embed message.embeds.Add(embed); } if (!string.IsNullOrEmpty(webhook.Partner)) { AddPartnerLink(message, webhook.Partner); } messageQueue.Add(new DiscordMessage { WebhookUrl = webhook.GetDecryptedWebhook(), Payload = message }); queued++; } log.Info($"Queued {queued} payloads for type {queuedBundle.Bundle.Type}"); }
public static async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequestMessage req, [Table("humbleBundles")] IQueryable <HumbleBundleEntity> currentTableBundles, [Table("webhookRegistration")] IQueryable <WebhookRegistrationEntity> existingWebhooks, [Queue("jsonmessagequeue")] ICollector <JsonMessage> jsonMessageQueue, [Queue("discordmessagequeue")] ICollector <DiscordMessage> discordMessageQueue, TraceWriter log) { req.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); dynamic data = await req.Content.ReadAsAsync <object>(); string webhook = data?.webhook; int bundleTypeValue = data?.type; WebhookType webhookType = data?.webhookType; string bundleName = data?.bundleName; var bundleType = (BundleTypes)bundleTypeValue; if (webhook == null) { log.Error("No webhook provided"); return(req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a webhook in the request body")); } var webhookToTest = existingWebhooks.ToList().FirstOrDefault(x => x.WebhookType == (int)webhookType && x.GetDecryptedWebhook() == webhook); if (webhookToTest == null) { return(req.CreateResponse(HttpStatusCode.NotFound, $"Could not find registered webhook {webhook} for type {webhookType}")); } var currentBundles = currentTableBundles.ToList().Select(x => x.GetBundle()).ToList(); var bundleToSend = currentBundles.First(x => x.Type == bundleType || bundleType == BundleTypes.ALL); if (!string.IsNullOrEmpty(bundleName)) { bundleToSend = currentBundles.FirstOrDefault(x => (x.Type == bundleType || bundleType == BundleTypes.ALL) && x.Name.Equals(bundleName, System.StringComparison.CurrentCultureIgnoreCase)); if (bundleToSend == null) { return(req.CreateResponse(HttpStatusCode.NotFound, "Could not find bundle " + bundleName)); } } var queuedBundle = new BundleQueue() { Bundle = bundleToSend, IsUpdate = false }; if (webhookType == WebhookType.Discord) { discordMessageQueue.Add(new DiscordMessage { WebhookUrl = webhook, Payload = new DiscordWebhookPayload(queuedBundle, bundleToSend) }); } else if (webhookType == WebhookType.RawJson) { jsonMessageQueue.Add(new JsonMessage() { WebhookUrl = webhook, Payload = queuedBundle }); } return(req.CreateResponse(HttpStatusCode.OK)); }
public static void Run( [QueueTrigger("bundlequeue")] BundleQueue queuedBundle, [Queue("discordmessagequeue")] ICollector <DiscordMessage> messageQueue, [Table("webhookRegistration")] IQueryable <WebhookRegistrationEntity> existingWebhooks, TraceWriter log) { log.Info($"Message generator trigger function processed: {queuedBundle.Bundle.Name}"); var bundle = queuedBundle.Bundle; var webhooks = GetAllWebhooksForBundleType(existingWebhooks, queuedBundle.Bundle.Type, queuedBundle.IsUpdate); log.Info($"Found {webhooks.Count} webhooks for type {queuedBundle.Bundle.Type}"); var content = "New Bundle: " + bundle.Name; if (queuedBundle.IsUpdate) { content = "Bundle Updated: " + bundle.Name; } var message = new DiscordWebhookPayload { content = content, embeds = new List <DiscordEmbed>() }; message.embeds.Add(new DiscordEmbed() { url = bundle.URL, title = bundle.Description, image = new ImageField() { url = bundle.ImageUrl }, author = new AuthorField() { name = "Humble Bundle", url = bundle.URL } }); foreach (var section in bundle.Sections) { var embed = new DiscordEmbed { title = section.Title, url = bundle.URL, description = "" }; var itemsAdded = 0; foreach (var item in section.Items) { embed.description += GetItemName(item, queuedBundle.UpdatedItems); itemsAdded++; // Create a new embed every 25 items if (itemsAdded % 25 == 0) { message.embeds.Add(embed); embed = new DiscordEmbed { title = section.Title + " (Continued)", url = bundle.URL, description = "" }; } } // Add last embed message.embeds.Add(embed); } log.Info("Created message " + JsonConvert.SerializeObject(message)); foreach (var webhook in webhooks) { messageQueue.Add(new DiscordMessage { WebhookUrl = webhook, Payload = message }); } }
public DiscordWebhookPayload(BundleQueue queuedBundle, HumbleBundle bundle) { content = "New Bundle: " + bundle.Name; if (queuedBundle.IsUpdate) { content = "Bundle Updated: " + bundle.Name; } embeds.Add(new DiscordEmbed() { url = bundle.URL, title = bundle.Description, image = new ImageField() { url = bundle.ImageUrl }, author = new AuthorField() { name = "Humble Bundle", url = bundle.URL }, fields = new List <EmbedField>() { new EmbedField { name = "Powered By", value = "https://github.com/cswendrowski/HumbleBundleBot" } } }); var random = new Random(); foreach (var section in bundle.Sections) { var embed = new DiscordEmbed { title = section.Title, url = bundle.URL + "?dedupe=" + random.Next(), description = "" }; var itemsAdded = 0; foreach (var item in section.Items) { embed.description += GetItemName(item, queuedBundle.UpdatedItems); itemsAdded++; // Create a new embed every 25 items if (itemsAdded % 25 == 0) { embeds.Add(embed); embed = new DiscordEmbed { title = section.Title + " (Continued)", url = bundle.URL + "?dedupe=" + random.Next(), description = "" }; } } // Add last embed embeds.Add(embed); } }