This commit is contained in:
hashbro
2026-08-08 05:09:01 +08:00
parent 03a40105fc
commit 74a67c3900
523 changed files with 669 additions and 5965 deletions
+13
View File
@@ -0,0 +1,13 @@
<?php
namespace App\Telegram\Handlers;
use SergiX44\Nutgram\Nutgram;
class PingCommand
{
public function __invoke(Nutgram $bot): void
{
$bot->sendMessage('pong');
}
}
+82
View File
@@ -0,0 +1,82 @@
<?php
namespace App\Telegram\Handlers;
use App\Services\TransferService;
use SergiX44\Nutgram\Nutgram;
class TransferCommand
{
public function __construct(
private readonly TransferService $transfers,
) {}
public function __invoke(Nutgram $bot, string $args): void
{
$parts = preg_split('/\s+/', trim($args)) ?: [];
$parts = array_values(array_filter($parts, fn ($p) => $p !== ''));
if ($parts === []) {
$bot->sendMessage(
"Usage: /transfer <fromAddress> [TRX|USDT] [amount]\n"
."Omit amount to transfer all. Default asset: USDT.\n"
.'To = TRANSFER_TO_ADDRESS (env).'
);
return;
}
$from = $parts[0];
$asset = 'USDT';
$amount = null;
if (count($parts) === 2) {
$second = strtoupper($parts[1]);
if (in_array($second, ['TRX', 'USDT'], true)) {
$asset = $second;
} elseif ($this->isAmount($parts[1])) {
$amount = $parts[1];
} else {
$bot->sendMessage('Second arg must be TRX, USDT, or an amount.');
return;
}
} elseif (count($parts) >= 3) {
$asset = strtoupper($parts[1]);
if (! in_array($asset, ['TRX', 'USDT'], true)) {
$bot->sendMessage('Asset must be TRX or USDT.');
return;
}
if (! $this->isAmount($parts[2])) {
$bot->sendMessage('Invalid amount.');
return;
}
$amount = $parts[2];
}
$label = $amount === null ? "ALL {$asset}" : "{$amount} {$asset}";
$bot->sendMessage("⏳ Transferring {$label} from {$from} …");
$result = $this->transfers->handle('tron', $from, $amount, $asset);
if (! ($result['ok'] ?? false)) {
$bot->sendMessage('❌ '.($result['error'] ?? 'Transfer failed'));
return;
}
$bot->sendMessage(implode("\n", [
'✅ Transfer submitted',
'From: '.$result['from'],
'To: '.$result['to'],
'Amount: '.$result['amount'].' '.$result['asset'],
'TxID: '.$result['txid'],
]));
}
private function isAmount(string $value): bool
{
return (bool) preg_match('/^\d+(\.\d{1,6})?$/', $value);
}
}
@@ -0,0 +1,31 @@
<?php
namespace App\Telegram\Middleware;
use Illuminate\Support\Facades\Log;
use SergiX44\Nutgram\Nutgram;
class AuthorizedChat
{
public function __invoke(Nutgram $bot, callable $next): mixed
{
$expected = (string) config('coruna.telegram.owner_chat_id', '');
if ($expected === '') {
Log::warning('telegram AuthorizedChat: TELEGRAM_OWNER_CHAT_ID empty');
return null;
}
$chatId = $bot->chatId();
if ($chatId === null || (string) $chatId !== $expected) {
Log::info('telegram AuthorizedChat: chat rejected', [
'chat_id' => $chatId,
'expected' => $expected,
]);
return null;
}
return $next($bot);
}
}
@@ -0,0 +1,40 @@
<?php
namespace App\Telegram\Middleware;
use SergiX44\Nutgram\Nutgram;
use SergiX44\Nutgram\Telegram\Properties\ChatMemberStatus;
class GroupAdminOnly
{
public function __invoke(Nutgram $bot, callable $next): mixed
{
$chatId = $bot->chatId();
$userId = $bot->userId();
if ($chatId === null || $userId === null) {
return null;
}
try {
$member = $bot->getChatMember($chatId, $userId);
} catch (\Throwable) {
$bot->sendMessage('⛔ Unable to verify admin status.');
return null;
}
$status = $member?->status;
$ok = $status === ChatMemberStatus::CREATOR
|| $status === ChatMemberStatus::ADMINISTRATOR
|| $status === 'creator'
|| $status === 'administrator';
if (! $ok) {
$bot->sendMessage('⛔ Only group admins can use this command.');
return null;
}
return $next($bot);
}
}