From 5c5604b6b8eb2693357da9181c7b86f3567753e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pisces=E2=99=93?= Date: Wed, 23 Jun 2021 02:32:19 +0800 Subject: [PATCH 1/2] Create dotNetCore.cs --- dotNetCore.cs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 dotNetCore.cs diff --git a/dotNetCore.cs b/dotNetCore.cs new file mode 100644 index 0000000..bd748ad --- /dev/null +++ b/dotNetCore.cs @@ -0,0 +1,58 @@ +using System; +using RestSharp; +using Newtonsoft.Json; +namespace WeCom.Demo +{ + class WeCom + { + public string SendToWeCom( + string text,// 推送消息 + string weComCId,// 企业Id① + string weComSecret,// 应用secret② + string weComAId,// 应用ID③ + string weComTouId = "@all") + { + // 获取Token + string getTokenUrl = $"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={weComCId}&corpsecret={weComSecret}"; + string token = JsonConvert + .DeserializeObject(new RestClient(getTokenUrl) + .Get(new RestRequest()).Content).access_token; + System.Console.WriteLine(token); + if (!String.IsNullOrWhiteSpace(token)) + { + var request = new RestRequest(); + var client = new RestClient($"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={token}"); + var data = new + { + touser = weComTouId, + agentid = weComAId, + msgtype = "text", + text = new + { + content = text + }, + duplicate_check_interval = 600 + }; + string serJson = JsonConvert.SerializeObject(data); + System.Console.WriteLine(serJson); + request.Method = Method.POST; + request.AddHeader("Accept", "application/json"); + request.Parameters.Clear(); + request.AddParameter("application/json", serJson, ParameterType.RequestBody); + return client.Execute(request).Content; + } + return "-1"; + } + static void Main(string[] args) + { // 测试 + Console.Write(new WeCom().SendToWeCom( + "msginfo", + "企业Id①" + , "应用secret②", + "应用ID③" + )); + } + + } +} + From b724460137e2244c0596ba79ec6e1807118c4e79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pisces=E2=99=93?= Date: Wed, 23 Jun 2021 02:44:22 +0800 Subject: [PATCH 2/2] Update README.md --- README.md | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/README.md b/README.md index 783005c..a1e3dda 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,73 @@ sendToWecom({ }) ``` +.NET Core 版: + +```C# +using System; +using RestSharp; +using Newtonsoft.Json; +namespace WeCom.Demo +{ + class WeCom + { + public string SendToWeCom( + string text,// 推送消息 + string weComCId,// 企业Id① + string weComSecret,// 应用secret② + string weComAId,// 应用ID③ + string weComTouId = "@all") + { + // 获取Token + string getTokenUrl = $"https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={weComCId}&corpsecret={weComSecret}"; + string token = JsonConvert + .DeserializeObject(new RestClient(getTokenUrl) + .Get(new RestRequest()).Content).access_token; + System.Console.WriteLine(token); + if (!String.IsNullOrWhiteSpace(token)) + { + var request = new RestRequest(); + var client = new RestClient($"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={token}"); + var data = new + { + touser = weComTouId, + agentid = weComAId, + msgtype = "text", + text = new + { + content = text + }, + duplicate_check_interval = 600 + }; + string serJson = JsonConvert.SerializeObject(data); + System.Console.WriteLine(serJson); + request.Method = Method.POST; + request.AddHeader("Accept", "application/json"); + request.Parameters.Clear(); + request.AddParameter("application/json", serJson, ParameterType.RequestBody); + return client.Execute(request).Content; + } + return "-1"; + } +} + + +``` +使用实例: +```C# + static void Main(string[] args) + { // 测试 + Console.Write(new WeCom().SendToWeCom( + "msginfo", + "企业Id①" + , "应用secret②", + "应用ID③" + )); + } + + } +``` + 其他版本的函数可参照上边的逻辑自行编写,欢迎PR。