Ejemplo n.º 1
0
        public void SendNotification(string message, TwitterSettings settings)
        {
            try
            {
                var oAuth = new TinyTwitter.OAuthInfo
                {
                    ConsumerKey    = settings.ConsumerKey,
                    ConsumerSecret = settings.ConsumerSecret,
                    AccessToken    = settings.AccessToken,
                    AccessSecret   = settings.AccessTokenSecret
                };

                var twitter = new TinyTwitter.TinyTwitter(oAuth);

                if (settings.DirectMessage)
                {
                    twitter.DirectMessage(message, settings.Mention);
                }

                else
                {
                    if (settings.Mention.IsNotNullOrWhiteSpace())
                    {
                        message += string.Format(" @{0}", settings.Mention);
                    }

                    twitter.UpdateStatus(message);
                }
            }
            catch (WebException ex)
            {
                using (var response = ex.Response)
                {
                    var httpResponse = (HttpWebResponse)response;

                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream == null)
                        {
                            _logger.Trace("Status Code: {0}", httpResponse.StatusCode);
                            throw new TwitterException("Error received from Twitter: " + httpResponse.StatusCode, ex);
                        }

                        using (var reader = new StreamReader(responseStream))
                        {
                            var responseBody = reader.ReadToEnd();
                            _logger.Trace("Reponse: {0} Status Code: {1}", responseBody, httpResponse.StatusCode);
                            throw new TwitterException("Error received from Twitter: " + responseBody, ex);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public static void OnClanState(SteamFriends.ClanStateCallback callback)
        {
            if (callback.Announcements.Count == 0)
            {
                return;
            }

            var groupName = callback.ClanName;

            if (string.IsNullOrEmpty(groupName))
            {
                groupName = Friends.GetClanName(callback.ClanID);
            }

            foreach (var announcement in callback.Announcements)
            {
                var message = announcement.Headline.Trim();

                if (!string.IsNullOrEmpty(groupName) && !announcement.Headline.Contains(groupName.Replace("Steam", string.Empty).Trim()))
                {
                    message = $"{announcement.Headline} ({groupName})";
                }

                // 240 max tweet length, minus 23 characters for the t.co link
                if (message.Length > 217)
                {
                    message = $"{message.Substring(0, 216)}…";
                }

                var url = $"https://steamcommunity.com/gid/{callback.ClanID.ConvertToUInt64()}/announcements/detail/{announcement.ID}";

                for (var i = 0; i < 2; i++)
                {
                    try
                    {
                        Log($"Tweeting \"{message}\" - {url}");

                        Twitter.UpdateStatus($"{message} {url}");

                        break;
                    }
                    catch (Exception e)
                    {
                        Log($"Exception: {e.Message}");
                    }
                }
            }
        }
        public static void OnClanState(SteamFriends.ClanStateCallback callback)
        {
            if (callback.Announcements.Count == 0)
            {
                return;
            }

            var groupName = callback.ClanName;

            if (string.IsNullOrEmpty(groupName))
            {
                groupName = Friends.GetClanName(callback.ClanID);
            }

            foreach (var announcement in callback.Announcements)
            {
                var message = announcement.Headline.Trim();

                if (!string.IsNullOrEmpty(groupName) && !announcement.Headline.Contains(groupName.Replace("Steam", string.Empty).Trim()))
                {
                    message = string.Format("{0}:\n{1}", groupName, announcement.Headline);
                }

                // 117 is a magical tweet length number
                if (message.Length > 117)
                {
                    message = string.Format("{0}…", message.Substring(0, 116));
                }

                var url = $"http://steamcommunity.com/gid/{callback.ClanID.ConvertToUInt64()}/announcements/detail/{announcement.ID}";

                for (var i = 0; i < 2; i++)
                {
                    try
                    {
                        Log("Tweeting \"{0}\" - {1}", message, url);

                        Twitter.UpdateStatus(string.Format("{0} {1}", message, url));

                        break;
                    }
                    catch (Exception e)
                    {
                        Log("Exception: {0}", e.Message);
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public void SendNotification(string message, TwitterSettings settings)
        {
            try
            {
                var oAuth = new TinyTwitter.OAuthInfo
                {
                    ConsumerKey = settings.ConsumerKey,
                    ConsumerSecret = settings.ConsumerSecret,
                    AccessToken = settings.AccessToken,
                    AccessSecret = settings.AccessTokenSecret
                };

                var twitter = new TinyTwitter.TinyTwitter(oAuth);

                if (settings.DirectMessage)
                {
                    twitter.DirectMessage(message, settings.Mention);
                }

                else
                {
                    if (settings.Mention.IsNotNullOrWhiteSpace())
                    {
                        message += string.Format(" @{0}", settings.Mention);
                    }

                    twitter.UpdateStatus(message);
                }
            }
            catch (WebException ex)
            {
                using (var response = ex.Response)
                {
                    var httpResponse = (HttpWebResponse)response;

                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream == null)
                        {
                            _logger.Trace("Status Code: {0}", httpResponse.StatusCode);
                            throw new TwitterException("Error received from Twitter: " + httpResponse.StatusCode, ex);
                        }

                        using (var reader = new StreamReader(responseStream))
                        {
                            var responseBody = reader.ReadToEnd();
                            _logger.Trace("Reponse: {0} Status Code: {1}", responseBody, httpResponse.StatusCode);
                            throw new TwitterException("Error received from Twitter: " + responseBody, ex);
                        }
                    }
                }
            }
        }