예제 #1
0
        public void Run()
        {
            while (_running)
            {
                try
                {
                    List <SlackMessageApi> messages = null;

                    if (_lastMessage == null)
                    {
                        messages = _api.GetMessagesFromChannel(_token, _channelId, 100).Result.ToList();
                    }
                    else
                    {
                        messages = _api.GetMessagesFromChannel(_token, _channelId, 100, _lastMessage.TimeStamp).Result.ToList();
                    }

                    if (_lastMessage == null)
                    {
                        _lastMessage = messages.Any() ? messages.FirstOrDefault() : null;
                    }
                    else
                    {
                        _lastMessage = messages.Any() ? messages.FirstOrDefault() : _lastMessage;

                        if (messages.Any())
                        {
                            foreach (var message in messages)
                            {
                                var result = _processor.ProcessString(message.Text, message.User, UnixTimeStampToDateTime(long.Parse(message.TimeStamp.Replace(".", "")) / 1000000));

                                if (result != null)
                                {
                                    var lines = result.Result.Split('\n');

                                    if (result.Result.StartsWith("{"))
                                    {
                                        _api.UploadFile(_channelName, _token, Encoding.UTF8.GetBytes(result.Result), message.TimeStamp, _logger);
                                    }
                                    else if (lines.Length > 25)
                                    {
                                        int count = 0;

                                        while (count < lines.Length)
                                        {
                                            int remain    = lines.Length - count;
                                            int tempCount = remain > 25 ? 25 : remain;
                                            var res       = _api.SendMessage2(_token, _channelName, string.Join("\n", lines.Skip(count).Take(tempCount)), Emoji.Ghost, _botName, _logger, null, message.TimeStamp, true).Result;
                                            count += tempCount;
                                        }
                                    }
                                    else
                                    {
                                        _api.SendMessage(_channelName, result.Result, Emoji.Ghost, _botName, _logger);
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex);
                }

                Thread.Sleep(1000);
            }
        }