public CSResult SendMessage(string toPhone, string msg, bool allowResend, bool cleanLog) { string SMSSentFile = Path.Combine(LogPath, "SMSSent.log"); string SendHash = toPhone + "[" + msg + "]"; if (!NeededToSend(SMSSentFile, SendHash, allowResend, cleanLog)) { return(new CSResult(false, "Same message to same phone recently", toPhone, SendHash)); } var accountSid = _config["Twilio:AccountSID"]; var authToken = _config["Twilio:AuthToken"]; TwilioClient.Init(accountSid, authToken); var msgResponse = MessageResource.Create( body: msg, from: new Twilio.Types.PhoneNumber(_config["Twilio:PhoneNum"]), to: new Twilio.Types.PhoneNumber(toPhone) ); CSResult csRes = new CSResult(); if (msgResponse.ErrorCode == null) { //successfully queued csRes.Succeeded = true; csRes.RefStr = toPhone; csRes.RespStr = "Successfully Sent"; csRes.SendHash = SendHash; using (StreamWriter sw = new StreamWriter(SMSSentFile, true)) { sw.WriteLine(PropMgr.ESTNowStr + "~" + SendHash); sw.Close(); } } else { //Twilio sent an error back string SMSFailedFile = Path.Combine(LogPath, "SMSFailed.log"); csRes.Succeeded = false; csRes.RefStr = toPhone; csRes.RespStr = msgResponse.ErrorCode + "[" + PropMgr.ESTNowStr + "]:" + msgResponse.ErrorMessage; using (StreamWriter sw = new StreamWriter(SMSFailedFile, true)) { sw.WriteLine(PropMgr.ESTNowStr + "~" + SendHash + "~" + csRes.RespStr); sw.Close(); } } return(csRes); }
public CSResult NotifyUser(string username, NotifyType nt, string msg, bool allowResend, bool cleanLog) { var Settings = CSSettings.GetCSSettings(_config, _userManager); var u = Settings.GetUser(username); if ((u == null) || (u.PhoneNum.Length < 7)) { var csRes = new CSResult(); csRes.Set(false, ((u == null) ? "No user found" : "No phone # found."), nt.ToString() + ">" + username, ""); return(csRes); } return(SendMessage(u.PhoneNum, msg, allowResend, cleanLog)); }
public CSResult NotifyUsers(NotifyType nt, string msg, bool allowResend, bool cleanLog) { var Settings = CSSettings.GetCSSettings(_config, _userManager); List <CSUser> nUsers; switch (nt) { case NotifyType.EquipNT: nUsers = Settings.UserSettings.Where(u => u.SendEquipText && (u.PhoneNum.Length > 6)).ToList(); break; case NotifyType.StockNT: nUsers = Settings.UserSettings.Where(u => u.SendStockText && (u.PhoneNum.Length > 6)).ToList(); break; case NotifyType.TaskNT: nUsers = Settings.UserSettings.Where(u => u.SendTaskText && (u.PhoneNum.Length > 6)).ToList(); break; default: nUsers = new List <CSUser>(); break; } int sentCount = 0; foreach (var u in nUsers) { CSResult csRes = NotifyUser(u.EMail, nt, msg, allowResend, cleanLog); if (csRes.Succeeded) { ++sentCount; } cleanLog = false; } return(new CSResult(sentCount == nUsers.Count, "NotifyUsers", msg, "All Users")); }