Skip to content

Commit

Permalink
Merge pull request #74 from tanhongit/main
Browse files Browse the repository at this point in the history
Event settings
  • Loading branch information
tanhongit authored Aug 11, 2023
2 parents 46d1a2a + 29ead91 commit c0d940a
Show file tree
Hide file tree
Showing 15 changed files with 517 additions and 144 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
- PHP ^8.0
- Composer
- Telegram Bot
- SSL Certificate

## Installation

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

### Set the webhook

#### Set the webhook from the source code
We have two ways to set the webhook:

#### 1. Set the webhook from this project

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

```text
<APP_URL>/setWebhook.php
<APP_URL>/webhook/set.php
```

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

#### Set the webhook manually
#### 2. Set the webhook manually

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

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

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

> **You can add multiple webhooks to your repository.**
> **Note: You can add multiple webhooks to your repository. Please similarly set up the webhook for each repository.**
2 changes: 1 addition & 1 deletion common/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function event_config(): array
*/
function all_events_notify(): bool
{
return (new Setting())->allEventsNotify();
return (new Setting())->allEventsNotifyStatus();
}
}

Expand Down
8 changes: 8 additions & 0 deletions resources/tools/custom_event_actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
/**
* @var string $event
*/

?>
Setting actions for the event: <b><?= $event ?></b>
Please select an action of this event to enable or disable notifications:
4 changes: 4 additions & 0 deletions resources/tools/custom_events.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
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.
---
<b>Click and configure child events if the option has theicon.</b>
And select an event to enable or disable notifications:
2 changes: 1 addition & 1 deletion resources/tools/menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/id - To get your Chat ID.
/token - To get this bot token.
/usage - How to use me.
/settings - Settings github notify.
/settings - Settings GitHub notify.
/menu - To get this menu.

Select a button:
2 changes: 1 addition & 1 deletion resources/tools/settings.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<b>Settings for github notify.</b>
<b>Settings for GitHub notify.</b>
2 changes: 1 addition & 1 deletion resources/tools/start.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@
Hey <b><?= $first_name ?></b>,

I can send you notifications from your GitHub Repository instantly to your Telegram.
Use /menu for more information about me.
Use /menu for more options.
22 changes: 11 additions & 11 deletions src/Http/Actions/SendNotifyAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,30 +36,30 @@ public function __construct()
*/
public function __invoke(): void
{
$chatMessageId = $this->telegramService->messageData['message']['chat']['id'] ?? '';

if (!empty($chatMessageId)) {
$this->handleEventInTelegram($chatMessageId);
return;
}

// Send a GitHub event result to all chat ids in env
if (!is_null($this->request->server->get('HTTP_X_GITHUB_EVENT'))) {
$this->sendNotification();
return;
}

// Telegram bot handler
$chatMessageId = $this->telegramService->messageData['message']['chat']['id'] ?? '';
if (!empty($chatMessageId)) {
$this->handleEventInTelegram($chatMessageId);
return;
}

$this->telegramService->checkCallback();
}

/**
* @param string $chatMessageId
* @return void
*/
public function handleEventInTelegram(string $chatMessageId): void
private function handleEventInTelegram(string $chatMessageId): void
{
// Send a result to only the bot owner
if ($chatMessageId == config('telegram-bot.chat_id')) {
if ($chatMessageId == $this->telegramService->chatId) {
$this->telegramService->telegramToolHandler($this->telegramService->messageData['message']['text']);
return;
}
Expand All @@ -73,11 +73,11 @@ public function handleEventInTelegram(string $chatMessageId): void
/**
* @return void
*/
protected function sendNotification(): void
private function sendNotification(): void
{
$payload = $this->notificationService->setPayload($this->request);

if (!$this->eventSettingService->validateAccessEvent($this->request, $payload)) {
if (empty($payload) || !$this->eventSettingService->validateAccessEvent($this->request, $payload)) {
return;
}

Expand Down
35 changes: 34 additions & 1 deletion src/Models/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Event
{
public const EVENT_FILE = __DIR__ . '/../../storage/tg-event.json';

public const EVENT_PREFIX = Setting::SETTING_CUSTOM_EVENTS . '.evt.';

public array $eventConfig = [];

public function __construct()
Expand All @@ -20,7 +22,7 @@ public function __construct()
*
* @return void
*/
public function setEventConfig(): void
private function setEventConfig(): void
{
$json = file_get_contents(self::EVENT_FILE);
$this->eventConfig = json_decode($json, true);
Expand All @@ -35,4 +37,35 @@ public function getEventConfig(): array
{
return $this->eventConfig;
}

/**
* Update event config by event and action
*
* @param string $event
* @param string|null $action
* @return void
*/
public function updateEvent(string $event, string|null $action): void
{
if (!empty($action)) {
$this->eventConfig[$event][$action] = !$this->eventConfig[$event][$action];
} else {
$this->eventConfig[$event] = !$this->eventConfig[$event];
}

$this->saveEventConfig();
}

/**
* Save event config
*
* @return void
*/
private function saveEventConfig(): void
{
if (file_exists(self::EVENT_FILE)) {
$json = json_encode($this->eventConfig, JSON_PRETTY_PRINT);
file_put_contents(self::EVENT_FILE, $json, LOCK_EX);
}
}
}
69 changes: 67 additions & 2 deletions src/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
class Setting
{
public const SETTING_FILE = __DIR__ . '/../../storage/tg-setting.json';
public const SETTING_PREFIX = 'stg.';

public const SETTING_IS_NOTIFIED = self::SETTING_PREFIX . 'is_notified';
public const SETTING_ALL_EVENTS_NOTIFY = self::SETTING_PREFIX . 'all_events_notify';
public const SETTING_CUSTOM_EVENTS = self::SETTING_PREFIX . 'cus';
public const SETTING_BACK = self::SETTING_PREFIX . 'back.';

public array $settings = [];

Expand All @@ -20,7 +26,7 @@ public function __construct()
*
* @return void
*/
public function setSettingConfig(): void
private function setSettingConfig(): void
{
$json = file_get_contents(self::SETTING_FILE);
$this->settings = json_decode($json, true);
Expand All @@ -39,12 +45,71 @@ public function getSettingConfig(): array
/**
* @return bool
*/
public function allEventsNotify(): bool
public function allEventsNotifyStatus(): bool
{
if (!empty($this->settings) && $this->settings['all_events_notify'] === true) {
return true;
}

return false;
}

/**
* @return bool
*/
public function isNotified(): bool
{
if (!empty($this->settings) && $this->settings['is_notified'] === true) {
return true;
}

return false;
}

/**
* Update setting item value and save to file
*
* @param string $settingName
* @param $settingValue
* @return bool
*/
public function updateSettingItem(string $settingName, $settingValue = null): bool
{
$keys = explode('.', $settingName);
$lastKey = array_pop($keys);
$nestedSettings = &$this->settings;

foreach ($keys as $key) {
if (!isset($nestedSettings[$key]) || !is_array($nestedSettings[$key])) {
return false;
}
$nestedSettings = &$nestedSettings[$key];
}

if (isset($nestedSettings[$lastKey])) {
$nestedSettings[$lastKey] = $settingValue ?? !$nestedSettings[$lastKey];
if ($this->saveSettingsToFile()) {
return true;
}
}

return false;
}

/**
* Save settings to json file
*
* @return bool
*/
private function saveSettingsToFile(): bool
{
if (file_exists(self::SETTING_FILE)) {
$json = json_encode($this->settings, JSON_PRETTY_PRINT);
file_put_contents(self::SETTING_FILE, $json, LOCK_EX);

return true;
}

return false;
}
}
Loading

0 comments on commit c0d940a

Please sign in to comment.