| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <?php
- declare(strict_types=1);
- namespace App\Controllers\Admin\Setting;
- use App\Controllers\BaseController;
- use App\Models\Config;
- use App\Services\I18n;
- use App\Services\IM\Discord;
- use App\Services\IM\Slack;
- use App\Services\IM\Telegram;
- use Exception;
- use GuzzleHttp\Exception\GuzzleException;
- use Psr\Http\Message\ResponseInterface;
- use Slim\Http\Response;
- use Slim\Http\ServerRequest;
- use Telegram\Bot\Exceptions\TelegramSDKException;
- final class ImController extends BaseController
- {
- private static array $update_field = [
- // TODO: rename these to im service independent
- 'im_bot_group_notify_add_node',
- 'im_bot_group_notify_update_node',
- 'im_bot_group_notify_delete_node',
- 'im_bot_group_notify_node_gfwed',
- 'im_bot_group_notify_node_ungfwed',
- 'im_bot_group_notify_node_online',
- 'im_bot_group_notify_node_offline',
- 'im_bot_group_notify_daily_job',
- 'im_bot_group_notify_diary',
- 'im_bot_group_notify_ann_create',
- 'im_bot_group_notify_ann_update',
- // Telegram
- 'telegram_token',
- 'telegram_chatid',
- 'enable_telegram_group_notify',
- 'telegram_bot',
- 'telegram_request_token',
- 'telegram_unbind_kick_member',
- 'telegram_group_bound_user',
- 'enable_welcome_message',
- 'telegram_group_quiet',
- 'allow_to_join_new_groups',
- 'group_id_allowed_to_join',
- 'help_any_command',
- // Discord
- 'discord_bot_token',
- 'discord_client_id',
- 'discord_client_secret',
- 'discord_guild_id',
- 'discord_channel_id',
- 'enable_discord_channel_notify',
- // Slack
- 'slack_token',
- 'slack_client_id',
- 'slack_client_secret',
- 'slack_team_id',
- 'slack_channel_id',
- 'enable_slack_channel_notify',
- ];
- private static string $success_msg = '测试信息发送成功';
- private static string $err_msg = '测试信息发送失败';
- /**
- * @throws Exception
- */
- public function index(ServerRequest $request, Response $response, array $args): ResponseInterface
- {
- $settings = Config::getClass('im');
- return $response->write(
- $this->view()
- ->assign('update_field', self::$update_field)
- ->assign('settings', $settings)
- ->fetch('admin/setting/im.tpl')
- );
- }
- public function save(ServerRequest $request, Response $response, array $args): ResponseInterface
- {
- foreach (self::$update_field as $item) {
- if (! Config::set($item, $request->getParam($item))) {
- return $response->withJson([
- 'ret' => 0,
- 'msg' => '保存 ' . $item . ' 时出错',
- ]);
- }
- }
- return $response->withJson([
- 'ret' => 1,
- 'msg' => '保存成功',
- ]);
- }
- public function testTelegram(ServerRequest $request, Response $response, array $args): ResponseInterface
- {
- try {
- (new Telegram())->send(
- (int) $request->getParam('telegram_chat_id'),
- I18n::trans('bot.test_message', $_ENV['locale']),
- );
- } catch (TelegramSDKException|Exception $e) {
- return $response->withJson([
- 'ret' => 0,
- 'msg' => $this::$err_msg . ' ' . $e->getMessage(),
- ]);
- }
- return $response->withJson([
- 'ret' => 1,
- 'msg' => $this::$success_msg,
- ]);
- }
- public function testDiscord(ServerRequest $request, Response $response, array $args): ResponseInterface
- {
- try {
- (new Discord())->send(
- (int) $request->getParam('discord_channel_id'),
- I18n::trans('bot.test_message', $_ENV['locale']),
- );
- } catch (GuzzleException|Exception $e) {
- return $response->withJson([
- 'ret' => 0,
- 'msg' => $this::$err_msg . ' ' . $e->getMessage(),
- ]);
- }
- return $response->withJson([
- 'ret' => 1,
- 'msg' => $this::$success_msg,
- ]);
- }
- public function testSlack(ServerRequest $request, Response $response, array $args): ResponseInterface
- {
- try {
- (new Slack())->send(
- (int) $request->getParam('slack_channel_id'),
- I18n::trans('bot.test_message', $_ENV['locale']),
- );
- } catch (GuzzleException|Exception $e) {
- return $response->withJson([
- 'ret' => 0,
- 'msg' => $this::$err_msg . ' ' . $e->getMessage(),
- ]);
- }
- return $response->withJson([
- 'ret' => 1,
- 'msg' => $this::$success_msg,
- ]);
- }
- }
|