public SlackStatusAddInConfig()
 {
     MySlackTokens      = Environment.GetEnvironmentVariable("SLACK_TOKEN")?.Split('|').ToList() ?? new List <string>();
     MyLastName         = Environment.GetEnvironmentVariable("SLACK_LAST_NAME");
     OfficeNetworkNames = Environment.GetEnvironmentVariable("SLACK_OFFICE_NETWORKS");
     InMeeting          = new SlackStatus(
         Environment.GetEnvironmentVariable("SLACK_STATUS_MEETING")
         ?? "In a meeting|:spiral_calendar_pad:");
     WorkingInOffice = new SlackStatus(
         Environment.GetEnvironmentVariable("SLACK_STATUS_WORKING_OFFICE")
         ?? "|");
     WorkingRemotely = new SlackStatus(
         Environment.GetEnvironmentVariable("SLACK_STATUS_WORKING_REMOTELY")
         ?? "Working remotely|:house_with_garden:");
     OnVacation = new SlackStatus(
         Environment.GetEnvironmentVariable("SLACK_STATUS_VACATION")
         ?? "Vacationing|:palm_tree:");
 }
Esempio n. 2
0
        private void SetSlackStatus(SlackStatus slackStatus)
        {
            WriteToLog("      >> Setting Slack status to " + slackStatus.Emoji + " " + slackStatus.Text);

            byte[] byteArray = Encoding.UTF8.GetBytes(
                $"profile={{'status_text': '{slackStatus.Text}', 'status_emoji': '{slackStatus.Emoji}', 'status_expiration': {slackStatus.Expiration} }}");

            // Change status for each Slack token we have
            _config.MySlackTokens.ForEach(slackToken =>
            {
                _webRequest               = WebRequest.Create("https://slack.com/api/users.profile.set");
                _webRequest.ContentType   = "application/x-www-form-urlencoded";
                _webRequest.ContentLength = byteArray.Length;
                _webRequest.Method        = "POST";
                _webRequest.Headers.Add("Authorization", $"Bearer {slackToken}");

                using (Stream s = _webRequest.GetRequestStream())
                {
                    s.Write(byteArray, 0, byteArray.Length);
                }
            });
        }
Esempio n. 3
0
        private void ThisAddIn_Reminder(Object item)
        {
            if (item is Outlook.AppointmentItem myAppointmentItem)
            {
                // This is the reminder for the start of an appointment
                WriteToLog("ThisAddIn_Reminder for APPOINTMENT: " + myAppointmentItem.Subject);
                WriteToLog("  Body: " + TruncateAndCleanUpText(myAppointmentItem.Body));
                WriteToLog("  Start: " + myAppointmentItem.Start + ", End: " + myAppointmentItem.End + ", BusyStatus: " + myAppointmentItem.BusyStatus);

                if (DateTime.Now >= myAppointmentItem.Start && DateTime.Now < myAppointmentItem.End && myAppointmentItem.BusyStatus != Outlook.OlBusyStatus.olFree)
                {
                    // This reminder has fired sometime between the start and end of the appointment,
                    // and the appointment has me bus, or out of the office, etc.

                    if (myAppointmentItem.Subject.Contains("PTO") && myAppointmentItem.Organizer.Contains(_config.MyLastName))
                    {
                        WriteToLog("    Is PTO");
                        // This appointment is for my PTO

                        var slackStatusText = GetSlackStatus().Item1;

                        if (slackStatusText.Contains("On PTO"))
                        {
                            // My Slack status says I'm on already PTO. Whatever set my status to PTO should have set the
                            // expiration for that status.
                        }
                        else
                        {
                            WriteToLog("    Is -NOT- PTO");
                            long slackStatusExpiration = 0;

                            // I'm not on PTO
                            if (DateTime.Now >= myAppointmentItem.Start)
                            {
                                // It's time to start my PTO!
                                slackStatusText = "On PTO ";
                                if (myAppointmentItem.End.Date == DateTime.Today ||
                                    myAppointmentItem.End == DateTime.Today.AddDays(1))
                                {
                                    // PTO ends sometime today or at midnight tomorrow
                                    slackStatusText      += "today";
                                    slackStatusExpiration = ConvertDateTimeToUnixTimeSeconds(myAppointmentItem.End);
                                }
                                else
                                {
                                    // If PTO does not end at midnight, then myAppointmentItem.End
                                    // is the day we're returning to work. If PTO ends at midnight,
                                    // then myAppointmentItem.End is the day AFTER our PTO ends,
                                    // and we should calculate the next working day.
                                    var nextWorkingDay = (myAppointmentItem.End.ToString("HHmmss") != "000000")
                                        ? myAppointmentItem.End
                                        : AddBusinessDays(myAppointmentItem.End.AddMinutes(-1), -1);
                                    var dateFormat = (nextWorkingDay.Date - DateTime.Today).TotalDays < 7
                                        ? "dddd"
                                        : "dddd, MMM d";
                                    slackStatusText      += "until " + nextWorkingDay.ToString(dateFormat);
                                    slackStatusExpiration = ConvertDateTimeToUnixTimeSeconds(nextWorkingDay);
                                }

                                SetSlackStatus(new SlackStatus
                                {
                                    Text       = slackStatusText,
                                    Emoji      = _config.OnVacation.Emoji,
                                    Expiration = slackStatusExpiration
                                });
                            }
                        }
                    }
                    else
                    {
                        WriteToLog("    Is not my PTO");

                        // For this appointment/meeting, we want to change Slack status if
                        //   - Meeting is starting now or has already started
                        //   - I am not free (ASSUMES that if I add a meeting to my calendar and status is Free,
                        //     then I want to be available by Slack)
                        var newStatus = new SlackStatus
                        {
                            Text       = _config.InMeeting.Text,
                            Emoji      = _config.InMeeting.Emoji,
                            Expiration = ConvertDateTimeToUnixTimeSeconds(myAppointmentItem.End)
                        };
                        SetSlackStatus(newStatus);
                    }
                }
            }
        }