ImController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Controllers\Admin\Setting;
  4. use App\Controllers\BaseController;
  5. use App\Models\Config;
  6. use App\Services\I18n;
  7. use App\Services\IM\Discord;
  8. use App\Services\IM\Slack;
  9. use App\Services\IM\Telegram;
  10. use Exception;
  11. use GuzzleHttp\Exception\GuzzleException;
  12. use Psr\Http\Message\ResponseInterface;
  13. use Slim\Http\Response;
  14. use Slim\Http\ServerRequest;
  15. use Telegram\Bot\Exceptions\TelegramSDKException;
  16. final class ImController extends BaseController
  17. {
  18. private static array $update_field = [
  19. // TODO: rename these to im service independent
  20. 'im_bot_group_notify_add_node',
  21. 'im_bot_group_notify_update_node',
  22. 'im_bot_group_notify_delete_node',
  23. 'im_bot_group_notify_node_gfwed',
  24. 'im_bot_group_notify_node_ungfwed',
  25. 'im_bot_group_notify_node_online',
  26. 'im_bot_group_notify_node_offline',
  27. 'im_bot_group_notify_daily_job',
  28. 'im_bot_group_notify_diary',
  29. 'im_bot_group_notify_ann_create',
  30. 'im_bot_group_notify_ann_update',
  31. // Telegram
  32. 'telegram_token',
  33. 'telegram_chatid',
  34. 'enable_telegram_group_notify',
  35. 'telegram_bot',
  36. 'telegram_request_token',
  37. 'telegram_unbind_kick_member',
  38. 'telegram_group_bound_user',
  39. 'enable_welcome_message',
  40. 'telegram_group_quiet',
  41. 'allow_to_join_new_groups',
  42. 'group_id_allowed_to_join',
  43. 'help_any_command',
  44. // Discord
  45. 'discord_bot_token',
  46. 'discord_client_id',
  47. 'discord_client_secret',
  48. 'discord_guild_id',
  49. 'discord_channel_id',
  50. 'enable_discord_channel_notify',
  51. // Slack
  52. 'slack_token',
  53. 'slack_client_id',
  54. 'slack_client_secret',
  55. 'slack_team_id',
  56. 'slack_channel_id',
  57. 'enable_slack_channel_notify',
  58. ];
  59. private static string $success_msg = '测试信息发送成功';
  60. private static string $err_msg = '测试信息发送失败';
  61. /**
  62. * @throws Exception
  63. */
  64. public function index(ServerRequest $request, Response $response, array $args): ResponseInterface
  65. {
  66. $settings = Config::getClass('im');
  67. return $response->write(
  68. $this->view()
  69. ->assign('update_field', self::$update_field)
  70. ->assign('settings', $settings)
  71. ->fetch('admin/setting/im.tpl')
  72. );
  73. }
  74. public function save(ServerRequest $request, Response $response, array $args): ResponseInterface
  75. {
  76. foreach (self::$update_field as $item) {
  77. if (! Config::set($item, $request->getParam($item))) {
  78. return $response->withJson([
  79. 'ret' => 0,
  80. 'msg' => '保存 ' . $item . ' 时出错',
  81. ]);
  82. }
  83. }
  84. return $response->withJson([
  85. 'ret' => 1,
  86. 'msg' => '保存成功',
  87. ]);
  88. }
  89. public function testTelegram(ServerRequest $request, Response $response, array $args): ResponseInterface
  90. {
  91. try {
  92. (new Telegram())->send(
  93. (int) $request->getParam('telegram_chat_id'),
  94. I18n::trans('bot.test_message', $_ENV['locale']),
  95. );
  96. } catch (TelegramSDKException|Exception $e) {
  97. return $response->withJson([
  98. 'ret' => 0,
  99. 'msg' => $this::$err_msg . ' ' . $e->getMessage(),
  100. ]);
  101. }
  102. return $response->withJson([
  103. 'ret' => 1,
  104. 'msg' => $this::$success_msg,
  105. ]);
  106. }
  107. public function testDiscord(ServerRequest $request, Response $response, array $args): ResponseInterface
  108. {
  109. try {
  110. (new Discord())->send(
  111. (int) $request->getParam('discord_channel_id'),
  112. I18n::trans('bot.test_message', $_ENV['locale']),
  113. );
  114. } catch (GuzzleException|Exception $e) {
  115. return $response->withJson([
  116. 'ret' => 0,
  117. 'msg' => $this::$err_msg . ' ' . $e->getMessage(),
  118. ]);
  119. }
  120. return $response->withJson([
  121. 'ret' => 1,
  122. 'msg' => $this::$success_msg,
  123. ]);
  124. }
  125. public function testSlack(ServerRequest $request, Response $response, array $args): ResponseInterface
  126. {
  127. try {
  128. (new Slack())->send(
  129. (int) $request->getParam('slack_channel_id'),
  130. I18n::trans('bot.test_message', $_ENV['locale']),
  131. );
  132. } catch (GuzzleException|Exception $e) {
  133. return $response->withJson([
  134. 'ret' => 0,
  135. 'msg' => $this::$err_msg . ' ' . $e->getMessage(),
  136. ]);
  137. }
  138. return $response->withJson([
  139. 'ret' => 1,
  140. 'msg' => $this::$success_msg,
  141. ]);
  142. }
  143. }