From 7415e7514e70e860bbd3a36fed4f1d3192b0c299 Mon Sep 17 00:00:00 2001 From: Tan Nguyen Date: Tue, 17 Oct 2023 23:58:21 +0700 Subject: [PATCH] fix: some traits and add docs to configs --- config/tg-notifier.php | 14 +++++-- src/Bot.php | 66 ++------------------------------- src/Structures/Notification.php | 4 +- src/Structures/TelegramBot.php | 33 +++++++++++++++++ src/Trait/BotSettingTrait.php | 25 +++++++++++++ 5 files changed, 73 insertions(+), 69 deletions(-) create mode 100644 src/Structures/TelegramBot.php diff --git a/config/tg-notifier.php b/config/tg-notifier.php index abf5021..6b78f2a 100644 --- a/config/tg-notifier.php +++ b/config/tg-notifier.php @@ -11,10 +11,14 @@ 'bot' => [ 'token' => $_ENV['TELEGRAM_BOT_TOKEN'] ?? '', 'chat_id' => $_ENV['TELEGRAM_BOT_CHAT_ID'] ?? '', - 'notify_chat_ids' => explode( - ',', - $_ENV['TELEGRAM_NOTIFY_CHAT_IDS'] ?? '' - ), + + /** + * Set the chat ids that will receive notifications + * You can add the owner bot id, group id, ... ( And please use semicolon ";" to separate chat ids ) + * The environment variable is expected to be in the format: + * "chat_id1;chat_id2,thread_id2;chat_id3,thread_id3;..." + */ + 'notify_chat_ids' => $_ENV['TELEGRAM_NOTIFY_CHAT_IDS'] ?? '', ], 'author' => [ @@ -24,6 +28,7 @@ 'https://github.com/lbiltech/telegram-git-notifier', ], + /** Set the path to the data file */ 'data_file' => [ 'setting' => $_ENV['TGN_PATH_SETTING'] ?? 'storage/json/tgn/tgn-settings.json', @@ -36,6 +41,7 @@ ], ], + /** Set the path to the view file */ 'view' => [ 'path' => $_ENV['TGN_VIEW_PATH'] ?? 'resources/views', diff --git a/src/Bot.php b/src/Bot.php index dfe9a5b..445c98a 100644 --- a/src/Bot.php +++ b/src/Bot.php @@ -6,10 +6,10 @@ use LbilTech\TelegramGitNotifier\Interfaces\BotInterface; use LbilTech\TelegramGitNotifier\Interfaces\Structures\AppInterface; use LbilTech\TelegramGitNotifier\Interfaces\EventInterface; -use LbilTech\TelegramGitNotifier\Interfaces\Structures\SettingInterface; use LbilTech\TelegramGitNotifier\Models\Event; use LbilTech\TelegramGitNotifier\Models\Setting; use LbilTech\TelegramGitNotifier\Structures\App; +use LbilTech\TelegramGitNotifier\Structures\TelegramBot; use LbilTech\TelegramGitNotifier\Trait\BotSettingTrait; use LbilTech\TelegramGitNotifier\Trait\EventSettingTrait; use LbilTech\TelegramGitNotifier\Trait\EventTrait; @@ -18,6 +18,8 @@ class Bot implements AppInterface, BotInterface, EventInterface { use App; + use TelegramBot; + use EventTrait; use BotSettingTrait; use EventSettingTrait; @@ -43,66 +45,4 @@ public function __construct( $this->setting = $setting ?? new Setting(); $this->updateSetting($settingFile); } - - public function updateSetting(?string $settingFile = null): void - { - if ($this->setting->getSettingFile()) { - return; - } - $settingFile = $settingFile ?? config('telegram-git-notifier.data_file.setting'); - $this->setting->setSettingFile($settingFile); - $this->setting->setSettingConfig(); - } - - public function setMyCommands( - array $menuCommand, - ?string $view = null - ): void { - $this->telegram->setMyCommands([ - 'commands' => json_encode($menuCommand) - ]); - $this->sendMessage( - view( - $view ?? - config('telegram-git-notifier.view.tools.set_menu_cmd') - ) - ); - } - - public function isCallback(): bool - { - if ($this->telegram->getUpdateType() === Telegram::CALLBACK_QUERY) { - return true; - } - - return false; - } - - public function isMessage(): bool - { - if ($this->telegram->getUpdateType() === Telegram::MESSAGE) { - return true; - } - - return false; - } - - public function isOwner(): bool - { - if ($this->telegram->ChatID() == $this->chatBotId) { - return true; - } - - return false; - } - - public function isNotifyChat(): bool - { - $chatIds = config('telegram-git-notifier.bot.notify_chat_ids'); - if (in_array($this->telegram->ChatID(), $chatIds)) { - return true; - } - - return false; - } } diff --git a/src/Structures/Notification.php b/src/Structures/Notification.php index 25661e5..cfdbef3 100644 --- a/src/Structures/Notification.php +++ b/src/Structures/Notification.php @@ -18,7 +18,7 @@ public function accessDenied( string $viewTemplate = null, ): void { $this->telegram->sendMessage([ - 'chat_id' => config('telegram-git-notifier.bot.chat_id'), + 'chat_id' => $this->chatBotId, 'text' => view( $viewTemplate ?? config('telegram-git-notifier.view.globals.access_denied'), ['chatId' => $chatId] @@ -64,7 +64,7 @@ private function setMessage(string $typeEvent): void public function sendNotify(string $message = null, array $options = []): bool { - $this->message = $message ?? $this->message; + $this->message = !empty($message) ? $message : $this->message; $queryParams = array_merge($this->createTelegramBaseContent(), ['text' => $this->message], $options); diff --git a/src/Structures/TelegramBot.php b/src/Structures/TelegramBot.php new file mode 100644 index 0000000..af865dd --- /dev/null +++ b/src/Structures/TelegramBot.php @@ -0,0 +1,33 @@ +telegram->getUpdateType() === Telegram::CALLBACK_QUERY; + } + + public function isMessage(): bool + { + return $this->telegram->getUpdateType() === Telegram::MESSAGE; + } + + public function isOwner(): bool + { + return $this->telegram->ChatID() == $this->chatBotId; + } + + public function isNotifyChat(): bool + { + $chatIds = config('telegram-git-notifier.bot.notify_chat_ids'); + if (in_array($this->telegram->ChatID(), $chatIds)) { + return true; + } + + return false; + } +} diff --git a/src/Trait/BotSettingTrait.php b/src/Trait/BotSettingTrait.php index 3d2593f..ef0ad2d 100644 --- a/src/Trait/BotSettingTrait.php +++ b/src/Trait/BotSettingTrait.php @@ -6,6 +6,31 @@ trait BotSettingTrait { + public function updateSetting(?string $settingFile = null): void + { + if ($this->setting->getSettingFile()) { + return; + } + $settingFile = $settingFile ?? config('telegram-git-notifier.data_file.setting'); + $this->setting->setSettingFile($settingFile); + $this->setting->setSettingConfig(); + } + + public function setMyCommands( + array $menuCommand, + ?string $view = null + ): void { + $this->telegram->setMyCommands([ + 'commands' => json_encode($menuCommand) + ]); + $this->sendMessage( + view( + $view ?? + config('telegram-git-notifier.view.tools.set_menu_cmd') + ) + ); + } + public function settingHandle(?string $view = null): void { $this->sendMessage(