Skip to content

Commit 29ead91

Browse files
authored
Merge pull request #29 from tanhongit/bot-tools
Handle setting for the action of events
2 parents 8ed028b + d77dc75 commit 29ead91

14 files changed

+304
-130
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.**
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/event.php

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

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/Models/Event.php

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

9-
public const EVENT_PREFIX = Setting::SETTING_PREFIX . '.event.';
9+
public const EVENT_PREFIX = Setting::SETTING_CUSTOM_EVENTS . '.evt.';
1010

1111
public array $eventConfig = [];
1212

@@ -37,4 +37,35 @@ public function getEventConfig(): array
3737
{
3838
return $this->eventConfig;
3939
}
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+
}
4071
}

src/Models/Setting.php

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
class Setting
66
{
77
public const SETTING_FILE = __DIR__ . '/../../storage/tg-setting.json';
8-
public const SETTING_PREFIX = 'setting.';
8+
public const SETTING_PREFIX = 'stg.';
99

1010
public const SETTING_IS_NOTIFIED = self::SETTING_PREFIX . 'is_notified';
1111
public const SETTING_ALL_EVENTS_NOTIFY = self::SETTING_PREFIX . 'all_events_notify';
12-
public const SETTING_CUSTOM_EVENTS = self::SETTING_PREFIX . 'custom_events';
12+
public const SETTING_CUSTOM_EVENTS = self::SETTING_PREFIX . 'cus';
13+
public const SETTING_BACK = self::SETTING_PREFIX . 'back.';
1314

1415
public array $settings = [];
1516

@@ -64,4 +65,51 @@ public function isNotified(): bool
6465

6566
return false;
6667
}
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+
}
67115
}

src/Services/AppService.php

Lines changed: 33 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,16 @@ public function sendMessage(string $message = '', array $options = [], string $s
3333
'parse_mode' => 'HTML'
3434
);
3535

36-
try {
37-
if ($sendType === 'Message') {
38-
$content['text'] = $message;
39-
} elseif ($sendType === 'Photo' && !empty($options)) {
40-
$content['photo'] = $options['photo'];
41-
$content['caption'] = $message;
42-
}
43-
44-
if (!empty($options) && isset($options['reply_markup'])) {
45-
$content['reply_markup'] = $this->telegram->buildInlineKeyBoard($options['reply_markup']);
46-
}
47-
48-
$this->telegram->{'send' . $sendType}($content);
49-
} catch (Exception $e) {
50-
error_log($e->getMessage());
36+
if ($sendType === 'Message') {
37+
$content['text'] = $message;
38+
} elseif ($sendType === 'Photo') {
39+
$content['photo'] = $options['photo'] ?? null;
40+
$content['caption'] = $message;
5141
}
42+
43+
$content['reply_markup'] = $options['reply_markup'] ? $this->telegram->buildInlineKeyBoard($options['reply_markup']) : null;
44+
45+
$this->telegram->{'send' . $sendType}($content);
5246
}
5347

5448
/**
@@ -81,10 +75,9 @@ public function answerCallbackQuery(string $text = null): void
8175
public function editMessageText(?string $text = null, array $options = []): void
8276
{
8377
try {
84-
$content = [
78+
$content = array_merge([
8579
'text' => $text ?? $this->Callback_Message_Text()
86-
];
87-
$content = array_merge($content, $this->setContentEditMessage($options));
80+
], $this->setCallbackContentMessage($options));
8881

8982
$this->telegram->editMessageText($content);
9083
} catch (Exception $e) {
@@ -102,7 +95,7 @@ public function editMessageText(?string $text = null, array $options = []): void
10295
public function editMessageReplyMarkup(array $options = []): void
10396
{
10497
try {
105-
$this->telegram->editMessageReplyMarkup($this->setContentEditMessage($options));
98+
$this->telegram->editMessageReplyMarkup($this->setCallbackContentMessage($options));
10699
} catch (Exception $e) {
107100
error_log($e->getMessage());
108101
}
@@ -119,10 +112,12 @@ public function Callback_Message_Text(): string
119112
}
120113

121114
/**
115+
* Create content for a callback message
116+
*
122117
* @param array $options
123118
* @return array
124119
*/
125-
public function setContentEditMessage(array $options = []): array
120+
public function setCallbackContentMessage(array $options = []): array
126121
{
127122
$content = array(
128123
'chat_id' => $this->telegram->Callback_ChatID(),
@@ -131,10 +126,25 @@ public function setContentEditMessage(array $options = []): array
131126
'parse_mode' => 'HTML',
132127
);
133128

134-
if (!empty($options) && isset($options['reply_markup'])) {
135-
$content['reply_markup'] = $this->telegram->buildInlineKeyBoard($options['reply_markup']);
136-
}
129+
$content['reply_markup'] = $options['reply_markup'] ? $this->telegram->buildInlineKeyBoard($options['reply_markup']) : null;
137130

138131
return $content;
139132
}
133+
134+
/**
135+
* Generate menu markup
136+
*
137+
* @return array[]
138+
*/
139+
public function menuMarkup(): array
140+
{
141+
return [
142+
[
143+
$this->telegram->buildInlineKeyBoardButton("📰 About", "", "about", ""),
144+
$this->telegram->buildInlineKeyBoardButton("📞 Contact", config('author.contact'))
145+
], [
146+
$this->telegram->buildInlineKeyBoardButton("💠 Source Code", config('author.source_code'))
147+
]
148+
];
149+
}
140150
}

0 commit comments

Comments
 (0)