Add Telegram connection flow (controller + webhook)
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.
2026-06-14 00:39:03 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Auth;
|
|
|
|
|
|
|
|
|
|
use App\Enums\SocialAccount\Platform as SocialPlatform;
|
2026-06-14 01:38:23 +00:00
|
|
|
use App\Enums\SocialAccount\TelegramConnectStatus;
|
2026-06-14 01:48:21 +00:00
|
|
|
use App\Http\Requests\App\Auth\TelegramStatusRequest;
|
Add Telegram connection flow (controller + webhook)
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.
2026-06-14 00:39:03 +00:00
|
|
|
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.
|
|
|
|
|
*/
|
2026-06-14 01:48:21 +00:00
|
|
|
public function status(TelegramStatusRequest $request): JsonResponse
|
Add Telegram connection flow (controller + webhook)
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.
2026-06-14 00:39:03 +00:00
|
|
|
{
|
|
|
|
|
$workspace = $request->user()->currentWorkspace;
|
|
|
|
|
abort_if($workspace === null, SymfonyResponse::HTTP_CONFLICT, 'No active workspace.');
|
|
|
|
|
|
|
|
|
|
$connectRequest = TelegramConnectRequest::query()
|
|
|
|
|
->where('workspace_id', $workspace->id)
|
2026-06-14 01:48:21 +00:00
|
|
|
->where('code', $request->validated('code'))
|
Add Telegram connection flow (controller + webhook)
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.
2026-06-14 00:39:03 +00:00
|
|
|
->first();
|
|
|
|
|
|
2026-06-14 01:38:23 +00:00
|
|
|
return response()->json([
|
|
|
|
|
'status' => TelegramConnectStatus::for($connectRequest)->value,
|
|
|
|
|
]);
|
Add Telegram connection flow (controller + webhook)
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.
2026-06-14 00:39:03 +00:00
|
|
|
}
|
|
|
|
|
}
|