private static void TimeTracker_LoadAccounts() { dctAccounts = new Dictionary <string, TimeTrackerAuthentication>(); System.IO.IsolatedStorage.IsolatedStorageFile isoStore; isoStore = System.IO.IsolatedStorage.IsolatedStorageFile.GetStore(System.IO.IsolatedStorage.IsolatedStorageScope.User | System.IO.IsolatedStorage.IsolatedStorageScope.Assembly, null, null); System.IO.IsolatedStorage.IsolatedStorageFileStream fsSFS; if (!isoStore.FileExists("settings.txt")) { return; } fsSFS = new System.IO.IsolatedStorage.IsolatedStorageFileStream("settings.txt", System.IO.FileMode.Open, isoStore); System.IO.StreamReader srSFS = new System.IO.StreamReader(fsSFS); String strFile = srSFS.ReadToEnd(); String[] arrLines = strFile.Split(new char[] { '\r', '\n' }); foreach (String strLine in arrLines) { if (strLine.Trim().Length == 0) { continue; } String[] arrAccount = strLine.Split('|'); TimeTrackerAuthentication auth = new TimeTrackerAuthentication(); auth.EmailAddress = arrAccount[1]; auth.Token = arrAccount[2]; dctAccounts.Add(arrAccount[0], auth); } srSFS.Close(); srSFS.Dispose(); srSFS = null; fsSFS.Close(); fsSFS.Dispose(); fsSFS = null; }
public static String TimeTracker_InOUT(TimeTrackerAuthentication auth, String strCommand) { try { String strURL = TIME_TRACKER_API_ENDPOINT + "?" + "email=" + System.Web.HttpUtility.UrlEncode(auth.EmailAddress) + "&token=" + System.Web.HttpUtility.UrlEncode(auth.Token) + "&command=" + System.Web.HttpUtility.UrlEncode(strCommand); System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new Uri(strURL)); httpWebRequest.Method = "GET"; String strResponse; System.Net.HttpWebResponse httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse(); using (System.IO.StreamReader streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream())) { strResponse = streamReader.ReadToEnd(); } dynamic Response = System.Web.Helpers.Json.Decode(strResponse); if (!Slack.Utility.TryGetProperty(Response, "success", false)) { return(Slack.Utility.TryGetProperty(Response, "reason")); } return(Slack.Utility.TryGetProperty(Response, "information") + " [" + strCommand + "]"); } catch (Exception ex) { throw new Exception("Could not get connection info.", ex); } }
private static void Silence(String strUserName, String strChannel) { TimeTrackerAuthentication auth; if (dctAccounts.ContainsKey(strUserName)) { auth = dctAccounts[strUserName]; } else { auth = new TimeTrackerAuthentication(); dctAccounts.Add(strUserName, auth); } auth.Silence = !auth.Silence; dctAccounts[strUserName] = auth; TimeTracker_SaveAccounts(); Slack.Chat.PostMessageArguments args = new Slack.Chat.PostMessageArguments(); args.channel = strChannel; if (auth.Silence) { args.text = "I won't bother you until you unsilence me."; } else { args.text = "I will now prompt you regarding time tracking."; } client.Chat.PostMessage(args); }
public static String TimeTracker_UserInactive(String strUserName, String strRealName) { try { if (!dctAccounts.ContainsKey(strUserName)) { return(""); } TimeTrackerAuthentication auth = new TimeTrackerAuthentication(); auth = dctAccounts[strUserName]; if (auth.Silence) { return(""); } String strURL = TIME_TRACKER_API_ENDPOINT + "?" + "email=" + System.Web.HttpUtility.UrlEncode(auth.EmailAddress) + "&token=" + System.Web.HttpUtility.UrlEncode(auth.Token) + "&command=current"; System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new Uri(strURL)); httpWebRequest.Method = "GET"; String strResponse = ""; System.Net.HttpWebResponse httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse(); using (System.IO.StreamReader streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream())) { strResponse = streamReader.ReadToEnd(); } String strOUT = ""; dynamic Response = System.Web.Helpers.Json.Decode(strResponse); if (Slack.Utility.TryGetProperty(Response, "success", false)) { if (Slack.Utility.TryGetProperty(Response, "clockedIn", false)) { strOUT += "You are currently clocked in to " + Slack.Utility.TryGetProperty(Response, "project") + "\r\n" + Slack.Utility.TryGetProperty(Response, "start") + "\t" + Slack.Utility.TryGetProperty(Response, "end") + "\t" + "Hours: " + (Math.Round(((Double)Slack.Utility.TryGetProperty(Response, "hours")), 2).ToString()).PadLeft(5, '0') + "\r\n" + "Did you forget to clock out?\r\n"; } else { strOUT = ""; } } else { strOUT = "Could not get current time information.\r\n" + Slack.Utility.TryGetProperty(Response, "reason"); } return(strOUT); } catch (Exception ex) { throw new Exception("Could not handle user active event.", ex); } }
public static String TimeTracker_Today(TimeTrackerAuthentication auth) { try { String strURL = TIME_TRACKER_API_ENDPOINT + "?" + "email=" + System.Web.HttpUtility.UrlEncode(auth.EmailAddress) + "&token=" + System.Web.HttpUtility.UrlEncode(auth.Token) + "&command=today"; System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new Uri(strURL)); httpWebRequest.Method = "GET"; String strResponse = ""; System.Net.HttpWebResponse httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse(); using (System.IO.StreamReader streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream())) { strResponse = streamReader.ReadToEnd(); } String strOUT = ""; dynamic Response = System.Web.Helpers.Json.Decode(strResponse); if (Slack.Utility.TryGetProperty(Response, "success", false)) { strOUT = "Today's Date: " + Slack.Utility.TryGetProperty(Response, "date") + "\r\n"; if (Slack.Utility.HasProperty(Response, "records")) { foreach (dynamic record in Response.records) { strOUT += Slack.Utility.TryGetProperty(record, "start") + "\t"; if (((String)Slack.Utility.TryGetProperty(record, "end")).Trim().Length == 0) { strOUT += "\t\t\t"; } else { strOUT += Slack.Utility.TryGetProperty(record, "end") + "\t"; } strOUT += "Hours: " + (Math.Round(((Double)Slack.Utility.TryGetProperty(record, "hours")), 2).ToString()).PadLeft(5, ' ') + "\t" + Slack.Utility.TryGetProperty(record, "project") + "\r\n"; } } strOUT += "Total Hours: " + Math.Round(((Double)Slack.Utility.TryGetProperty(Response, "totalHours")), 2).ToString(); } else { strOUT = "Could not get today's time information.\r\n" + Slack.Utility.TryGetProperty(Response, "reason"); } return(strOUT); } catch (Exception ex) { throw new Exception("Could get today's time information.", ex); } }
public static String TimeTracker_Project(TimeTrackerAuthentication auth, String strProject) { try { String strURL = TIME_TRACKER_API_ENDPOINT + "?" + "email=" + System.Web.HttpUtility.UrlEncode(auth.EmailAddress) + "&token=" + System.Web.HttpUtility.UrlEncode(auth.Token) + "&command=project" + "&project=" + strProject; System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new Uri(strURL)); httpWebRequest.Method = "GET"; String strResponse = ""; System.Net.HttpWebResponse httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse(); using (System.IO.StreamReader streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream())) { strResponse = streamReader.ReadToEnd(); } String strOUT = ""; dynamic Response = System.Web.Helpers.Json.Decode(strResponse); if (Slack.Utility.TryGetProperty(Response, "success", false)) { strOUT += "Project: " + Slack.Utility.TryGetProperty(Response, "project") + "\r\n" + "As of: " + Slack.Utility.TryGetProperty(Response, "asOf") + "\r\n" + "Month Start Day: " + Slack.Utility.TryGetProperty(Response, "monthStartDay") + "\r\n" + "Min. Hours: " + Slack.Utility.TryGetProperty(Response, "minHours") + "\r\n" + "Max. Hours: " + Slack.Utility.TryGetProperty(Response, "maxHours") + "\r\n" + "Current Hours: " + (Math.Round(((Double)Slack.Utility.TryGetProperty(Response, "currentHours")), 2).ToString()).PadLeft(5, '0') + "\r\n"; } else { strOUT = "Could not get current project information.\r\n" + Slack.Utility.TryGetProperty(Response, "reason"); } return(strOUT); } catch (Exception ex) { throw new Exception("Could not get current project information.", ex); } }
private static TimeTrackerAuthentication Associate(String strUserName, String strChannel, String[] arrParams) { TimeTrackerAuthentication auth = new TimeTrackerAuthentication(); if (arrParams.Length < 5) { Send_Help(strChannel, "Invalid syntax: "); return(null); } if (arrParams[1] == "email") { auth.EmailAddress = arrParams[2]; } else { auth.Token = arrParams[2]; } if (arrParams[3] == "email") { auth.EmailAddress = arrParams[4]; } else { auth.Token = arrParams[4]; } if (!dctAccounts.ContainsKey(strUserName)) { dctAccounts.Add(strUserName, auth); } else { dctAccounts[strUserName] = auth; } TimeTracker_SaveAccounts(); Slack.Chat.PostMessageArguments args3 = new Slack.Chat.PostMessageArguments(); args3.channel = strChannel; args3.text = "Your time tracker account has been associated with your slack account."; client.Chat.PostMessage(args3); return(auth); }
public static String TimeTracker_MyTime(TimeTrackerAuthentication auth) { try { String strURL = TIME_TRACKER_API_ENDPOINT + "?" + "email=" + System.Web.HttpUtility.UrlEncode(auth.EmailAddress) + "&token=" + System.Web.HttpUtility.UrlEncode(auth.Token) + "&command=myTime"; System.Net.HttpWebRequest httpWebRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(new Uri(strURL)); httpWebRequest.Method = "GET"; String strResponse = ""; System.Net.HttpWebResponse httpResponse = (System.Net.HttpWebResponse)httpWebRequest.GetResponse(); using (System.IO.StreamReader streamReader = new System.IO.StreamReader(httpResponse.GetResponseStream())) { strResponse = streamReader.ReadToEnd(); } String strOUT = ""; dynamic Response = System.Web.Helpers.Json.Decode(strResponse); if (Slack.Utility.TryGetProperty(Response, "success", false)) { strOUT += "Hours Today: " + Math.Round(((Double)Slack.Utility.TryGetProperty(Response, "todayHours")), 2).ToString() + "\r\n" + "Hours This Week: " + Math.Round(((Double)Slack.Utility.TryGetProperty(Response, "weekHours")), 2).ToString() + "\r\n" + "Hours This Pay Period: " + Math.Round(((Double)Slack.Utility.TryGetProperty(Response, "payPeriodHours")), 2).ToString() + "\r\n"; } else { strOUT = "Could not get current project information.\r\n" + Slack.Utility.TryGetProperty(Response, "reason"); } return(strOUT); } catch (Exception ex) { throw new Exception("Could not get current project information.", ex); } }
private static bool TimeTracker(String strUserName, String strChannel, String strCommand) { if (!strCommand.StartsWith("tt")) { return(false); } strCommand = strCommand.ToLower(); strCommand = strCommand.Substring(2).Trim(new char[] { ' ', '/' }); String[] arrParams = strCommand.Split(new char[] { '/', ':' }); TimeTrackerAuthentication auth = new TimeTrackerAuthentication(); if (!dctAccounts.ContainsKey(strUserName)) { if (arrParams[0].Trim() == "associate") { auth = Associate(strUserName, strChannel, arrParams); } else if (arrParams[0].Trim() == "?") { Send_Help(strChannel, "Available commands:"); } else { Slack.Chat.PostMessageArguments args2 = new Slack.Chat.PostMessageArguments(); args2.channel = strChannel; args2.text = "Your slack account is not associated with a time tracker account"; client.Chat.PostMessage(args2); } return(true); } auth = dctAccounts[strUserName]; Slack.Chat.PostMessageArguments args = new Slack.Chat.PostMessageArguments(); args.channel = strChannel; switch (arrParams[0].Trim()) { case "?": Send_Help(strChannel, "Available commands:"); return(true); case "associate": auth = Associate(strUserName, strChannel, arrParams); return(true); case "current": args.text = TimeTracker_Current(auth); break; case "mytime": args.text = TimeTracker_MyTime(auth); break; case "project": if (arrParams.Length == 1) { Send_Help(strChannel, "Invalid syntax:"); return(false); } args.text = TimeTracker_Project(auth, arrParams[1].Trim()); break; case "today": args.text = TimeTracker_Today(auth); break; default: args.text = TimeTracker_InOUT(auth, strCommand); break; } client.Chat.PostMessage(args); return(true); }
private static void client_PresenceChanged(Slack.PresenceChangeEventArgs e) { Console.Write(System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "\tPresence Changed.\t["); if (e.UserInfo == null) { Console.Write("Not Found (" + e.user + ")"); } else { Console.Write(e.UserInfo.name); } Console.WriteLine("] [" + e.presence + "]"); String strChannel = client.IM.Open(e.user).ChannelID; if (strChannel.Length == 0) { return; } String strMessage = ""; switch (e.presence) { case "active": if (!dctAccounts.ContainsKey(e.UserInfo.name)) { strMessage = "Welcome " + e.UserInfo.real_name + "\nWould you like to associate your account with a Time Tracker account?\nFor help type tt /?)"; } else { TimeTrackerAuthentication auth = dctAccounts[e.UserInfo.name]; if (auth.Silence) { return; } if (dctAccounts[e.UserInfo.name].Token.Length == 0) { strMessage = "Welcome " + e.UserInfo.real_name + "\nWould you like to associate your account with a Time Tracker account?\nFor help type tt /?)"; } else { strMessage = TimeTracker_UserActive(e.UserInfo.name, e.UserInfo.real_name); } } break; case "away": if (dctAccounts.ContainsKey(e.UserInfo.name)) { strMessage = TimeTracker_UserInactive(e.UserInfo.name, e.UserInfo.real_name); } break; default: // do nothing break; } if (strMessage.Length == 0) { return; } Slack.Chat.PostMessageArguments args = new Slack.Chat.PostMessageArguments(); args.channel = strChannel; args.text = strMessage; client.Chat.PostMessage(args); }