Skip to content

Commit

Permalink
fix: handle the message from telegram - new core version
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhongit committed Oct 18, 2023

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
1 parent 0a44ff5 commit 4e1bcb6
Showing 7 changed files with 67 additions and 43 deletions.
20 changes: 15 additions & 5 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
TGN_APP_NAME='Telegram Git Notify'

# Set your app url here (please change to your bot domain)
# Set your app url here (Required for the bot to work properly)
TGN_APP_URL=https://152.132.127.172:3000

TELEGRAM_BOT_TOKEN="###########:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
TELEGRAM_BOT_CHAT_ID="##########"

# Set the chat ids that will receive notifications here.
# You can add the owner bot id, group id, ... ( And please use semicolon ";" to separate chat ids )
# You can add the owner bot id, group id, ...
# -------------------------------------------------------
# Note:
# Please use semicolon ";" to separate chat ids
# And use colon ":" to separate chat id and thread id
# And use comma "," if you want to add multiple thread ids
# -------------------------------------------------------
# The environment variable is expected to be in the format:
# "chat_id1;chat_id2,thread_id2;chat_id3,thread_id3;..."
TELEGRAM_NOTIFY_CHAT_IDS="-###########1;-############2,######2;-############3,######3"
# "chat_id1;chat_id2:thread_id2;chat_id3:thread_id3_1,thread_id3_2;..."
TELEGRAM_NOTIFY_CHAT_IDS="-###########1;-############2:######2;-############3:######1,######2"

# Docker
PHP_VERSION_SELECTED=8.2
@@ -24,7 +30,9 @@ APP_PORT=3000
# Note: Don't set blank value
# -----------------------------------------------------

TIMEZONE=Asia/Ho_Chi_Minh
#TGN_PATH_SETTING=
#TGN_PATH_PLATFORM_GITLAB=
#TGN_PATH_PLATFORM_GITHUB=

#TGN_VIEW_PATH=
#TGN_VIEW_EVENT_DEFAULT=
@@ -35,3 +43,5 @@ TIMEZONE=Asia/Ho_Chi_Minh
#TGN_VIEW_TOOL_CUSTOM_EVENT_ACTION=
#TGN_VIEW_TOOL_CUSTOM_EVENT=
#TGN_VIEW_TOOL_SET_MENU_COMMAND=

TIMEZONE=Asia/Ho_Chi_Minh
34 changes: 12 additions & 22 deletions src/Http/Actions/CommandAction.php
Original file line number Diff line number Diff line change
@@ -2,32 +2,22 @@

namespace LbilTech\TelegramGitNotifierApp\Http\Actions;

use LbilTech\TelegramGitNotifier\Bot;
use LbilTech\TelegramGitNotifier\Exceptions\EntryNotFoundException;
use LbilTech\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
use LbilTech\TelegramGitNotifier\Services\TelegramService;
use LbilTech\TelegramGitNotifierApp\Services\AppService;
use LbilTech\TelegramGitNotifierApp\Services\CommandService;
use LbilTech\TelegramGitNotifierApp\Services\SettingService;

class CommandAction
{
protected AppService $appService;
protected Bot $bot;

protected CommandService $commandService;

protected TelegramService $telegramService;

public SettingService $settingService;

public function __construct(
AppService $appService,
TelegramService $telegramService,
SettingService $settingService
Bot $bot,
) {
$this->appService = $appService;
$this->telegramService = $telegramService;
$this->bot = $bot;
$this->commandService = new CommandService();
$this->settingService = $settingService;
}

/**
@@ -37,32 +27,32 @@ public function __construct(
*/
public function __invoke(): void
{
$text = $this->appService->getCommandMessage();
$text = $this->bot->getCommandMessage();

switch ($text) {
case '/start':
$this->commandService->sendStartMessage($this->appService);
$this->commandService->sendStartMessage($this->bot);
break;
case '/menu':
$this->appService->sendMessage(
$this->bot->sendMessage(
view('tools.menu'),
['reply_markup' => $this->commandService->menuMarkup($this->appService->telegram)]
['reply_markup' => $this->commandService->menuMarkup($this->bot->telegram)]
);
break;
case '/token':
case '/id':
case '/usage':
case '/server':
$this->appService->sendMessage(view('tools.' . trim($text, '/')));
$this->bot->sendMessage(view('tools.' . trim($text, '/')));
break;
case '/settings':
$this->settingService->settingHandle();
$this->bot->settingHandle();
break;
case '/set_menu':
$this->telegramService->setMyCommands(CommandService::MENU_COMMANDS);
$this->bot->setMyCommands(CommandService::MENU_COMMANDS);
break;
default:
$this->appService->sendMessage('🤨 Invalid Request!');
$this->bot->sendMessage('🤨 Invalid Request!');
}
}
}
12 changes: 5 additions & 7 deletions src/Http/Actions/IndexAction.php
Original file line number Diff line number Diff line change
@@ -9,8 +9,6 @@
use LbilTech\TelegramGitNotifier\Exceptions\MessageIsEmptyException;
use LbilTech\TelegramGitNotifier\Exceptions\SendNotificationException;
use LbilTech\TelegramGitNotifier\Notifier;
use LbilTech\TelegramGitNotifierApp\Services\AppService;
use LbilTech\TelegramGitNotifierApp\Services\SettingService;
use Symfony\Component\HttpFoundation\Request;
use Telegram;

@@ -50,11 +48,11 @@ public function __invoke(): void
// return;
// }
//
// if ($this->telegramService->isMessage()) {
// $commandAction = new CommandAction($this->appService, $this->telegramService, $this->settingService);
// $commandAction();
// return;
// }
if ($this->bot->isMessage()) {
$commandAction = new CommandAction($this->bot);
$commandAction();
return;
}

$sendNotificationAction = new SendNotificationAction($this->notifier, $this->bot->setting);
$sendNotificationAction();
17 changes: 12 additions & 5 deletions src/Http/Actions/SendNotificationAction.php
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@ public function __construct(
) {
$this->request = Request::createFromGlobals();
$this->notifier = $notifier;
$this->chatIds = config('telegram-git-notifier.bot.notify_chat_ids');
$this->chatIds = $this->notifier->parseNotifyChatIds();

$this->setting = $setting;
}
@@ -58,14 +58,21 @@ private function sendNotification(string $event): void
return;
}

foreach ($this->chatIds as $chatId) {
foreach ($this->chatIds as $chatId => $thread) {
if (empty($chatId)) {
continue;
}

$this->notifier->sendNotify(null, [
'chat_id' => $chatId,
]);
if (empty($thread)) {
$this->notifier->sendNotify(null, ['chat_id' => $chatId]);
continue;
}

foreach ($thread as $threadId) {
$this->notifier->sendNotify(null, [
'chat_id' => $chatId, 'message_thread_id' => $threadId
]);
}
}
}

10 changes: 10 additions & 0 deletions src/Http/Actions/WebhookAction.php
Original file line number Diff line number Diff line change
@@ -36,4 +36,14 @@ public function delete(): false|string
{
return $this->webhook->deleteWebHook();
}

/**
* Get webhook update
*
* @return false|string
*/
public function getUpdates(): false|string
{
return $this->webhook->getUpdates();
}
}
9 changes: 5 additions & 4 deletions src/Services/CommandService.php
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@

namespace LbilTech\TelegramGitNotifierApp\Services;

use LbilTech\TelegramGitNotifier\Bot;
use LbilTech\TelegramGitNotifier\Exceptions\EntryNotFoundException;
use Telegram;

@@ -49,18 +50,18 @@ public function menuMarkup(Telegram $telegram): array
}

/**
* @param AppService $appService
* @param Bot $bot
*
* @return void
* @throws EntryNotFoundException
*/
public function sendStartMessage(AppService $appService): void
public function sendStartMessage(Bot $bot): void
{
$reply = view(
'tools.start',
['first_name' => $appService->telegram->FirstName()]
['first_name' => $bot->telegram->FirstName()]
);
$appService->sendPhoto(
$bot->sendPhoto(
__DIR__ . '/../../resources/images/start.png',
$reply
);
8 changes: 8 additions & 0 deletions webhook/getUpdate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use LbilTech\TelegramGitNotifierApp\Http\Actions\WebhookAction;

require __DIR__ . '/../init.php';

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

0 comments on commit 4e1bcb6

Please sign in to comment.