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 16:32:03 +00:00
|
|
|
use App\Services\Social\Telegram\TelegramConnectCode;
|
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 Illuminate\Http\JsonResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
|
|
|
|
|
|
|
|
|
|
class TelegramController extends SocialController
|
|
|
|
|
{
|
|
|
|
|
protected SocialPlatform $platform = SocialPlatform::Telegram;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-06-14 11:57:38 +00:00
|
|
|
* Start a connection: issue a signed one-off code the user posts in their
|
|
|
|
|
* channel (`/connect <code>`). The code carries the workspace, so the webhook
|
2026-06-14 13:58:13 +00:00
|
|
|
* can link the channel without any persisted state. The returned `nonce` lets
|
|
|
|
|
* the UI recognise its own connection on the broadcast channel.
|
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
|
|
|
*/
|
|
|
|
|
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);
|
|
|
|
|
|
2026-06-14 11:57:38 +00:00
|
|
|
$expiresAt = now()->addMinutes(15);
|
2026-06-14 13:58:13 +00:00
|
|
|
$code = TelegramConnectCode::issue($workspace->id, $expiresAt);
|
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
|
|
|
|
|
|
|
|
return response()->json([
|
2026-06-14 13:58:13 +00:00
|
|
|
'code' => $code,
|
|
|
|
|
'nonce' => data_get(TelegramConnectCode::decode($code), 'nonce'),
|
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
|
|
|
'bot_username' => config('trypost.platforms.telegram.bot_username'),
|
2026-06-14 11:57:38 +00:00
|
|
|
'expires_at' => $expiresAt->toIso8601String(),
|
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
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|