private async Task ProcessNotification(ApplicationDbContext context, NotificationQueueEntry item) { var webhooks = context.Webhooks.Where(o => o.Status == StatusType.Active); try { log.Trace($"Attempting find for {item}"); foreach (var webhook in webhooks) { var t = new Thread(() => { HandleWebhook(webhook, item); }) { IsBackground = true }; t.Start(); } //if no webhooks setup, the queue simply gets flushed await Queue.DequeueAsync(new CancellationToken()); } catch (Exception e) { log.Trace($"Error in item {e} - {item}"); } }
public async Task <IActionResult> Test([FromRoute] Guid id) { var webhook = await _context.Webhooks.SingleOrDefaultAsync(m => m.Id == id); var timeline = new HistoryTimeline(); var payload = new NotificationQueueEntry(); payload.Type = NotificationQueueEntry.NotificationType.Timeline; payload.Payload = (JObject)JToken.FromObject(timeline); QueueSyncService.HandleWebhook(webhook, payload); return(NoContent()); }
internal static async void HandleWebhook(Webhook webhook, NotificationQueueEntry payload) { var historyTimeline = JsonConvert.DeserializeObject <HistoryTimeline>(payload.Payload.ToString()); // Serialize our concrete class into a JSON String var formattedResponse = webhook.PostbackFormat; var isValid = false; var reg = new Regex(@"\[(.*?)\]"); foreach (Match match in reg.Matches(formattedResponse)) { switch (match.Value.ToLower()) { case "[machinename]": formattedResponse = formattedResponse.Replace(match.Value, historyTimeline.MachineId.ToString()); break; case "[datetime.utcnow]": //json formatted date! formattedResponse = formattedResponse.Replace(match.Value, historyTimeline.CreatedUtc.ToString("s")); break; case "[messagetype]": formattedResponse = formattedResponse.Replace(match.Value, "Binary"); break; case "[messagepayload]": if (payload.Payload["Result"] != null && !string.IsNullOrEmpty(payload.Payload["Result"].ToString())) { var p = payload.Payload["Result"].ToString().Trim().Trim('"').Trim().Trim('"'); p = $"\"{HttpUtility.JavaScriptStringEncode(p)}\""; formattedResponse = formattedResponse.Replace(match.Value, p); isValid = true; } break; } } if (!isValid) { log.Trace($"Webhook has no payload, exiting"); return; } try { // Wrap our JSON inside a StringContent which then can be used by the HttpClient class var httpContent = new StringContent(formattedResponse, Encoding.UTF8, "application/json"); using (var httpClient = new HttpClient()) { // Do the actual request and await the response var httpResponse = await httpClient.PostAsync(webhook.PostbackUrl, httpContent); log.Trace($"Webhook response {webhook.PostbackUrl} {webhook.PostbackMethod} {httpResponse.StatusCode}"); // If the response contains content we want to read it! if (httpResponse.Content != null) { var responseContent = await httpResponse.Content.ReadAsStringAsync(); log.Trace($"Webhook notification sent with {responseContent}"); // From here on you could deserialize the ResponseContent back again to a concrete C# type using Json.Net } } } catch (Exception e) { log.Trace($"Webhook failed response {webhook.PostbackUrl} {webhook.PostbackMethod} - {e}"); } }