diff --git a/DbusTest/ProcessSmsContent/GetSmsContentCode.cs b/DbusTest/ProcessSmsContent/GetSmsContentCode.cs new file mode 100644 index 0000000..5b9afe5 --- /dev/null +++ b/DbusTest/ProcessSmsContent/GetSmsContentCode.cs @@ -0,0 +1,115 @@ +using DbusSmsForward.SMSModel; +using Microsoft.VisualBasic; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace DbusSmsForward.ProcessSmsContent +{ + public static class GetSmsContentCode + { + public static string GetSmsCodeStr(string smscontent) + { + SmsCodeModel smsCodeModel = new SmsCodeModel(); + smsCodeModel = GetSmsCodeModel(smscontent); + if (smsCodeModel.HasCode) + { + return smsCodeModel.CodeFrom + smsCodeModel.CodeValue; + } + else + { + return ""; + } + } + public static SmsCodeModel GetSmsCodeModel(string smscontent) + { + SmsCodeModel smsCodeModel = new SmsCodeModel(); + if (JudgeSmsContentHasCode(smscontent)) + { + string smscode= GetCode(smscontent).Trim(); + if (string.IsNullOrEmpty(smscode)) + { + smsCodeModel.HasCode = false; + } + else + { + smsCodeModel.HasCode = true; + smsCodeModel.CodeValue = smscode; + smsCodeModel.CodeFrom = GetCodeSmsFrom(smscontent); + } + + } + else + { + smsCodeModel.HasCode = false; + } + return smsCodeModel; + } + public static bool JudgeSmsContentHasCode(string smscontent) + { + string[] flagStrList = { "验证码", "verification", "code", "인증", "代码" }; + foreach (string flag in flagStrList) + { + if (smscontent.IndexOf(flag) > -1) + { + return true; + } + } + return false; + } + public static string GetCode(string smscontent) + { + string pattern = @"(?<=\b[\p{L}\p{N}]{0,1000})[A-Za-z0-9]{4,7}\b"; + MatchCollection SmsCodeMatches = Regex.Matches(smscontent, pattern); + if (SmsCodeMatches.Count()> 1) + { + int maxDigits = 0; + string maxDigitsString = ""; + foreach (Match match in SmsCodeMatches) + { + string currentString = match.Value; + int digitCount = CountDigits(currentString); + if (digitCount > maxDigits) + { + maxDigits = digitCount; + maxDigitsString = currentString; + } + } + return maxDigitsString; + } + else if (SmsCodeMatches.Count()==1) + { + return SmsCodeMatches[0].Value; + } + else + { + return ""; + } + } + public static string GetCodeSmsFrom(string smscontent) + { + string pattern = @"^\【(.*?)\】"; + Match match = Regex.Match(smscontent, pattern); + if (match.Success) + { + return match.Value; + } + return ""; + } + public static int CountDigits(string input) + { + int count = 0; + foreach (char c in input) + { + if (char.IsDigit(c)) + { + count++; + } + } + return count; + } + } +} diff --git a/DbusTest/ProcessUserSend/ProcessSend.cs b/DbusTest/ProcessUserSend/ProcessSend.cs index 591133b..db8b4fe 100644 --- a/DbusTest/ProcessUserSend/ProcessSend.cs +++ b/DbusTest/ProcessUserSend/ProcessSend.cs @@ -1,4 +1,5 @@ using DbusSmsForward.SendMethod; +using DbusSmsForward.SMSModel; using System; using System.Collections.Generic; using System.Linq; @@ -9,33 +10,35 @@ namespace DbusSmsForward.ProcessUserSend { public class ProcessSend { - public static void sendSms(string sendMethodGuideResult,string tel,string body) + public static void sendSms(string sendMethodGuideResult,SmsContentModel smsmodel) { + string body = "发信电话:" + smsmodel.TelNumber + "\n" + "时间:" + smsmodel.SmsDate + "\n" + "短信内容:" + smsmodel.SmsContent; + Console.WriteLine(body); try { if (sendMethodGuideResult == "1") { - SendByEmail.SendSms(tel, body); + SendByEmail.SendSms(smsmodel, body); } if (sendMethodGuideResult == "2") { - SendByPushPlus.SendSms(tel, body); + SendByPushPlus.SendSms(smsmodel, body); } if (sendMethodGuideResult == "3") { - SendByWeComApplication.SendSms(tel, body); + SendByWeComApplication.SendSms(smsmodel, body); } if (sendMethodGuideResult == "4") { - SendByTelegramBot.SendSms(tel, body); + SendByTelegramBot.SendSms(smsmodel, body); } if(sendMethodGuideResult == "5") { - SendByDingTalkBot.SendSms(tel, body); + SendByDingTalkBot.SendSms(smsmodel, body); } if (sendMethodGuideResult == "6") { - SendByBark.SendSms(tel, body); + SendByBark.SendSms(smsmodel, body); } } catch(Exception e) diff --git a/DbusTest/Program.cs b/DbusTest/Program.cs index a21d257..e894af7 100644 --- a/DbusTest/Program.cs +++ b/DbusTest/Program.cs @@ -3,6 +3,7 @@ using DbusSmsForward.SendMethod; using DbusSmsForward.ProcessUserChoise; using DbusSmsForward.ProcessUserSend; +using DbusSmsForward.SMSModel; string startGuideChoiseNum = ""; string sendMethodGuideChoiseNum = ""; @@ -66,17 +67,17 @@ await imsg.WatchAddedAsync( { Console.WriteLine(change.path); var isms = connection.CreateProxy("org.freedesktop.ModemManager1", change.path); - string tel = await isms.GetNumberAsync(); - string stime = (await isms.GetTimestampAsync()).Replace("T", " ").Replace("+08:00", " "); + SmsContentModel smsmodel = new SmsContentModel(); + smsmodel.TelNumber = await isms.GetNumberAsync(); + smsmodel.SmsDate = (await isms.GetTimestampAsync()).Replace("T", " ").Replace("+08:00", " "); string smscontent = ""; do { smscontent = ""; smscontent = await isms.GetTextAsync(); } while (string.IsNullOrEmpty(smscontent)); - string body = "发信电话:" + tel + "\n" + "时间:" + stime + "\n" + "短信内容:" + smscontent; - Console.WriteLine(body); - ProcessSend.sendSms(sendMethodGuideResult, tel, body); + smsmodel.SmsContent = smscontent; + ProcessSend.sendSms(sendMethodGuideResult, smsmodel); } } ); diff --git a/DbusTest/SMSModel/SmsCodeModel.cs b/DbusTest/SMSModel/SmsCodeModel.cs new file mode 100644 index 0000000..75e5f9b --- /dev/null +++ b/DbusTest/SMSModel/SmsCodeModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DbusSmsForward.SMSModel +{ + public class SmsCodeModel + { + public bool HasCode { get; set; } + public string CodeFrom { get; set; } + public string CodeValue { get; set; } + } +} diff --git a/DbusTest/SMSModel/SmsContentModel.cs b/DbusTest/SMSModel/SmsContentModel.cs new file mode 100644 index 0000000..af19420 --- /dev/null +++ b/DbusTest/SMSModel/SmsContentModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DbusSmsForward.SMSModel +{ + public class SmsContentModel + { + public string TelNumber { get; set; } + public string SmsContent { get; set; } + public string SmsDate { get; set; } + } +} diff --git a/DbusTest/SendMethod/SendByBark.cs b/DbusTest/SendMethod/SendByBark.cs index 698c988..2c47820 100644 --- a/DbusTest/SendMethod/SendByBark.cs +++ b/DbusTest/SendMethod/SendByBark.cs @@ -1,4 +1,6 @@ using DbusSmsForward.Helper; +using DbusSmsForward.ProcessSmsContent; +using DbusSmsForward.SMSModel; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; @@ -29,16 +31,17 @@ public static void SetupBarkInfo() } } - public static void SendSms(string number, string body) + public static void SendSms(SmsContentModel smsmodel, string body) { try { ConfigurationManager.RefreshSection("appSettings"); string BarkUrl = ConfigurationManager.AppSettings["BarkUrl"]; string BrakKey = ConfigurationManager.AppSettings["BrakKey"]; + string SmsCodeStr = GetSmsContentCode.GetSmsCodeStr(smsmodel.SmsContent); string url = BarkUrl + "/" + BrakKey + "/"; url += System.Web.HttpUtility.UrlEncode(body); - url += "?group=" + number + "&title=短信转发" + number; + url += "?group=" + smsmodel.TelNumber + "&title="+ (string.IsNullOrEmpty(SmsCodeStr) ? "" : SmsCodeStr + " ") + "短信转发" + smsmodel.TelNumber; string msgresult = HttpHelper.HttpGet(url); Console.WriteLine("Bark转发成功"); } diff --git a/DbusTest/SendMethod/SendByDingTalkBot.cs b/DbusTest/SendMethod/SendByDingTalkBot.cs index b3e8aff..5ac7a1c 100644 --- a/DbusTest/SendMethod/SendByDingTalkBot.cs +++ b/DbusTest/SendMethod/SendByDingTalkBot.cs @@ -6,6 +6,8 @@ using System.Text.Encodings.Web; using System.Text; using System.Web; +using DbusSmsForward.SMSModel; +using DbusSmsForward.ProcessSmsContent; namespace DbusSmsForward.SendMethod { @@ -31,12 +33,13 @@ public static void SetupDingtalkBotMsg() } } - public static void SendSms(string number, string body) + public static void SendSms(SmsContentModel smsmodel, string body) { ConfigurationManager.RefreshSection("appSettings"); string dingTalkAccessToken = ConfigurationManager.AppSettings["DingTalkAccessToken"]; string dingTalkSecret = ConfigurationManager.AppSettings["DingTalkSecret"]; string url = DING_TALK_BOT_URL + dingTalkAccessToken; + string SmsCodeStr = GetSmsContentCode.GetSmsCodeStr(smsmodel.SmsContent); long timestamp = ConvertDateTimeToInt(DateTime.Now); string sign = addSign(timestamp, dingTalkSecret); @@ -44,7 +47,7 @@ public static void SendSms(string number, string body) JObject msgContent = new() { - { "content", body } + { "content", (string.IsNullOrEmpty(SmsCodeStr) ? "" : SmsCodeStr + "\n") + "短信转发\n" + body } }; JObject msgObj = new() diff --git a/DbusTest/SendMethod/SendByEmail.cs b/DbusTest/SendMethod/SendByEmail.cs index 4aeadb2..d46b50b 100644 --- a/DbusTest/SendMethod/SendByEmail.cs +++ b/DbusTest/SendMethod/SendByEmail.cs @@ -1,6 +1,8 @@ using System.Net.Mail; using System.Net; using System.Configuration; +using DbusSmsForward.SMSModel; +using DbusSmsForward.ProcessSmsContent; namespace DbusSmsForward.SendMethod { @@ -42,7 +44,7 @@ public static void SetupEmailInfo() } - public static void SendSms(string number,string body) + public static void SendSms(SmsContentModel smsmodel, string body) { ConfigurationManager.RefreshSection("appSettings"); string smtpHost = ConfigurationManager.AppSettings["smtpHost"]; @@ -57,7 +59,8 @@ public static void SendSms(string number,string body) SmtpClient sc = new SmtpClient(smtpHost); try { - mm.Subject = "短信转发" + number; + string SmsCodeStr = GetSmsContentCode.GetSmsCodeStr(smsmodel.SmsContent); + mm.Subject = (string.IsNullOrEmpty(SmsCodeStr)?"": SmsCodeStr + " ") + "短信转发" + smsmodel.TelNumber; mm.Body = body; sc.DeliveryMethod = SmtpDeliveryMethod.Network; sc.Credentials = new NetworkCredential(sendEmial, emailKey); diff --git a/DbusTest/SendMethod/SendByPushPlus.cs b/DbusTest/SendMethod/SendByPushPlus.cs index 3b05399..276f7b0 100644 --- a/DbusTest/SendMethod/SendByPushPlus.cs +++ b/DbusTest/SendMethod/SendByPushPlus.cs @@ -1,4 +1,6 @@ using DbusSmsForward.Helper; +using DbusSmsForward.ProcessSmsContent; +using DbusSmsForward.SMSModel; using Newtonsoft.Json.Linq; using System.Configuration; @@ -19,14 +21,16 @@ public static void SetupPushPlusInfo() } } - public static void SendSms(string number, string body) + public static void SendSms(SmsContentModel smsmodel, string body) { ConfigurationManager.RefreshSection("appSettings"); string pushPlusToken = ConfigurationManager.AppSettings["pushPlusToken"]; string pushPlusUrl = "http://www.pushplus.plus/send/"; + string SmsCodeStr = GetSmsContentCode.GetSmsCodeStr(smsmodel.SmsContent); + JObject obj = new JObject(); obj.Add("token", pushPlusToken); - obj.Add("title", "短信转发"+ number); + obj.Add("title", (string.IsNullOrEmpty(SmsCodeStr) ? "" : SmsCodeStr+" ") + "短信转发" + smsmodel.TelNumber); obj.Add("content", body); obj.Add("topic",""); string msgresult = HttpHelper.Post(pushPlusUrl, obj); diff --git a/DbusTest/SendMethod/SendByTelegramBot.cs b/DbusTest/SendMethod/SendByTelegramBot.cs index 9d3d05f..e845ac2 100644 --- a/DbusTest/SendMethod/SendByTelegramBot.cs +++ b/DbusTest/SendMethod/SendByTelegramBot.cs @@ -1,4 +1,6 @@ using DbusSmsForward.Helper; +using DbusSmsForward.ProcessSmsContent; +using DbusSmsForward.SMSModel; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; @@ -52,13 +54,14 @@ public static void SetupTGBotInfo() } } - public static void SendSms(string number, string body) + public static void SendSms(SmsContentModel smsmodel, string body) { ConfigurationManager.RefreshSection("appSettings"); string TGBotToken = ConfigurationManager.AppSettings["TGBotToken"]; string TGBotChatID = ConfigurationManager.AppSettings["TGBotChatID"]; string IsEnableCustomTGBotApi = ConfigurationManager.AppSettings["IsEnableCustomTGBotApi"]; string CustomTGBotApi = ConfigurationManager.AppSettings["CustomTGBotApi"]; + string SmsCodeStr = GetSmsContentCode.GetSmsCodeStr(smsmodel.SmsContent); string url = ""; if (IsEnableCustomTGBotApi=="true") @@ -70,7 +73,7 @@ public static void SendSms(string number, string body) url = "https://api.telegram.org"; } url+= "/bot" + TGBotToken + "/sendMessage?chat_id=" + TGBotChatID + "&text="; - url += System.Web.HttpUtility.UrlEncode(body); + url += System.Web.HttpUtility.UrlEncode((string.IsNullOrEmpty(SmsCodeStr) ? "" : SmsCodeStr + "\n") + "短信转发\n" + body); string msgresult = HttpHelper.HttpGet(url); JObject jsonObjresult = JObject.Parse(msgresult); string status = jsonObjresult["ok"].ToString(); diff --git a/DbusTest/SendMethod/SendByWeComApplication.cs b/DbusTest/SendMethod/SendByWeComApplication.cs index 72b17bb..17dfb7c 100644 --- a/DbusTest/SendMethod/SendByWeComApplication.cs +++ b/DbusTest/SendMethod/SendByWeComApplication.cs @@ -1,4 +1,6 @@ using DbusSmsForward.Helper; +using DbusSmsForward.ProcessSmsContent; +using DbusSmsForward.SMSModel; using Newtonsoft.Json.Linq; using System.Configuration; @@ -30,7 +32,7 @@ public static void SetupWeComInfo() } } - public static void SendSms(string number, string body) + public static void SendSms(SmsContentModel smsmodel, string body) { ConfigurationManager.RefreshSection("appSettings"); string corpid = ConfigurationManager.AppSettings["WeChatQYID"]; @@ -41,6 +43,7 @@ public static void SendSms(string number, string body) JObject jsonObj = JObject.Parse(result); string errcode = jsonObj["errcode"].ToString(); string errmsg = jsonObj["errmsg"].ToString(); + string SmsCodeStr = GetSmsContentCode.GetSmsCodeStr(smsmodel.SmsContent); if (errcode == "0" && errmsg == "ok") { string access_token = jsonObj["access_token"].ToString(); @@ -51,7 +54,7 @@ public static void SendSms(string number, string body) obj.Add("totag", ""); obj.Add("msgtype", "text"); obj.Add("agentid", agentid); - obj1.Add("content", "短信转发\n" + body); + obj1.Add("content", (string.IsNullOrEmpty(SmsCodeStr) ? "" : SmsCodeStr+"\n")+"短信转发\n" + body); obj.Add("text", obj1); obj.Add("safe", 0); obj.Add("enable_id_trans", 0); diff --git "a/musl\345\217\221\345\270\203/DbusSmsForward" "b/musl\345\217\221\345\270\203/DbusSmsForward" index 6842813..290d2df 100644 Binary files "a/musl\345\217\221\345\270\203/DbusSmsForward" and "b/musl\345\217\221\345\270\203/DbusSmsForward" differ diff --git "a/\345\217\221\345\270\203/DbusSmsForward" "b/\345\217\221\345\270\203/DbusSmsForward" index 9afed89..4940af8 100644 Binary files "a/\345\217\221\345\270\203/DbusSmsForward" and "b/\345\217\221\345\270\203/DbusSmsForward" differ