Skip to content

Commit

Permalink
Merge pull request #23 from tanhongit/bot-tools
Browse files Browse the repository at this point in the history
(#30) update answer callback queries
  • Loading branch information
tanhongit authored Aug 9, 2023
2 parents 88684aa + d6e3078 commit 8ed028b
Show file tree
Hide file tree
Showing 9 changed files with 219 additions and 47 deletions.
4 changes: 4 additions & 0 deletions resources/tools/event.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Please select an event to enable or disable notifications.
<b>Click and configure child events if the option has theicon.</b>
---
Please 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.
8 changes: 4 additions & 4 deletions src/Http/Actions/SendNotifyAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ public function __invoke(): void
* @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
4 changes: 3 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_PREFIX . '.event.';

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 Down
2 changes: 1 addition & 1 deletion src/Models/Setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,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 Down
92 changes: 91 additions & 1 deletion src/Services/AppService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ class AppService
{
public Telegram $telegram;

public string $chatId;

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

/**
Expand All @@ -25,7 +28,7 @@ public function __construct()
public function sendMessage(string $message = '', array $options = [], string $sendType = 'Message'): void
{
$content = array(
'chat_id' => config('telegram-bot.chat_id'),
'chat_id' => $this->chatId,
'disable_web_page_preview' => true,
'parse_mode' => 'HTML'
);
Expand All @@ -47,4 +50,91 @@ public function sendMessage(string $message = '', array $options = [], string $s
error_log($e->getMessage());
}
}

/**
* Send callback response to telegram (show alert)
*
* @param string|null $text
* @return void
*/
public function answerCallbackQuery(string $text = null): void
{
try {
$this->telegram->answerCallbackQuery([
'callback_query_id' => $this->telegram->Callback_ID(),
'text' => $text,
'show_alert' => true
]);
} catch (Exception $e) {
error_log($e->getMessage());
}
}

/**
* Edit message from telegram
* (Edit message text and reply markup)
*
* @param string|null $text
* @param array $options
* @return void
*/
public function editMessageText(?string $text = null, array $options = []): void
{
try {
$content = [
'text' => $text ?? $this->Callback_Message_Text()
];
$content = array_merge($content, $this->setContentEditMessage($options));

$this->telegram->editMessageText($content);
} catch (Exception $e) {
error_log($e->getMessage());
}
}

/**
* Edit message reply markup from a telegram
* (Edit message reply markup only)
*
* @param array $options
* @return void
*/
public function editMessageReplyMarkup(array $options = []): void
{
try {
$this->telegram->editMessageReplyMarkup($this->setContentEditMessage($options));
} catch (Exception $e) {
error_log($e->getMessage());
}
}

/**
* Get the text from callback message
*
* @return string
*/
public function Callback_Message_Text(): string
{
return $this->telegram->Callback_Message()['text'];
}

/**
* @param array $options
* @return array
*/
public function setContentEditMessage(array $options = []): array
{
$content = array(
'chat_id' => $this->telegram->Callback_ChatID(),
'message_id' => $this->telegram->MessageID(),
'disable_web_page_preview' => true,
'parse_mode' => 'HTML',
);

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

return $content;
}
}
64 changes: 61 additions & 3 deletions src/Services/EventService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@
namespace TelegramGithubNotify\App\Services;

use Symfony\Component\HttpFoundation\Request;
use TelegramGithubNotify\App\Models\Event;
use TelegramGithubNotify\App\Models\Setting;

class EventService
class EventService extends AppService
{
public const LINE_ITEM_COUNT = 2;

protected Setting $setting;

protected Event $event;

public function __construct()
{
parent::__construct();

$this->setting = new Setting();
$this->event = new Event();
}

/**
Expand All @@ -31,7 +39,7 @@ public function validateAccessEvent(Request $request, $payload): bool
return true;
}

$eventConfig = event_config();
$eventConfig = $this->event->getEventConfig();

$event = singularity($request->server->get('HTTP_X_GITHUB_EVENT'));
$eventConfig = $eventConfig[$event] ?? false;
Expand All @@ -47,7 +55,57 @@ public function validateAccessEvent(Request $request, $payload): bool
return (bool)$eventConfig;
}

public function eventHandle()
/**
* Create markup for select event
*
* @param string|null $event
* @return array
*/
public function eventMarkup(?string $event = null): array
{
$replyMarkup = $replyMarkupItem = [];

$events = $event === null ? $this->event->eventConfig : $this->event->eventConfig[$event];

foreach ($events as $event => $value) {
if (count($replyMarkupItem) === self::LINE_ITEM_COUNT) {
$replyMarkup[] = $replyMarkupItem;
$replyMarkupItem = [];
}

$callbackData = $this->event::EVENT_PREFIX . $event;

if (is_array($value)) {
$eventName = '' . $event;
$callbackData .= '.child';
} elseif ($value) {
$eventName = '' . $event;
} else {
$eventName = '' . $event;
}

$replyMarkupItem[] = $this->telegram->buildInlineKeyBoardButton($eventName, '', $callbackData);
}

// add last item to a reply_markup array
if (count($replyMarkupItem) > 0) {
$replyMarkup[] = $replyMarkupItem;
}

$replyMarkup[] = [$this->telegram->buildInlineKeyBoardButton('🔙 Back', '', 'back')];
$replyMarkup[] = [$this->telegram->buildInlineKeyBoardButton('📚 Menu', '', $this->setting::SETTING_PREFIX . '.back.menu')];

return $replyMarkup;
}

/**
* @return void
*/
public function eventHandle(): void
{
$this->editMessageText(
view('tools.event'),
['reply_markup' => $this->eventMarkup()]
);
}
}
9 changes: 4 additions & 5 deletions src/Services/NotificationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

class NotificationService
{
public mixed $payload;
protected mixed $payload;

public string $message = "";
protected string $message = "";

/**
* Notify access denied to other chat ids
Expand Down Expand Up @@ -38,8 +38,7 @@ public function accessDenied(TelegramService $telegramService, string $chatId =
public function setPayload(Request $request)
{
if (is_null($request->server->get('HTTP_X_GITHUB_EVENT'))) {
echo 'invalid request';
exit;
return null;
}

$this->payload = json_decode($request->request->get('payload'));
Expand All @@ -54,7 +53,7 @@ public function setPayload(Request $request)
* @param string $typeEvent
* @return void
*/
public function setMessage(string $typeEvent): void
private function setMessage(string $typeEvent): void
{
if (isset($this->payload->action) && !empty($this->payload->action)) {
$this->message = view(
Expand Down
Loading

0 comments on commit 8ed028b

Please sign in to comment.