Connect a channel by issuing a one-time code the user posts as /connect <code> in their channel. A secret-token-guarded webhook matches the code, links the channel as a SocialAccount (chat_id in meta), and records it on the request so the connect endpoint can poll for completion. Adds the TelegramConnectRequest model + migration, the connect/status endpoints, the public webhook route (CSRF exempt), a ConnectionVerifier branch (getChat liveness), and a telegram:set-webhook command. Tests cover the code issue, webhook link, secret rejection, expired/ unknown codes, status polling, and the command.
68 lines
2.3 KiB
PHP
68 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
use App\Enums\SocialAccount\Platform as SocialPlatform;
|
|
use App\Models\TelegramConnectRequest;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
|
|
|
|
class TelegramController extends SocialController
|
|
{
|
|
protected SocialPlatform $platform = SocialPlatform::Telegram;
|
|
|
|
/**
|
|
* Start a connection: issue a one-time code the user posts in their channel
|
|
* (`/connect <code>`) so the webhook can tie the channel to this workspace.
|
|
*/
|
|
public function connect(Request $request): JsonResponse
|
|
{
|
|
$this->ensurePlatformEnabled();
|
|
|
|
$workspace = $request->user()->currentWorkspace;
|
|
abort_if($workspace === null, SymfonyResponse::HTTP_CONFLICT, 'No active workspace.');
|
|
|
|
$this->authorize('manageAccounts', $workspace);
|
|
$this->ensureSocialAccountLimit($workspace);
|
|
|
|
$connectRequest = TelegramConnectRequest::create([
|
|
'workspace_id' => $workspace->id,
|
|
'user_id' => $request->user()->id,
|
|
'code' => Str::lower(Str::random(12)),
|
|
'expires_at' => now()->addMinutes(15),
|
|
]);
|
|
|
|
return response()->json([
|
|
'code' => $connectRequest->code,
|
|
'bot_username' => config('trypost.platforms.telegram.bot_username'),
|
|
'expires_at' => $connectRequest->expires_at->toIso8601String(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Poll whether the channel has been linked yet.
|
|
*/
|
|
public function status(Request $request): JsonResponse
|
|
{
|
|
$workspace = $request->user()->currentWorkspace;
|
|
abort_if($workspace === null, SymfonyResponse::HTTP_CONFLICT, 'No active workspace.');
|
|
|
|
$connectRequest = TelegramConnectRequest::query()
|
|
->where('workspace_id', $workspace->id)
|
|
->where('code', (string) $request->query('code'))
|
|
->first();
|
|
|
|
$status = match (true) {
|
|
$connectRequest === null => 'unknown',
|
|
$connectRequest->social_account_id !== null => 'connected',
|
|
$connectRequest->isExpired() => 'expired',
|
|
default => 'pending',
|
|
};
|
|
|
|
return response()->json(['status' => $status]);
|
|
}
|
|
}
|