钉钉消息通知到企业个人
一、创建小程序
- 登录钉钉开放平台钉钉开放平台
- 在应用开发->企业内部开发->小程序->创建应用
3.打开刚刚创建的小程序,配置IP白名单等相应的信息,特别要配置的是"添加接口权限",和"选择权限范围"
二、在线调试
打开在线调试地址,在最上方选择企业内部应用,找到消息通知->普通消息,就可以发送消息给企业内部人员了。
发送消息前,您需要获得该应用的access_token和员工ID
三、代码实现
1、 在创建的小程序中找到这三个值赋值 $appkey , a p p s e c r e t , appsecret , appsecret,agentId
2、 调用方法 get_by_mobile() 通过手机号获取userid
3、 调用sendMsg()发送消息。
userid 填写在sendMsg()方法的 userid_list字段,多个用户以逗号分隔,内容自定义
<?php
include "./sdk/TopSdk.php";
date_default_timezone_set('Asia/Shanghai');
/*
* 使用钉钉发送消息给个人
*/
class Index
{
protected $appkey = '';
protected $appsecret = '';
protected $agentId = ;
public $access_token = '';
public function __construct()
{
$this->access_token = $this->get_access_token();
}
/*
* 获取 access_token
*/
public function get_access_token()
{
$path = './data/access_token.txt';
if (file_exists($path) && filemtime($path) + 3600 > time()) {
return file_get_contents('./data/access_token.txt');
}
$url = "https://oapi.dingtalk.com/gettoken?appkey={$this->appkey}&appsecret={$this->appsecret}";
$info = json_decode(file_get_contents($url), true);
if ($info) {
file_put_contents($path, $info['access_token']);
return $info['access_token'];
}
return '';
}
public function getuserinfo()
{
$url = "https://oapi.dingtalk.com/user/getuserinfo?access_token={$this->access_token}&code=$this->code";
$info = json_decode(file_get_contents($url), true);
return $info;
}
/*
* 根据手机号获取userid
*/
public function get_by_mobile()
{
$url = "https://oapi.dingtalk.com/user/get_by_mobile?access_token={$this->access_token}&mobile={$_GET['mobile']}";
$info = json_decode(file_get_contents($url), true);
return $info;
}
/*
* 获取用户详情 - 通过用户ID
*/
public function get_user_info($user_id)
{
$url = "https://oapi.dingtalk.com/user/get?access_token={$this->access_token}&userid={$user_id}";
$info = json_decode(file_get_contents($url), true);
return $info;
}
/*
* 发送消息
*/
public function sendMsg()
{
$url = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2?access_token={$this->access_token}";
$time = date('m/d H:i');
$context = [
'agent_id' => $this->agentId,
'userid_list' => '04203525061283395',
'msg' => json_encode([
'msgtype' => 'markdown',
'markdown' => [
"title" => "业务通知",
"text" => "### 通知测试 \n\n > 有 1 笔新订单成交,请处理。\n\n\n {$time}"
],
])
];
$info = $this->send_post($url, $context);
return json_decode($info, true);
}
/**
* 发送POST请示
* @param $url 地址
* @param $data 数据
* @return bool|string
*/
function send_post($url, $data)
{
//初使化init方法
$ch = curl_init();
//指定URL
curl_setopt($ch, CURLOPT_URL, $url);
//设定请求后返回结果
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//声明使用POST方式来进行发送
curl_setopt($ch, CURLOPT_POST, 1);
//发送什么数据呢
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//忽略证书
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//忽略header头信息
curl_setopt($ch, CURLOPT_HEADER, 0);
//设置超时时间
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
//发送请求
$output = curl_exec($ch);
//关闭curl
curl_close($ch);
//返回数据
return $output;
}
}
// 实际调用
$c = new Index();
$c->sendMsg();
最终效果
完