Skip to content

Commit

Permalink
Merge pull request #65 from tanhongit/main
Browse files Browse the repository at this point in the history
Wip: add bot settings
  • Loading branch information
tanhongit authored Jul 29, 2023
2 parents 9602965 + cc89d58 commit 89392d9
Show file tree
Hide file tree
Showing 13 changed files with 222 additions and 103 deletions.
13 changes: 13 additions & 0 deletions common/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use TelegramGithubNotify\App\Helpers\ConfigHelper;
use TelegramGithubNotify\App\Helpers\EventHelper;
use TelegramGithubNotify\App\Helpers\SettingHelper;

if (!function_exists('config')) {
/**
Expand Down Expand Up @@ -93,3 +94,15 @@ function event_config(): array
return (new EventHelper())->getEventConfig();
}
}

if (!function_exists('enable_all_events')) {
/**
* Return enable all events
*
* @return bool
*/
function enable_all_events(): bool
{
return (new SettingHelper())->enableAllEvents();
}
}
6 changes: 4 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@
"scripts": {
"post-install-cmd": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\"",
"php -r \"file_exists('storage/tg-event.json') || copy('storage/tg-event.example.json', 'storage/tg-event.json');\""
"php -r \"file_exists('storage/tg-event.json') || copy('config/jsons/tg-event.json', 'storage/tg-event.json');\"",
"php -r \"file_exists('storage/tg-setting.json') || copy('config/jsons/tg-setting.json', 'storage/tg-setting.json');\""
],
"post-update-cmd": [
"php -r \"file_exists('.env') || copy('.env.example', '.env');\"",
"php -r \"file_exists('storage/tg-event.json') || copy('storage/tg-event.example.json', 'storage/tg-event.json');\""
"php -r \"file_exists('storage/tg-event.json') || copy('config/jsons/tg-event.json', 'storage/tg-event.json');\"",
"php -r \"file_exists('storage/tg-setting.json') || copy('config/jsons/tg-setting.json', 'storage/tg-setting.json');\""
]
}
}
44 changes: 36 additions & 8 deletions storage/tg-event.example.json → config/jsons/tg-event.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
{
"branch_protection_rule": {
"created": true,
"edited": false,
"deleted": false
},
"package": {
"published": true,
"updated": false
},
"ping": true,
"project": {
"created": true,
"closed": true,
"reopened": false,
"edited": false,
"deleted": false
},
"push": true,
"fork": true,
"gollum": true,
"pull_request": {
"closed": true,
"opened": true,
Expand All @@ -13,7 +31,7 @@
"edited": false,
"assigned": false,
"unassigned": false,
"review_request": false
"review_request": true
},
"pull_request_review": {
"submitted": true,
Expand All @@ -25,6 +43,10 @@
"edited": false,
"deleted": false
},
"pull_request_review_thread": {
"resolved": false,
"unresolved": false
},
"issue": {
"closed": true,
"opened": true,
Expand All @@ -49,29 +71,35 @@
"deleted": false
},
"release": {
"published": false,
"published": true,
"unpublished": false,
"created": false,
"edited": false,
"deleted": false,
"prereleased": false,
"released": true
},
"watch": {
"started": false
"secret_scanning_alert": {
"created": true,
"reopened": false,
"resolved": false
},
"fork": {
"created": false
"star": {
"created": true,
"deleted": false
},
"watch": {
"started": true
},
"member": {
"added": false,
"added": true,
"edited": false,
"removed": false
},
"check_run": {
"created": false,
"rerequested": false,
"completed": false
"completed": true
},
"workflow_dispatch": false,
"workflow_job": {
Expand Down
4 changes: 4 additions & 0 deletions config/jsons/tg-setting.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"is_notified": true,
"enable_all_event": false
}
2 changes: 1 addition & 1 deletion resources/tools/help.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<b>Available Commands</b>

/server - To get Server Information.
/id - To get chat id.
/id - To get your Chat ID.
/token - To get this bot token.
/usage - How to use me.
/help - To show this Message.
Expand Down
12 changes: 7 additions & 5 deletions src/Helpers/EventHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,25 @@

class EventHelper
{
public const EVENT_FILE = __DIR__ . '/../../storage/tg-event.json';

public array $eventConfig = [];

public function __construct()
{
if (file_exists(__DIR__ . '/../../storage/tg-event.json')) {
$this->loadEventConfig();
if (file_exists(self::EVENT_FILE)) {
$this->setEventConfig();
}
}

/**
* Load event config
* Set event config
*
* @return void
*/
public function loadEventConfig(): void
public function setEventConfig(): void
{
$json = file_get_contents(__DIR__ . '/../../storage/tg-event.json');
$json = file_get_contents(self::EVENT_FILE);
$this->eventConfig = json_decode($json, true);
}

Expand Down
50 changes: 50 additions & 0 deletions src/Helpers/SettingHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace TelegramGithubNotify\App\Helpers;

class SettingHelper
{
public const SETTING_FILE = __DIR__ . '/../../storage/tg-setting.json';

public array $settings = [];

public function __construct()
{
if (file_exists(self::SETTING_FILE)) {
$this->setSettingConfig();
}
}

/**
* Set settings
*
* @return void
*/
public function setSettingConfig(): void
{
$json = file_get_contents(self::SETTING_FILE);
$this->settings = json_decode($json, true);
}

/**
* Get settings
*
* @return array
*/
public function getSettingConfig(): array
{
return $this->settings;
}

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

return false;
}
}
2 changes: 1 addition & 1 deletion src/Http/Actions/SendNotifyAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __invoke(): void
public function handleEventInTelegram(string $chatMessageId): void
{
// Send a result to only the bot owner
if ($chatMessageId == $this->telegramService->chatId) {
if ($chatMessageId == config('telegram-bot.chat_id')) {
$this->telegramService->telegramToolHandler($this->telegramService->messageData['message']['text']);
return;
}
Expand Down
45 changes: 45 additions & 0 deletions src/Services/AppService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace TelegramGithubNotify\App\Services;

use Telegram;

class AppService
{
public Telegram $telegram;

public function __construct()
{
$this->telegram = new Telegram(config('telegram-bot.token'));
}

/**
* Send a message to telegram
*
* @param string $message
* @param array $options
* @param string $sendType
* @return void
*/
public function sendMessage(string $message = '', array $options = [], string $sendType = 'Message'): void
{
$content = array(
'chat_id' => config('telegram-bot.chat_id'),
'disable_web_page_preview' => true,
'parse_mode' => 'HTML'
);

if ($sendType === 'Message') {
$content['text'] = $message;
} elseif ($sendType === 'Photo' && !empty($options)) {
$content['photo'] = $options['photo'];
$content['caption'] = $message;
}

if (!empty($options) && isset($options['reply_markup'])) {
$content['reply_markup'] = $this->telegram->buildInlineKeyBoard($options['reply_markup']);
}

$this->telegram->{'send' . $sendType}($content);
}
}
11 changes: 8 additions & 3 deletions src/Services/EventSettingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
class EventSettingService
{
/**
* Validate access event before send notify
*
* @param Request $request
* @param $payload
* @return bool
Expand All @@ -24,10 +26,13 @@ public function validateAccessEvent(Request $request, $payload): bool
$eventConfig = $eventConfig[$event] ?? false;

if (isset($payload->action) && isset($eventConfig[$payload->action])) {
return (bool)$eventConfig[$payload->action];
$eventConfig = $eventConfig[$payload->action];
}

error_log('\n Event config is not found \n');
return false;
if (!$eventConfig) {
error_log('\n Event config is not found \n');
}

return (bool)$eventConfig;
}
}
30 changes: 30 additions & 0 deletions src/Services/SettingService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace TelegramGithubNotify\App\Services;

use Telegram;

class SettingService extends AppService
{
public function settingMarkup(Telegram $telegram): void
{
$keyboard = [
[
$telegram->buildInlineKeyBoardButton('🔔 Notification', '', '/notification'),
]
];

if (enable_all_events()) {
$eventSetting = $telegram->buildInlineKeyBoardButton('🔕 Disable All Events', '', '/disable_all_events');
} else {
$eventSetting = $telegram->buildInlineKeyBoardButton('🔔 Enable All Events', '', '/enable_all_events');
}

$keyboard[0][] = [
$eventSetting,
$telegram->buildInlineKeyBoardButton('Check Events', '', '/check_events'),
];

$this->sendMessage(view('tools.settings'), ['reply_markup' => $keyboard]);
}
}
Loading

0 comments on commit 89392d9

Please sign in to comment.