Esempio n. 1
0
 public async Task<List<SlackEvent>> ReactionsList(string user = null, bool full = false, int count = 100, int page = 1, bool allItems = false)
 {
     TrexUri url = new TrexUri(SlackConstants.BaseUrl).AppendPathSegment("reactions.list");
     if (!string.IsNullOrEmpty(user))
     {
         url.SetQueryParam("user", user);
     }
     url.SetQueryParams(new Dictionary<string, object>()
     {
         { "full", full },
         { "count", count },
         { "page", page }
     });
     JObject response = await DoAuthSlackCall(new Uri(url), allItems);
     List<SlackEvent> reactions = new List<SlackEvent>();
     foreach (JObject item in (JArray)response["items"])
     {
         if ((string)item["type"] == "message")
         {
             Message message = JsonConvert.DeserializeObject<Message>(item["message"].ToString());
             reactions.Add(message);
         }
     }
     return reactions;
 }
Esempio n. 2
0
        public async Task ReactionsAdd(string name, string file = null, string fileComment = null, string channel = null, string timestamp = null)
        {
            TrexUri url = new TrexUri(SlackConstants.BaseUrl).AppendPathSegment("reactions.add");
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("required", nameof(name));
            }
            if (string.IsNullOrEmpty(file) && string.IsNullOrEmpty(fileComment) && (string.IsNullOrEmpty(channel) || string.IsNullOrEmpty(timestamp)))
            {
                throw new ArgumentException($"One of {nameof(file)}, {nameof(fileComment)}, or the combination of {nameof(channel)} and {nameof(timestamp)} must be specified.");
            }

            url.SetQueryParam("name", name);
            if (file != null)
            {
                url.SetQueryParam("file", file);
            }
            else if (fileComment != null)
            {
                url.SetQueryParam("file_comment", fileComment);
            }
            else
            {
                url.SetQueryParam("channel", channel)
                .SetQueryParam("timestamp", timestamp);
            }
            await DoAuthSlackCall(new Uri(url));
        }
Esempio n. 3
0
        private IEnumerable <string> GetPairs(bool encodeSpaceAsPlus)
        {
            foreach (string key in orderedKeys)
            {
                object val = this[key];
                if (val == null)
                {
                    continue;
                }

                if (val is string || !(val is IEnumerable))
                {
                    yield return(key + "=" + TrexUri.EncodeQueryParamValue(val, encodeSpaceAsPlus));
                }
                else
                {
                    foreach (var subval in val as IEnumerable)
                    {
                        if (subval == null)
                        {
                            continue;
                        }
                        yield return(key + "=" + TrexUri.EncodeQueryParamValue(subval, encodeSpaceAsPlus));
                    }
                }
            }
        }
Esempio n. 4
0
        public static QueryParamCollection Parse(string queryString)
        {
            QueryParamCollection result = new QueryParamCollection();

            if (string.IsNullOrEmpty(queryString))
            {
                return(result);
            }

            queryString = queryString.TrimStart('?').Split('?').Last();

            var pairs = (
                from keyValue in queryString.Split('&')
                let pair = keyValue.Split('=')
                           let key = pair[0]
                                     let value = pair.Length >= 2 ? TrexUri.DecodeQueryParamValue(pair[1]) : ""
                                                 group value by key into g
                                                 select new { Key = g.Key, Values = g.ToArray() });

            foreach (var g in pairs)
            {
                if (g.Values.Length == 1)
                {
                    result.Add(g.Key, g.Values[0]);
                }
                else
                {
                    result.Add(g.Key, g.Values);
                }
            }

            return(result);
        }
Esempio n. 5
0
 public async Task<List<SlackChannel>> ChannelsList(int excludeArchived = 0)
 {
     TrexUri url = new TrexUri(SlackConstants.BaseUrl).AppendPathSegment("channels.list").SetQueryParam("exclude_archived", excludeArchived);
     JObject response = await DoAuthSlackCall(new Uri(url));
     List<SlackChannel> channels = new List<SlackChannel>();
     foreach (JObject item in (JArray) response["channels"])
     {
         channels.Add(JsonConvert.DeserializeObject<SlackChannel>(item.ToString()));
     }
     return channels;
 }
Esempio n. 6
0
 public async Task<List<SlackUser>> UsersList(int presence = 0)
 {
     TrexUri url = new TrexUri(SlackConstants.BaseUrl).AppendPathSegment("users.list").SetQueryParam("presence", presence);
     JObject response = await DoAuthSlackCall(new Uri(url));
     List<SlackUser> users = new List<SlackUser>();
     foreach (JObject item in (JArray)response["members"])
     {
         users.Add(JsonConvert.DeserializeObject<SlackUser>(item.ToString()));
     }
     return users;
 }
Esempio n. 7
0
 public static async Task<JObject> DoSlackCall(Uri uri, bool all = false, HttpMethod method = null, IEnumerable<KeyValuePair<string, string>> args = null)
 {
     HttpClient httpClient = new HttpClient();
     HttpResponseMessage response = null;
     if (method == null)
     {
         method = HttpMethod.Get;
     }
     if (method == HttpMethod.Get)
     {
         response = await httpClient.GetAsync(uri);
     }
     else if (method == HttpMethod.Post)
     {
         response = await httpClient.PostAsync(uri, new FormUrlEncodedContent(args));
     }
     else
     {
         throw new ArgumentException("Can only use GET and POST", nameof(method));
     }
     string responseString = await response.Content.ReadAsStringAsync();
     JObject responseObject = JObject.Parse(responseString);
     bool ok = (bool)responseObject["ok"];
     if (ok)
     {
         if (all)
         {
             JObject paging = (JObject)responseObject["paging"];
             int pages = (int)paging["pages"];
             int page = (int)paging["page"];
             for (int i = 1; i < pages; i++)
             {
                 TrexUri url = new TrexUri(uri.ToString());
                 page++;
                 url.SetQueryParam("page", page);
                 JObject pageResponse = await DoSlackCall(new Uri(url), false, method, args);
                 string array = null;
                 foreach (KeyValuePair<string, JToken> token in pageResponse)
                 {
                     if (token.Value.Type == JTokenType.Array)
                     {
                         array = token.Key;
                         break;
                     }
                 }
                 if (string.IsNullOrEmpty(array))
                 {
                     throw new Exception("Couldn't find array");
                 }
                 foreach (JToken item in pageResponse[array])
                 {
                     ((JArray)responseObject[array]).Add(item);
                 }
             }
             responseObject.Remove("paging");
         }
         return responseObject;
     }
     else
     {
         throw new Exception("not ok");
     }
 }
Esempio n. 8
0
 public async Task<JObject> DoAuthSlackCall(Uri uri, bool all = false, HttpMethod method = null, IEnumerable<KeyValuePair<string, string>> args = null)
 {
     TrexUri url = new TrexUri(uri.ToString());
     url.SetQueryParam("token", AccessToken);
     return await SlackCore.DoSlackCall(new Uri(url), all, method, args);
 }
Esempio n. 9
0
 public static async Task<JObject> DoSlackCall(string endpoint, bool all = false, HttpMethod method = null, IEnumerable<KeyValuePair<string, string>> args = null)
 {
     TrexUri url = new TrexUri(SlackConstants.BaseUrl).AppendPathSegment(endpoint);
     return await SlackCore.DoSlackCall(url, all, method, args);
 }
Esempio n. 10
0
        public static async Task<SlackCore> CompleteOAuth(string clientId, string clientSecret, string code, Uri redirectUri = null)
        {
            if (string.IsNullOrEmpty(clientId))
            {
                throw new ArgumentException("required", nameof(clientId));
            }
            if (string.IsNullOrEmpty(clientSecret))
            {
                throw new ArgumentException("required", nameof(clientSecret));
            }
            if (string.IsNullOrEmpty(code))
            {
                throw new ArgumentException("required", nameof(code));
            }

            TrexUri url = new TrexUri(SlackConstants.BaseUrl).AppendPathSegment("oauth.access").SetQueryParams(new Dictionary<string, object>()
            {
                { "client_id", clientId },
                { "client_secret", clientSecret },
                { "code", code }
            });
            if (redirectUri != null)
            {
                url.SetQueryParam("redirect_uri", redirectUri.ToString());
            }
            JObject response = await SlackCore.DoSlackCall(new Uri(url));
            string accessToken = (string)response["access_token"];
            return new SlackCore(accessToken);
        }
Esempio n. 11
0
 public static Uri StartOAuth(string clientId, List<SlackConstants.SlackScope> scopes, Uri redirectUri = null, string state = null, string team = null)
 {
     TrexUri url = new TrexUri(SlackConstants.BaseUrl).AppendPathSegments("oauth", "authorize");
     if (string.IsNullOrEmpty(clientId))
     {
         throw new ArgumentException("required", nameof(clientId));
     }
     url.SetQueryParam("client_id", clientId);
     
     if (scopes == null || scopes.Count == 0)
     {
         throw new ArgumentException("Must set at least 1 scope", nameof(scopes));
     }
     string scopesString = string.Empty;
     for (int i = 0; i < scopes.Count; i++)
     {
         if (i != scopes.Count - 1)
         {
             scopesString += scopes[i].SlackScopeToString() + " ";
         }
         else
         {
             scopesString += scopes[i].SlackScopeToString();
         }
     }
     url.SetQueryParam("scope", scopesString);
     if (redirectUri != null)
     {
         url.SetQueryParam("redirect_uri", redirectUri.ToString());
     }
     if (!string.IsNullOrEmpty(state))
     {
         url.SetQueryParam("state", state);
     }
     if (!string.IsNullOrEmpty(team))
     {
         url.SetQueryParam("team", team);
     }
     return new Uri(url);
 }