feat: bot

This commit is contained in:
hashbro
2026-08-15 23:05:51 +08:00
parent 9049fdccad
commit 376b442880
26 changed files with 944 additions and 352 deletions
+31 -4
View File
@@ -5,8 +5,8 @@ namespace App\Telegram;
use App\Models\User;
/**
* Request-scoped Telegram bot identity.
* null agent = official system bot; otherwise an agent-configured bot.
* Request-scoped Telegram identity.
* null agent = official owner chat; otherwise the agent whose chat_id matched.
*/
class TelegramBotContext
{
@@ -41,8 +41,35 @@ class TelegramBotContext
return trim((string) config('coruna.telegram.owner_chat_id', ''));
}
public static function webhookSecretFor(User $agent): string
/**
* Bind official vs agent from the incoming chat_id.
* Official owner_chat_id wins if it collides with an agent.
*/
public function bindFromChatId(int|string|null $chatId): bool
{
return hash_hmac('sha256', 'telegram-agent:'.$agent->id, (string) config('app.key'));
$chatId = trim((string) $chatId);
if ($chatId === '') {
$this->agent = null;
return false;
}
$owner = trim((string) config('coruna.telegram.owner_chat_id', ''));
if ($owner !== '' && $chatId === $owner) {
$this->agent = null;
return true;
}
$agent = User::findEnabledByChatId($chatId);
if ($agent === null) {
$this->agent = null;
return false;
}
$this->agent = $agent;
return true;
}
}