其实解决问题的方法有很多,对于定时推送的功能来说,.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";
}
再加上数据库作业,可以实现很多你想要的功能。