Skip to content

Commit

Permalink
添加短信验证码提取逻辑,自动添加验证码至转发标题
Browse files Browse the repository at this point in the history
  • Loading branch information
lkiuyu committed Sep 28, 2023
1 parent a891ba6 commit f235e38
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 24 deletions.
115 changes: 115 additions & 0 deletions DbusTest/ProcessSmsContent/GetSmsContentCode.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
17 changes: 10 additions & 7 deletions DbusTest/ProcessUserSend/ProcessSend.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using DbusSmsForward.SendMethod;
using DbusSmsForward.SMSModel;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -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)
Expand Down
11 changes: 6 additions & 5 deletions DbusTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using DbusSmsForward.SendMethod;
using DbusSmsForward.ProcessUserChoise;
using DbusSmsForward.ProcessUserSend;
using DbusSmsForward.SMSModel;

string startGuideChoiseNum = "";
string sendMethodGuideChoiseNum = "";
Expand Down Expand Up @@ -66,17 +67,17 @@ await imsg.WatchAddedAsync(
{
Console.WriteLine(change.path);
var isms = connection.CreateProxy<ISms>("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);
}
}
);
Expand Down
15 changes: 15 additions & 0 deletions DbusTest/SMSModel/SmsCodeModel.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
15 changes: 15 additions & 0 deletions DbusTest/SMSModel/SmsContentModel.cs
Original file line number Diff line number Diff line change
@@ -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; }
}
}
7 changes: 5 additions & 2 deletions DbusTest/SendMethod/SendByBark.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using DbusSmsForward.Helper;
using DbusSmsForward.ProcessSmsContent;
using DbusSmsForward.SMSModel;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -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转发成功");
}
Expand Down
7 changes: 5 additions & 2 deletions DbusTest/SendMethod/SendByDingTalkBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Text.Encodings.Web;
using System.Text;
using System.Web;
using DbusSmsForward.SMSModel;
using DbusSmsForward.ProcessSmsContent;

namespace DbusSmsForward.SendMethod
{
Expand All @@ -31,20 +33,21 @@ 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);
url += $"&timestamp={timestamp}&sign={sign}";

JObject msgContent = new()
{
{ "content", body }
{ "content", (string.IsNullOrEmpty(SmsCodeStr) ? "" : SmsCodeStr + "\n") + "短信转发\n" + body }
};

JObject msgObj = new()
Expand Down
7 changes: 5 additions & 2 deletions DbusTest/SendMethod/SendByEmail.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Net.Mail;
using System.Net;
using System.Configuration;
using DbusSmsForward.SMSModel;
using DbusSmsForward.ProcessSmsContent;

namespace DbusSmsForward.SendMethod
{
Expand Down Expand Up @@ -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"];
Expand All @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions DbusTest/SendMethod/SendByPushPlus.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using DbusSmsForward.Helper;
using DbusSmsForward.ProcessSmsContent;
using DbusSmsForward.SMSModel;
using Newtonsoft.Json.Linq;
using System.Configuration;

Expand All @@ -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);
Expand Down
7 changes: 5 additions & 2 deletions DbusTest/SendMethod/SendByTelegramBot.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using DbusSmsForward.Helper;
using DbusSmsForward.ProcessSmsContent;
using DbusSmsForward.SMSModel;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
Expand Down Expand Up @@ -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")
Expand All @@ -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();
Expand Down
Loading

0 comments on commit f235e38

Please sign in to comment.