├── .gitignore ├── src ├── SendClient.php ├── Messages │ ├── Text.php │ ├── Markdown.php │ ├── Link.php │ ├── Message.php │ ├── FeedCard.php │ └── ActionCard.php ├── helpers.php ├── DingNoticeServiceProvider.php ├── HttpClient.php ├── DingTalk.php └── DingTalkService.php ├── .travis.yml ├── config └── ding.php ├── phpunit.xml ├── composer.json ├── tests ├── Feature │ ├── TextTest.php │ ├── LinkTest.php │ ├── MarkdownTest.php │ ├── ActionTest.php │ └── FeedTest.php └── TestCase.php └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | /vendor 3 | .php_cs.cache 4 | composer.lock -------------------------------------------------------------------------------- /src/SendClient.php: -------------------------------------------------------------------------------- 1 | message = [ 10 | 'msgtype' => 'text', 11 | 'text' => [ 12 | 'content' => $content 13 | ] 14 | ]; 15 | } 16 | 17 | } -------------------------------------------------------------------------------- /src/Messages/Markdown.php: -------------------------------------------------------------------------------- 1 | setMessage($title,$markdown); 10 | } 11 | 12 | public function setMessage($title,$markdown){ 13 | $this->message = [ 14 | 'msgtype' => 'markdown', 15 | 'markdown' => [ 16 | 'title' => $title, 17 | 'text' => $markdown 18 | ] 19 | ]; 20 | } 21 | 22 | } -------------------------------------------------------------------------------- /src/helpers.php: -------------------------------------------------------------------------------- 1 | with($robot)->text($arguments[0]); 23 | } 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /src/Messages/Link.php: -------------------------------------------------------------------------------- 1 | setMessage($title,$text,$messageUrl,$picUrl); 11 | } 12 | 13 | public function setMessage($title,$text,$messageUrl,$picUrl = ''){ 14 | $this->message = [ 15 | 'msgtype' => 'link', 16 | 'link' => [ 17 | 'text' => $text, 18 | 'title' => $title, 19 | 'picUrl' => $picUrl, 20 | 'messageUrl' => $messageUrl 21 | ] 22 | ]; 23 | } 24 | } -------------------------------------------------------------------------------- /config/ding.php: -------------------------------------------------------------------------------- 1 | [ 8 | // 是否要开启机器人,关闭则不再发送消息 9 | 'enabled' => env('DING_ENABLED',true), 10 | // 机器人的access_token 11 | 'token' => env('DING_TOKEN',''), 12 | // 钉钉请求的超时时间 13 | 'timeout' => env('DING_TIME_OUT',2.0), 14 | 'ssl_verify' => env('DING_SSL_VERIFY',true) 15 | ], 16 | 17 | 'other' => [ 18 | 'enabled' => env('OTHER_DING_ENABLED',true), 19 | 20 | 'token' => env('OTHER_DING_TOKEN',''), 21 | 22 | 'timeout' => env('OTHER_DING_TIME_OUT',2.0), 23 | 24 | 'ssl_verify' => env('DING_SSL_VERIFY',true) 25 | ] 26 | 27 | ]; -------------------------------------------------------------------------------- /src/Messages/Message.php: -------------------------------------------------------------------------------- 1 | message; 13 | } 14 | 15 | protected function makeAt($mobiles = [],$atAll = false){ 16 | return [ 17 | 'at' => [ 18 | 'atMobiles' => $mobiles, 19 | 'isAtAll' => $atAll 20 | ] 21 | ]; 22 | } 23 | 24 | public function sendAt($mobiles = [],$atAll = false){ 25 | $this->at = $this->makeAt($mobiles,$atAll); 26 | return $this; 27 | } 28 | 29 | public function getBody(){ 30 | 31 | if (empty($this->at)){ 32 | $this->sendAt(); 33 | } 34 | return $this->message + $this->at; 35 | } 36 | 37 | } -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 13 | ./tests/Unit 14 | 15 | 16 | ./tests/Feature 17 | 18 | 19 | 20 | 21 | ./src 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/DingNoticeServiceProvider.php: -------------------------------------------------------------------------------- 1 | publishes([ 17 | __DIR__ . '/../config/ding.php' => base_path('config/ding.php'), 18 | ]); 19 | } 20 | 21 | /** 22 | * Register services. 23 | * 24 | * @return void 25 | */ 26 | public function register() 27 | { 28 | $this->registerLaravelBindings(); 29 | } 30 | 31 | 32 | /** 33 | * Register Laravel bindings. 34 | * 35 | * @return void 36 | */ 37 | protected function registerLaravelBindings() 38 | { 39 | $this->app->singleton(DingTalk::class, function ($app) { 40 | return new DingTalk($app['config']['ding']); 41 | }); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/Messages/FeedCard.php: -------------------------------------------------------------------------------- 1 | service = $service; 14 | $this->setMessage(); 15 | 16 | } 17 | 18 | public function setMessage(){ 19 | $this->message = [ 20 | 'feedCard' => [ 21 | 'links' => [] 22 | ], 23 | 'msgtype' => 'feedCard' 24 | ]; 25 | } 26 | 27 | public function addLinks($title,$messageUrl,$picUrl){ 28 | $this->message['feedCard']['links'][] = [ 29 | 'title' => $title, 30 | 'messageURL' => $messageUrl, 31 | 'picURL' => $picUrl 32 | ]; 33 | return $this; 34 | } 35 | 36 | public function send(){ 37 | $this->service->setMessage($this); 38 | return $this->service->send(); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "wangju/ding-notice", 3 | "description": "a dingtalk robot message handle for send message", 4 | "keywords": ["laravel", "ding talk", "ding notice"], 5 | "require": { 6 | "php": ">=7.0", 7 | "guzzlehttp/guzzle": "^6.2" 8 | }, 9 | "require-dev": { 10 | "phpunit/phpunit": "^5.7", 11 | "mockery/mockery": "^1.2" 12 | }, 13 | "autoload": { 14 | "psr-4": { 15 | "DingNotice\\": "src/" 16 | }, 17 | "files": [ 18 | "src/helpers.php" 19 | ] 20 | }, 21 | "autoload-dev": { 22 | "psr-4": { 23 | "DingNotice\\Tests\\": "tests" 24 | } 25 | }, 26 | "extra": { 27 | "laravel": { 28 | "providers": [ 29 | "DingNotice\\DingNoticeServiceProvider" 30 | ] 31 | } 32 | }, 33 | "license": "MIT", 34 | "authors": [ 35 | { 36 | "name": "王举", 37 | "email": "35649084@qq.com" 38 | } 39 | ] 40 | } 41 | -------------------------------------------------------------------------------- /tests/Feature/TextTest.php: -------------------------------------------------------------------------------- 1 | setUp(); 15 | } 16 | 17 | /** 18 | * available content to set 19 | * @param $content 20 | * @return bool 21 | * @author wangju 2019-05-17 21:50 22 | */ 23 | protected function matchContent($content) 24 | { 25 | $text = $content['content']; 26 | return !empty($text); 27 | } 28 | 29 | /** 30 | * A basic test example. 31 | * 32 | * @return void 33 | */ 34 | public function testPushTextMessage() 35 | { 36 | $result =$this->ding->text("我就是我,@{$this->testUser} 是不一样的烟火"); 37 | $this->assertSame([ 38 | 'errmsg' => 'ok', 39 | 'errcode' => 0 40 | ],$result); 41 | } 42 | 43 | public function testPushTextMessageAtAllUser(){ 44 | $result =$this->ding 45 | ->at([],true) 46 | ->text("我就是我,@{$this->testUser} 是不一样的烟火"); 47 | $this->assertSame([ 48 | 'errmsg' => 'ok', 49 | 'errcode' => 0 50 | ],$result); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /tests/Feature/LinkTest.php: -------------------------------------------------------------------------------- 1 | setUp(); 20 | 21 | } 22 | 23 | /** 24 | * available content to set 25 | * @param $content 26 | * @return bool 27 | * @author wangju 2019-05-17 21:50 28 | */ 29 | protected function matchContent($content) 30 | { 31 | return $content['text'] && $content['title'] && $content['messageUrl']; 32 | } 33 | 34 | /** 35 | * A basic test example. 36 | * 37 | * @return void 38 | */ 39 | public function testPushLinkMessage() 40 | { 41 | $result = $this->ding->link($this->title,$this->text,$this->messageUrl,$this->picUrl); 42 | $this->assertSame([ 43 | 'errmsg' => 'ok', 44 | 'errcode' => 0 45 | ],$result); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/Messages/ActionCard.php: -------------------------------------------------------------------------------- 1 | service = $service; 15 | $this->setMessage($title,$markdown,$hideAvatar,$btnOrientation); 16 | } 17 | 18 | public function setMessage($title, $markdown, $hideAvatar = 0, $btnOrientation = 0){ 19 | $this->message = [ 20 | 'msgtype' => 'actionCard', 21 | 'actionCard' => [ 22 | 'title' => $title, 23 | 'text' => $markdown, 24 | 'hideAvatar' => $hideAvatar, 25 | 'btnOrientation' => $btnOrientation 26 | ] 27 | ]; 28 | } 29 | 30 | public function single($title,$url){ 31 | $this->message['actionCard']['singleTitle'] = $title; 32 | $this->message['actionCard']['singleURL'] = $url; 33 | $this->service->setMessage($this); 34 | return $this; 35 | } 36 | 37 | public function addButtons($title,$url){ 38 | $this->message['actionCard']['btns'][] = [ 39 | 'title' => $title, 40 | 'actionURL' => $url 41 | ]; 42 | return $this; 43 | } 44 | 45 | public function send(){ 46 | $this->service->setMessage($this); 47 | return $this->service->send(); 48 | } 49 | 50 | } -------------------------------------------------------------------------------- /tests/Feature/MarkdownTest.php: -------------------------------------------------------------------------------- 1 | 9度,@1825718XXXX 西北风1级,空气良89,相对温度73%\n\n ". 13 | "> ![screenshot](http://i01.lw.aliimg.com/media/lALPBbCc1ZhJGIvNAkzNBLA_1200_588.png)\n". 14 | "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) "; 15 | 16 | public function __construct($name = null, array $data = [], $dataName = '') 17 | { 18 | parent::__construct($name, $data, $dataName); 19 | $this->setUp(); 20 | } 21 | 22 | /** 23 | * available content to set 24 | * @param $content 25 | * @return bool 26 | * @author wangju 2019-05-17 21:50 27 | */ 28 | protected function matchContent($content) 29 | { 30 | return $content['title'] && $content['text']; 31 | } 32 | 33 | /** 34 | * A basic test example. 35 | * 36 | * @return void 37 | */ 38 | public function testPushMarkdownMessage() 39 | { 40 | $result =$this->ding->markdown($this->title,$this->markdown); 41 | $this->assertSame([ 42 | 'errmsg' => 'ok', 43 | 'errcode' => 0 44 | ],$result); 45 | } 46 | 47 | public function testPushMarkdownMessageAtAllUser(){ 48 | $result =$this->ding 49 | ->at([],true) 50 | ->markdown($this->title,$this->markdown); 51 | $this->assertSame([ 52 | 'errmsg' => 'ok', 53 | 'errcode' => 0 54 | ],$result); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /tests/Feature/ActionTest.php: -------------------------------------------------------------------------------- 1 | setUp(); 20 | } 21 | 22 | /** 23 | * available content to set 24 | * @param $content 25 | * @return bool 26 | * @author wangju 2019-05-17 21:50 27 | */ 28 | protected function matchContent($content) 29 | { 30 | return $content['title'] && $content['text']; 31 | } 32 | 33 | /** 34 | * A basic test example. 35 | * 36 | * @return void 37 | */ 38 | public function testPushActionSingleMessage() 39 | { 40 | 41 | $result = $this->ding 42 | ->actionCard($this->title,$this->text,1) 43 | ->single("阅读全文","https://www.dingtalk.com/") 44 | ->send(); 45 | $this->assertSame([ 46 | 'errmsg' => 'ok', 47 | 'errcode' => 0 48 | ],$result); 49 | } 50 | 51 | public function testPushActionBtnsMessageAtAllUser(){ 52 | $result = $result = $this->ding 53 | ->actionCard($this->title,$this->text,1) 54 | ->addButtons("内容不错","https://www.dingtalk.com/") 55 | ->addButtons("不感兴趣","https://www.dingtalk.com/") 56 | ->send(); 57 | $this->assertSame([ 58 | 'errmsg' => 'ok', 59 | 'errcode' => 0 60 | ],$result); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /tests/TestCase.php: -------------------------------------------------------------------------------- 1 | testUser = '18888888888'; 22 | 23 | $robot1['timeout'] = 30.0; 24 | $robot1['enabled'] = true; 25 | $robot1['token'] = $token; 26 | 27 | $config['default'] = $robot1; 28 | 29 | $this->config = $config; 30 | $this->ding = $this->mockDingClient(); 31 | } 32 | 33 | /** 34 | * mock ding client 35 | * @param null $client 36 | * @return DingTalk 37 | * @author wangju 2019-05-17 20:53 38 | */ 39 | protected function mockDingClient($client = null) 40 | { 41 | $client = \Mockery::mock(SendClient::class); 42 | $client->shouldReceive('send')->withArgs(function ($arg) { 43 | $messageType = $arg['msgtype']; 44 | 45 | if (!in_array($messageType, ['text', 'actionCard', 'feedCard', 'link', 'markdown'])) { 46 | return false; 47 | } 48 | if (!array_key_exists($messageType, $arg)) { 49 | return false; 50 | } 51 | return $this->matchContent($arg[$messageType]); 52 | })->andReturn([ 53 | 'errmsg' => 'ok', 54 | 'errcode' => 0 55 | ]); 56 | $ding = new DingTalk($this->config, $client); 57 | return $ding; 58 | } 59 | 60 | protected function matchContent($content) 61 | { 62 | return true; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /tests/Feature/FeedTest.php: -------------------------------------------------------------------------------- 1 | setUp(); 17 | } 18 | 19 | /** 20 | * available content to set 21 | * @param $content 22 | * @return bool 23 | * @author wangju 2019-05-17 21:50 24 | */ 25 | protected function matchContent($content) 26 | { 27 | if (empty($content)){ 28 | return false; 29 | } 30 | return array_reduce($content,function ($carry,$item){ 31 | if ($carry === null) return true; 32 | return $carry && $item['title'] && $item['messageURL'] && $item['picURL']; 33 | }); 34 | } 35 | 36 | /** 37 | * A basic test example. 38 | * 39 | * @return void 40 | */ 41 | public function testPushTextMessage() 42 | { 43 | $result =$this->ding->text("我就是我,@{$this->testUser} 是不一样的烟火"); 44 | $this->assertSame([ 45 | 'errmsg' => 'ok', 46 | 'errcode' => 0 47 | ],$result); 48 | } 49 | 50 | public function testPushTextMessageAtAllUser(){ 51 | $result =$this->ding 52 | ->feed() 53 | ->addLinks('时代的火车向前开',$this->messageUrl,$this->picUrl) 54 | ->addLinks('时代的火车向前开2',$this->messageUrl,$this->picUrl) 55 | ->send(); 56 | 57 | $this->assertSame([ 58 | 'errmsg' => 'ok', 59 | 'errcode' => 0 60 | ],$result); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /src/HttpClient.php: -------------------------------------------------------------------------------- 1 | config = $config; 31 | $this->setAccessToken(); 32 | $this->client = $this->createClient(); 33 | } 34 | 35 | /** 36 | * 37 | */ 38 | public function setAccessToken(){ 39 | $this->accessToken = $this->config['token']; 40 | } 41 | 42 | /** 43 | * create a guzzle client 44 | * @return Client 45 | * @author wangju 2019-05-17 20:25 46 | */ 47 | protected function createClient() 48 | { 49 | $client = new Client([ 50 | 'timeout' => $this->config['timeout'] ?? 2.0, 51 | ]); 52 | return $client; 53 | } 54 | 55 | /** 56 | * @return string 57 | */ 58 | public function getRobotUrl(){ 59 | return $this->hookUrl . "?access_token={$this->accessToken}"; 60 | } 61 | 62 | /** 63 | * send message 64 | * @param $url 65 | * @param $params 66 | * @return array 67 | * @author wangju 2019-05-17 20:48 68 | */ 69 | public function send($params): array 70 | { 71 | $request = $this->client->post($this->getRobotUrl(), [ 72 | 'body' => json_encode($params), 73 | 'headers' => [ 74 | 'Content-Type' => 'application/json', 75 | ], 76 | 'verify' => $this->config['ssl_verify'] ?? true, 77 | ]); 78 | 79 | $result = $request->getBody()->getContents(); 80 | return json_decode($result, true); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/DingTalk.php: -------------------------------------------------------------------------------- 1 | config = $config; 31 | $this->client = $client; 32 | $this->with(); 33 | } 34 | 35 | /** 36 | * @param string $robot 37 | * @return $this 38 | */ 39 | public function with($robot = 'default'){ 40 | $this->robot = $robot; 41 | $this->dingTalkService = new DingTalkService($this->config[$robot],$this->client); 42 | return $this; 43 | } 44 | 45 | 46 | /** 47 | * @param string $content 48 | * @return mixed 49 | */ 50 | public function text($content = ''){ 51 | return $this->dingTalkService 52 | ->setTextMessage($content) 53 | ->send(); 54 | } 55 | 56 | /** 57 | * @param $title 58 | * @param $text 59 | * @return mixed 60 | */ 61 | public function action($title, $text){ 62 | return $this->dingTalkService 63 | ->setActionCardMessage($title,$text); 64 | } 65 | 66 | /** 67 | * @param array $mobiles 68 | * @param bool $atAll 69 | * @return $this 70 | */ 71 | public function at($mobiles = [], $atAll = false){ 72 | $this->dingTalkService 73 | ->setAt($mobiles,$atAll); 74 | return $this; 75 | } 76 | 77 | /** 78 | * @param $title 79 | * @param $text 80 | * @param $url 81 | * @param string $picUrl 82 | * @return mixed 83 | */ 84 | public function link($title, $text, $url, $picUrl = ''){ 85 | return $this->dingTalkService 86 | ->setLinkMessage($title,$text,$url,$picUrl) 87 | ->send(); 88 | } 89 | 90 | /** 91 | * @param $title 92 | * @param $markdown 93 | * @return mixed 94 | */ 95 | public function markdown($title, $markdown){ 96 | return $this->dingTalkService 97 | ->setMarkdownMessage($title,$markdown) 98 | ->send(); 99 | } 100 | 101 | /** 102 | * @param $title 103 | * @param $markdown 104 | * @param int $hideAvatar 105 | * @param int $btnOrientation 106 | * @return mixed 107 | */ 108 | public function actionCard($title, $markdown, $hideAvatar = 0, $btnOrientation = 0){ 109 | return $this->dingTalkService 110 | ->setActionCardMessage($title,$markdown,$hideAvatar,$btnOrientation); 111 | } 112 | 113 | /** 114 | * @return mixed 115 | */ 116 | public function feed(){ 117 | return $this->dingTalkService 118 | ->setFeedCardMessage(); 119 | } 120 | 121 | } 122 | -------------------------------------------------------------------------------- /src/DingTalkService.php: -------------------------------------------------------------------------------- 1 | config = $config; 44 | $this->setTextMessage('null'); 45 | 46 | if ($client != null) { 47 | $this->client = $client; 48 | return; 49 | } 50 | $this->client = $this->createClient($config); 51 | 52 | } 53 | 54 | /** 55 | * @param Message $message 56 | */ 57 | public function setMessage($message) 58 | { 59 | $this->message = $message; 60 | } 61 | 62 | /** 63 | * @return array 64 | */ 65 | public function getMessage() 66 | { 67 | return $this->message->getMessage(); 68 | } 69 | 70 | /** 71 | * @param array $mobiles 72 | * @param bool $atAll 73 | */ 74 | public function setAt($mobiles = [], $atAll = false) 75 | { 76 | $this->mobiles = $mobiles; 77 | $this->atAll = $atAll; 78 | if ($this->message) { 79 | $this->message->sendAt($mobiles, $atAll); 80 | } 81 | } 82 | 83 | /** 84 | * create a guzzle client 85 | * @return HttpClient 86 | * @author wangju 2019-05-17 20:25 87 | */ 88 | protected function createClient($config) 89 | { 90 | $client = new HttpClient($config); 91 | return $client; 92 | } 93 | 94 | 95 | /** 96 | * @param $content 97 | * @return $this 98 | */ 99 | public function setTextMessage($content) 100 | { 101 | $this->message = new Text($content); 102 | $this->message->sendAt($this->mobiles, $this->atAll); 103 | return $this; 104 | } 105 | 106 | /** 107 | * @param $title 108 | * @param $text 109 | * @param $messageUrl 110 | * @param string $picUrl 111 | * @return $this 112 | */ 113 | public function setLinkMessage($title, $text, $messageUrl, $picUrl = '') 114 | { 115 | $this->message = new Link($title, $text, $messageUrl, $picUrl); 116 | $this->message->sendAt($this->mobiles, $this->atAll); 117 | return $this; 118 | } 119 | 120 | /** 121 | * @param $title 122 | * @param $text 123 | * @return $this 124 | */ 125 | public function setMarkdownMessage($title, $markdown) 126 | { 127 | $this->message = new Markdown($title, $markdown); 128 | $this->message->sendAt($this->mobiles, $this->atAll); 129 | return $this; 130 | } 131 | 132 | 133 | /** 134 | * @param $title 135 | * @param $text 136 | * @param int $hideAvatar 137 | * @param int $btnOrientation 138 | * @return ActionCard|Message 139 | */ 140 | public function setActionCardMessage($title, $markdown, $hideAvatar = 0, $btnOrientation = 0) 141 | { 142 | $this->message = new ActionCard($this, $title, $markdown, $hideAvatar, $btnOrientation); 143 | $this->message->sendAt($this->mobiles, $this->atAll); 144 | return $this->message; 145 | } 146 | 147 | /** 148 | * @return FeedCard|Message 149 | */ 150 | public function setFeedCardMessage() 151 | { 152 | $this->message = new FeedCard($this); 153 | $this->message->sendAt($this->mobiles, $this->atAll); 154 | return $this->message; 155 | } 156 | 157 | /** 158 | * @return bool|array 159 | */ 160 | public function send() 161 | { 162 | if (!$this->config['enabled']) { 163 | return false; 164 | } 165 | return $this->client->send($this->message->getBody()); 166 | } 167 | 168 | } 169 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 钉钉推送机器人消息发送laravel扩展包 2 | 3 | [![Build Status](https://travis-ci.org/wowiwj/ding-notice.svg?branch=master)](https://travis-ci.org/wowiwj/ding-notice) 4 | [![Latest Stable Version](https://poser.pugx.org/wangju/ding-notice/v/stable)](https://packagist.org/packages/wangju/ding-notice) 5 | [![Total Downloads](https://poser.pugx.org/wangju/ding-notice/downloads)](https://packagist.org/packages/wangju/ding-notice) 6 | [![Latest Unstable Version](https://poser.pugx.org/wangju/ding-notice/v/unstable)](https://packagist.org/packages/wangju/ding-notice) 7 | [![License](https://poser.pugx.org/wangju/ding-notice/license)](https://packagist.org/packages/wangju/ding-notice) 8 | [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/wowiwj/ding-notice/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/wowiwj/ding-notice/?branch=master) 9 | [![Code Intelligence Status](https://scrutinizer-ci.com/g/wowiwj/ding-notice/badges/code-intelligence.svg?b=master)](https://scrutinizer-ci.com/code-intelligence) 10 | [![Open Source Love](https://badges.frapsoft.com/os/v1/open-source.svg?v=103)](https://github.com/ellerbrock/open-source-badge/) 11 | 12 | 13 | ### 请先阅读 [钉钉官方文档](https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq) 14 | 15 | 16 | # 介绍 17 | ding-notie 是一款钉钉机器人消息发送的Laravel扩展,您可以通过此扩展便捷的发送钉钉消息,进行监控和提醒操作 18 | 19 | # 要求 20 | - php版本:>=7.0 21 | - laravel版本: Laravel5.5+ 22 | 23 | 24 | # 安装 25 | 26 | ```php 27 | composer require wangju/ding-notice 28 | 29 | ``` 30 | 31 | # 在非laravel项目中使用 32 | ```php 33 | $ding = new \DingNotice\DingTalk([ 34 | "default" => [ 35 | 'enabled' => true, 36 | 'token' => "you-push-token", 37 | 'timeout' => 2.0, 38 | 'ssl_verify' => true 39 | ] 40 | ]); 41 | 42 | $ding->text('我就是我, xxx 是不一样的烟火'); 43 | ``` 44 | 45 | # 在laravel项目中使用 46 | 47 | 安装成功后执行 48 | ```php 49 | php artisan vendor:publish --provider="DingNotice\DingNoticeServiceProvider" 50 | 51 | ``` 52 | 会自动将`ding.php`添加到您项目的配置文件当中 53 | 54 | # 相关配置 55 | 56 | ### 钉钉启用开关 57 | (可选)默认为开启 58 | ```php 59 | DING_ENABLED=true 60 | ``` 61 | ### 钉钉的推送token 62 | - (必选)发送钉钉机器人的token,即在您创建机器人之后的access_token 63 | - 钉钉推送链接:https://oapi.dingtalk.com/robot/send?access_token=you-push-token 64 | ```php 65 | DING_TOKEN=you-push-token 66 | ``` 67 | 68 | 69 | ### 多机器人配置 70 | 如果想要添加多个机器人,则在`ding.php`当中添加机器人名字和相关的配置即可 71 | 72 | ```php 73 | return [ 74 | 75 | 'default' => [ 76 | 'enabled' => env('DING_ENABLED',true), 77 | 78 | 'token' => env('DING_TOKEN',''), 79 | 80 | 'timeout' => env('DING_TIME_OUT',2.0), 81 | 82 | 'ssl_verify' => env('DING_SSL_VERIFY',true) 83 | ], 84 | 85 | 'other' => [ 86 | 'enabled' => env('OTHER_DING_ENABLED',true), 87 | 88 | 'token' => env('OTHER_DING_TOKEN',''), 89 | 90 | 'timeout' => env('OTHER_DING_TIME_OUT',2.0), 91 | 92 | 'ssl_verify' => env('DING_SSL_VERIFY',true) 93 | ] 94 | 95 | ]; 96 | ``` 97 | 98 | 99 | ### 钉钉发送的超时时间 100 | - (可选) 默认为2.0秒 101 | ```php 102 | DING_TIME_OUT= 103 | ``` 104 | 105 | ### 是否开启SSL验证 106 | 107 | - (可选)默认为开启,关闭请手动设置 108 | ```php 109 | DING_SSL_VERIFY=false 110 | ``` 111 | 112 | # 使用 113 | 114 | ## 发送纯文字消息 115 | ```php 116 | ding('我就是我, xxx 是不一样的烟火') 117 | ``` 118 | or 119 | ```php 120 | ding()->text('我就是我, xxx 是不一样的烟火') 121 | ``` 122 | 发送过程@其他人或者所有人 123 | 124 | ```php 125 | ding()->at(["13888888888"],true) 126 | ->text("我就是我,@13888888888 是不一样的烟火") 127 | ``` 128 | 129 | ## 发送链接类型的消息 130 | 131 | 132 | ```php 133 | 134 | $title = "自定义机器人协议"; 135 | $text = "群机器人是钉钉群的高级扩展功能。群机器人可以将第三方服务的信息聚合到群聊中,实现自动化的信息同步。例如:通过聚合GitHub,GitLab等源码管理服务,实现源码更新同步;通过聚合Trello,JIRA等项目协调服务,实现项目信息同步。不仅如此,群机器人支持Webhook协议的自定义接入,支持更多可能性,例如:你可将运维报警提醒通过自定义机器人聚合到钉钉群。"; 136 | $picUrl = ""; 137 | $messageUrl = "https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.Rqyvqo&treeId=257&articleId=105735&docType=1"; 138 | 139 | ding()->link($title,$text,$messageUrl,$picUrl) 140 | ``` 141 | 142 | ## 发送markdown类型的消息 143 | 144 | ```php 145 | $title = '杭州天气'; 146 | $markdown = "#### 杭州天气 \n ". 147 | "> 9度,@1825718XXXX 西北风1级,空气良89,相对温度73%\n\n ". 148 | "> ![screenshot](http://i01.lw.aliimg.com/media/lALPBbCc1ZhJGIvNAkzNBLA_1200_588.png)\n". 149 | "> ###### 10点20分发布 [天气](http://www.thinkpage.cn/) "; 150 | 151 | ding()->markdown($title,$markdown); 152 | ``` 153 | or 154 | 155 | ```php 156 | ding()->at([],true) 157 | ->markdown($title,$markdown) 158 | ``` 159 | 160 | ## 发送Action类型的消息 161 | 162 | ### 发送single类型的消息 163 | ```php 164 | $title = "乔布斯 20 年前想打造一间苹果咖啡厅,而它正是 Apple Store 的前身"; 165 | $text = "![screenshot](@lADOpwk3K80C0M0FoA) \n". 166 | " #### 乔布斯 20 年前想打造的苹果咖啡厅 \n\n". 167 | " Apple Store 的设计正从原来满满的科技感走向生活化,而其生活化的走向其实可以追溯到 20 年前苹果一个建立咖啡馆的计划"; 168 | 169 | ding()->actionCard($title,$text,1) 170 | ->single("阅读全文","https://www.dingtalk.com/") 171 | ->send() 172 | ``` 173 | ### 发送btns类型的消息 174 | 175 | ```php 176 | ding()->actionCard($title,$text,1) 177 | ->addButtons("内容不错","https://www.dingtalk.com/") 178 | ->addButtons("不感兴趣","https://www.dingtalk.com/") 179 | ->send(); 180 | ``` 181 | 182 | ## 发送Feed类型的消息 183 | 184 | ```php 185 | $messageUrl = "https://mp.weixin.qq.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI"; 186 | $picUrl = "https://www.dingtalk.com"; 187 | ding()->feed() 188 | ->addLinks('时代的火车向前开',$messageUrl,$picUrl) 189 | ->addLinks('时代的火车向前开2',$messageUrl,$picUrl) 190 | ->send(); 191 | ``` 192 | ## 多机器人消息发送 193 | 194 | ### 发送纯文字消息 195 | ```php 196 | ding('我就是我, xxx 是不一样的烟火','other') 197 | ``` 198 | or 199 | ```php 200 | ding()->with('other')->text('我就是我, xxx 是不一样的烟火'); 201 | ``` 202 | 203 | ### 通过其他机器人发送其他类型消息 204 | ```php 205 | ding()->with('other')->markdown($title,$markdown); 206 | 207 | ding()->with('other') 208 | ->feed() 209 | ->addLinks('时代的火车向前开',$messageUrl,$picUrl) 210 | ->addLinks('时代的火车向前开2',$messageUrl,$picUrl) 211 | ->send(); 212 | ``` 213 | enjoy :) 214 | 215 | 216 | - 效果 217 | ![file](https://lccdn.phphub.org/uploads/images/201805/23/6932/q3nLCOPbRj.png?imageView2/2/w/1240/h/0) 218 | 219 | 220 | 221 | --------------------------------------------------------------------------------