예제 #1
0
        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);
            }
        }
예제 #2
0
        public void Parse()
        {
            try
            {
                reader.MoveToContent();
                while (reader.Read())
                {
                    var arg = new ParseEventArg
                    {
                        NodeType     = reader.NodeType,
                        Depth        = reader.Depth,
                        Name         = reader.Name,
                        EmptyElement = reader.IsEmptyElement
                    };
                    if (reader.HasValue)
                    {
                        arg.Value = reader.Value;
                    }
                    else
                    {
                        arg.Value = null;
                    }
                    if (reader.HasAttributes)
                    {
                        var arr = new Attribute[reader.AttributeCount];
                        for (var i = 0; i < reader.AttributeCount; i++)
                        {
                            reader.MoveToAttribute(i);
                            arr[i] = new Attribute {
                                Name = reader.Name, Value = reader.Value, Index = i
                            };
                        }
                        reader.MoveToElement();
                        arg.Attributes = new ReadOnlyCollection <Attribute>(arr);
                    }
                    else
                    {
                        arg.Attributes = new ReadOnlyCollection <Attribute>(empty);
                    }
                    OnNode?.Invoke(this, arg);
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                        OnElement?.Invoke(this, arg);
                        break;

                    case XmlNodeType.CDATA:
                        OnCData?.Invoke(this, arg);
                        break;

                    case XmlNodeType.Comment:
                        OnComment?.Invoke(this, arg);
                        break;

                    case XmlNodeType.DocumentType:
                        OnDocType?.Invoke(this, arg);
                        break;

                    case XmlNodeType.EndElement:
                        OnEndElement?.Invoke(this, arg);
                        break;

                    case XmlNodeType.EntityReference:
                        OnEntityReferenceUnresolved?.Invoke(this, arg);
                        reader.ResolveEntity();
                        reader.Read();
                        arg.Name     = reader.Name;
                        arg.Value    = reader.Value;
                        arg.Depth    = reader.Depth;
                        arg.NodeType = reader.NodeType;
                        OnEntityReference?.Invoke(this, arg);
                        break;

                    case XmlNodeType.ProcessingInstruction:
                        OnProcessingInstruction?.Invoke(this, arg);
                        break;

                    case XmlNodeType.Text:
                        OnText?.Invoke(this, arg);
                        break;
                    }
                }
            }
            catch (XmlException e)
            {
                OnException?.Invoke(this, e);
            }
        }