初始版本
This commit is contained in:
commit
09f050888e
24 files changed
+1341
No files matched your search
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace DingNotice;
|
||||
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
|
||||
class DingNoticeServiceProvider extends ServiceProvider
|
||||
{
|
||||
/**
|
||||
* Bootstrap services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function boot()
|
||||
{
|
||||
$this->publishes([
|
||||
__DIR__ . '/../config/ding.php' => base_path('config/ding.php'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Register services.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$this->registerLaravelBindings();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Register Laravel bindings.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function registerLaravelBindings()
|
||||
{
|
||||
$this->app->singleton(DingTalk::class, function ($app) {
|
||||
return new DingTalk($app['config']['ding']);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace DingNotice;
|
||||
|
||||
class DingTalk
|
||||
{
|
||||
|
||||
/**
|
||||
* @var
|
||||
*/
|
||||
protected $config;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $robot = 'default';
|
||||
/**
|
||||
* @var DingTalkService
|
||||
*/
|
||||
protected $dingTalkService;
|
||||
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* DingTalk constructor.
|
||||
* @param $config
|
||||
* @param SendClient $client
|
||||
*/
|
||||
public function __construct($config,$client = null)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->client = $client;
|
||||
$this->with();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $robot
|
||||
* @return $this
|
||||
*/
|
||||
public function with($robot = 'default'){
|
||||
$this->robot = $robot;
|
||||
$this->dingTalkService = new DingTalkService($this->config[$robot],$this->client);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $content
|
||||
* @return mixed
|
||||
*/
|
||||
public function text($content = ''){
|
||||
return $this->dingTalkService
|
||||
->setTextMessage($content)
|
||||
->send();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $title
|
||||
* @param $text
|
||||
* @return mixed
|
||||
*/
|
||||
public function action($title, $text){
|
||||
return $this->dingTalkService
|
||||
->setActionCardMessage($title,$text);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $mobiles
|
||||
* @param bool $atAll
|
||||
* @return $this
|
||||
*/
|
||||
public function at($mobiles = [], $atAll = false){
|
||||
$this->dingTalkService
|
||||
->setAt($mobiles,$atAll);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $title
|
||||
* @param $text
|
||||
* @param $url
|
||||
* @param string $picUrl
|
||||
* @return mixed
|
||||
*/
|
||||
public function link($title, $text, $url, $picUrl = ''){
|
||||
return $this->dingTalkService
|
||||
->setLinkMessage($title,$text,$url,$picUrl)
|
||||
->send();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $title
|
||||
* @param $markdown
|
||||
* @return mixed
|
||||
*/
|
||||
public function markdown($title, $markdown){
|
||||
return $this->dingTalkService
|
||||
->setMarkdownMessage($title,$markdown)
|
||||
->send();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $title
|
||||
* @param $markdown
|
||||
* @param int $hideAvatar
|
||||
* @param int $btnOrientation
|
||||
* @return mixed
|
||||
*/
|
||||
public function actionCard($title, $markdown, $hideAvatar = 0, $btnOrientation = 0){
|
||||
return $this->dingTalkService
|
||||
->setActionCardMessage($title,$markdown,$hideAvatar,$btnOrientation);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function feed(){
|
||||
return $this->dingTalkService
|
||||
->setFeedCardMessage();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,168 @@
|
||||
<?php
|
||||
|
||||
namespace DingNotice;
|
||||
|
||||
use DingNotice\Messages\ActionCard;
|
||||
use DingNotice\Messages\FeedCard;
|
||||
use DingNotice\Messages\Link;
|
||||
use DingNotice\Messages\Markdown;
|
||||
use DingNotice\Messages\Message;
|
||||
use DingNotice\Messages\Text;
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class DingTalkService
|
||||
{
|
||||
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* @var Message
|
||||
*/
|
||||
protected $message;
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $mobiles = [];
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $atAll = false;
|
||||
|
||||
/**
|
||||
* @var SendClient
|
||||
*/
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
* DingTalkService constructor.
|
||||
* @param $config
|
||||
* @param null $client
|
||||
*/
|
||||
public function __construct($config, SendClient $client = null)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->setTextMessage('null');
|
||||
|
||||
if ($client != null) {
|
||||
$this->client = $client;
|
||||
return;
|
||||
}
|
||||
$this->client = $this->createClient($config);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Message $message
|
||||
*/
|
||||
public function setMessage($message)
|
||||
{
|
||||
$this->message = $message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getMessage()
|
||||
{
|
||||
return $this->message->getMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $mobiles
|
||||
* @param bool $atAll
|
||||
*/
|
||||
public function setAt($mobiles = [], $atAll = false)
|
||||
{
|
||||
$this->mobiles = $mobiles;
|
||||
$this->atAll = $atAll;
|
||||
if ($this->message) {
|
||||
$this->message->sendAt($mobiles, $atAll);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* create a guzzle client
|
||||
* @return HttpClient
|
||||
* @author wangju 2019-05-17 20:25
|
||||
*/
|
||||
protected function createClient($config)
|
||||
{
|
||||
$client = new HttpClient($config);
|
||||
return $client;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $content
|
||||
* @return $this
|
||||
*/
|
||||
public function setTextMessage($content)
|
||||
{
|
||||
$this->message = new Text($content);
|
||||
$this->message->sendAt($this->mobiles, $this->atAll);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $title
|
||||
* @param $text
|
||||
* @param $messageUrl
|
||||
* @param string $picUrl
|
||||
* @return $this
|
||||
*/
|
||||
public function setLinkMessage($title, $text, $messageUrl, $picUrl = '')
|
||||
{
|
||||
$this->message = new Link($title, $text, $messageUrl, $picUrl);
|
||||
$this->message->sendAt($this->mobiles, $this->atAll);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $title
|
||||
* @param $text
|
||||
* @return $this
|
||||
*/
|
||||
public function setMarkdownMessage($title, $markdown)
|
||||
{
|
||||
$this->message = new Markdown($title, $markdown);
|
||||
$this->message->sendAt($this->mobiles, $this->atAll);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $title
|
||||
* @param $text
|
||||
* @param int $hideAvatar
|
||||
* @param int $btnOrientation
|
||||
* @return ActionCard|Message
|
||||
*/
|
||||
public function setActionCardMessage($title, $markdown, $hideAvatar = 0, $btnOrientation = 0)
|
||||
{
|
||||
$this->message = new ActionCard($this, $title, $markdown, $hideAvatar, $btnOrientation);
|
||||
$this->message->sendAt($this->mobiles, $this->atAll);
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return FeedCard|Message
|
||||
*/
|
||||
public function setFeedCardMessage()
|
||||
{
|
||||
$this->message = new FeedCard($this);
|
||||
$this->message->sendAt($this->mobiles, $this->atAll);
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool|array
|
||||
*/
|
||||
public function send()
|
||||
{
|
||||
if (!$this->config['enabled']) {
|
||||
return false;
|
||||
}
|
||||
return $this->client->send($this->message->getBody());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: wangju
|
||||
* Date: 2019-05-17
|
||||
* Time: 20:38
|
||||
*/
|
||||
|
||||
namespace DingNotice;
|
||||
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
|
||||
class HttpClient implements SendClient
|
||||
{
|
||||
protected $client;
|
||||
protected $config;
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $hookUrl = "https://oapi.dingtalk.com/robot/send";
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
protected $accessToken = "";
|
||||
|
||||
public function __construct($config)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->setAccessToken();
|
||||
$this->client = $this->createClient();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public function setAccessToken(){
|
||||
$this->accessToken = $this->config['token'];
|
||||
}
|
||||
|
||||
/**
|
||||
* create a guzzle client
|
||||
* @return Client
|
||||
* @author wangju 2019-05-17 20:25
|
||||
*/
|
||||
protected function createClient()
|
||||
{
|
||||
$client = new Client([
|
||||
'timeout' => $this->config['timeout'] ?? 2.0,
|
||||
]);
|
||||
return $client;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getRobotUrl()
|
||||
{
|
||||
$query['access_token'] = $this->accessToken;
|
||||
if (isset($this->config['secret']) && $secret = $this->config['secret']) {
|
||||
$timestamp = time() . sprintf('%03d', rand(1, 999));
|
||||
$sign = hash_hmac('sha256', $timestamp . "\n" . $secret, $secret, true);
|
||||
$query['timestamp'] = $timestamp;
|
||||
$query['sign'] = base64_encode($sign);
|
||||
}
|
||||
return $this->hookUrl . "?" . http_build_query($query);
|
||||
}
|
||||
|
||||
/**
|
||||
* send message
|
||||
* @param $url
|
||||
* @param $params
|
||||
* @return array
|
||||
* @author wangju 2019-05-17 20:48
|
||||
*/
|
||||
public function send($params): array
|
||||
{
|
||||
$request = $this->client->post($this->getRobotUrl(), [
|
||||
'body' => json_encode($params),
|
||||
'headers' => [
|
||||
'Content-Type' => 'application/json',
|
||||
],
|
||||
'verify' => $this->config['ssl_verify'] ?? true,
|
||||
]);
|
||||
|
||||
$result = $request->getBody()->getContents();
|
||||
return json_decode($result, true) ?? [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace DingNotice\Messages;
|
||||
|
||||
use DingNotice\DingTalkService;
|
||||
|
||||
class ActionCard extends Message
|
||||
{
|
||||
|
||||
protected $service;
|
||||
|
||||
public function __construct(DingTalkService $service,$title, $markdown, $hideAvatar = 0, $btnOrientation = 0)
|
||||
{
|
||||
$this->service = $service;
|
||||
$this->setMessage($title,$markdown,$hideAvatar,$btnOrientation);
|
||||
}
|
||||
|
||||
public function setMessage($title, $markdown, $hideAvatar = 0, $btnOrientation = 0){
|
||||
$this->message = [
|
||||
'msgtype' => 'actionCard',
|
||||
'actionCard' => [
|
||||
'title' => $title,
|
||||
'text' => $markdown,
|
||||
'hideAvatar' => $hideAvatar,
|
||||
'btnOrientation' => $btnOrientation
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function single($title,$url){
|
||||
$this->message['actionCard']['singleTitle'] = $title;
|
||||
$this->message['actionCard']['singleURL'] = $url;
|
||||
$this->service->setMessage($this);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addButtons($title,$url){
|
||||
$this->message['actionCard']['btns'][] = [
|
||||
'title' => $title,
|
||||
'actionURL' => $url
|
||||
];
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function send(){
|
||||
$this->service->setMessage($this);
|
||||
return $this->service->send();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace DingNotice\Messages;
|
||||
|
||||
use DingNotice\DingTalkService;
|
||||
|
||||
class FeedCard extends Message
|
||||
{
|
||||
protected $service;
|
||||
|
||||
public function __construct(DingTalkService $service)
|
||||
{
|
||||
$this->service = $service;
|
||||
$this->setMessage();
|
||||
|
||||
}
|
||||
|
||||
public function setMessage(){
|
||||
$this->message = [
|
||||
'feedCard' => [
|
||||
'links' => []
|
||||
],
|
||||
'msgtype' => 'feedCard'
|
||||
];
|
||||
}
|
||||
|
||||
public function addLinks($title,$messageUrl,$picUrl){
|
||||
$this->message['feedCard']['links'][] = [
|
||||
'title' => $title,
|
||||
'messageURL' => $messageUrl,
|
||||
'picURL' => $picUrl
|
||||
];
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function send(){
|
||||
$this->service->setMessage($this);
|
||||
return $this->service->send();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace DingNotice\Messages;
|
||||
|
||||
class Link extends Message
|
||||
{
|
||||
|
||||
public function __construct($title,$text,$messageUrl,$picUrl = '')
|
||||
{
|
||||
$this->setMessage($title,$text,$messageUrl,$picUrl);
|
||||
}
|
||||
|
||||
public function setMessage($title,$text,$messageUrl,$picUrl = ''){
|
||||
$this->message = [
|
||||
'msgtype' => 'link',
|
||||
'link' => [
|
||||
'text' => $text,
|
||||
'title' => $title,
|
||||
'picUrl' => $picUrl,
|
||||
'messageUrl' => $messageUrl
|
||||
]
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace DingNotice\Messages;
|
||||
|
||||
class Markdown extends Message
|
||||
{
|
||||
public function __construct($title,$markdown)
|
||||
{
|
||||
$this->setMessage($title,$markdown);
|
||||
}
|
||||
|
||||
public function setMessage($title,$markdown){
|
||||
$this->message = [
|
||||
'msgtype' => 'markdown',
|
||||
'markdown' => [
|
||||
'title' => $title,
|
||||
'text' => $markdown
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace DingNotice\Messages;
|
||||
|
||||
abstract class Message
|
||||
{
|
||||
protected $message = [];
|
||||
protected $at;
|
||||
|
||||
|
||||
public function getMessage(){
|
||||
return $this->message;
|
||||
}
|
||||
|
||||
protected function makeAt($mobiles = [],$atAll = false){
|
||||
return [
|
||||
'at' => [
|
||||
'atMobiles' => $mobiles,
|
||||
'isAtAll' => $atAll
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
public function sendAt($mobiles = [],$atAll = false){
|
||||
$this->at = $this->makeAt($mobiles,$atAll);
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getBody(){
|
||||
|
||||
if (empty($this->at)){
|
||||
$this->sendAt();
|
||||
}
|
||||
return $this->message + $this->at;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace DingNotice\Messages;
|
||||
|
||||
class Text extends Message
|
||||
{
|
||||
public function __construct($content)
|
||||
{
|
||||
$this->message = [
|
||||
'msgtype' => 'text',
|
||||
'text' => [
|
||||
'content' => $content
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: wangju
|
||||
* Date: 2019-05-17
|
||||
* Time: 20:37
|
||||
*/
|
||||
|
||||
namespace DingNotice;
|
||||
|
||||
|
||||
interface SendClient
|
||||
{
|
||||
public function send($params): array;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
use DingNotice\DingTalk;
|
||||
|
||||
if (!function_exists('ding')){
|
||||
|
||||
/**
|
||||
* @return bool|DingTalk
|
||||
*/
|
||||
function ding(){
|
||||
|
||||
$arguments = func_get_args();
|
||||
|
||||
$dingTalk = app(DingTalk::class);
|
||||
|
||||
if (empty($arguments)) {
|
||||
return $dingTalk;
|
||||
}
|
||||
|
||||
if (is_string($arguments[0])) {
|
||||
$robot = $arguments[1] ?? 'default';
|
||||
return $dingTalk->with($robot)->text($arguments[0]);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user