예제 #1
0
        /// <summary>
        /// Starts the slack api
        /// </summary>
        /// <param name="token">Generated token from slack which allows the client to connect to a team</param>
        /// <param name="name">Bot's name on slack</param>
        public SlackBotClient(string token)
        {
            //Register the current token and name
            SlackBotAPI.SetupToken(token);

            //Start the bot
            _start();
        }
예제 #2
0
        /// <summary>
        /// Starts the slack api
        /// </summary>
        /// <param name="token">Generated token from slack which allows the client to connect to a team</param>
        /// <param name="name">Bot's name on slack</param>
        public SlackBotClient(string token, UserIdentity identity)
        {
            this.Identity = identity;

            //Register the current token and name
            SlackBotAPI.SetupToken(token);

            //Start the bot
            _start();
        }
예제 #3
0
        /// <summary>
        /// Send a file to the given channel
        /// </summary>
        public JObject SendFile(SlackChannel channel, string filePath)
        {
            JObject parameters = new JObject
            {
                { "channel", channel.Id },
                { "filetype", "text" },
                { "filename", filePath }
            };

            var retorno = SlackBotAPI.Call(SlackAPICalls.FilesUpload, parameters, filePath);

            return(retorno);
        }
예제 #4
0
        /// <summary>
        /// Send a reaction to some message
        /// </summary>
        /// <param name="channel">Slack channel to send the message to</param>
        /// <param name="message">Message list to be sent. Messages can have slack's formatting</param>
        /// <param name="reactions">Array of current channel's emojis (without ":") to react</param>
        public void SendReaction(SlackChannel channel, SlackMessage message, params string[] reactions)
        {
            foreach (var reaction in reactions)
            {
                JObject parameters = new JObject
                {
                    { "channel", channel.Id },
                    { "name", reaction },
                    { "timestamp", message.TimeStamp }
                };

                SlackBotAPI.Call(SlackAPICalls.ReactionsAdd, parameters);
            }
        }
예제 #5
0
        /// <summary>
        /// Sends a message to a given slack channel
        /// </summary>
        /// <param name="channel">Slack channel to send the message to</param>
        /// <param name="message">Message to be sent. It can have slack's formatting</param>
        public bool SendMessage(SlackMessage message)
        {
            JObject parameters = JObject.FromObject(message);

            //Adds the current identity to the message
            parameters = Identity.AddIdentityParameters(parameters);

            var result = SlackBotAPI.Call(SlackAPICalls.ChatPostMessage, parameters);

            bool retorno;

            bool.TryParse(result["ok"].ToString(), out retorno);

            return(retorno);
        }
예제 #6
0
        private string GetTodayLastBotMessageInGroup(string groupdId)
        {
            var request = new JObject();

            request.Add("channel", groupdId);
            request.Add("oldest", DateTime.Now.Date.Subtract(new DateTime(1970, 1, 1)).TotalSeconds);

            JArray channelArray = (JArray)SlackBotAPI.Call(SlackAPICalls.GroupsHistory, request)["messages"];

            //#ToDo: Filtrar pelo Id
            var lastMsg = channelArray.ToObject <IEnumerable <SlackMessage> >().FirstOrDefault(x => x.Subtype == SlackMessage.SubTypeEnum.botMessage);

            return(lastMsg == null
                ? null
                : lastMsg.Text);
        }
예제 #7
0
        public SlackUser GetBot(string id)
        {
            var param = new JObject();

            param.Add("bot", id);

            JObject response = SlackBotAPI.Call(SlackAPICalls.BotsInfo, param);

            if (!((bool)response["ok"]))
            {
                throw new Exception(response["error"].ToString());
            }

            var       bot  = (JObject)response["bot"];
            SlackUser user = bot.ToObject <SlackUser>();

            return(user);
        }
예제 #8
0
        /// <summary>
        /// Sends a message to a given slack channel
        /// </summary>
        /// <param name="channel">Slack channel to send the message to</param>
        /// <param name="message">Message to be sent. It can have slack's formatting</param>
        public bool SendMessage(SlackChannel channel, string message)
        {
            JObject parameters = new JObject
            {
                { "channel", channel.Id },
                { "text", WebUtility.HtmlDecode(message).Replace('&', 'E') }
            };

            //Adds the current identity to the message
            parameters = Identity.AddIdentityParameters(parameters);

            var result = SlackBotAPI.Call(SlackAPICalls.ChatPostMessage, parameters);

            bool retorno;

            bool.TryParse(result["ok"].ToString(), out retorno);

            return(retorno);
        }
예제 #9
0
        /// <summary>
        /// Execute the first run for the api
        /// </summary>
        private void _start()
        {
            Console.WriteLine("Starting...");
            JObject response = SlackBotAPI.Call(SlackAPICalls.RTMStart, null);

            _socket            = new WebSocket(response[SlackJSONProperties.Url].ToString());
            _socket.OnMessage += _parseRequest;

            _socket.Connect();

            //When connected, set the bot's user and id.
            BotUser = response["self"].ToObject <SlackUser>();

            //Creates an identity
            if (Identity == null)
            {
                Identity = new UserIdentity(BotUser.Name);
            }
        }
예제 #10
0
        private void JoinedChannel(JObject message)
        {
            var channel = SlackBotAPI.GetChannelFromId((string)message[SlackMessageJSONProps.Channel]);

            OnChannelJoined?.Invoke(this, channel);
        }