private async void WebhookServer_GetReceived(WebhookEventArgs e) { try { string field = "crc_token"; var connection = e.Request.HttpContext.Connection; if (e.Request.Query.ContainsKey(field)) { var crcToken = e.Request.Query[field]; Logger.LogWarning(EventId, Resources.SubscriptionSuccess.Format(connection.RemoteIpAddress, connection.RemotePort)); e.Request.ContentType = ContentTypes.ApplicationJson; e.Response.StatusCode = (int)HttpStatusCode.OK; await e.Response.WriteAsync(CRC(ConsumerSecret, crcToken)); e.IsValid = true; } else { Logger.LogWarning(EventId, Resources.SubscriptionFail.Format(connection.RemoteIpAddress, connection.RemotePort)); e.Response.StatusCode = (int)HttpStatusCode.Forbidden; } } catch (Exception ex) { Logger.LogError(EventId, ex, ex.Message); } }
private static string GetLogEntry(WebhookEventArgs e) { return(string.Concat( DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), "\t", e.ActiveHandler.ForTopic.EndsWith(".publish") ? "Published" : "Unpublished", "\t", e.Request.ContentId)); }
public static WebhookHandlerLogEntry LogPublishEvent(WebhookEventArgs e) { var path = LOG_PATH; if (!Path.IsPathRooted(path)) { path = HttpContext.Current.Server.MapPath(path); } Directory.CreateDirectory(Path.GetDirectoryName(path)); File.AppendAllLines(path, new[] { GetLogEntry(e) }); return(null); }
private static async void Bot_PostReceived(WebhookEventArgs e) { if (!e.IsValid) { return; } await Task.Delay(50); if (RawDebug) { object o = JsonConvert.DeserializeObject(e.Body); string s = JsonConvert.SerializeObject(o, Formatting.Indented); Console.WriteLine("\n" + s); } }
public static WebhookHandlerLogEntry SerializePublishedContent(WebhookEventArgs e) { // Establish the archive directory var path = ARCHIVE_DIRECTORY_PATH; if (!Path.IsPathRooted(path)) { path = HttpContext.Current.Server.MapPath(path); } Directory.CreateDirectory(path); // The actual path to the file will be the content ID var fullPath = Path.Combine(path, string.Concat(e.Request.ContentId, ".", FILE_EXTENSION)); File.WriteAllText(fullPath, e.Request.Body); return(new WebhookHandlerLogEntry("Content serialized to " + fullPath)); }
private static async void Bot_InvalidPostReceived(WebhookEventArgs e) { await Task.Delay(50); Console.WriteLine("Invalid POST received (invalid or empty signature)"); }
private async void WebhookServer_PostReceived(WebhookEventArgs e) { try { #if !DEBUG const string signatureHeader = "X-Twitter-Webhooks-Signature"; if (!e.Request.Headers.Keys.Contains(signatureHeader)) { Logger.LogWarning(Resources.InvalidSignature); InvalidPostReceived?.Invoke(e); return; } var signature = e.Request.Headers[signatureHeader][0]; if (!VerifySignature(signature, e.BodyRaw)) { Logger.LogWarning(Resources.InvalidSignature); InvalidPostReceived?.Invoke(e); return; } #endif e.IsValid = true; if (Recipient != 0) { RecipientChecker check = e.Body.FromJson <RecipientChecker>(); if (Recipient != check.Recipient) { return; } } WebhookEvent webhookEvent = e.Body.FromJson <WebhookEvent>(); if (webhookEvent.DirectMessageEvents != null) { if (OnMessage != null) { foreach (var item in webhookEvent.DirectMessageEvents) { MessageEventArgs args = new MessageEventArgs() { Recipient = webhookEvent.Recipient, Message = item.ToMessage() }; await Task.Run(() => { OnMessage.Invoke(args); }); } } } if (webhookEvent.FollowEvents != null) { foreach (var item in webhookEvent.FollowEvents) { if (item.Type == "follow" && OnFollow != null) { FollowEventArgs args = new FollowEventArgs() { Recipient = webhookEvent.Recipient, Timestamp = item.Timestamp, Type = FollowType.Follow, Target = item.Target, Source = item.Source }; await Task.Run(() => { OnFollow.Invoke(args); }); } if (item.Type == "unfollow" && OnUnFollow != null) { FollowEventArgs args = new FollowEventArgs() { Recipient = webhookEvent.Recipient, Timestamp = item.Timestamp, Type = FollowType.Unfollow, Target = item.Target, Source = item.Source }; await Task.Run(() => { OnUnFollow.Invoke(args); }); } } } if (webhookEvent.TweetCreateEvents != null) { foreach (var item in webhookEvent.TweetCreateEvents) { TweetEventArgs args = new TweetEventArgs() { Recipient = webhookEvent.Recipient, Tweet = item }; bool processed = false; if (item.RetweetedFrom != null) { if (OnRetweet != null) { await Task.Run(() => { OnRetweet.Invoke(args); }); } processed = true; } if (item.QuotedFrom != null) { if (OnQuote != null) { await Task.Run(() => { OnQuote.Invoke(args); }); } processed = true; } if (item.ReplyToUserId != null && item.ReplyToStatusId != null) { if (OnComment != null) { await Task.Run(() => { OnComment.Invoke(args); }); } processed = true; } if (item.ReplyToUserId != null && item.ReplyToStatusId == null) { if (OnMention != null) { await Task.Run(() => { OnMention.Invoke(args); }); } processed = true; } if (!processed) { if (OnTweet != null) { await Task.Run(() => { OnTweet.Invoke(args); }); } } } #region //if (Tweeted != null) //{ // foreach (var item in webhookEvent.TweetCreateEvents) // { // TweetCreateEventArgs args = new TweetCreateEventArgs() // { // Tweet = item // }; // Tweeted.Invoke(args); // } //} #endregion } if (webhookEvent.LikeEvents != null) { if (OnLike != null) { foreach (var item in webhookEvent.LikeEvents) { LikeEventArgs args = new LikeEventArgs() { Recipient = webhookEvent.Recipient, Id = item.Id, Timestamp = item.Timestamp, Tweet = item.Tweet, User = item.User }; await Task.Run(() => { OnLike.Invoke(args); }); } } } } catch (Exception ex) { Logger.LogError(EventId, ex, ex.Message); } }