钉钉(DingTalk)是阿里巴巴集团专为中国企业打造的免费沟通和协同的多端平台,提供PC版,Web版,Mac版和手机版,支持手机和电脑间文件互传。钉钉因中国企业而生,帮助中国企业通过系统化的解决方案(微应用),全方位提升中国企业沟通和协同效率。(来自百度百科)
本文档主要介绍后台服务器通过钉钉自定义机器人向钉钉群定时发送信息
备注:另外还有一种功能是在钉钉群@钉钉机器人+字符串可以通过钉钉机器人发送信息到服务器,服务器收到顶顶群发送的信息后进行处理再通过钉钉机器人回复信息来完成信息的交互
一、创建群自定义钉钉机器人
1.打开钉钉群设置,找到智能群助手
2.选择添加机器人,自定义机器人
3.设置机器人姓名,设置安全模式
安全模式分为三种:
①自定义关键词,含义当后台给钉钉机器人发送消息时,消息文本内容需要包含关键词中的内容才能发送成功。
②加签,含义根据官方文档给的是把timestamp+"\n"+密钥当做签名字符串,使用HmacSHA256算法计算签名,然后进行Base64 encode,最后再把签名参数再进行urlEncode,得到最终的签名(需要使用UTF-8字符集)
③IP地址段,分析应该是填写公网IP
4.完成之后会产生webhook地址,即后台发送信息的接口地址,牢记接口地址,谨防泄露
二、PHP对接钉钉机器人接口发送信息
1.主服务页面,主要内容:PHP连接mysql数据库,时间判断,定时刷新页面,页面效果如下图所示,数据库连接成功,当前服务器时间,页面每秒刷新一次用来判断时间匹配定时(具体到秒)跳转钉钉信息发送页面。
2.主服务页面代码
官方给的文档里面有curl命令发送信息,可以在Linux系统上做下测试
curl 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxx'\
-H 'Content-Type: application/json' \
-d '{"msgtype": "text","text": {"content":"我就是我, 是不一样的烟火"}}'
<?php
//链接数据库配置开始
//start--------------------------------------------------------------------------
$mysql_server_name = ''; //改成自己的mysql数据库服务器
$mysql_username = ''; //改成自己的mysql数据库用户名
$mysql_password = ''; //改成自己的mysql数据库密码
$mysql_database = ''; //改成自己的mysql数据库名
$conn=mysqli_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database); //连接数据库
//连接数据库错误提示, '数据库连接:SUCCESS'
if (mysqli_connect_errno($conn)) {
die("连接 MySQL 失败: " . mysqli_connect_error());
}else{
echo("<b>"."数据库连接:SUCCESS!"."</b>");
echo "<br/>";
echo "<b>"."当前系统时间为:".date('Y-m-d H:i:s', time())."</b>";
}
mysqli_query($conn,"set names utf8mb64"); //数据库编码格式
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>
//获取系统时间
var myDate = new Date();
//console.log(myDate.getHours());
var LocalTime = myDate.toLocaleTimeString();//获取当前时间
var dt= myDate.toLocaleString(); //获取日期与时间
var hh = myDate.getHours();//获取小时数
var ms = myDate.getMinutes(); //获取当前分钟数(0-59)
var ss = myDate.getSeconds(); //获取当前秒数(0-59)
var sss = myDate.getTime();
console.log(dt);//时
//console.log(sss);//时
console.log(hh);//分
console.log(ms);//秒
console.log(ss);
//延时加载,可以避免重复跳转页面,延迟3秒钟
var timer_reload = setTimeout(function(){
//window.location.reload();
//window.history.back(-1);
},3000);
//每1s刷新页面用来监控条件语句的执行,即定时跳转页面
var timer_reload = setInterval(function(){
console.log("di 1 ci shu chu");
window.location.reload();
},1000);
//时间判断语句,条件完成跳转页面test3.php
//时间9点-10点
if(hh == 10){ //小时判断
if(ms == 0){ //分钟判断
if(ss == 40){ //秒判断
//console.log(ms);
//console.log(hh);
var timer_reload = function(){
window.location.reload();
console.log("di 2 ci shu chu");
window.location.href="http://10.16.20.13:8088/dingding_rebot.php";//跳转test3.php 发送钉钉消息
};
timer_reload();//执行函数
}
}
}
//时间10点-11点
if(hh == 11){
if(ms == 26){
if(ss == 30){
console.log(ms);
console.log(hh);
var timer_reload = function(){
window.location.reload();
console.log("di 2 ci shu chu");
window.location.href="http://10.16.20.13:8088/dingding_rebot.php";
};
timer_reload();
}
}
}
//时间11点-12点
if(hh == 12){
if(ms == 0){
if(ss == 40){
console.log(ms);
console.log(hh);
var timer_reload = function(){
window.location.reload();
console.log("di 2 ci shu chu");
window.location.href="http://10.16.20.13:8088/dingding_rebot.php";
};
timer_reload();
}
}
}
//时间12点-13点
if(hh == 13){
if(ms == 0){
if(ss == 40){
console.log(ms);
console.log(hh);
var timer_reload = function(){
window.location.reload();
console.log("di 2 ci shu chu");
window.location.href="http://10.16.20.13:8088/dingding_rebot.php";
};
timer_reload();
}
}
}
//时间13点-14点
if(hh == 14){
if(ms == 0){
if(ss == 40){
console.log(ms);
console.log(hh);
var timer_reload = function(){
window.location.reload();
console.log("di 2 ci shu chu");
window.location.href="http://10.16.20.13:8088/dingding_rebot.php";
};
timer_reload();
}
}
}
//时间14点-15点
if(hh == 15){
if(ms == 0){
if(ss == 40){
console.log(ms);
console.log(hh);
var timer_reload = function(){
window.location.reload();
console.log("di 2 ci shu chu");
window.location.href="http://10.16.20.13:8088/dingding_rebot.php";
};
timer_reload();
}
}
}
//时间15点-16点
if(hh == 16){
if(ms == 0){
if(ss == 40){
console.log(ms);
console.log(hh);
var timer_reload = function(){
window.location.reload();
console.log("di 2 ci shu chu");
window.location.href="http://10.16.20.13:8088/dingding_rebot.php";
};
timer_reload();
}
}
}
//时间16点-17点
if(hh == 17){
if(ms == 0){
if(ss == 40){
console.log(ms);
console.log(hh);
var timer_reload = function(){
window.location.reload();
console.log("di 2 ci shu chu");
window.location.href="http://10.16.20.13:8088/dingding_rebot.php";
};
timer_reload();
}
}
}
//时间17点-18点
if(hh == 18){
if(ms == 0){
if(ss == 40){
console.log(ms);
console.log(hh);
var timer_reload = function(){
window.location.reload();
console.log("di 2 ci shu chu");
window.location.href="http://10.16.20.13:8088/dingding_rebot.php";
};
timer_reload();
}
}
}
//时间18点-19点
if(hh == 19){
if(ms == 0){
if(ss == 40){
console.log(ms);
console.log(hh);
var timer_reload = function(){
window.location.reload();
console.log("di 2 ci shu chu");
window.location.href="http://10.16.20.13:8088/dingding_rebot.php";
};
timer_reload();
}
}
}
//时间19点-20点
if(hh == 20){
if(ms == 0){
if(ss == 40){
console.log(ms);
console.log(hh);
var timer_reload = function(){
window.location.reload();
console.log("di 2 ci shu chu");
window.location.href="http://10.16.20.13:8088/dingding_rebot.php";
};
timer_reload();
}
}
}
//时间20点-21点
if(hh == 21){
if(ms == 0){
if(ss == 40){
//console.log(ms);
//console.log(hh);
var timer_reload = function(){
//window.location.reload();
//console.log("di 2 ci shu chu");
window.location.href="http://10.16.20.13:8088/dingding_rebot.php";
};
timer_reload();
}
}
}
</script>
</body>
</html>
2.链接数据库代码
<?php
//链接数据库配置开始
//start--------------------------------------------------------------------------
$mysql_server_name = ''; //改成自己的mysql数据库服务器
$mysql_username = ''; //改成自己的mysql数据库用户名
$mysql_password = ''; //改成自己的mysql数据库密码
$mysql_database = ''; //改成自己的mysql数据库名
$conn=mysqli_connect($mysql_server_name,$mysql_username,$mysql_password,$mysql_database); //连接数据库
//连接数据库错误提示
if (mysqli_connect_errno($conn)) {
die("连接 MySQL 失败: " . mysqli_connect_error());
}else{
echo("<b>"."数据库连接:SUCCESS!------成功跳转页面"."</b>");
echo "<br/>";
}
//获取当前系统时间
//echo date('Y-m-d H:i:s', time())."</br>"; //2021-10-09 20:02:54
//echo date('H', time())."</br>"; //2021-10-09 20:02:54
//当天开始时间
$start_time=strtotime(date("Y-m-d",time()));
//当天结束之间
$end_time=$start_time+60*60*24-1;
//echo $start_time."</br>"; //1633708800
//echo $end_time."</br>"; //1633795199
$start_time_1=date("Y-m-d H:i:s",$start_time);
$end_time=date("Y-m-d H:i:s",$end_time);
//当前小时整点时间的前一秒
$day_hour = date('H', time());
$day_hour_start = date("Y-m-d H:i:s",$start_time+($day_hour-1)*3600);//2021-10-10 11:00:00
$day_hour_pre = date("Y-m-d H:i:s",$start_time+$day_hour*3600-1);//2021-10-10 11:59:59
//$day_hour_pre = date("Y-m-d H:i:s",$start_time+$day_hour*3600-1);
echo $day_hour_start;
echo $start_time_1."</br>";
echo $day_hour_pre;
//echo "</br>".$start_time."</br>"; //2021-10-09 00:00:00
//echo $end_time; //2021-10-09 23:59:59
//查询语句,时段人数查询
$sql_select_day = "select wocc.parttimes,sum(wocc.groupnum) peoples from v_WorkOffCard_Cursor wocc where 1=1 and wocc.ADDTIME >='".$day_hour_start."' and wocc.ADDTIME <='".$day_hour_pre."' group by wocc.parttimes order by wocc.parttimes";
echo "</br>".$sql_select_day;
//查询语句,总人数查询
$sql_select_count= "select sum(wocc.groupnum) peoples from v_WorkOffCard_Cursor wocc where 1=1 and wocc.ADDTIME >='".$start_time_1."' and wocc.ADDTIME <='".$day_hour_pre."'";
echo "</br>".$sql_select_count."</br>";
//查询出园人数
$sql_select_out = "select leaver_num from t_out_peoples top order by currentTime desc limit 1";
mysqli_query($conn,"set names utf8mb64"); //数据库编码格式
//$sql = "select msgtype,'content' => 'dingding' text from digdinng_test";
//$query = mysqli_query($conn,$sql);
//$sql2 = "select distinct encode,left(decode,17) as decode from `encode_decode` where encode = '".$encode."'and '".$encode."' is not null order by datetime desc";
//$re_query = mysqli_query($conn,$sql2);
//$re_query_id = mysqli_fetch_array(mysqli_query($conn, $sql));
//$sql = "select count(*) peoples from digdinng_test";
//$sql_select_day
$results = mysqli_query($conn, $sql_select_day);
while ($rows = mysqli_fetch_assoc($results)) {
$res[] = $rows; //时段
}
//$sql_select_count
$results_count = mysqli_query($conn, $sql_select_count);
while ($rows = mysqli_fetch_assoc($results_count)) {
$res_count[] = $rows; //人数
}
//$sql_select_out
$results_out = mysqli_query($conn, $sql_select_out);
while ($rows = mysqli_fetch_assoc($results_out)) {
$res_out[] = $rows; //出园
}
//结果
//print_r($res_count);
//时段入园人数(10:00-21:00)
//时间段
$times_curr_start_end = $res[0][parttimes];//时段
echo "时段:".$times_curr_start_end."</br>";
//与时间段相对应的人数
$peo_curr_start_end = $res_count[0][peoples];//时段人数
echo "</br>"."入园人数:".$peo_curr_start_end;
//echo $times_20_21."入园人数为:".$peo_20_21."人"."</br>";
//echo $times_10_11."入园人数为:".$peo_10_11."人"."</br>";
//入园总人数
$times_day_count = $res_count[0][peoples];
//echo "今日入园总人数:".$times_day_count."人"."</br>";
//echo "<br/>";
//$msgtype = $res[0][peoples];
//$content = '总数量为:'.$msgtype;
//var_dump($content);
//出园总人数
$times_out = $res_out[0][leaver_num];
//在园人数
$times_in_park = $times_day_count - $times_out;
3.钉钉发送信息页面
<?php
include 'test_mysql_check.php';
//以下是钉钉推送消息配置
//start-----------------------------------------------------------------------
//
//webhook:https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxx
echo "</br>"."跳转钉钉发送程序成功!3秒后返回!";
$url = 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxx';
/*
// 第一步,把timestamp+"\n"+密钥当做签名字符串,使用HmacSHA256算法计算签名,然后进行Base64 encode,最后再把签名参数再进行urlEncode,得到最终的签名(需要使用UTF-8字符集)。
$time = time() *1000;//毫秒级时间戳,我这里为了方便,直接把时间*1000了
$secret = '这是密钥';
$sign = hash_hmac('sha256', $time . "\n" . $secret,$secret,true);
$sign = base64_encode($sign);
$sign = urlencode($sign);
$url = "{$url}×tamp={$time}&sign={$sign}";
*/
//使用关键字
//类型1:文本
$msg1 = [
'msgtype' => 'text',//这是文件发送类型,可以根据需求调整
'text' => [
'content' => 'dingding',
],
];
/*
//测试短信$msg1,修改发送文本信息
//定义数量
$num = 1;
$sum[10] = $num++;
var_dump($sum);
echo "</br>";
$content = '下午好!这是测试文本,请忽略!'.$count1.$num;
//输出拼接后的content
echo $content."</br>";
$msg1[text][content]=$content;
var_dump($msg1);
echo "</br>";
*/
//获取当前小时数
$h_cuur = date('H', time());
$day_today = date('Y年m月d日', time());
$curr_today = date('Y-m-d H:i', time());
//9-10点
if($h_cuur == 10){
if($times_curr_start_end != ''){
//echo "</br>"."21212121"."</br>";
$msg1[text][content] = $day_today."\n"."当前时间:".$curr_today."\n".$times_curr_start_end."入园人数为:".$peo_curr_start_end."人"."\n"."今日总入园人数为:".$times_day_count."人"."\n"."今日总出园人数为:".$times_out."人"."\n"."当前在园人数为:".$times_in_park."人";
$curl = curl_init();
}
}
//10-11点
if($h_cuur == 11){
if($times_curr_start_end != ''){
//echo "</br>"."21212121"."</br>";
$msg1[text][content] = $day_today."\n"."当前时间:".$curr_today."\n".$times_curr_start_end."入园人数为:".$peo_curr_start_end."人"."\n"."今日总入园人数为:".$times_day_count."人"."\n"."今日总出园人数为:".$times_out."人"."\n"."当前在园人数为:".$times_in_park."人";
$curl = curl_init();
}
}
//11-12点
if($h_cuur == 12){
if($times_curr_start_end != ''){
//echo "</br>"."21212121"."</br>";
$msg1[text][content] = $day_today."\n"."当前时间:".$curr_today."\n".$times_curr_start_end."入园人数为:".$peo_curr_start_end."人"."\n"."今日总入园人数为:".$times_day_count."人"."\n"."今日总出园人数为:".$times_out."人"."\n"."当前在园人数为:".$times_in_park."人";
$curl = curl_init();
}
}
//12-13点
if($h_cuur == 13){
if($times_curr_start_end != ''){
//echo "</br>"."21212121"."</br>";
$msg1[text][content] = $day_today."\n"."当前时间:".$curr_today."\n".$times_curr_start_end."入园人数为:".$peo_curr_start_end."人"."\n"."今日总入园人数为:".$times_day_count."人"."\n"."今日总出园人数为:".$times_out."人"."\n"."当前在园人数为:".$times_in_park."人";
$curl = curl_init();
}
}
//13-14点
if($h_cuur == 14){
if($times_curr_start_end != ''){
//echo "</br>"."21212121"."</br>";
$msg1[text][content] = $day_today."\n"."当前时间:".$curr_today."\n".$times_curr_start_end."入园人数为:".$peo_curr_start_end."人"."\n"."今日总入园人数为:".$times_day_count."人"."\n"."今日总出园人数为:".$times_out."人"."\n"."当前在园人数为:".$times_in_park."人";
$curl = curl_init();
}
}
//14-15点
if($h_cuur == 15){
if($times_curr_start_end != ''){
//echo "</br>"."21212121"."</br>";
$msg1[text][content] = $day_today."\n"."当前时间:".$curr_today."\n".$times_curr_start_end."入园人数为:".$peo_curr_start_end."人"."\n"."今日总入园人数为:".$times_day_count."人"."\n"."今日总出园人数为:".$times_out."人"."\n"."当前在园人数为:".$times_in_park."人";
$curl = curl_init();
}
}
//15-16点
if($h_cuur == 16){
if($times_curr_start_end != ''){
//echo "</br>"."21212121"."</br>";
$msg1[text][content] = $day_today."\n"."当前时间:".$curr_today."\n".$times_curr_start_end."入园人数为:".$peo_curr_start_end."人"."\n"."今日总入园人数为:".$times_day_count."人"."\n"."今日总出园人数为:".$times_out."人"."\n"."当前在园人数为:".$times_in_park."人";
$curl = curl_init();
}
}
//16-17点
if($h_cuur == 17){
if($times_curr_start_end != ''){
//echo "</br>"."21212121"."</br>";
$msg1[text][content] = $day_today."\n"."当前时间:".$curr_today."\n".$times_curr_start_end."入园人数为:".$peo_curr_start_end."人"."\n"."今日总入园人数为:".$times_day_count."人"."\n"."今日总出园人数为:".$times_out."人"."\n"."当前在园人数为:".$times_in_park."人";
$curl = curl_init();
}
}
//17-18点
if($h_cuur == 18){
if($times_curr_start_end != ''){
//echo "</br>"."21212121"."</br>";
$msg1[text][content] = $day_today."\n"."当前时间:".$curr_today."\n".$times_curr_start_end."入园人数为:".$peo_curr_start_end."人"."\n"."今日总入园人数为:".$times_day_count."人"."\n"."今日总出园人数为:".$times_out."人"."\n"."当前在园人数为:".$times_in_park."人";
$curl = curl_init();
}
}
//18-19点
if($h_cuur == 19){
if($times_curr_start_end != ''){
//echo "</br>"."21212121"."</br>";
$msg1[text][content] = $day_today."\n"."当前时间:".$curr_today."\n".$times_curr_start_end."入园人数为:".$peo_curr_start_end."人"."\n"."今日总入园人数为:".$times_day_count."人"."\n"."今日总出园人数为:".$times_out."人"."\n"."当前在园人数为:".$times_in_park."人";
$curl = curl_init();
}
}
//19-20点
if($h_cuur == 20){
if($times_curr_start_end != ''){
//echo "</br>"."21212121"."</br>";
$msg1[text][content] = $day_today."\n"."当前时间:".$curr_today."\n".$times_curr_start_end."入园人数为:".$peo_curr_start_end."人"."\n"."今日总入园人数为:".$times_day_count."人"."\n"."今日总出园人数为:".$times_out."人"."\n"."当前在园人数为:".$times_in_park."人";
$curl = curl_init();
}
}
//20-21点
if($h_cuur == 21){
if($times_curr_start_end != ''){
//echo "</br>"."21212121"."</br>";
$msg1[text][content] = $day_today."\n"."当前时间:".$curr_today."\n".$times_curr_start_end."入园人数为:".$peo_curr_start_end."人"."\n"."今日总入园人数为:".$times_day_count."人"."\n"."今日总出园人数为:".$times_out."人"."\n"."当前在园人数为:".$times_in_park."人";
$curl = curl_init();
}else{
$times_curr_start_end = "20点-21点";
$peo_curr_start_end = 0;
$msg1[text][content] = $day_today."\n"."当前时间:".$curr_today."\n".$times_curr_start_end."入园人数为:".$peo_curr_start_end."人"."\n"."今日总入园人数为:".$times_day_count."人"."\n"."今日总出园人数为:".$times_out."人"."\n"."当前在园人数为:".$times_in_park."人";
$curl = curl_init();
}
}
//21-22点
if($h_cuur == 22){
if($times_curr_start_end != ''){
//echo "</br>"."21212121"."</br>";
$msg1[text][content] = $day_today."\n"."当前时间:".$curr_today."\n".$times_curr_start_end."入园人数为:".$peo_curr_start_end."人"."\n"."今日总入园人数为:".$times_day_count."人"."\n"."今日总出园人数为:".$times_out."人"."\n"."当前在园人数为:".$times_in_park."人";
$curl = curl_init();
}
}
//类型2:Markdown
$msg2 = [
"msgtype" => "markdown",
"markdown" => [
"title" => "dingding!杭州天气",
"text" => "#### 杭州天气 @13721424030 \n > 9度,西北风1级,空气良89,相对温度73%\n > ![screenshot](http://www.dianyingxiaozhen.cn/ext/images/pc_index_banner1.jpg)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n"
],
"at" => [
"atMobiles" => [
"13721424030"
],
"atUserIds" => [
"user123"
],
"isAtAll" => false
]
];
//类型3:link
$msg3 = [
"msgtype" => "link",
"link" => [
"text" => "dingding这个即将发布的新版本,创始人xx称它为红树林。而在此之前,每当面临重大升级,产品经理们都会取一个应景的代号,这一次,为什么是红树林",
"title" => "时代的火车向前开",
"picUrl" => "",
"messageUrl" => ""
]
];
//调用curl函数发送信息
//$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($msg1));
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($curl);
curl_close($curl);
echo '返回发送结果:'."</br>";
var_dump($res);
//end----------------------------------------------------------------------------------
?>
<!-- 以下是html5设置,定时设置 -->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>
//获取系统时间
var myDate = new Date();
console.log(myDate.getYear);
//3s后跳转主页面
var timer_reload = setTimeout(function(){
//window.location.reload();
//window.history.back(-1);
window.location.href="http://10.16.20.13:8088/dd_rebot_index.php";
},3000);
</script>
</body>
</html>
三、钉钉群成功定时发送信息效果
备注:后续在钉钉群@钉钉机器人+字符串可以通过钉钉机器人发送信息到服务器,服务器收到顶顶群发送的信息后进行处理再通过钉钉机器人回复信息研究中,需要用到公网,在没有公网的情况下官方文档给出了内网穿透的解决方法,研究中...