Exemple #1
0
        static async Task CheckStuff(SlackTaskClient slackClient, Channel channel, UserListResponse users)
        {
            await _semaphore.WaitAsync();

            threadCount++;
            //Console.WriteLine("Thread {0} starting", threadCount);
            try {
                var history = await slackClient.GetChannelHistoryAsync(channel, null, DateTime.Now.AddDays(-7), 1000);

                if (history.messages == null)
                {
                    Console.WriteLine("Rate Limited: Backing Off for 10s");
                    await Task.Delay(10000);

                    history = await slackClient.GetChannelHistoryAsync(channel, null, DateTime.Now.AddDays(-7), 1000);

                    if (history.messages == null)
                    {
                        throw new Exception("Something went bad. You was rate limited");
                    }
                }
                if (history.messages.Count() == 0)
                {
                    return;
                }

                Console.WriteLine("{0}: Count: {1}, More Messages? : {2}", channel.name, history.messages.Count(), history.has_more);
                foreach (var message in history.messages)
                {
                    var user = users.members.FirstOrDefault(s => s.id == message.user);
                    var name = user != null ? user.name : "bot or unknown";
                    lock (key) {
                        peoples.Add(name);
                    }
                }
            }
            catch (Exception e) {
                Console.WriteLine("Exception: " + e.Message);
                throw;
            }
            finally
            {
                //Console.WriteLine("Thread {0} finished", threadCount);
                threadCount--;
                _semaphore.Release();
            }
        }
        public async Task <string> GetManualDiary()
        {
            var client = new SlackTaskClient(OAuthAccessToken);

            var channels = await client.GetChannelListAsync();

            var diaryChannel = channels.channels
                               .Where(x => x.name == "diary")
                               .FirstOrDefault();

            if (diaryChannel == null)
            {
                Console.WriteLine("not found diary channel.");
                return(null);
            }

            var history = await client.GetChannelHistoryAsync(diaryChannel);

            var yesterday = DateTime.UtcNow.AddDays(-1);

            var stringBuilder = new StringBuilder();

            var targetMessages = history.messages
                                 .Where(x => string.IsNullOrEmpty(x.subtype))
                                 .Reverse()
                                 .ToArray();

            foreach (var targetMessage in targetMessages)
            {
                var messageUtc = targetMessage.ts.ToUniversalTime();

                if (messageUtc >= yesterday)
                {
                    stringBuilder.AppendLine("(jst) " + messageUtc.AddHours(9).ToString("yyyy/MM/dd HH:mm:ss"));

                    var lines = targetMessage.text.Split('\n');

                    foreach (var line in lines)
                    {
                        if (line.StartsWith("<http") && line.EndsWith(">"))
                        {
                            var url = line.TrimStart('<').TrimEnd('>');
                            stringBuilder.AppendLine(url);
                        }
                        else
                        {
                            stringBuilder.AppendLine(line);
                        }
                    }

                    stringBuilder.AppendLine();
                }
            }

            return(stringBuilder.ToString());
        }