Skip to content

Commit 45be999

Browse files
committed
update handle send notification & env variables
1 parent 8236b89 commit 45be999

File tree

4 files changed

+13
-19
lines changed

4 files changed

+13
-19
lines changed

.env.example

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ APP_PORT=3000
88
TELEGRAM_BOT_TOKEN=###########:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
99
TELEGRAM_BOT_CHAT_ID=##########
1010

11-
# Set your telegram group chat ids here ( please use comma to separate )
12-
TELEGRAM_GROUP_CHAT_IDS="-############,-############"
11+
# Set telegram chat ids you want to receive notifications here.
12+
# You can add the owner bot id, group id, ... ( And please use comma to separate )
13+
TELEGRAM_NOTIFY_CHAT_IDS="-############,-############"
1314

1415
TIMEZONE=Asia/Ho_Chi_Minh

config/tg-notifier.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
'telegram-bot' => [
1111
'token' => $_ENV['TELEGRAM_BOT_TOKEN'] ?? '',
1212
'chat_id' => $_ENV['TELEGRAM_BOT_CHAT_ID'] ?? '',
13-
'gr_chat_ids' => explode(',', $_ENV['TELEGRAM_GROUP_CHAT_IDS'] ?? ''),
13+
'notify_chat_ids' => explode(',', $_ENV['TELEGRAM_NOTIFY_CHAT_IDS'] ?? ''),
1414

1515
'set_webhook_url' => $_ENV['SET_WEBHOOK_URL'] ?? $_ENV['APP_URL'] . '/setWebhook',
1616
],

index.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
use GuzzleHttp\Exception\GuzzleException;
43
use TelegramGithubNotify\App\Http\Actions\SendNotifyAction;
54
use Dotenv\Dotenv;
65

@@ -11,9 +10,5 @@
1110

1211
date_default_timezone_set(config('app.timezone'));
1312

14-
try {
15-
$sendNotifyAction = new SendNotifyAction();
16-
$sendNotifyAction->handle();
17-
} catch (GuzzleException $e) {
18-
echo $e->getMessage();
19-
}
13+
$sendNotifyAction = new SendNotifyAction();
14+
$sendNotifyAction();

src/Http/Actions/SendNotifyAction.php

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,15 @@ public function __construct()
2323
$this->telegramService = new TelegramService();
2424
$this->notificationService = new NotificationService();
2525

26-
$this->chatIds = config('telegram-bot.gr_chat_ids');
26+
$this->chatIds = config('telegram-bot.notify_chat_ids');
2727
}
2828

2929
/**
30-
* Send notify to telegram
30+
* Handle send notify to telegram action
3131
*
3232
* @return void
33-
* @throws GuzzleException
3433
*/
35-
public function handle(): void
34+
public function __invoke(): void
3635
{
3736
$this->telegramService->checkCallback();
3837
$chatMessageId = $this->telegramService->messageData['message']['chat']['id'] ?? '';
@@ -44,15 +43,14 @@ public function handle(): void
4443
}
4544

4645
// Send a result to all chat ids in config
47-
if (!in_array($chatMessageId, $this->chatIds)) {
46+
try {
4847
$this->notificationService->setPayload($this->request);
4948
foreach ($this->chatIds as $chatId) {
5049
$this->notificationService->sendNotify($chatId);
5150
}
52-
return;
51+
} catch (GuzzleException $e) {
52+
error_log($e->getMessage());
53+
$this->notificationService->accessDenied($this->telegramService); // Notify access denied to other chat ids
5354
}
54-
55-
// Notify access denied to other chat ids
56-
$this->notificationService->accessDenied($this->telegramService);
5755
}
5856
}

0 commit comments

Comments
 (0)