Skip to content

Commit 8ed028b

Browse files
authored
Merge pull request #23 from tanhongit/bot-tools
(#30) update answer callback queries
2 parents 88684aa + d6e3078 commit 8ed028b

File tree

9 files changed

+219
-47
lines changed

9 files changed

+219
-47
lines changed

resources/tools/event.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Please select an event to enable or disable notifications.
2+
<b>Click and configure child events if the option has theicon.</b>
3+
---
4+
Please check the <a href="https://docs.github.com/en/developers/webhooks-and-events/webhook-events-and-payloads">GitHub documentation</a> for more information about events.

src/Http/Actions/SendNotifyAction.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ public function __invoke(): void
5656
* @param string $chatMessageId
5757
* @return void
5858
*/
59-
public function handleEventInTelegram(string $chatMessageId): void
59+
private function handleEventInTelegram(string $chatMessageId): void
6060
{
6161
// Send a result to only the bot owner
62-
if ($chatMessageId == config('telegram-bot.chat_id')) {
62+
if ($chatMessageId == $this->telegramService->chatId) {
6363
$this->telegramService->telegramToolHandler($this->telegramService->messageData['message']['text']);
6464
return;
6565
}
@@ -73,11 +73,11 @@ public function handleEventInTelegram(string $chatMessageId): void
7373
/**
7474
* @return void
7575
*/
76-
protected function sendNotification(): void
76+
private function sendNotification(): void
7777
{
7878
$payload = $this->notificationService->setPayload($this->request);
7979

80-
if (!$this->eventSettingService->validateAccessEvent($this->request, $payload)) {
80+
if (empty($payload) || !$this->eventSettingService->validateAccessEvent($this->request, $payload)) {
8181
return;
8282
}
8383

src/Models/Event.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ class Event
66
{
77
public const EVENT_FILE = __DIR__ . '/../../storage/tg-event.json';
88

9+
public const EVENT_PREFIX = Setting::SETTING_PREFIX . '.event.';
10+
911
public array $eventConfig = [];
1012

1113
public function __construct()
@@ -20,7 +22,7 @@ public function __construct()
2022
*
2123
* @return void
2224
*/
23-
public function setEventConfig(): void
25+
private function setEventConfig(): void
2426
{
2527
$json = file_get_contents(self::EVENT_FILE);
2628
$this->eventConfig = json_decode($json, true);

src/Models/Setting.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct()
2525
*
2626
* @return void
2727
*/
28-
public function setSettingConfig(): void
28+
private function setSettingConfig(): void
2929
{
3030
$json = file_get_contents(self::SETTING_FILE);
3131
$this->settings = json_decode($json, true);

src/Services/AppService.php

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,12 @@ class AppService
99
{
1010
public Telegram $telegram;
1111

12+
public string $chatId;
13+
1214
public function __construct()
1315
{
1416
$this->telegram = new Telegram(config('telegram-bot.token'));
17+
$this->chatId = config('telegram-bot.chat_id');
1518
}
1619

1720
/**
@@ -25,7 +28,7 @@ public function __construct()
2528
public function sendMessage(string $message = '', array $options = [], string $sendType = 'Message'): void
2629
{
2730
$content = array(
28-
'chat_id' => config('telegram-bot.chat_id'),
31+
'chat_id' => $this->chatId,
2932
'disable_web_page_preview' => true,
3033
'parse_mode' => 'HTML'
3134
);
@@ -47,4 +50,91 @@ public function sendMessage(string $message = '', array $options = [], string $s
4750
error_log($e->getMessage());
4851
}
4952
}
53+
54+
/**
55+
* Send callback response to telegram (show alert)
56+
*
57+
* @param string|null $text
58+
* @return void
59+
*/
60+
public function answerCallbackQuery(string $text = null): void
61+
{
62+
try {
63+
$this->telegram->answerCallbackQuery([
64+
'callback_query_id' => $this->telegram->Callback_ID(),
65+
'text' => $text,
66+
'show_alert' => true
67+
]);
68+
} catch (Exception $e) {
69+
error_log($e->getMessage());
70+
}
71+
}
72+
73+
/**
74+
* Edit message from telegram
75+
* (Edit message text and reply markup)
76+
*
77+
* @param string|null $text
78+
* @param array $options
79+
* @return void
80+
*/
81+
public function editMessageText(?string $text = null, array $options = []): void
82+
{
83+
try {
84+
$content = [
85+
'text' => $text ?? $this->Callback_Message_Text()
86+
];
87+
$content = array_merge($content, $this->setContentEditMessage($options));
88+
89+
$this->telegram->editMessageText($content);
90+
} catch (Exception $e) {
91+
error_log($e->getMessage());
92+
}
93+
}
94+
95+
/**
96+
* Edit message reply markup from a telegram
97+
* (Edit message reply markup only)
98+
*
99+
* @param array $options
100+
* @return void
101+
*/
102+
public function editMessageReplyMarkup(array $options = []): void
103+
{
104+
try {
105+
$this->telegram->editMessageReplyMarkup($this->setContentEditMessage($options));
106+
} catch (Exception $e) {
107+
error_log($e->getMessage());
108+
}
109+
}
110+
111+
/**
112+
* Get the text from callback message
113+
*
114+
* @return string
115+
*/
116+
public function Callback_Message_Text(): string
117+
{
118+
return $this->telegram->Callback_Message()['text'];
119+
}
120+
121+
/**
122+
* @param array $options
123+
* @return array
124+
*/
125+
public function setContentEditMessage(array $options = []): array
126+
{
127+
$content = array(
128+
'chat_id' => $this->telegram->Callback_ChatID(),
129+
'message_id' => $this->telegram->MessageID(),
130+
'disable_web_page_preview' => true,
131+
'parse_mode' => 'HTML',
132+
);
133+
134+
if (!empty($options) && isset($options['reply_markup'])) {
135+
$content['reply_markup'] = $this->telegram->buildInlineKeyBoard($options['reply_markup']);
136+
}
137+
138+
return $content;
139+
}
50140
}

src/Services/EventService.php

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,23 @@
33
namespace TelegramGithubNotify\App\Services;
44

55
use Symfony\Component\HttpFoundation\Request;
6+
use TelegramGithubNotify\App\Models\Event;
67
use TelegramGithubNotify\App\Models\Setting;
78

8-
class EventService
9+
class EventService extends AppService
910
{
11+
public const LINE_ITEM_COUNT = 2;
12+
1013
protected Setting $setting;
1114

15+
protected Event $event;
16+
1217
public function __construct()
1318
{
19+
parent::__construct();
20+
1421
$this->setting = new Setting();
22+
$this->event = new Event();
1523
}
1624

1725
/**
@@ -31,7 +39,7 @@ public function validateAccessEvent(Request $request, $payload): bool
3139
return true;
3240
}
3341

34-
$eventConfig = event_config();
42+
$eventConfig = $this->event->getEventConfig();
3543

3644
$event = singularity($request->server->get('HTTP_X_GITHUB_EVENT'));
3745
$eventConfig = $eventConfig[$event] ?? false;
@@ -47,7 +55,57 @@ public function validateAccessEvent(Request $request, $payload): bool
4755
return (bool)$eventConfig;
4856
}
4957

50-
public function eventHandle()
58+
/**
59+
* Create markup for select event
60+
*
61+
* @param string|null $event
62+
* @return array
63+
*/
64+
public function eventMarkup(?string $event = null): array
65+
{
66+
$replyMarkup = $replyMarkupItem = [];
67+
68+
$events = $event === null ? $this->event->eventConfig : $this->event->eventConfig[$event];
69+
70+
foreach ($events as $event => $value) {
71+
if (count($replyMarkupItem) === self::LINE_ITEM_COUNT) {
72+
$replyMarkup[] = $replyMarkupItem;
73+
$replyMarkupItem = [];
74+
}
75+
76+
$callbackData = $this->event::EVENT_PREFIX . $event;
77+
78+
if (is_array($value)) {
79+
$eventName = '' . $event;
80+
$callbackData .= '.child';
81+
} elseif ($value) {
82+
$eventName = '' . $event;
83+
} else {
84+
$eventName = '' . $event;
85+
}
86+
87+
$replyMarkupItem[] = $this->telegram->buildInlineKeyBoardButton($eventName, '', $callbackData);
88+
}
89+
90+
// add last item to a reply_markup array
91+
if (count($replyMarkupItem) > 0) {
92+
$replyMarkup[] = $replyMarkupItem;
93+
}
94+
95+
$replyMarkup[] = [$this->telegram->buildInlineKeyBoardButton('🔙 Back', '', 'back')];
96+
$replyMarkup[] = [$this->telegram->buildInlineKeyBoardButton('📚 Menu', '', $this->setting::SETTING_PREFIX . '.back.menu')];
97+
98+
return $replyMarkup;
99+
}
100+
101+
/**
102+
* @return void
103+
*/
104+
public function eventHandle(): void
51105
{
106+
$this->editMessageText(
107+
view('tools.event'),
108+
['reply_markup' => $this->eventMarkup()]
109+
);
52110
}
53111
}

src/Services/NotificationService.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88

99
class NotificationService
1010
{
11-
public mixed $payload;
11+
protected mixed $payload;
1212

13-
public string $message = "";
13+
protected string $message = "";
1414

1515
/**
1616
* Notify access denied to other chat ids
@@ -38,8 +38,7 @@ public function accessDenied(TelegramService $telegramService, string $chatId =
3838
public function setPayload(Request $request)
3939
{
4040
if (is_null($request->server->get('HTTP_X_GITHUB_EVENT'))) {
41-
echo 'invalid request';
42-
exit;
41+
return null;
4342
}
4443

4544
$this->payload = json_decode($request->request->get('payload'));
@@ -54,7 +53,7 @@ public function setPayload(Request $request)
5453
* @param string $typeEvent
5554
* @return void
5655
*/
57-
public function setMessage(string $typeEvent): void
56+
private function setMessage(string $typeEvent): void
5857
{
5958
if (isset($this->payload->action) && !empty($this->payload->action)) {
6059
$this->message = view(

0 commit comments

Comments
 (0)