From 24421af0d7c0c2e9289fd693b1e58d3c301c78d1 Mon Sep 17 00:00:00 2001
From: lamson <171004297@qq.com>
Date: Mon, 19 Feb 2024 17:51:04 +0800
Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E9=A3=9E=E4=B9=A6?=
=?UTF-8?q?=E8=AD=A6=E6=8A=A5=E5=8A=9F=E8=83=BD?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
src/Feishu/Config.php | 63 ++++++++++++++++++++++++++
src/Feishu/Message/Base.php | 79 +++++++++++++++++++++++++++++++++
src/Feishu/Message/Text.php | 18 ++++++++
src/Feishu/Message/Textarea.php | 39 ++++++++++++++++
src/Feishu/Notify.php | 54 ++++++++++++++++++++++
5 files changed, 253 insertions(+)
create mode 100644 src/Feishu/Config.php
create mode 100644 src/Feishu/Message/Base.php
create mode 100644 src/Feishu/Message/Text.php
create mode 100644 src/Feishu/Message/Textarea.php
create mode 100644 src/Feishu/Notify.php
diff --git a/src/Feishu/Config.php b/src/Feishu/Config.php
new file mode 100644
index 0000000..c018e98
--- /dev/null
+++ b/src/Feishu/Config.php
@@ -0,0 +1,63 @@
+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);
+ }
+}
diff --git a/src/Feishu/Message/Base.php b/src/Feishu/Message/Base.php
new file mode 100644
index 0000000..d277903
--- /dev/null
+++ b/src/Feishu/Message/Base.php
@@ -0,0 +1,79 @@
+isAtAll) {
+ case true:
+ $text .= '所有人';
+ break;
+ case false:
+ break;
+ default:
+ foreach ($this->atUserID as $id => $name) {
+ $text .= '' . $name . '';
+ }
+ }
+
+ 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')
+ ]);
+ }
+}
diff --git a/src/Feishu/Message/Text.php b/src/Feishu/Message/Text.php
new file mode 100644
index 0000000..e7ebbc8
--- /dev/null
+++ b/src/Feishu/Message/Text.php
@@ -0,0 +1,18 @@
+ 'text',
+ 'content' => [
+ 'text' => $this->getAtText($this->getServerText($this->content)),
+ ],
+ ];
+ }
+}
diff --git a/src/Feishu/Message/Textarea.php b/src/Feishu/Message/Textarea.php
new file mode 100644
index 0000000..0e2a9e5
--- /dev/null
+++ b/src/Feishu/Message/Textarea.php
@@ -0,0 +1,39 @@
+ '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;
+ }
+}
diff --git a/src/Feishu/Notify.php b/src/Feishu/Notify.php
new file mode 100644
index 0000000..caff5de
--- /dev/null
+++ b/src/Feishu/Notify.php
@@ -0,0 +1,54 @@
+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 异常处理
+ }
+ }
+}