예제 #1
0
        static void Main(string[] args)
        {
            //Get a list of slack channels
            List <SlackChannel> channels = SlackApi.GetChannelsList();
            //Get a channel by name ( change name with your channel name )
            SlackChannel channel = channels.SingleOrDefault(c => c.name == "test");

            //Post a message into this channel
            SlackApi.PostMessage(channel, "Hello World !");
        }
예제 #2
0
        /// <summary>
        /// Get the list of Channels
        /// Return a list of SlackChannel Object
        /// </summary>
        /// <returns></returns>
        public static List <SlackChannel> GetChannelsList()
        {
            List <SlackChannel> channels = new List <SlackChannel>();

            try
            {
                WebRequest request = WebRequest.Create(string.Format("{0}channels.list?token={1}", SlackURL, Token));
                request.Credentials = CredentialCache.DefaultCredentials;
                request.Method      = "GET";
                request.ContentType = "application/x-www-form-urlencoded";
                request.Proxy       = SetProxy();

                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                Console.WriteLine(response.StatusDescription);
                Stream       dataStream         = response.GetResponseStream();
                StreamReader reader             = new StreamReader(dataStream);
                string       responseFromServer = reader.ReadToEnd();
                JObject      data = (JObject)JsonConvert.DeserializeObject(responseFromServer);
                if (data["channels"] != null)
                {
                    foreach (var c in data["channels"])
                    {
                        SlackChannel sc = new SlackChannel();
                        sc.id         = c["id"].ToString();
                        sc.name       = c["name"].ToString();
                        sc.is_channel = c["is_channel"].ToString();
                        sc.created    = c["created"].ToString();

                        channels.Add(sc);
                    }
                }
                reader.Close();
                dataStream.Close();
                response.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(channels);
        }
예제 #3
0
 /// <summary>
 /// Post a message in a specific channel
 /// Return a Http Web Response
 /// </summary>
 /// <param name="channel"></param>
 /// <param name="message"></param>
 public static HttpWebResponse PostMessage(SlackChannel channel, string message)
 {
     try
     {
         if (channel != null)
         {
             WebRequest request = WebRequest.Create(string.Format("{0}chat.postMessage?token={1}&channel={2}&text={3}&username={4}", SlackURL, Token, channel.id, message, Username));
             request.Method      = "POST";
             request.ContentType = "application/x-www-form-urlencoded,application/json";
             request.Credentials = CredentialCache.DefaultCredentials;
             request.Proxy       = SetProxy();
             return((HttpWebResponse)request.GetResponse());
         }
         else
         {
             throw new Exception("Le Channel n'existe pas");
         }
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }