Skip to content

Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.

Fetch Telegram channel photo synchronously in the connect action (drop avatar job)

Paulo Castellanof5d6c4a18694
diff --git a/app/Actions/SocialAccount/ConnectTelegramChannel.php b/app/Actions/SocialAccount/ConnectTelegramChannel.php
index 0e2514f45..b5711919b 100644
--- a/app/Actions/SocialAccount/ConnectTelegramChannel.php
+++ b/app/Actions/SocialAccount/ConnectTelegramChannel.php
@@ -7,11 +7,12 @@ namespace App\Actions\SocialAccount;
use App\Enums\SocialAccount\Platform;
use App\Enums\SocialAccount\Status;
use App\Features\SocialAccountLimit;
-use App\Jobs\SyncTelegramAccountAvatar;
use App\Models\SocialAccount;
use App\Models\Workspace;
use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\Http;
use Laravel\Pennant\Feature;
+use Throwable;
class ConnectTelegramChannel
{
@@ -42,7 +43,7 @@ class ConnectTelegramChannel
return null;
}
- $account = $workspace->socialAccounts()->updateOrCreate(
+ return $workspace->socialAccounts()->updateOrCreate(
[
'platform' => Platform::Telegram->value,
'platform_user_id' => $chatId,
@@ -50,6 +51,7 @@ class ConnectTelegramChannel
[
'username' => $username,
'display_name' => data_get($chat, 'title') ?? $username ?? "Telegram {$chatId}",
+ 'avatar_url' => self::fetchChannelAvatar($chatId),
'access_token' => '',
'refresh_token' => '',
'token_expires_at' => null,
@@ -65,10 +67,37 @@ class ConnectTelegramChannel
],
],
);
+ }
- SyncTelegramAccountAvatar::dispatch($account);
+ /**
+ * Download the channel's photo via the Bot API and store it, returning the path.
+ */
+ private static function fetchChannelAvatar(string $chatId): ?string
+ {
+ $token = (string) config('trypost.platforms.telegram.bot_token');
+ $api = rtrim((string) config('trypost.platforms.telegram.api'), '/');
+
+ if ($token === '') {
+ return null;
+ }
- return $account;
+ try {
+ $fileId = data_get(Http::get("{$api}/bot{$token}/getChat", ['chat_id' => $chatId])->json(), 'result.photo.big_file_id');
+
+ if (! is_string($fileId)) {
+ return null;
+ }
+
+ $filePath = data_get(Http::get("{$api}/bot{$token}/getFile", ['file_id' => $fileId])->json(), 'result.file_path');
+
+ if (! is_string($filePath)) {
+ return null;
+ }
+
+ return uploadFromUrl("{$api}/file/bot{$token}/{$filePath}");
+ } catch (Throwable) {
+ return null;
+ }
}
private static function workspaceAtAccountLimit(Workspace $workspace): bool
diff --git a/app/Jobs/SyncTelegramAccountAvatar.php b/app/Jobs/SyncTelegramAccountAvatar.php
deleted file mode 100644
index de2268b4e..000000000
--- a/app/Jobs/SyncTelegramAccountAvatar.php
+++ /dev/null
@@ -1,50 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-namespace App\Jobs;
-
-use App\Models\SocialAccount;
-use Illuminate\Contracts\Queue\ShouldQueue;
-use Illuminate\Foundation\Queue\Queueable;
-use Illuminate\Support\Facades\Http;
-
-class SyncTelegramAccountAvatar implements ShouldQueue
-{
- use Queueable;
-
- public int $tries = 1;
-
- public function __construct(public SocialAccount $account) {}
-
- public function handle(): void
- {
- $token = (string) config('trypost.platforms.telegram.bot_token');
- $api = rtrim((string) config('trypost.platforms.telegram.api'), '/');
- $chatId = data_get($this->account->meta, 'chat_id');
-
- if ($token === '' || $chatId === null) {
- return;
- }
-
- $chat = Http::get("{$api}/bot{$token}/getChat", ['chat_id' => $chatId]);
- $fileId = data_get($chat->json(), 'result.photo.big_file_id');
-
- if (! is_string($fileId)) {
- return;
- }
-
- $file = Http::get("{$api}/bot{$token}/getFile", ['file_id' => $fileId]);
- $filePath = data_get($file->json(), 'result.file_path');
-
- if (! is_string($filePath)) {
- return;
- }
-
- $avatar = uploadFromUrl("{$api}/file/bot{$token}/{$filePath}");
-
- if ($avatar !== null) {
- $this->account->update(['avatar_url' => $avatar]);
- }
- }
-}
diff --git a/tests/Feature/Jobs/SyncTelegramAccountAvatarTest.php b/tests/Feature/Jobs/SyncTelegramAccountAvatarTest.php
deleted file mode 100644
index 9072c7f6c..000000000
--- a/tests/Feature/Jobs/SyncTelegramAccountAvatarTest.php
+++ /dev/null
@@ -1,39 +0,0 @@
-<?php
-
-declare(strict_types=1);
-
-use App\Jobs\SyncTelegramAccountAvatar;
-use App\Models\SocialAccount;
-use Illuminate\Support\Facades\Http;
-use Illuminate\Support\Facades\Storage;
-
-beforeEach(function () {
- config(['trypost.platforms.telegram.bot_token' => 'TESTTOKEN']);
- Storage::fake();
-});
-
-it('stores the telegram channel photo as the account avatar', function () {
- $account = SocialAccount::factory()->telegram()->create(['avatar_url' => null]);
-
- Http::fake([
- '*/botTESTTOKEN/getChat*' => Http::response(['ok' => true, 'result' => ['photo' => ['big_file_id' => 'BIGFILE']]], 200),
- '*/botTESTTOKEN/getFile*' => Http::response(['ok' => true, 'result' => ['file_path' => 'photos/file_1.jpg']], 200),
- '*/file/botTESTTOKEN/photos/file_1.jpg' => Http::response('image-bytes', 200, ['Content-Type' => 'image/jpeg']),
- ]);
-
- (new SyncTelegramAccountAvatar($account))->handle();
-
- expect($account->fresh()->getRawOriginal('avatar_url'))->not->toBeNull();
-});
-
-it('does nothing when the channel has no photo', function () {
- $account = SocialAccount::factory()->telegram()->create(['avatar_url' => null]);
-
- Http::fake([
- '*/botTESTTOKEN/getChat*' => Http::response(['ok' => true, 'result' => []], 200),
- ]);
-
- (new SyncTelegramAccountAvatar($account))->handle();
-
- expect($account->fresh()->getRawOriginal('avatar_url'))->toBeNull();
-});
diff --git a/tests/Feature/Social/TelegramConnectionTest.php b/tests/Feature/Social/TelegramConnectionTest.php
index 2f8451d5d..b5acad5a2 100644
--- a/tests/Feature/Social/TelegramConnectionTest.php
+++ b/tests/Feature/Social/TelegramConnectionTest.php
@@ -4,18 +4,15 @@ declare(strict_types=1);
use App\Enums\SocialAccount\Platform;
use App\Enums\UserWorkspace\Role;
-use App\Jobs\SyncTelegramAccountAvatar;
use App\Models\SocialAccount;
use App\Models\User;
use App\Models\Workspace;
use App\Services\Social\ConnectionVerifier;
use App\Services\Social\TelegramConnectCode;
use Illuminate\Support\Facades\Http;
-use Illuminate\Support\Facades\Queue;
+use Illuminate\Support\Facades\Storage;
beforeEach(function () {
- Queue::fake();
-
config([
'trypost.platforms.telegram.bot_token' => 'TESTTOKEN',
'trypost.platforms.telegram.bot_username' => 'TryPostBot',
@@ -59,6 +56,7 @@ it('issues a signed connect code carrying the workspace', function () {
});
it('links the channel when the webhook receives a matching /connect', function () {
+ Http::fake();
$code = TelegramConnectCode::issue($this->workspace->id, now()->addMinutes(15));
$this->withHeader('X-Telegram-Bot-Api-Secret-Token', 'shh-secret')
@@ -76,11 +74,30 @@ it('links the channel when the webhook receives a matching /connect', function (
expect(data_get($account->meta, 'chat_id'))->toBe('-1001234567890');
expect(data_get($account->meta, 'connect_nonce'))
->toBe(data_get(TelegramConnectCode::decode($code), 'nonce'));
+});
+
+it('stores the channel photo as the account avatar on connect', function () {
+ Storage::fake();
+
+ Http::fake([
+ '*/botTESTTOKEN/getChat*' => Http::response(['ok' => true, 'result' => ['photo' => ['big_file_id' => 'BIGFILE']]], 200),
+ '*/botTESTTOKEN/getFile*' => Http::response(['ok' => true, 'result' => ['file_path' => 'photos/file_1.jpg']], 200),
+ '*/file/botTESTTOKEN/photos/file_1.jpg' => Http::response('image-bytes', 200, ['Content-Type' => 'image/jpeg']),
+ ]);
+
+ $code = TelegramConnectCode::issue($this->workspace->id, now()->addMinutes(15));
+
+ $this->withHeader('X-Telegram-Bot-Api-Secret-Token', 'shh-secret')
+ ->postJson(route('telegram.webhook'), telegramUpdate($code))
+ ->assertNoContent();
+
+ $account = SocialAccount::where('platform', Platform::Telegram)->first();
- Queue::assertPushed(SyncTelegramAccountAvatar::class);
+ expect($account->getRawOriginal('avatar_url'))->not->toBeNull();
});
it('links a private channel that has no username', function () {
+ Http::fake();
$code = TelegramConnectCode::issue($this->workspace->id, now()->addMinutes(15));
$this->withHeader('X-Telegram-Bot-Api-Secret-Token', 'shh-secret')
@@ -112,6 +129,7 @@ it('does not create a new telegram account when the workspace is at its limit',
});
it('still reconnects an existing telegram channel even at the account limit', function () {
+ Http::fake();
config(['trypost.self_hosted' => false]);
SocialAccount::factory()->count(4)->create(['workspace_id' => $this->workspace->id]);
@@ -133,6 +151,7 @@ it('still reconnects an existing telegram channel even at the account limit', fu
});
it('consumes the code once so it cannot be replayed for another chat', function () {
+ Http::fake();
$code = TelegramConnectCode::issue($this->workspace->id, now()->addMinutes(15));
$this->withHeader('X-Telegram-Bot-Api-Secret-Token', 'shh-secret')