Skip to content

Commit c0d940a

Browse files
authored
Merge pull request #74 from tanhongit/main
Event settings
2 parents 46d1a2a + 29ead91 commit c0d940a

15 files changed

+517
-144
lines changed

README.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
- PHP ^8.0
1717
- Composer
1818
- Telegram Bot
19-
- SSL Certificate
2019

2120
## Installation
2221

@@ -70,12 +69,14 @@ APP_URL=https://123456789.ngrok.io
7069

7170
### Set the webhook
7271

73-
#### Set the webhook from the source code
72+
We have two ways to set the webhook:
73+
74+
#### 1. Set the webhook from this project
7475

7576
After setting up your domain and SSL certificate, you need to set up the webhook for your bot. Go to:
7677

7778
```text
78-
<APP_URL>/setWebhook.php
79+
<APP_URL>/webhook/set.php
7980
```
8081

8182
> **Note:** Replace `<APP_URL>` with your app URL in .env file.
@@ -86,7 +87,7 @@ If you see the following message, it means that the webhook has been sent succes
8687
{"ok":true,"result":true,"description":"Webhook was set"}
8788
```
8889

89-
#### Set the webhook manually
90+
#### 2. Set the webhook manually
9091

9192
If you want to set the webhook manually, you can use the following URL:
9293

@@ -140,4 +141,4 @@ Now you can send a message to your bot, and you will receive a notification.
140141

141142
Here is the first notification you will receive: ♻️ **Connection Successful**
142143

143-
> **You can add multiple webhooks to your repository.**
144+
> **Note: You can add multiple webhooks to your repository. Please similarly set up the webhook for each repository.**

common/helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function event_config(): array
103103
*/
104104
function all_events_notify(): bool
105105
{
106-
return (new Setting())->allEventsNotify();
106+
return (new Setting())->allEventsNotifyStatus();
107107
}
108108
}
109109

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
/**
3+
* @var string $event
4+
*/
5+
6+
?>
7+
Setting actions for the event: <b><?= $event ?></b>
8+
Please select an action of this event to enable or disable notifications:

resources/tools/custom_events.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Go to 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.
2+
---
3+
<b>Click and configure child events if the option has theicon.</b>
4+
And select an event to enable or disable notifications:

resources/tools/menu.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/id - To get your Chat ID.
55
/token - To get this bot token.
66
/usage - How to use me.
7-
/settings - Settings github notify.
7+
/settings - Settings GitHub notify.
88
/menu - To get this menu.
99

1010
Select a button:

resources/tools/settings.php

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

resources/tools/start.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
Hey <b><?= $first_name ?></b>,
1010

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

src/Http/Actions/SendNotifyAction.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,30 @@ public function __construct()
3636
*/
3737
public function __invoke(): void
3838
{
39-
$chatMessageId = $this->telegramService->messageData['message']['chat']['id'] ?? '';
40-
41-
if (!empty($chatMessageId)) {
42-
$this->handleEventInTelegram($chatMessageId);
43-
return;
44-
}
45-
4639
// Send a GitHub event result to all chat ids in env
4740
if (!is_null($this->request->server->get('HTTP_X_GITHUB_EVENT'))) {
4841
$this->sendNotification();
4942
return;
5043
}
5144

45+
// Telegram bot handler
46+
$chatMessageId = $this->telegramService->messageData['message']['chat']['id'] ?? '';
47+
if (!empty($chatMessageId)) {
48+
$this->handleEventInTelegram($chatMessageId);
49+
return;
50+
}
51+
5252
$this->telegramService->checkCallback();
5353
}
5454

5555
/**
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: 34 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_CUSTOM_EVENTS . '.evt.';
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);
@@ -35,4 +37,35 @@ public function getEventConfig(): array
3537
{
3638
return $this->eventConfig;
3739
}
40+
41+
/**
42+
* Update event config by event and action
43+
*
44+
* @param string $event
45+
* @param string|null $action
46+
* @return void
47+
*/
48+
public function updateEvent(string $event, string|null $action): void
49+
{
50+
if (!empty($action)) {
51+
$this->eventConfig[$event][$action] = !$this->eventConfig[$event][$action];
52+
} else {
53+
$this->eventConfig[$event] = !$this->eventConfig[$event];
54+
}
55+
56+
$this->saveEventConfig();
57+
}
58+
59+
/**
60+
* Save event config
61+
*
62+
* @return void
63+
*/
64+
private function saveEventConfig(): void
65+
{
66+
if (file_exists(self::EVENT_FILE)) {
67+
$json = json_encode($this->eventConfig, JSON_PRETTY_PRINT);
68+
file_put_contents(self::EVENT_FILE, $json, LOCK_EX);
69+
}
70+
}
3871
}

src/Models/Setting.php

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
class Setting
66
{
77
public const SETTING_FILE = __DIR__ . '/../../storage/tg-setting.json';
8+
public const SETTING_PREFIX = 'stg.';
9+
10+
public const SETTING_IS_NOTIFIED = self::SETTING_PREFIX . 'is_notified';
11+
public const SETTING_ALL_EVENTS_NOTIFY = self::SETTING_PREFIX . 'all_events_notify';
12+
public const SETTING_CUSTOM_EVENTS = self::SETTING_PREFIX . 'cus';
13+
public const SETTING_BACK = self::SETTING_PREFIX . 'back.';
814

915
public array $settings = [];
1016

@@ -20,7 +26,7 @@ public function __construct()
2026
*
2127
* @return void
2228
*/
23-
public function setSettingConfig(): void
29+
private function setSettingConfig(): void
2430
{
2531
$json = file_get_contents(self::SETTING_FILE);
2632
$this->settings = json_decode($json, true);
@@ -39,12 +45,71 @@ public function getSettingConfig(): array
3945
/**
4046
* @return bool
4147
*/
42-
public function allEventsNotify(): bool
48+
public function allEventsNotifyStatus(): bool
4349
{
4450
if (!empty($this->settings) && $this->settings['all_events_notify'] === true) {
4551
return true;
4652
}
4753

4854
return false;
4955
}
56+
57+
/**
58+
* @return bool
59+
*/
60+
public function isNotified(): bool
61+
{
62+
if (!empty($this->settings) && $this->settings['is_notified'] === true) {
63+
return true;
64+
}
65+
66+
return false;
67+
}
68+
69+
/**
70+
* Update setting item value and save to file
71+
*
72+
* @param string $settingName
73+
* @param $settingValue
74+
* @return bool
75+
*/
76+
public function updateSettingItem(string $settingName, $settingValue = null): bool
77+
{
78+
$keys = explode('.', $settingName);
79+
$lastKey = array_pop($keys);
80+
$nestedSettings = &$this->settings;
81+
82+
foreach ($keys as $key) {
83+
if (!isset($nestedSettings[$key]) || !is_array($nestedSettings[$key])) {
84+
return false;
85+
}
86+
$nestedSettings = &$nestedSettings[$key];
87+
}
88+
89+
if (isset($nestedSettings[$lastKey])) {
90+
$nestedSettings[$lastKey] = $settingValue ?? !$nestedSettings[$lastKey];
91+
if ($this->saveSettingsToFile()) {
92+
return true;
93+
}
94+
}
95+
96+
return false;
97+
}
98+
99+
/**
100+
* Save settings to json file
101+
*
102+
* @return bool
103+
*/
104+
private function saveSettingsToFile(): bool
105+
{
106+
if (file_exists(self::SETTING_FILE)) {
107+
$json = json_encode($this->settings, JSON_PRETTY_PRINT);
108+
file_put_contents(self::SETTING_FILE, $json, LOCK_EX);
109+
110+
return true;
111+
}
112+
113+
return false;
114+
}
50115
}

0 commit comments

Comments
 (0)