private async Task ReadTweetFromStream(StreamReader reader) { var requestTask = reader.ReadLineAsync(); var resultingTask = await Task.WhenAny(requestTask, Task.Delay(READ_TWEET_TIMEOUT)).ConfigureAwait(false); var timedOut = resultingTask != requestTask; if (timedOut) { requestTask.Dispose(); throw new TimeoutException($"TwitterService: Tweet stream timed out. Timeout in milliseconds {READ_TWEET_TIMEOUT}"); } var jsonResponse = await requestTask.ConfigureAwait(false); if (string.IsNullOrEmpty(jsonResponse)) { return; } var tweet = JsonConvert.DeserializeObject <TweetDto>(jsonResponse); OnTweet?.Invoke(this, tweet); }
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); } }