哨兵系统 ---钉钉群消息推送
前言:
之前哨兵系统提醒只是在前台使用声音提醒,现在把消息推送到值班人员群里。避免值班人员在前台,专注写代码。忽略了铃声,还有一点铃声确实有点打扰其他同事工作。所以下一个版本哨兵系统,的确闹铃可以完全取消,替换成群消息提醒。
下面是钉钉群消息推送的代码
目录
页面调用代码
//声音提醒 这段代码放在lable框的内容改变事件里
try
{
System.Media.SoundPlayer sndPlayer = new System.Media.SoundPlayer(Application.StartupPath + @"/pm3.wav");
if (label_similarity.Text == "Stranger(陌生人)")
{
sndPlayer.PlayLooping();
//创建群发送消息
AccessToken.SendMsg(textBox1.Text,textBox2.Text);
}
else
{
sndPlayer.Stop();
}
业务实现代码
发送群消息类
/**
* Namespace: 钉钉消息Demo
*
* Function: 获取接口全局ID
* Name: AccessToken
*
* Ver Time Author
* 0.10 2020/12/2 11:23:43 康世行
*
*/
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.Net;
namespace WinForm_IPC_FaceRecognition_Demo
{
public class AccessToken
{
string getchatid = "";//群会话id(系统停止之后消失)
/// <summary>
/// 获取接口全局ID
/// </summary>
/// <returns></returns>
public string accessToken() {
string corpid = "钉钉企业id";
string CorpSecret = "钉钉企业秘钥";
string AccessToke = "";
string url = string.Format("https://oapi.dingtalk.com/gettoken?appkey={0}&appsecret={1}", corpid, CorpSecret);
JToken json = JToken.Parse(HttpUtiy.HttpGet(url));
AccessToke = json["access_token"].ToString();
return AccessToke;
}
//获取部门列表(白名单问题)
public string det_id() {
string url = string.Format("https://oapi.dingtalk.com/department/list?access_token={0}", accessToken());
string id = HttpUtiy.HttpGet(url);
return id;
}
//根据手机号获取用户UserID (没有获取接口的权限,无法获取到UserID)
private string UserID(string phone) {
// 通过手机号获取 userId
string url = string.Format("https://oapi.dingtalk.com/user/get_by_mobile?access_token={0}&mobile={1}",accessToken(), phone);
JToken json = JToken.Parse(HttpUtiy.HttpGet(url));
string userid = json["userid"].ToString();
return userid;
}
/// <summary>
/// 创建群回话
/// </summary>
/// <param name="access_token">调用接口凭证</param>
/// <param name="name">群名称</param>
/// <param name="owner">群主userid</param>
/// <param name="useridlist">群成员</param>
/// <returns></returns>
private string Creatchatid(string Chatidname,string Chatidowner,string[] Chatiduseridlist
) {
string chat_id = "";//群回话id
string MessageUrl =string.Format( "https://oapi.dingtalk.com/chat/create?access_token={0}",accessToken());
var json_req = new
{
name =Chatidname, // 群名称
owner = Chatidowner, // 群主id
useridlist=Chatiduseridlist
};
string jsonRequest = JsonConvert.SerializeObject(json_req);//将对象转换为json
JToken json = JToken.Parse(HttpUtiy.HttpPost(MessageUrl, jsonRequest));//返回值转换为JToken格式
chat_id = json["chatid"].ToString();
return chat_id;
}
/// <summary>
/// 发送群消息
/// </summary>
/// <param name="phone1">第一个值班员手机号</param>
/// <param name="phone2">第二个值班员手机号</param>
public void SendMsg(string phone1,string phone2) {
string userid =UserID(phone1);//获取用户userId(第一个人作为群主)
string userid2 = UserID(phone2);
//判断系统当前是否已经创建群回话
if (getchatid.Trim() == "") {
getchatid= Creatchatid("哨兵系统" + DateTime.Now.ToString("ddd") + "值班组", userid, new string[] { userid,userid2 });//创建群回话,获取群回话id
}
string MessageUrl =string.Format("https://oapi.dingtalk.com/chat/send?access_token={0}",accessToken());
var json_req = new
{
chatid = getchatid,//回去群会话id
msg=new {
msgtype="text",
text = new {
content = "有陌生人闯入,请及时查看!"
}
}
};
string jsonRequest = JsonConvert.SerializeObject(json_req);//将对象转换为json
HttpUtiy.HttpPost(MessageUrl, jsonRequest);
}
}
}
http接口访问类
/**
* Namespace: 钉钉消息Demo
*
* Function: http接口请求
* Name: HttpUtiy
*
* Ver Time Author
* 0.10 2020/12/2 11:35:51 康世行
*
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace WinForm_IPC_FaceRecognition_Demo
{
public class HttpUtiy
{
/// <summary>
/// 发送GET请求
/// </summary>
/// <param name="url">请求URL,如果需要传参,在URL末尾加上“?+参数名=参数值”即可</param>
/// <returns></returns>
public static string HttpGet(string url)
{
//创建
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
//设置请求方法
httpWebRequest.Method = "GET";
//请求超时时间
httpWebRequest.Timeout = 20000;
//发送请求
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
//利用Stream流读取返回数据
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
//获得最终数据,一般是json
string responseContent = streamReader.ReadToEnd();
streamReader.Close();
httpWebResponse.Close();
return responseContent;
}
/// <summary>
/// 发送POST请求
/// </summary>
/// <param name="url">请求URL</param>
/// <param name="data">请求参数</param>
/// <returns></returns>
public static string HttpPost(string url, string data)
{
//创建http请求
HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
//字符串转换为字节码
byte[] bs = Encoding.UTF8.GetBytes(data);
//参数类型,这里是json类型
//还有别的类型如"application/x-www-form-urlencoded",不过我没用过(逃
httpWebRequest.ContentType = "application/json";
//参数数据长度
httpWebRequest.ContentLength = bs.Length;
//设置请求类型
httpWebRequest.Method = "POST";
//设置超时时间
httpWebRequest.Timeout = 20000;
//将参数写入请求地址中
httpWebRequest.GetRequestStream().Write(bs, 0, bs.Length);
//发送请求
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
//读取返回数据
StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream(), Encoding.UTF8);
string responseContent = streamReader.ReadToEnd();
streamReader.Close();
httpWebResponse.Close();
httpWebRequest.Abort();
return responseContent;
}
}
}
欢迎访问csdn的博客:
https://blog.csdn.net/kangshihang1998?spm=1010.2135.3001.5343