C# 企業微信消息推送對接,實現天氣預報推送


C# 企業微信消息推送對接,實現天氣預報推送

迷戀自留地

准備工作

需要獲取的東西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)
        {
            // 獲取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";
        }
    }

直接調用

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);
                            }                         
                        }                                       
				

效果

我的公眾號


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM