Skip to content

Commit

Permalink
Merge pull request #11 from tanhongit/main
Browse files Browse the repository at this point in the history
Deploy
  • Loading branch information
tanhongit authored Jul 28, 2023
2 parents 1e40d67 + cc89d58 commit 21d8458
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 93 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');\""
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
},
"push": true,
"fork": true,
"gollum": false,
"gollum": true,
"pull_request": {
"closed": true,
"opened": true,
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);
}
}
2 changes: 2 additions & 0 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 Down
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]);
}
}
106 changes: 23 additions & 83 deletions src/Services/TelegramService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,16 @@

namespace TelegramGithubNotify\App\Services;

use Telegram;

class TelegramService
class TelegramService extends AppService
{
public string $token;

public string $chatId;

public Telegram $telegram;

public array $messageData;

public function __construct()
{
$this->setToken();
$this->setChatId();
$this->storeByToken();
$this->getDataOfMessage();
}

/**
* @return void
*/
private function setToken(): void
{
$this->token = config('telegram-bot.token');
}

/**
* @return void
*/
private function setChatId(): void
{
$this->chatId = config('telegram-bot.chat_id');
}
public SettingService $settingService;

/**
* @return void
*/
private function storeByToken(): void
public function __construct()
{
$this->telegram = new Telegram($this->token);
}
parent::__construct();

/**
* @return void
*/
private function getDataOfMessage(): void
{
$this->messageData = $this->telegram->getData() ?? [];
}

Expand All @@ -68,56 +29,22 @@ public function telegramToolHandler(string $text = null): void
$this->sendMessage($reply, ['photo' => curl_file_create(config('app.image'), 'image/png')], 'Photo');
break;
case '/help':
$replyMarkup = [
[
$this->telegram->buildInlineKeyBoardButton("📰 About", "", "about", ""),
$this->telegram->buildInlineKeyBoardButton("📞 Contact", config('author.contact'))
],
[$this->telegram->buildInlineKeyBoardButton("💠 Source Code", config('author.source_code'))]
];
$this->sendMessage(view('tools.help'), ['reply_markup' => $replyMarkup]);
$this->sendMessage(view('tools.help'), ['reply_markup' => $this->helpMarkup()]);
break;
case '/token':
case '/id':
case '/usage':
case '/server':
$this->sendMessage(view('tools.' . trim($text, '/')));
break;
case '/settings':
$this->settingService->settingMarkup($this->telegram);
break;
default:
$this->sendMessage('🤨 Invalid Request!');
}
}

/**
* 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' => $this->chatId,
'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);
}

/**
* Send callback response to telegram
*
Expand Down Expand Up @@ -147,10 +74,23 @@ public function checkCallback(): bool
if (!is_null($this->telegram->Callback_ChatID())) {
$callback = $this->telegram->Callback_Data();
$this->sendCallbackResponse($callback);

return true;
}

return false;
}

/**
* @return array[]
*/
public function helpMarkup(): array
{
return [
[
$this->telegram->buildInlineKeyBoardButton("📰 About", "", "about", ""),
$this->telegram->buildInlineKeyBoardButton("📞 Contact", config('author.contact'))
], [
$this->telegram->buildInlineKeyBoardButton("💠 Source Code", config('author.source_code'))
]
];
}
}
Empty file added storage/.gitkeep
Empty file.

0 comments on commit 21d8458

Please sign in to comment.