Browse Source

feat: slack service

M1Screw 2 years ago
parent
commit
6acff70536

+ 1 - 0
app/routes.php

@@ -237,6 +237,7 @@ return static function (Slim\App $app): void {
         $group->post('/setting/support', App\Controllers\Admin\Setting\SupportController::class . ':saveSupport');
         $group->post('/setting/test_email', App\Controllers\Admin\Setting\EmailController::class . ':testEmail');
         $group->post('/setting/test_discord', App\Controllers\Admin\Setting\ImController::class . ':testDiscord');
+        $group->post('/setting/test_slack', App\Controllers\Admin\Setting\ImController::class . ':testSlack');
         // 礼品卡
         $group->get('/giftcard', App\Controllers\Admin\GiftCardController::class . ':index');
         $group->post('/giftcard', App\Controllers\Admin\GiftCardController::class . ':add');

+ 10 - 0
config/settings.json

@@ -649,6 +649,16 @@
         "default": "",
         "mark": "Discord Bot Token"
     },
+    {
+        "id": null,
+        "item": "slack_token",
+        "value": "",
+        "class": "slack",
+        "is_public": 0,
+        "type": "string",
+        "default": "",
+        "mark": "Slack App Token"
+    },
     {
         "id": null,
         "item": "tawk_id",

+ 42 - 0
resources/views/tabler/admin/setting/im.tpl

@@ -39,6 +39,9 @@
                         <li class="nav-item">
                             <a href="#discord_bot" class="nav-link" data-bs-toggle="tab">Discord Bot</a>
                         </li>
+                        <li class="nav-item">
+                            <a href="#slack_bot" class="nav-link" data-bs-toggle="tab">Slack Bot</a>
+                        </li>
                     </ul>
                 </div>
                 <div class="card-body">
@@ -340,6 +343,25 @@
                                 </div>
                             </div>
                         </div>
+                        <div class="tab-pane" id="slack_bot">
+                            <div class="card-body">
+                                <div class="form-group mb-3 row">
+                                    <label class="form-label col-3 col-form-label">App Token</label>
+                                    <div class="col">
+                                        <input id="slack_token" type="text" class="form-control" value="{$settings['slack_token']}">
+                                    </div>
+                                </div>
+                                <div class="form-group mb-3 row">
+                                    <label class="form-label col-3 col-form-label">Discord 用户 ID</label>
+                                    <input type="text" class="form-control" id="slack_user_id" value="">
+                                    <div class="row my-3">
+                                        <div class="col">
+                                            <button id="test-slack" class="btn btn-primary">发送测试信息</button>
+                                        </div>
+                                    </div>
+                                </div>
+                            </div>
+                        </div>
                     </div>
                 </div>
             </div>
@@ -389,6 +411,26 @@
             }
         })
     });
+
+    $("#test-slack").click(function() {
+        $.ajax({
+            url: '/admin/setting/test_slack',
+            type: 'POST',
+            dataType: "json",
+            data: {
+                slack_user_id: $('#slack_user_id').val(),
+            },
+            success: function(data) {
+                if (data.ret === 1) {
+                    $('#success-noreload-message').text(data.msg);
+                    $('#success-noreload-dialog').modal('show');
+                } else {
+                    $('#fail-message').text(data.msg);
+                    $('#fail-dialog').modal('show');
+                }
+            }
+        })
+    });
 </script>
 
 {include file='admin/footer.tpl'}

+ 23 - 3
src/Controllers/Admin/Setting/ImController.php

@@ -7,6 +7,7 @@ namespace App\Controllers\Admin\Setting;
 use App\Controllers\BaseController;
 use App\Models\Setting;
 use App\Services\IM\Discord;
+use App\Services\IM\Slack;
 use Exception;
 use GuzzleHttp\Exception\GuzzleException;
 use function json_encode;
@@ -51,6 +52,7 @@ final class ImController extends BaseController
         'telegram_general_pricing',
         'telegram_general_terms',
         'discord_bot_token',
+        'slack_token',
     ];
 
     /**
@@ -106,11 +108,29 @@ final class ImController extends BaseController
 
     public function testDiscord($request, $response, $args)
     {
-        $to = $request->getParam('discord_user_id');
-
         try {
             (new Discord())->send(
-                $to,
+                $request->getParam('discord_user_id'),
+                '这是一条测试消息',
+            );
+        } catch (GuzzleException|Exception $e) {
+            return $response->withJson([
+                'ret' => 0,
+                'msg' => '测试信息发送失败 ' . $e->getMessage(),
+            ]);
+        }
+
+        return $response->withJson([
+            'ret' => 1,
+            'msg' => '测试信息发送成功',
+        ]);
+    }
+
+    public function testSlack($request, $response, $args)
+    {
+        try {
+            (new Slack())->send(
+                $request->getParam('slack_user_id'),
                 '这是一条测试消息',
             );
         } catch (GuzzleException|Exception $e) {

+ 50 - 0
src/Services/IM/Slack.php

@@ -0,0 +1,50 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Services\IM;
+
+use App\Models\Setting;
+use Exception;
+use GuzzleHttp\Client;
+use GuzzleHttp\Exception\GuzzleException;
+
+final class Slack extends Base
+{
+    private string $token;
+    private Client $client;
+
+    public function __construct()
+    {
+        $this->token = Setting::obtain('slack_token');
+        $this->client = new Client();
+    }
+
+    /**
+     * @throws GuzzleException
+     * @throws Exception
+     */
+    public function send($to, $msg): void
+    {
+        $url = "https://slack.com/api/chat.postMessage";
+
+        $headers = [
+            'Authorization' => 'Bearer '.$this->token,
+            'Content-Type' => 'application/json',
+        ];
+
+        $body = [
+            'channel' => $to,
+            'text' => $msg,
+        ];
+
+        $response = $this->client->post($url, [
+            'headers' => $headers,
+            'json' => $body,
+        ]);
+
+        if ($response->getStatusCode() !== 200) {
+            throw new Exception($response->getBody()->getContents());
+        }
+    }
+}