-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from tanhongit/tools
Handle command and callback in the bot tool
- Loading branch information
Showing
11 changed files
with
334 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
return [ | ||
'title' => 'BOT MENU', | ||
|
||
'start' => 'Welcome to the bot', | ||
'menu' => 'Show menu of the bot', | ||
'token' => 'Show token of the bot', | ||
'id' => 'Show the ID of the current chat', | ||
'usage' => 'Show step by step usage', | ||
'server' => 'To get Server Information', | ||
'settings' => 'Go to settings of the bot', | ||
'back' => 'Back', | ||
|
||
'discussion' => '🗨 Discussion', | ||
'source_code' => '💠 Source Code', | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
use CSlant\LaravelTelegramGitNotifier\Services\CommandService; | ||
$menuCommands = CommandService::menuCommands() ?? []; | ||
?> | ||
|
||
<b>{{ __('tg-notifier::tools/menu.title') }}</b> 🤖 | ||
|
||
<?php foreach ($menuCommands as $menuCommand) : ?> | ||
<b><?= $menuCommand['command'] ?></b> - <?= $menuCommand['description'] ?> | ||
|
||
<?php endforeach; ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace CSlant\LaravelTelegramGitNotifier\Services; | ||
|
||
use CSlant\LaravelTelegramGitNotifier\Traits\Markup; | ||
use CSlant\TelegramGitNotifier\Bot; | ||
use CSlant\TelegramGitNotifier\Constants\SettingConstant; | ||
use CSlant\TelegramGitNotifier\Exceptions\BotException; | ||
use CSlant\TelegramGitNotifier\Exceptions\CallbackException; | ||
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException; | ||
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException; | ||
|
||
class CallbackService | ||
{ | ||
use Markup; | ||
|
||
private Bot $bot; | ||
|
||
protected string $viewNamespace = ''; | ||
|
||
public function __construct(Bot $bot) | ||
{ | ||
$this->bot = $bot; | ||
$this->viewNamespace = config('telegram-git-notifier.view.namespace'); | ||
} | ||
|
||
/** | ||
* Answer the back button. | ||
* | ||
* @param string $callback | ||
* @return void | ||
* | ||
* @throws MessageIsEmptyException | ||
* @throws BotException | ||
* @throws CallbackException | ||
*/ | ||
public function answerBackButton(string $callback): void | ||
{ | ||
$callback = str_replace(SettingConstant::SETTING_BACK, '', $callback); | ||
switch ($callback) { | ||
case 'settings': | ||
$view = view("$this->viewNamespace::tools.settings"); | ||
$markup = $this->bot->settingMarkup(); | ||
|
||
break; | ||
case 'settings.custom_events.github': | ||
$view = view("$this->viewNamespace::tools.custom_event", ['platform' => 'github']); | ||
$markup = $this->bot->eventMarkup(); | ||
|
||
break; | ||
case 'settings.custom_events.gitlab': | ||
$view = view("$this->viewNamespace::tools.custom_event", ['platform' => 'gitlab']); | ||
$markup = $this->bot->eventMarkup(null, 'gitlab'); | ||
|
||
break; | ||
case 'menu': | ||
$view = view("$this->viewNamespace::tools.menu"); | ||
$markup = $this->menuMarkup($this->bot->telegram); | ||
|
||
break; | ||
default: | ||
$this->bot->answerCallbackQuery(__('tg-notifier::app.unknown_callback')); | ||
|
||
return; | ||
} | ||
|
||
$this->bot->editMessageText($view, [ | ||
'reply_markup' => $markup, | ||
]); | ||
} | ||
|
||
/** | ||
* @return void | ||
* | ||
* @throws MessageIsEmptyException | ||
* @throws InvalidViewTemplateException | ||
* @throws BotException|CallbackException | ||
*/ | ||
public function handle(): void | ||
{ | ||
$callback = $this->bot->telegram->Callback_Data(); | ||
|
||
if (str_contains($callback, SettingConstant::SETTING_CUSTOM_EVENTS)) { | ||
$this->bot->eventHandle($callback); | ||
|
||
return; | ||
} | ||
|
||
if (str_contains($callback, SettingConstant::SETTING_BACK)) { | ||
$this->answerBackButton($callback); | ||
|
||
return; | ||
} | ||
|
||
$callback = str_replace(SettingConstant::SETTING_PREFIX, '', $callback); | ||
|
||
$settings = $this->bot->setting->getSettings(); | ||
if (array_key_exists($callback, $settings) | ||
&& $this->bot->setting->updateSetting( | ||
$callback, | ||
!$settings[$callback] | ||
) | ||
) { | ||
$this->bot->editMessageReplyMarkup([ | ||
'reply_markup' => $this->bot->settingMarkup(), | ||
]); | ||
} else { | ||
$this->bot->answerCallbackQuery(__('tg-notifier::app.unknown_callback')); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
|
||
namespace CSlant\LaravelTelegramGitNotifier\Services; | ||
|
||
use CSlant\LaravelTelegramGitNotifier\Traits\Markup; | ||
use CSlant\TelegramGitNotifier\Bot; | ||
use CSlant\TelegramGitNotifier\Exceptions\EntryNotFoundException; | ||
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException; | ||
|
||
class CommandService | ||
{ | ||
use Markup; | ||
|
||
private Bot $bot; | ||
|
||
protected string $viewNamespace = ''; | ||
|
||
public function __construct(Bot $bot) | ||
{ | ||
$this->bot = $bot; | ||
$this->viewNamespace = config('telegram-git-notifier.view.namespace'); | ||
} | ||
|
||
/** | ||
* @param Bot $bot | ||
* @return void | ||
* | ||
* @throws EntryNotFoundException | ||
*/ | ||
public function sendStartMessage(Bot $bot): void | ||
{ | ||
$reply = view( | ||
"$this->viewNamespace::tools.start", | ||
['first_name' => $bot->telegram->FirstName()] | ||
); | ||
$bot->sendPhoto( | ||
__DIR__.'/../../resources/images/start.png', | ||
['caption' => $reply] | ||
); | ||
} | ||
|
||
/** | ||
* @return void | ||
* | ||
* @throws EntryNotFoundException | ||
* @throws MessageIsEmptyException | ||
*/ | ||
public function handle(): void | ||
{ | ||
$text = $this->bot->telegram->Text(); | ||
|
||
switch ($text) { | ||
case '/start': | ||
$this->sendStartMessage($this->bot); | ||
|
||
break; | ||
case '/menu': | ||
$this->bot->sendMessage( | ||
view("$this->viewNamespace::tools.menu"), | ||
['reply_markup' => $this->menuMarkup($this->bot->telegram)] | ||
); | ||
|
||
break; | ||
case '/token': | ||
case '/id': | ||
case '/usage': | ||
case '/server': | ||
$this->bot->sendMessage(view("$this->viewNamespace::tools.".trim($text, '/'))); | ||
|
||
break; | ||
case '/settings': | ||
$this->bot->settingHandle(); | ||
|
||
break; | ||
case '/set_menu': | ||
$this->bot->setMyCommands(self::menuCommands()); | ||
|
||
break; | ||
default: | ||
$this->bot->sendMessage(__('tg-notifier::app.invalid_request')); | ||
} | ||
} | ||
|
||
/** | ||
* @return array[] | ||
*/ | ||
public static function menuCommands(): array | ||
{ | ||
return [ | ||
[ | ||
'command' => '/start', | ||
'description' => __('tg-notifier::tools/menu.start'), | ||
], [ | ||
'command' => '/menu', | ||
'description' => __('tg-notifier::tools/menu.menu'), | ||
], [ | ||
'command' => '/token', | ||
'description' => __('tg-notifier::tools/menu.token'), | ||
], [ | ||
'command' => '/id', | ||
'description' => __('tg-notifier::tools/menu.id'), | ||
], [ | ||
'command' => '/usage', | ||
'description' => __('tg-notifier::tools/menu.usage'), | ||
], [ | ||
'command' => '/server', | ||
'description' => __('tg-notifier::tools/menu.server'), | ||
], [ | ||
'command' => '/settings', | ||
'description' => __('tg-notifier::tools/menu.settings'), | ||
], | ||
]; | ||
} | ||
} |
Oops, something went wrong.