Beispiel #1
0
        public HomeModule()
        {
            Get["/start"] = _ =>
            {
                //variables - start
                var            datewords = "";
                string[]       done      = new string[2];
                string         name;
                int            i    = 0;
                string         path = AppDomain.CurrentDomain.BaseDirectory;
                UserCredential credential;
                SlackSend      client = new SlackSend(urlWithAccessToken);
                //variables - end
                int        j          = 0;
                int        attending  = 0;
                developers lastpayer  = new developers();
                developers lastpayer2 = new developers(); // incase 2 payers are needed
                ValueRange response2  = response;

                //fetch list of devs
                // look for and read credentials for accessing and updating dev table

                using (var stream =
                           new System.IO.FileStream(path + "client_secret.json", FileMode.Open, FileAccess.Read))
                {
                    string credPath = Path.Combine(path, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");
                    credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                        GoogleClientSecrets.Load(stream).Secrets,
                        scopes,
                        "user",
                        CancellationToken.None,
                        new FileDataStore(credPath, true)).Result;
                }
                service = new SheetsService(new BaseClientService.Initializer()
                {
                    HttpClientInitializer = credential,
                    ApplicationName       = applicationName,
                });

                // Define request parameters.
                SpreadsheetsResource.ValuesResource.GetRequest request =
                    service.Spreadsheets.Values.Get(spreadsheetId, range);

                // Prints the slacknames and paid dates of devs in a spreadsheet:
                // https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
                response = request.Execute(); // fetch everything from range(in worksheet : Sheet1, with a range of A2:all of D(should be 14))
                values   = response.Values;   // put into indexed list of individualised objects
                                              // if a list of values has been found
                if (values.Count > 0)
                {
                    // for each person in the list, create a new developer for them and add them to ateending list
                    foreach (var row in values)
                    {
                        name      = (string)row[1];
                        datewords = (string)row[2];
                        done      = datewords.Split('/');
                        temp.Add(new developers
                        {
                            slackname = name,
                            lastpay   = new date {
                                day = done[0], month = done[1], year = done[2]
                            }
                        });
                    }
                }

                client.PostMessage(text: "Process: Start!",
                                   channel: "#breakytest");

                foreach (var dev in temp)
                {
                    client.PostMessage(text: "@" + temp[i].slackname + " can you make it for breakfast",
                                       channel: "@hannes"); //+ temp[i].slackname);
                    i++;
                }
                return(null);
            };
            Post["/msg"] = _ =>
            {
                var model   = this.Bind <HookMessage>();
                var message = string.Empty;
                Console.WriteLine(model.text.ToLower());
                if (model.token != "YWrKxANIXAOvx4NrEzJAIgqU")
                {                 //checks the token of incoming message if found
                    Console.WriteLine("Invalid Token\n Ignored!");
                    return(null); //ignored if not recognised
                }
                if (model.text == "breaky yes")
                {                                // if the trigger word was found - written in correct format and response was yes
                    message = string.Format("@" + model.user_name + " Recieved! Added to breakfast list");
                    breaklist.Add(new developers // add them to yeslist -> goes into breakfastList in update method
                    {
                        slackname = model.user_name,
                        lastpay   = { }
                    });
                    Console.WriteLine("'" + message + "' sent back to " + model.user_name);
                }
                if (model.text == "breaky no")
                {// if response is no
                    message = string.Format("@" + model.user_name + " Recieved! removed from this weeks breky list");
                    string name = model.user_name;
                    buuuulist.Add(new developers// adds them to nolist -> goes into buuuuList in update method
                    {
                        slackname = model.user_name,
                        lastpay   = { }
                    });
                    Console.WriteLine("'" + message + "' sent back to " + model.user_name);
                }
                if (!string.IsNullOrWhiteSpace(message))// if message is not empty
                {
                    return new SlackMessage {
                               Text = message, Username = "******", Channel = "#breakytest"
                    }
                }
                ;
                else
                {
                    return new SlackMessage {
                               Text = "was empty", Username = "******", Channel = "#breakytest"
                    }
                };
            };
        }
    }
        public ActionResult Index()
        {
            //setup start finish proccess

            HostConfiguration hostConfigs = new HostConfiguration()
            {
                UrlReservations = new UrlReservations()
                {
                    CreateAutomatically = true, User = "******"
                }
            };
            //variables - start
            var datewords = "";

            string[]       done = new string[2];
            string         name;
            int            i    = 0;
            string         path = AppDomain.CurrentDomain.BaseDirectory;
            UserCredential credential;
            SlackSend      client = new SlackSend(urlWithAccessToken);
            //variables - end
            int        j          = 0;
            int        attending  = 0;
            developers lastpayer  = new developers();
            developers lastpayer2 = new developers(); // incase 2 payers are needed
            ValueRange response2  = response;

            //fetch list of devs
            // look for and read credentials for accessing and updating dev table

            using (var stream =
                       new System.IO.FileStream(path + "client_secret.json", FileMode.Open, FileAccess.Read))
            {
                string credPath = Path.Combine(path, ".credentials/sheets.googleapis.com-dotnet-quickstart.json");
                credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                    GoogleClientSecrets.Load(stream).Secrets,
                    scopes,
                    "user",
                    CancellationToken.None,
                    new FileDataStore(credPath, true)).Result;
            }
            service = new SheetsService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credential,
                ApplicationName       = applicationName,
            });

            // Define request parameters.
            SpreadsheetsResource.ValuesResource.GetRequest request =
                service.Spreadsheets.Values.Get(spreadsheetId, range);

            // Prints the slacknames and paid dates of devs in a spreadsheet:
            // https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
            response = request.Execute(); // fetch everything from range(in worksheet : Sheet1, with a range of A2:all of D(should be 14))
            values   = response.Values;   // put into indexed list of individualised objects
            // if a list of values has been found
            if (values.Count > 0)
            {
                // for each person in the list, create a new developer for them and add them to ateending list
                foreach (var row in values)
                {
                    name      = (string)row[1];
                    datewords = (string)row[2];
                    done      = datewords.Split('/');
                    temp.Add(new developers
                    {
                        slackname = name,
                        lastpay   = new date {
                            day = done[0], month = done[1], year = done[2]
                        }
                    });
                }
            }

            client.PostMessage(text: "Process: Start!",
                               channel: "#breakytest");

            foreach (var dev in temp)
            {
                client.PostMessage(text: "@" + temp[i].slackname + " can you make it for breakfast",
                                   channel: "@hannes"); //+ temp[i].slackname);
                i++;
            }

            using (var host = new NancyHost(hostConfigs, new Uri("http://breakfastbot.aquepreview.com:81")))
            {
                host.Start();
                // System.Threading.Thread.Sleep(300000);//30 mins
                Thread.Sleep(3600000);//1 hour
            }
            //after everything has been done
            i = 0;

            //SpreadsheetsResource.ValuesResource.UpdateRequest request2 =
            //  service.Spreadsheets.Values.Update(response,spreadsheetId, range);

            foreach (var devs in temp)
            {
                foreach (var dev in breaklist)
                {
                    if (breaklist[i].slackname == temp[j].slackname)
                    {
                        breaklist[i] = temp[j];
                    }
                    i++;
                }
                j++;
            }
            //we now have list of people going

            attending = breaklist.Count;
            i         = 0;
            // find last person to pay

            if (attending != 0)
            {
                lastpayer = breaklist[i];
                foreach (var cooldev in breaklist)
                {
                    if (Int32.Parse(lastpayer.lastpay.year) < Int32.Parse(breaklist[i].lastpay.year))
                    {
                        if (Int32.Parse(lastpayer.lastpay.month) < Int32.Parse(breaklist[i].lastpay.month))
                        {
                            if (Int32.Parse(lastpayer.lastpay.day) < Int32.Parse(breaklist[i].lastpay.day))
                            {
                                lastpayer = breaklist[i];
                            }
                        }
                    }
                    i++;
                }// end foreach
                 // if there more than 10 people attending find another person to help pay
                 // the next person would be the next person who would pay

                if (breaklist.Count > 10)
                {
                    foreach (var otherdev in breaklist)
                    {
                        j = 0;
                        if (Int32.Parse(lastpayer2.lastpay.year) <= Int32.Parse(breaklist[j].lastpay.year))
                        {
                            if (Int32.Parse(lastpayer2.lastpay.month) <= Int32.Parse(breaklist[j].lastpay.month))
                            {
                                if (Int32.Parse(lastpayer2.lastpay.day) < Int32.Parse(breaklist[j].lastpay.day))
                                {
                                    if (lastpayer.slackname != breaklist[j].slackname)
                                    {
                                        lastpayer2 = breaklist[j];
                                    }
                                }
                            }
                        }
                        j++;
                    }// end foreach otherdev
                }

                // posting messages to channel on results of proccess
                client.PostMessage(text: "It is @" + lastpayer.slackname + " turn to pay",
                                   channel: "#breakytest");
                if (!(lastpayer2.slackname == null))
                {
                    client.PostMessage(text: "and @" + lastpayer2.slackname + " has to pay aswell\n because of too many people!",
                                       channel: "#breakytest");
                }
                client.PostMessage(text: "there are a total of " + attending + " people attending breakfast!",
                                   channel: "#breakytest");
            }
            else
            {
                client.PostMessage(text: "no breakky :(",
                                   channel: "#breakytest");
            }
            //start updating dates for google sheets docs

            for (i = 0; i < response2.Values.Count; i++)
            {
                if (response2.Values[i][1].ToString() == lastpayer.slackname)
                {
                    response2.Values[i][2] = DateTime.Now.ToString("dd/MM/yyyy");
                }
                if (lastpayer2.slackname != null || lastpayer2.slackname != "")
                {
                    if (response2.Values[i][1].ToString() == lastpayer2.slackname)
                    {
                        response2.Values[i][2] = DateTime.Now.ToString("dd/MM/yyyy");
                    }
                }
            }
            // update table with new last pay date of devs who just payed for breakfast

            string spreadsheetId2 = "1YMLuQ1tJnTJs1FQN0yruMHAS41nIRm1FHT87pP3GCV0";
            string range2         = "Sheet1!A2:D14";

            SpreadsheetsResource.ValuesResource.UpdateRequest request3 =
                service.Spreadsheets.Values.Update(response2, spreadsheetId2, range2);
            // execute order 666

            request3.ValueInputOption = SpreadsheetsResource.ValuesResource.UpdateRequest.ValueInputOptionEnum.RAW;
            request3.Execute();

            client.PostMessage(text: "better luck next time",
                               channel: "#breakytest");
            client.PostMessage(text: "Process: Stop",
                               channel: "#breakytest");


            var model = new BotModels {
                Message = "Process started successfully"
            };

            return(View(model));
        }