吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 3282|回复: 11
收起左侧

[其他转载] C# 企业微信消息推送对接,实现天气预报推送

[复制链接]
迷恋自留地 发表于 2021-9-25 11:45

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

效果

推荐文章:https://www.52pojie.cn/forum.php?mod=viewthread&tid=1488806

https://blog.csdn.net/qq_40732336/article/details/120469106

壁纸:

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
jamessteed + 1 + 1 谢谢@Thanks!
zyjia + 1 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| 迷恋自留地 发表于 2021-9-25 16:17
wyb1109_2008 发表于 2021-9-25 14:50
谢谢楼主分享。被楼主的壁纸深深吸引住了。

可以参考gitee上面的哦,所有的壁纸都在这里面放着
https://gitee.com/Pridejoy/Bing
整理不易
lg560852 发表于 2021-9-25 16:40
迷恋自留地 发表于 2021-9-25 16:15
推送到的就是可以到普通微信,实现要在企业微信上面打开推送到微信

但是前提是要企业微信(发送方)要加接收方的微信号好友吧?
lg560852 发表于 2021-9-25 13:20
sgh2zlx 发表于 2021-9-25 14:03
这个真是好
haile319 发表于 2021-9-25 14:17
给弄成云函数多好
wyb1109_2008 发表于 2021-9-25 14:50
谢谢楼主分享。被楼主的壁纸深深吸引住了。
 楼主| 迷恋自留地 发表于 2021-9-25 16:15

推送到的就是可以到普通微信,实现要在企业微信上面打开推送到微信
 楼主| 迷恋自留地 发表于 2021-9-25 16:18
haile319 发表于 2021-9-25 14:17
给弄成云函数多好

语言不一样,实现的方式不一样,都大差不差的
n_g 发表于 2021-9-25 19:35
感谢分享。如果要只推送给某个人应该怎么写?
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-26 22:46

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表