准备工作
需要获取的东西1. 企业Id
,2.应用secret 和 应用ID
获取企业id
注册完成后,在我的企业=》企业信息=》最下面企业id
获取应用secret 和 应用ID
发送微信消息
class WeCom{public static string weComCId = "ww2b b0bf8";//企业Id①public static string weComSecret = "EbKnQqG2y1qAVNL42 6 E"; //应用secret②public static string weComAId = "100 "; //应用ID③public static string weComTouId = "@all";/// <summary>/// 发送微信通知/// </summary>/// <param name="text">消息</param>/// <returns></returns>public string SendToWeCom(string text){// 获取Tokenstring 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";}}
直接调用
new WeCom().SendToWeCom("你好");
就会发送消息
实现天气的推送
主要是天气接口的获取,还有就是定时任务任务 可以通过 .NET Core 基于 IHostedService 实现后台定时任务或者 QuartzJob(目前用的这个)
var RESULT= DoRequest.SendRequest_Get("http://t.weather.itboy.net/api/weather/city/101020100");var Jo = JObject.Parse(RESULT);if (Jo["status"].ToString()=="200"){var AA = Jo["data"]["forecast"].ToString().JsonToList<Forecast>();var entity= AA.Where(x => x.ymd == DateTime.Now.ToString("yyyy-MM-dd")).FirstOrDefault();if (entity != null){var str = $" ---上海天气--- \n" +$" 时间: { entity.ymd} \n" +$" 农历: { CnCanlendar_nongli.GetChineseDateTime(DateTime.Now)} \n" +$" 星期: { entity.week} \n" +$" 天气: { entity.type} \n" +$" 风速: { entity.fl} + { entity.fx} \n" +$" 提示: { entity.notice} ";new WeCom().SendToWeCom(str);} }