Skip to content

Commit

Permalink
Merge pull request #69 from tanhongit/main
Browse files Browse the repository at this point in the history
update validate event and bot setting
  • Loading branch information
tanhongit authored Jul 29, 2023
2 parents 89392d9 + 0fbafc0 commit c87866d
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 67 deletions.
12 changes: 12 additions & 0 deletions common/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,15 @@ function enable_all_events(): bool
return (new SettingHelper())->enableAllEvents();
}
}

if (!function_exists('setting_config')) {
/**
* Return setting config
*
* @return array
*/
function setting_config(): array
{
return (new SettingHelper())->getSettingConfig();
}
}
3 changes: 2 additions & 1 deletion resources/tools/help.php → resources/tools/menu.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
/id - To get your Chat ID.
/token - To get this bot token.
/usage - How to use me.
/help - To show this Message.
/settings - Settings github notify.
/menu - To get this menu.

Select a command:
1 change: 1 addition & 0 deletions resources/tools/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<b>Settings for github notify.</b>
3 changes: 2 additions & 1 deletion resources/tools/start.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@

Hey <b><?= $first_name ?></b>,

I can send you notifications from your GitHub Repository instantly to your Telegram. use /help for more information about me.
I can send you notifications from your GitHub Repository instantly to your Telegram.
Use /menu for more information about me.
12 changes: 0 additions & 12 deletions setWebhook.php

This file was deleted.

6 changes: 4 additions & 2 deletions src/Http/Actions/SendNotifyAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ public function __construct()
*/
public function __invoke(): void
{
$this->telegramService->checkCallback();
$chatMessageId = $this->telegramService->messageData['message']['chat']['id'] ?? '';

if (!empty($chatMessageId)) {
Expand All @@ -47,7 +46,10 @@ public function __invoke(): void
// 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;
}

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

/**
Expand All @@ -62,7 +64,7 @@ public function handleEventInTelegram(string $chatMessageId): void
return;
}

// Notify access denied to other chat ids
// Notify access denied to other/invalid chat ids
if (!in_array($chatMessageId, $this->chatIds)) {
$this->notificationService->accessDenied($this->telegramService);
}
Expand Down
20 changes: 0 additions & 20 deletions src/Http/Actions/SetWebhookAction.php

This file was deleted.

38 changes: 38 additions & 0 deletions src/Http/Actions/WebhookAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace TelegramGithubNotify\App\Http\Actions;

class WebhookAction
{
protected string $token;

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

/**
* Set webhook for telegram bot
*
* @return false|string
*/
public function set(): false|string
{
$appUrl = config('app.url');
$url = "https://api.telegram.org/bot{$this->token}/setWebhook?url={$appUrl}";

return file_get_contents($url);
}

/**
* Delete webhook for telegram bot
*
* @return false|string
*/
public function delete(): false|string
{
$url = "https://api.telegram.org/bot{$this->token}/deleteWebhook";

return file_get_contents($url);
}
}
30 changes: 19 additions & 11 deletions src/Services/AppService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace TelegramGithubNotify\App\Services;

use Exception;
use Telegram;

class AppService
Expand Down Expand Up @@ -29,17 +30,24 @@ public function sendMessage(string $message = '', array $options = [], string $s
'parse_mode' => 'HTML'
);

if ($sendType === 'Message') {
$content['text'] = $message;
} elseif ($sendType === 'Photo' && !empty($options)) {
$content['photo'] = $options['photo'];
$content['caption'] = $message;
try {
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);
} catch (Exception $e) {
$content['text'] = $e->getMessage();
$this->telegram->sendMessage($content);
}

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

$this->telegram->{'send' . $sendType}($content);
}
}
4 changes: 4 additions & 0 deletions src/Services/EventSettingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class EventSettingService
*/
public function validateAccessEvent(Request $request, $payload): bool
{
if (enable_all_events()) {
return true;
}

$eventConfig = event_config();

if (empty($eventConfig)) {
Expand Down
2 changes: 1 addition & 1 deletion src/Services/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function accessDenied(TelegramService $telegramService, string $chatId =
{
$reply = view('globals.access_denied', ['chatId' => $chatId]);
$content = array(
'chat_id' => $telegramService->chatId,
'chat_id' => config('telegram-bot.chat_id'),
'text' => $reply,
'disable_web_page_preview' => true,
'parse_mode' => 'HTML'
Expand Down
39 changes: 25 additions & 14 deletions src/Services/SettingService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,40 @@

namespace TelegramGithubNotify\App\Services;

use Telegram;

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

if ($settings['is_notified']) {
$notificationSetting = $this->telegram->buildInlineKeyBoardButton('🔕 Disable Notification', '', 'setting.disable_notification');
} else {
$notificationSetting = $this->telegram->buildInlineKeyBoardButton('🔔 Enable Notification', '', 'setting.enable_notification');
}

if (enable_all_events()) {
$eventSetting = $telegram->buildInlineKeyBoardButton('🔕 Disable All Events', '', '/disable_all_events');
if ($settings['enable_all_event']) {
$eventSetting = $this->telegram->buildInlineKeyBoardButton('🔕 Disable All Events', '', 'setting.disable_all_events');
} else {
$eventSetting = $telegram->buildInlineKeyBoardButton('🔔 Enable All Events', '', '/enable_all_events');
$eventSetting = $this->telegram->buildInlineKeyBoardButton('🔔 Enable All Events', '', 'setting.enable_all_events');
}

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

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

public function settingCallbackHandler(string $callback)
{
}
}
17 changes: 12 additions & 5 deletions src/Services/TelegramService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public function __construct()
parent::__construct();

$this->messageData = $this->telegram->getData() ?? [];
$this->settingService = new SettingService();
}

/**
Expand All @@ -28,8 +29,8 @@ public function telegramToolHandler(string $text = null): void
$reply = view('tools.start', ['first_name' => $this->telegram->FirstName()]);
$this->sendMessage($reply, ['photo' => curl_file_create(config('app.image'), 'image/png')], 'Photo');
break;
case '/help':
$this->sendMessage(view('tools.help'), ['reply_markup' => $this->helpMarkup()]);
case '/menu':
$this->sendMessage(view('tools.menu'), ['reply_markup' => $this->menuMarkup()]);
break;
case '/token':
case '/id':
Expand All @@ -38,7 +39,7 @@ public function telegramToolHandler(string $text = null): void
$this->sendMessage(view('tools.' . trim($text, '/')));
break;
case '/settings':
$this->settingService->settingMarkup($this->telegram);
$this->settingService->settingHandle();
break;
default:
$this->sendMessage('🤨 Invalid Request!');
Expand All @@ -53,14 +54,20 @@ public function telegramToolHandler(string $text = null): void
*/
protected function sendCallbackResponse(string $callback = null): void
{
if (!empty($callback) && $callback == 'about') {
if (empty($callback)) {
return;
}

if ($callback === 'about') {
$reply = view('tools.about');
$content = array(
'callback_query_id' => $this->telegram->Callback_ID(),
'text' => $reply,
'show_alert' => true
);
$this->telegram->answerCallbackQuery($content);
} elseif (str_contains($callback, 'setting.')) {
$this->settingService->settingCallbackHandler($callback);
}
}

Expand All @@ -82,7 +89,7 @@ public function checkCallback(): bool
/**
* @return array[]
*/
public function helpMarkup(): array
public function menuMarkup(): array
{
return [
[
Expand Down
12 changes: 12 additions & 0 deletions webhook/delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use TelegramGithubNotify\App\Http\Actions\WebhookAction;
use Dotenv\Dotenv;

require __DIR__ . '/../vendor/autoload.php';

$dotenv = Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->load();

$webhookAction = new WebhookAction();
echo $webhookAction->delete();
12 changes: 12 additions & 0 deletions webhook/set.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use TelegramGithubNotify\App\Http\Actions\WebhookAction;
use Dotenv\Dotenv;

require __DIR__ . '/../vendor/autoload.php';

$dotenv = Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->load();

$webhookAction = new WebhookAction();
echo $webhookAction->set();

0 comments on commit c87866d

Please sign in to comment.