PHP使用钉钉webhook发送@消息,AND 钉钉发送工作消息

2 篇文章 0 订阅

钉钉webhook发送@消息:

这个比较简单 :先添加一个机器人:,获取webhook的url,用于发送信息

开始开发:每个群每个机器人都是有唯一的access_token:

$param['message']] = "产品已经审核通过了,请查看!"';  //发送信息

$param['mobiles'] = '176****7056';   //发送手机号

$isAtAll = false; 
            $data = [
                'msgtype' => 'text',
                'text'    =>
                    ['content' => $param['message']],
                'at'      => [
                    'atMobiles' =>[$param['mobiles']],
                    'isAtAll' => $isAtAll,
                ],  
            ];

$data_string = json_encode($data);

$this->request_post_curl($config['access_path'].$config['access_token'],$data_string)

public function request_post_curl($remote_server, $post_string)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $remote_server);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
        curl_setopt($ch, CURLOPT_HTTPHEADER,
            ['Content-Type: application/json;charset=utf-8']);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        // 线下环境不用开启curl证书验证, 未调通情况可尝试添加该代码
        curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }

成功返回:"{"errcode":0,"errmsg":"ok"}"   

钉钉接收消息:  ,ok

 

钉钉发送工作消息:

你需要自己注册个企业,然后获取appKey和appSecret然后在获取access_token ,请查看:https://open-doc.dingtalk.com/microapp/serverapi2/eev437

第一步:

获取到:AgentId,AppKey,AppSecret,三个必备参数:

//获取access_token    获取access_token连接:https://oapi.dingtalk.com/gettoken

$gettoken_config = 'https://oapi.dingtalk.com/gettoken'.'?corpid='.你的AppKey.'&corpsecret='.你的AppSecret;
$access_token=$this->request_get_curl($gettoken_config); 

//获取access_token 

$access_token = json_decode($access_token['access_token ']);

第二步:先获取部门下的用户,在根据用户信息获取当前每个人的信息  :获取部门用户url:https://oapi.dingtalk.com/department/list     ,获取用户信息url:https://oapi.dingtalk.com/user/list

$user = '';
        $getDepartment_config =‘https://oapi.dingtalk.com/department/list ’.'?access_token='.$access_token;
        $getDepartment = json_decode($this->request_get_curl($getDepartment_config),true)['department'];
        foreach ($getDepartment as $v) {
            //获取用户信息
            $UserList_config =https://oapi.dingtalk.com/user/list.'?access_token='.$access_token.'&department_id='.$v['id'];
            $GetUser =  json_decode($this->request_get_curl($UserList_config),true)['userlist'];
            foreach ($GetUser as $Usermsg) {

                  $Usermsg['mobile']   = $Usermsg['userid'];
            }
        }

然后就获取到了对应用户的userid了。这个是个大数组,需要你重新获取单个然后在发送

第三部:发送信息  : 发送url:https://oapi.dingtalk.com/message/send

$SendToUser_config = https://oapi.dingtalk.com/message/send.'?access_token='.$access_token;
        $SendToUser_data   = [
                                'touser' =>$GetUserId      ,//就是你获取的userid
                                'agentid'=>你的AgentId,
                                'msgtype'=>'text',
                                'text'   =>[
                                        'content'=>'当前有用户提交信息请尽快审核音频信息!!!',
                                ],
                              ];
        $SendToUser        = (new Curl)->request_post_curl($SendToUser_config,json_encode($SendToUser_data));

 

返回状态码为 :0  ,ok 等信息    ,钉钉里是工作通知:

这里就完成了@@

 


     

public function request_get_curl($url){//用curl传参
        $ch = curl_init();    
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);//关闭ssl验证    
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);     
        curl_setopt($ch,CURLOPT_HEADER, 0);   
        $data = curl_exec($ch);
        curl_close($ch);   
        return $data;
    }

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值