public static async Task Run( [QueueTrigger("webpage-scan-jobs", Connection = "AzureWebJobsStorage")] AddWebPageUpdatedJobDto job, //[TimerTrigger(runEvery5MinutesBetween8AMAnd6PM, RunOnStartup = runOnStartUp)]TimerInfo myTimer, [Queue("webpage-scan-jobs", Connection = "AzureWebJobsStorage")] CloudQueue outputQueue, ILogger log) { var pageService = await WebPageService.LoadPage(job.WebPageUrl); var eleMd5 = await pageService.GetMd5ValueOfElement(job.PathOfElementToWatch); // If element has been updated if (!eleMd5.Equals(job.ElementMd5LastRun)) { var screenshot = await pageService.TakeScreenshot(); var screenshotStorageUrl = await SaveScreenshotToStorage(screenshot); await SendNotificationEmail(job, screenshotStorageUrl); log.LogInformation("It's updated! - element: \"{0}\" on {1}", job.PathOfElementToWatch, job.WebPageUrl); // If we're only watching for one change, then we're finished if (!job.WatchIndefinitely) { return; } } job.ElementMd5LastRun = eleMd5; // Add message to queue and make it visible after a specific time. var cqm = new CloudQueueMessage(JsonConvert.SerializeObject(job)); await outputQueue.AddMessageAsync(cqm, null, TimeSpan.FromMinutes(double.Parse(Environment.GetEnvironmentVariable("HideNewMessagesForInMinutes"))), null, null); log.LogInformation("No change for element: \"{0}\" on {1}", job.PathOfElementToWatch, job.WebPageUrl); }
private async static Task SendNotificationEmail(AddWebPageUpdatedJobDto job, string screenshot) { var apiKey = Environment.GetEnvironmentVariable("SendGridApiKey"); var client = new SendGridClient(apiKey); var from = new EmailAddress("*****@*****.**", "Webpage Updated"); var subject = $"{job.WebPageUrl} has been updated!"; var to = new EmailAddress(job.Email); //var plainTextContent = "https://docs.abp.io/en/abp/latest has been updated."; var template = await File.ReadAllTextAsync(Environment.CurrentDirectory + "\\email-template.html"); var renderedTemplate = template .Replace("~~Text~~", $"{job.WebPageUrl}, has been updated!") .Replace("~~ImageSrc~~", screenshot) .Replace("~~WebPage~~", job.WebPageUrl); var msg = MailHelper.CreateSingleEmail(from, to, subject, null, renderedTemplate); await client.SendEmailAsync(msg); }