Skip to content

Commit c87866d

Browse files
authored
Merge pull request #69 from tanhongit/main
update validate event and bot setting
2 parents 89392d9 + 0fbafc0 commit c87866d

File tree

15 files changed

+144
-67
lines changed

15 files changed

+144
-67
lines changed

common/helpers.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,15 @@ function enable_all_events(): bool
106106
return (new SettingHelper())->enableAllEvents();
107107
}
108108
}
109+
110+
if (!function_exists('setting_config')) {
111+
/**
112+
* Return setting config
113+
*
114+
* @return array
115+
*/
116+
function setting_config(): array
117+
{
118+
return (new SettingHelper())->getSettingConfig();
119+
}
120+
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
/id - To get your Chat ID.
55
/token - To get this bot token.
66
/usage - How to use me.
7-
/help - To show this Message.
7+
/settings - Settings github notify.
8+
/menu - To get this menu.
89

910
Select a command:

resources/tools/settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<b>Settings for github notify.</b>

resources/tools/start.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@
88

99
Hey <b><?= $first_name ?></b>,
1010

11-
I can send you notifications from your GitHub Repository instantly to your Telegram. use /help for more information about me.
11+
I can send you notifications from your GitHub Repository instantly to your Telegram.
12+
Use /menu for more information about me.

setWebhook.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/Http/Actions/SendNotifyAction.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public function __construct()
3636
*/
3737
public function __invoke(): void
3838
{
39-
$this->telegramService->checkCallback();
4039
$chatMessageId = $this->telegramService->messageData['message']['chat']['id'] ?? '';
4140

4241
if (!empty($chatMessageId)) {
@@ -47,7 +46,10 @@ public function __invoke(): void
4746
// Send a GitHub event result to all chat ids in env
4847
if (!is_null($this->request->server->get('HTTP_X_GITHUB_EVENT'))) {
4948
$this->sendNotification();
49+
return;
5050
}
51+
52+
$this->telegramService->checkCallback();
5153
}
5254

5355
/**
@@ -62,7 +64,7 @@ public function handleEventInTelegram(string $chatMessageId): void
6264
return;
6365
}
6466

65-
// Notify access denied to other chat ids
67+
// Notify access denied to other/invalid chat ids
6668
if (!in_array($chatMessageId, $this->chatIds)) {
6769
$this->notificationService->accessDenied($this->telegramService);
6870
}

src/Http/Actions/SetWebhookAction.php

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/Http/Actions/WebhookAction.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace TelegramGithubNotify\App\Http\Actions;
4+
5+
class WebhookAction
6+
{
7+
protected string $token;
8+
9+
public function __construct()
10+
{
11+
$this->token = config('telegram-bot.token');
12+
}
13+
14+
/**
15+
* Set webhook for telegram bot
16+
*
17+
* @return false|string
18+
*/
19+
public function set(): false|string
20+
{
21+
$appUrl = config('app.url');
22+
$url = "https://api.telegram.org/bot{$this->token}/setWebhook?url={$appUrl}";
23+
24+
return file_get_contents($url);
25+
}
26+
27+
/**
28+
* Delete webhook for telegram bot
29+
*
30+
* @return false|string
31+
*/
32+
public function delete(): false|string
33+
{
34+
$url = "https://api.telegram.org/bot{$this->token}/deleteWebhook";
35+
36+
return file_get_contents($url);
37+
}
38+
}

src/Services/AppService.php

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace TelegramGithubNotify\App\Services;
44

5+
use Exception;
56
use Telegram;
67

78
class AppService
@@ -29,17 +30,24 @@ public function sendMessage(string $message = '', array $options = [], string $s
2930
'parse_mode' => 'HTML'
3031
);
3132

32-
if ($sendType === 'Message') {
33-
$content['text'] = $message;
34-
} elseif ($sendType === 'Photo' && !empty($options)) {
35-
$content['photo'] = $options['photo'];
36-
$content['caption'] = $message;
33+
try {
34+
if ($sendType === 'Message') {
35+
$content['text'] = $message;
36+
} elseif ($sendType === 'Photo' && !empty($options)) {
37+
$content['photo'] = $options['photo'];
38+
$content['caption'] = $message;
39+
}
40+
41+
if (!empty($options) && isset($options['reply_markup'])) {
42+
$content['reply_markup'] = $this->telegram->buildInlineKeyBoard(
43+
$options['reply_markup']
44+
);
45+
}
46+
47+
$this->telegram->{'send' . $sendType}($content);
48+
} catch (Exception $e) {
49+
$content['text'] = $e->getMessage();
50+
$this->telegram->sendMessage($content);
3751
}
38-
39-
if (!empty($options) && isset($options['reply_markup'])) {
40-
$content['reply_markup'] = $this->telegram->buildInlineKeyBoard($options['reply_markup']);
41-
}
42-
43-
$this->telegram->{'send' . $sendType}($content);
4452
}
4553
}

src/Services/EventSettingService.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ class EventSettingService
1515
*/
1616
public function validateAccessEvent(Request $request, $payload): bool
1717
{
18+
if (enable_all_events()) {
19+
return true;
20+
}
21+
1822
$eventConfig = event_config();
1923

2024
if (empty($eventConfig)) {

0 commit comments

Comments
 (0)