Merge pull request #7 from PSC-F/main

#贡献 C#/.NET Core 版本  dotNetCore.cs
This commit is contained in:
Easy 2021-06-23 10:54:40 +08:00 committed by GitHub
commit f583ad3815
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 125 additions and 0 deletions

View File

@ -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<dynamic>(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。 其他版本的函数可参照上边的逻辑自行编写欢迎PR。

58
dotNetCore.cs Normal file
View File

@ -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<dynamic>(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③"
));
}
}
}