| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- namespace App\Notification;
- class DingDingRobot
- {
- private $accessToken;
- private $secret;
- public function __construct($accessToken, $secret)
- {
- $this->accessToken = $accessToken;
- $this->secret = $secret;
- }
- const URL = 'https://oapi.dingtalk.com/robot/send';
- public function markdown($title, $text)
- {
- return TelegramBot::getDefault()->sendMsg($title."\n".$text);
- $now = time()*1000;
- $sign = $this->sign($now);
- $url = self::URL."?access_token={$this->accessToken}×tamp={$now}&sign={$sign}";
- $data = [
- 'msgtype' => 'markdown',
- 'markdown' => [
- 'title' => $title,
- 'text' => $text,
- ]
- ];
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array ('Content-Type: application/json;charset=utf-8'));
- curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
- 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;
- }
- private function sign($now)
- {
- $secret = $this->secret;
- $string = "$now\n$secret";
- $sign = urlencode(base64_encode(hash_hmac('sha256', $string, $secret, true)));
- return $sign;
- }
- }
|