This repository has been archived by the owner on May 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6a7c026
commit 24421af
Showing
5 changed files
with
253 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace WonderGame\EsNotify\Feishu; | ||
|
||
use WonderGame\EsNotify\Interfaces\ConfigInterface; | ||
use WonderGame\EsNotify\Interfaces\NotifyInterface; | ||
use EasySwoole\Spl\SplBean; | ||
|
||
class Config extends SplBean implements ConfigInterface | ||
{ | ||
/** | ||
* WebHook | ||
* @var string | ||
*/ | ||
protected $url = ''; | ||
|
||
/** | ||
* 密钥 | ||
* @var string | ||
*/ | ||
protected $signKey = ''; | ||
|
||
/** | ||
* 要@哪些人(Open ID 或 User ID), true-所有人,false-谁也不鸟 | ||
* @var array|bool | ||
*/ | ||
protected $at = false; | ||
|
||
public function setUrl($url) | ||
{ | ||
$this->url = $url; | ||
} | ||
|
||
public function getUrl() | ||
{ | ||
return $this->url; | ||
} | ||
|
||
public function setSignKey($signKey) | ||
{ | ||
$this->signKey = $signKey; | ||
} | ||
|
||
public function getSignKey() | ||
{ | ||
return $this->signKey; | ||
} | ||
|
||
public function setAt($at) | ||
{ | ||
$this->at = $at; | ||
} | ||
|
||
public function getAt() | ||
{ | ||
return $this->at; | ||
} | ||
|
||
public function getNotifyClass(): NotifyInterface | ||
{ | ||
return new Notify($this); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace WonderGame\EsNotify\Feishu\Message; | ||
|
||
use EasySwoole\Spl\SplBean; | ||
use WonderGame\EsNotify\Interfaces\MessageInterface; | ||
|
||
abstract class Base extends SplBean implements MessageInterface | ||
{ | ||
/** | ||
* OpenID | ||
* @var array | ||
*/ | ||
protected $atOpenID = []; | ||
|
||
/** | ||
* UserID | ||
* @var array | ||
*/ | ||
protected $atUserID = []; | ||
|
||
protected $isAtAll = false; | ||
|
||
public function getAtText($text = '') | ||
{ | ||
switch ($this->isAtAll) { | ||
case true: | ||
$text .= '<at user_id="all">所有人</at>'; | ||
break; | ||
case false: | ||
break; | ||
default: | ||
foreach ($this->atUserID as $id => $name) { | ||
$text .= '<at user_id="' . $id . '">' . $name . '</at>'; | ||
} | ||
} | ||
|
||
return $text; | ||
} | ||
|
||
public function getAtArray() | ||
{ | ||
$at = []; | ||
switch ($this->isAtAll) { | ||
case true: | ||
$at = [ | ||
[ | ||
'tag' => 'at', | ||
'user_id' => 'all', | ||
'user_name' => '所有人', | ||
] | ||
]; | ||
break; | ||
case false: | ||
break; | ||
default: | ||
foreach (['atOpenID', 'atUserID'] as $item) { | ||
foreach ($this->{$item} as $id => $name) { | ||
$at[] = [ | ||
'tag' => 'at', | ||
'user_id' => $id, | ||
'user_name' => $name, | ||
]; | ||
} | ||
} | ||
} | ||
|
||
return $at; | ||
} | ||
|
||
public function getServerText($text = '') | ||
{ | ||
return $text . PHP_EOL . implode(PHP_EOL, [ | ||
'系统:' . APP_MODULE, | ||
'服务器:' . config('SERVNAME'), | ||
'时间:' . date('Y年m月d日 H:i:s') | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace WonderGame\EsNotify\Feishu\Message; | ||
|
||
class Text extends Base | ||
{ | ||
protected $content = ''; | ||
|
||
public function fullData() | ||
{ | ||
return [ | ||
'msg_type' => 'text', | ||
'content' => [ | ||
'text' => $this->getAtText($this->getServerText($this->content)), | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace WonderGame\EsNotify\Feishu\Message; | ||
|
||
class Textarea extends Base | ||
{ | ||
protected $content = ''; | ||
|
||
// https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot#%E6%94%AF%E6%8C%81%E5%8F%91%E9%80%81%E7%9A%84%E6%B6%88%E6%81%AF%E7%B1%BB%E5%9E%8B%E8%AF%B4%E6%98%8E | ||
public function fullData() | ||
{ | ||
$data = [ | ||
'msg_type' => 'post', | ||
'content' => [ | ||
'post' => [ | ||
'zh_cn' => [ | ||
'tittle' => '程序异常', | ||
'content' => [ | ||
[ | ||
[ | ||
'tag' => 'text', | ||
'text' => $this->getServerText($this->content), | ||
], | ||
[ | ||
'tag' => 'at', | ||
'user_id' => 'all', | ||
'user_name' => '所有人', | ||
], | ||
], | ||
], | ||
], | ||
], | ||
], | ||
]; | ||
// $at = $this->getAtArray(); | ||
// $data['content']['post']['zh_cn']['content'][0] = array_merge($at); | ||
return $data; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace WonderGame\EsNotify\Feishu; | ||
|
||
use EasySwoole\HttpClient\HttpClient; | ||
use WonderGame\EsNotify\Interfaces\ConfigInterface; | ||
use WonderGame\EsNotify\Interfaces\MessageInterface; | ||
use WonderGame\EsNotify\Interfaces\NotifyInterface; | ||
|
||
class Notify implements NotifyInterface | ||
{ | ||
/** | ||
* @var Config | ||
*/ | ||
protected $Config = null; | ||
|
||
public function __construct(ConfigInterface $Config) | ||
{ | ||
$this->Config = $Config; | ||
} | ||
|
||
/** | ||
* @document https://open.feishu.cn/document/client-docs/bot-v3/add-custom-bot | ||
* 自定义机器人的频率控制和普通应用不同,为 100 次/分钟,5 次/秒 | ||
* @param MessageInterface $message | ||
* @return void | ||
*/ | ||
public function does(MessageInterface $message) | ||
{ | ||
$data = $message->fullData(); | ||
|
||
$url = $this->Config->getUrl(); | ||
$secret = $this->Config->getSignKey(); | ||
|
||
$timestamp = time(); | ||
|
||
$sign = base64_encode(hash_hmac('sha256', '', $timestamp . "\n" . $secret, true)); | ||
|
||
$data['timestamp'] = $timestamp; | ||
$data['sign'] = $sign; | ||
|
||
$client = new HttpClient($url); | ||
|
||
// 支持文本(text)、富文本(textarea)、群名片(share_chat)、图片(image)、消息卡片(interactive)消息类型 | ||
|
||
$response = $client->postJson(json_encode($data)); | ||
$json = json_decode($response->getBody(), true); | ||
|
||
if ($json['code'] !== 0) | ||
{ | ||
// todo 异常处理 | ||
} | ||
} | ||
} |