コード例 #1
0
 static void ShowRes(MsgHistroyRes res)
 {
     if (res != null)
     {
         res.messages.ForEach(x => {
             Console.WriteLine($"Ts: {x.ts}\tText: {x.text}");
         });
     }
 }
コード例 #2
0
    static async Task <MsgHistroyRes> GetMsgHistoryAsync(string path)
    {
        MsgHistroyRes       res      = null;
        HttpResponseMessage response = await client.GetAsync(path);

        if (response.IsSuccessStatusCode)
        {
            res = await response.Content.ReadAsAsync <MsgHistroyRes>();
        }
        return(res);
    }
コード例 #3
0
    static async Task RunAsync(string token, string channel, string searchCount, string userId)
    {
        // Update port # in the following line.
        client.BaseAddress = new Uri("https://slack.com/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));

        //Int32 unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(2018, 10, 11))).TotalSeconds;


        //long unixTime = ((DateTimeOffset)foo).ToUnixTimeSeconds();

        List <Task <DelRes> > tasks = new List <Task <DelRes> >();

        try
        {
            MsgHistroyRes res        = new MsgHistroyRes();
            List <MsgRes> msgRes     = new List <MsgRes>();
            var           delResList = new List <DelRes>();
            //Task<List<DelRes>> delRes;

            bool flag = true;
            Console.WriteLine("1. 프로그램 시작");

            //res = await GetMsgHistoryAsync("api/channels.history?token=" + token + "&channel=" + channel +"&count=" + count);

            string lastTs = null;

            while (msgRes.Count < 10000 && flag)
            {
                // private 채널은 groups.history API를 이용해서 삭제해야함.
                var temp = await GetMsgHistoryAsync("api/groups.history?token=" + token + "&channel=" + channel + "&count=1000&latest=" + lastTs);

                msgRes.AddRange(temp.messages);
                if (temp.has_more.Equals(false))
                {
                    flag = false;
                }
                else
                {
                    lastTs = temp.messages.Last().ts;
                }
            }

            Console.WriteLine("2. 조회결과 - 총 {0} 개 입니다.", msgRes.Count);

            var resMsgList = msgRes.Where(x => x.user == userId).ToList();

            Console.WriteLine("3. 삭제대상 - 총 {0} 개 입니다.", resMsgList.Count);


            foreach (var resMsg in resMsgList)
            {
                tasks.Add(Task.Run(() => DeleteMsgAsync("api/chat.delete?token=" + token + "&channel=" + channel + "&ts=" + resMsg.ts)));
            }

            Task.WaitAll(tasks.ToArray());
            foreach (Task t in tasks)
            {
                Console.WriteLine("...");
                //Console.WriteLine("Task {0} Status: {1}", t.Id, t.Status);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
        }

        Console.WriteLine("4. 프로그램 종료 - 총 {0} 개 삭제되었습니다.", tasks.Count);
        Console.ReadLine();
    }