Skip to content

Commit 605049f

Browse files
committed
feat: add callback and handle command feature in the bot
- core is cslant/telegram-git-notifier - clone logic in cslant/telegram-git-notifier-app
1 parent c2157c0 commit 605049f

File tree

5 files changed

+265
-0
lines changed

5 files changed

+265
-0
lines changed

lang/en/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22

33
return [
44
'by' => 'by',
5+
'unknown_callback' => 'Unknown Callback. Something went wrong!',
56
];

src/Http/Actions/IndexAction.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44

55
use CSlant\LaravelTelegramGitNotifier\Services\NotificationService;
66
use CSlant\TelegramGitNotifier\Bot;
7+
use CSlant\TelegramGitNotifier\Exceptions\BotException;
8+
use CSlant\TelegramGitNotifier\Exceptions\CallbackException;
79
use CSlant\TelegramGitNotifier\Exceptions\ConfigFileException;
10+
use CSlant\TelegramGitNotifier\Exceptions\EntryNotFoundException;
811
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
912
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
1013
use CSlant\TelegramGitNotifier\Exceptions\SendNotificationException;
1114
use CSlant\TelegramGitNotifier\Notifier;
15+
use CSlant\LaravelTelegramGitNotifier\Services\CallbackService;
16+
use CSlant\LaravelTelegramGitNotifier\Services\CommandService;
1217
use GuzzleHttp\Client;
1318
use Symfony\Component\HttpFoundation\Request;
1419
use Telegram;
@@ -43,9 +48,26 @@ public function __construct()
4348
* @throws InvalidViewTemplateException
4449
* @throws MessageIsEmptyException
4550
* @throws SendNotificationException
51+
* @throws BotException
52+
* @throws CallbackException
53+
* @throws EntryNotFoundException
4654
*/
4755
public function __invoke(): void
4856
{
57+
if ($this->bot->isCallback()) {
58+
$callbackAction = new CallbackService($this->bot);
59+
$callbackAction->handle();
60+
61+
return;
62+
}
63+
64+
if ($this->bot->isMessage() && $this->bot->isOwner()) {
65+
$commandAction = new CommandService($this->bot);
66+
$commandAction->handle();
67+
68+
return;
69+
}
70+
4971
$sendNotification = new NotificationService(
5072
$this->notifier,
5173
$this->bot->setting

src/Services/CallbackService.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
3+
namespace CSlant\LaravelTelegramGitNotifier\Services;
4+
5+
use CSlant\TelegramGitNotifier\Bot;
6+
use CSlant\TelegramGitNotifier\Constants\SettingConstant;
7+
use CSlant\TelegramGitNotifier\Exceptions\BotException;
8+
use CSlant\TelegramGitNotifier\Exceptions\CallbackException;
9+
use CSlant\TelegramGitNotifier\Exceptions\InvalidViewTemplateException;
10+
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
11+
use CSlant\LaravelTelegramGitNotifier\Traits\Markup;
12+
13+
class CallbackService
14+
{
15+
use Markup;
16+
17+
private Bot $bot;
18+
19+
protected string $viewNamespace = '';
20+
21+
public function __construct(Bot $bot)
22+
{
23+
$this->bot = $bot;
24+
$this->viewNamespace = config('telegram-git-notifier.view.namespace');
25+
}
26+
27+
/**
28+
* Answer the back button
29+
*
30+
* @param string $callback
31+
*
32+
* @return void
33+
* @throws MessageIsEmptyException
34+
* @throws BotException
35+
* @throws CallbackException
36+
*/
37+
public function answerBackButton(string $callback): void
38+
{
39+
$callback = str_replace(SettingConstant::SETTING_BACK, '', $callback);
40+
switch ($callback) {
41+
case 'settings':
42+
$view = view("$this->viewNamespace::tools.settings");
43+
$markup = $this->bot->settingMarkup();
44+
45+
break;
46+
case 'settings.custom_events.github':
47+
$view = view("$this->viewNamespace::tools.custom_event", ['platform' => 'github']);
48+
$markup = $this->bot->eventMarkup();
49+
50+
break;
51+
case 'settings.custom_events.gitlab':
52+
$view = view("$this->viewNamespace::tools.custom_event", ['platform' => 'gitlab']);
53+
$markup = $this->bot->eventMarkup(null, 'gitlab');
54+
55+
break;
56+
case 'menu':
57+
$view = view("$this->viewNamespace::tools.menu");
58+
$markup = $this->menuMarkup($this->bot->telegram);
59+
60+
break;
61+
default:
62+
$this->bot->answerCallbackQuery(__('tg-notifier::app.unknown_callback'));
63+
64+
return;
65+
}
66+
67+
$this->bot->editMessageText($view, [
68+
'reply_markup' => $markup,
69+
]);
70+
}
71+
72+
/**
73+
* @return void
74+
* @throws MessageIsEmptyException
75+
* @throws InvalidViewTemplateException
76+
* @throws BotException|CallbackException
77+
*/
78+
public function handle(): void
79+
{
80+
$callback = $this->bot->telegram->Callback_Data();
81+
82+
if (str_contains($callback, SettingConstant::SETTING_CUSTOM_EVENTS)) {
83+
$this->bot->eventHandle($callback);
84+
85+
return;
86+
}
87+
88+
if (str_contains($callback, SettingConstant::SETTING_BACK)) {
89+
$this->answerBackButton($callback);
90+
91+
return;
92+
}
93+
94+
$callback = str_replace(SettingConstant::SETTING_PREFIX, '', $callback);
95+
96+
$settings = $this->bot->setting->getSettings();
97+
if (array_key_exists($callback, $settings)
98+
&& $this->bot->setting->updateSetting(
99+
$callback,
100+
!$settings[$callback]
101+
)
102+
) {
103+
$this->bot->editMessageReplyMarkup([
104+
'reply_markup' => $this->bot->settingMarkup(),
105+
]);
106+
} else {
107+
$this->bot->answerCallbackQuery(__('tg-notifier::app.unknown_callback'));
108+
}
109+
}
110+
}

src/Services/CommandService.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
<?php
2+
3+
namespace CSlant\LaravelTelegramGitNotifier\Services;
4+
5+
use CSlant\TelegramGitNotifier\Bot;
6+
use CSlant\TelegramGitNotifier\Exceptions\EntryNotFoundException;
7+
use CSlant\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
8+
use CSlant\LaravelTelegramGitNotifier\Traits\Markup;
9+
10+
class CommandService
11+
{
12+
use Markup;
13+
14+
public const MENU_COMMANDS
15+
= [
16+
[
17+
'command' => '/start',
18+
'description' => 'Welcome to the bot',
19+
], [
20+
'command' => '/menu',
21+
'description' => 'Show menu of the bot',
22+
], [
23+
'command' => '/token',
24+
'description' => 'Show token of the bot',
25+
], [
26+
'command' => '/id',
27+
'description' => 'Show the ID of the current chat',
28+
], [
29+
'command' => '/usage',
30+
'description' => 'Show step by step usage',
31+
], [
32+
'command' => '/server',
33+
'description' => 'To get Server Information',
34+
], [
35+
'command' => '/settings',
36+
'description' => 'Go to settings of the bot',
37+
],
38+
];
39+
40+
private Bot $bot;
41+
42+
protected string $viewNamespace = '';
43+
44+
public function __construct(Bot $bot)
45+
{
46+
$this->bot = $bot;
47+
$this->viewNamespace = config('telegram-git-notifier.view.namespace');
48+
}
49+
50+
/**
51+
* @param Bot $bot
52+
*
53+
* @return void
54+
* @throws EntryNotFoundException
55+
*/
56+
public function sendStartMessage(Bot $bot): void
57+
{
58+
$reply = view(
59+
"$this->viewNamespace::tools.start",
60+
['first_name' => $bot->telegram->FirstName()]
61+
);
62+
$bot->sendPhoto(
63+
__DIR__ . '/../../resources/images/start.png',
64+
['caption' => $reply]
65+
);
66+
}
67+
68+
/**
69+
* @return void
70+
* @throws EntryNotFoundException
71+
* @throws MessageIsEmptyException
72+
*/
73+
public function handle(): void
74+
{
75+
$text = $this->bot->telegram->Text();
76+
77+
switch ($text) {
78+
case '/start':
79+
$this->sendStartMessage($this->bot);
80+
81+
break;
82+
case '/menu':
83+
$this->bot->sendMessage(
84+
view("$this->viewNamespace::tools.menu"),
85+
['reply_markup' => $this->menuMarkup($this->bot->telegram)]
86+
);
87+
88+
break;
89+
case '/token':
90+
case '/id':
91+
case '/usage':
92+
case '/server':
93+
$this->bot->sendMessage(view('tools.' . trim($text, '/')));
94+
95+
break;
96+
case '/settings':
97+
$this->bot->settingHandle();
98+
99+
break;
100+
case '/set_menu':
101+
$this->bot->setMyCommands(CommandService::MENU_COMMANDS);
102+
103+
break;
104+
default:
105+
$this->bot->sendMessage('🤨 Invalid Request!');
106+
}
107+
}
108+
}

src/Traits/Markup.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace CSlant\LaravelTelegramGitNotifier\Traits;
4+
5+
use Telegram;
6+
7+
trait Markup
8+
{
9+
/**
10+
* Generate menu markup
11+
*
12+
* @return array[]
13+
*/
14+
public function menuMarkup(Telegram $telegram): array
15+
{
16+
return [
17+
[
18+
$telegram->buildInlineKeyBoardButton('🗨 Discussion', config('telegram-git-notifier.author.discussion')),
19+
], [
20+
$telegram->buildInlineKeyBoardButton('💠 Source Code', config('telegram-git-notifier.author.source_code')),
21+
],
22+
];
23+
}
24+
}

0 commit comments

Comments
 (0)