sqlserver 调用接口往企业微信推送消息

其实解决问题的方法有很多,对于定时推送的功能来说,.net和java 都有自己的定时功能,但对于这些不熟悉,只熟悉sqlserver 的人来说,肯定希望从sqlserver下手。于是,我就尝试做了下,发现是可以的。

一、开启sqlserver调用接口组件

/*开启组件的配置*/
sp_configure 'show advanced options', 1;
GO
RECONFIGURE;
GO
sp_configure 'Ole Automation Procedures', 1;
GO
RECONFIGURE;
GO
EXEC sp_configure 'Ole Automation Procedures';
GO

二、调用企业微信接口

上述代码,你可以写一个存储过程,逻辑什么的随便写,什么时候调,多久调,都可以。

三、调用企业微信接口

1.获取token       

 private string GetQYAccessToken(string WechatKey, string CompanyID)
        {  
            string accessToken = string.Empty;
            string respText = string.Empty;

            //获取josn数据
            var url = $"http://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={CompanyID}&corpsecret={WechatKey}";

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            using (Stream resStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(resStream, Encoding.Default);
                respText = reader.ReadToEnd();
                resStream.Close();
            }

            try
            {
                JavaScriptSerializer Jss = new JavaScriptSerializer();
                Dictionary<string, object> respDic = (Dictionary<string, object>)Jss.DeserializeObject(respText);
                //通过键access_token获取值
                accessToken = respDic["access_token"].ToString();
            }
            catch (Exception ex)
            {
                throw new ArgumentNullException(ex.Message);
            }

            return accessToken;
        }

2.调用企业微信推送消息接口

   [HttpPost]
        public ActionResult Send(string account, string Conent, string WechatKey, string CompanyID, string ApplyId) {

            //account  企业微信账号
            //Conent  需要发送的内容
            //WechatKey 微信密匙
            //CompanyID  企业id
            //ApplyId 应用id
            var res = SendText(account, Conent, WechatKey, CompanyID, ApplyId);

                return Content(res.ToJson()); 
            
            }
           
             
        

     
        /// <summary>
        /// 推送信息
        /// </summary>
        /// <returns></returns>
        public string SendText(string account, string Conent, string WechatKey,string CompanyID, string ApplyId )
        {
            try
            {
                var accessToken = GetQYAccessToken(WechatKey, CompanyID);
                var postUrl = $"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={accessToken}";
                var param = JsonConvert.SerializeObject(new { touser = account, toparty = string.Empty, totag = string.Empty, msgtype = "text", agentid = ApplyId, text = new { content = Conent }, safe = 0 });
                var postResult = PostWebRequest(postUrl, param, Encoding.UTF8);
                return postResult;
            }
            catch (Exception e)
            {

                return e.Message;
            }
           //var qyid = "1000004";
             
           
        
        }

再加上数据库作业,可以实现很多你想要的功能。

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用Python编写代码,通过企业微信机器人将MySQL数据库中的数据推送到指定的群。首先,您需要获取企业微信机器人的webhook地址,并确保您的Python环境为Python 3.9。以下是一种简单的方法: 引用:将以下代码添加到您的Python脚本中,以连接到MySQL数据库并执行查询操作: ```python import pymysql # 连接到MySQL数据库 conn = pymysql.connect(host='localhost', port=3306, user='your_username', password='your_password', db='your_database') cursor = conn.cursor() # 执行查询语句 sql = 'SELECT * FROM your_table' cursor.execute(sql) # 获取查询结果 result = cursor.fetchall() # 关闭数据库连接 cursor.close() conn.close() ``` 引用:接下来,将查询结果格式化为您想要的通知格式,并使用企业微信机器人的webhook地址将通知发送到群里。您可以使用`requests`库来发送HTTP请求,代码示例如下: ```python import requests # 将查询结果格式化为通知内容 notification = "" for row in result: notification += f"字段1: {row[0]}, 字段2: {row[1]}, 字段3: {row[2]}\n" # 构造请求的数据 data = { "msgtype": "markdown", "markdown": { "content": f"新增数据通知:\n\n{notification}" } } # 发送POST请求 api_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=cxxxxxxxxxxxxxxxx" response = requests.post(api_url, json=data) # 检查请求是否成功 if response.status_code == 200 and response.json().get("errcode") == 0: print("数据推送成功!") else: print("数据推送失败!") ``` 请记得替换代码中的数据库连接信息、查询语句、字段名以及企业微信机器人的webhook地址。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值