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.

Harden Telegram reactions/analytics from PR review; add scoping + edge-case tests

Paulo Castellanoc719480cf1b7
diff --git a/app/Actions/SocialAccount/StoreTelegramReactions.php b/app/Actions/SocialAccount/StoreTelegramReactions.php
index 83998c5d1..2b2f1be5b 100644
--- a/app/Actions/SocialAccount/StoreTelegramReactions.php
+++ b/app/Actions/SocialAccount/StoreTelegramReactions.php
@@ -35,10 +35,12 @@ class StoreTelegramReactions
return;
}
+ $rawReactions = data_get($update, 'reactions');
+
$reactions = array_values(array_map(fn (array $reaction): array => [
'type' => (string) (data_get($reaction, 'type.emoji') ?? __('analytics.metrics.custom_reaction')),
'count' => (int) data_get($reaction, 'total_count'),
- ], data_get($update, 'reactions', [])));
+ ], is_array($rawReactions) ? $rawReactions : []));
$postPlatform->update(['meta' => [...$postPlatform->meta ?? [], 'reactions' => $reactions]]);
diff --git a/app/Services/Social/TelegramAnalytics.php b/app/Services/Social/TelegramAnalytics.php
index 1cf9f1503..196809e03 100644
--- a/app/Services/Social/TelegramAnalytics.php
+++ b/app/Services/Social/TelegramAnalytics.php
@@ -7,6 +7,7 @@ namespace App\Services\Social;
use App\Models\PostPlatform;
use App\Models\SocialAccount;
use Illuminate\Support\Facades\Http;
+use Throwable;
class TelegramAnalytics
{
@@ -25,10 +26,14 @@ class TelegramAnalytics
return [];
}
- $count = data_get(
- Http::get("{$api}/bot{$token}/getChatMemberCount", ['chat_id' => $chatId])->json(),
- 'result',
- );
+ try {
+ $count = data_get(
+ Http::get("{$api}/bot{$token}/getChatMemberCount", ['chat_id' => $chatId])->json(),
+ 'result',
+ );
+ } catch (Throwable) {
+ return [];
+ }
if (! is_int($count)) {
return [];
diff --git a/resources/js/pages/analytics/Index.vue b/resources/js/pages/analytics/Index.vue
index 8c4b009b6..fa0587702 100644
--- a/resources/js/pages/analytics/Index.vue
+++ b/resources/js/pages/analytics/Index.vue
@@ -36,7 +36,16 @@ const selectedAccount = computed(() =>
const platformSupportsDateRange = computed(() => {
if (!selectedAccount.value) return false;
- return ['instagram', 'instagram-facebook', 'facebook', 'youtube', 'pinterest', 'threads', 'x', 'linkedin-page'].includes(selectedAccount.value.platform);
+ return [
+ 'instagram',
+ 'instagram-facebook',
+ 'facebook',
+ 'youtube',
+ 'pinterest',
+ 'threads',
+ 'x',
+ 'linkedin-page',
+ ].includes(selectedAccount.value.platform);
});
</script>
@@ -44,7 +53,9 @@ const platformSupportsDateRange = computed(() => {
<AppLayout>
<Head :title="trans('sidebar.analytics')" />
- <div class="mx-auto flex h-full w-full max-w-6xl flex-col gap-6 px-6 py-8">
+ <div
+ class="mx-auto flex h-full w-full max-w-6xl flex-col gap-6 px-6 py-8"
+ >
<div class="flex flex-wrap items-center justify-between gap-3">
<PageHeader :title="$t('sidebar.analytics')" />
<div class="flex flex-wrap items-center gap-3">
@@ -61,11 +72,17 @@ const platformSupportsDateRange = computed(() => {
</div>
</div>
- <div v-if="accounts.length === 0" class="flex flex-1 items-center justify-center text-sm font-medium text-foreground/60">
+ <div
+ v-if="accounts.length === 0"
+ class="flex flex-1 items-center justify-center text-sm font-medium text-foreground/60"
+ >
{{ $t('analytics.no_accounts') }}
</div>
- <div v-else-if="!selectedAccountId" class="flex flex-1 items-center justify-center text-sm font-medium text-foreground/60">
+ <div
+ v-else-if="!selectedAccountId"
+ class="flex flex-1 items-center justify-center text-sm font-medium text-foreground/60"
+ >
{{ $t('analytics.select_account') }}
</div>
@@ -75,7 +92,10 @@ const platformSupportsDateRange = computed(() => {
/>
<InstagramAnalytics
- v-else-if="selectedAccount?.platform === 'instagram' || selectedAccount?.platform === 'instagram-facebook'"
+ v-else-if="
+ selectedAccount?.platform === 'instagram' ||
+ selectedAccount?.platform === 'instagram-facebook'
+ "
:account-id="selectedAccountId"
:date-range="dateRange"
/>
@@ -121,7 +141,10 @@ const platformSupportsDateRange = computed(() => {
:account-id="selectedAccountId"
/>
- <div v-else class="flex flex-1 items-center justify-center text-sm font-medium text-foreground/60">
+ <div
+ v-else
+ class="flex flex-1 items-center justify-center text-sm font-medium text-foreground/60"
+ >
{{ $t('analytics.no_data') }}
</div>
</div>
diff --git a/tests/Feature/Social/TelegramConnectionTest.php b/tests/Feature/Social/TelegramConnectionTest.php
index bb80d30ba..767f43757 100644
--- a/tests/Feature/Social/TelegramConnectionTest.php
+++ b/tests/Feature/Social/TelegramConnectionTest.php
@@ -206,6 +206,78 @@ it('stores reaction counts on the matching post from a reaction update', functio
->toBe([['type' => '👍', 'count' => 12], ['type' => '❤️', 'count' => 5]]);
});
+it('does not store reactions on a post from a different channel with the same message id', function () {
+ $otherAccount = SocialAccount::factory()->telegram()->create([
+ 'workspace_id' => $this->workspace->id,
+ 'platform_user_id' => '-1009999999999',
+ 'meta' => ['chat_id' => '-1009999999999', 'username' => 'other', 'type' => 'channel'],
+ ]);
+ $post = Post::factory()->create(['workspace_id' => $this->workspace->id, 'user_id' => $this->user->id]);
+ $postPlatform = PostPlatform::factory()->published()->create([
+ 'post_id' => $post->id,
+ 'social_account_id' => $otherAccount->id,
+ 'platform' => Platform::Telegram,
+ 'platform_post_id' => '42',
+ ]);
+
+ $update = [
+ 'message_reaction_count' => [
+ 'chat' => ['id' => -1001234567890],
+ 'message_id' => 42,
+ 'reactions' => [['type' => ['type' => 'emoji', 'emoji' => '👍'], 'total_count' => 9]],
+ ],
+ ];
+
+ $this->withHeader('X-Telegram-Bot-Api-Secret-Token', 'shh-secret')
+ ->postJson(route('telegram.webhook'), $update)
+ ->assertNoContent();
+
+ expect(data_get($postPlatform->fresh()->meta, 'reactions'))->toBeNull();
+});
+
+it('labels custom emoji reactions with a fallback', function () {
+ $account = SocialAccount::factory()->telegram()->create(['workspace_id' => $this->workspace->id]);
+ $post = Post::factory()->create(['workspace_id' => $this->workspace->id, 'user_id' => $this->user->id]);
+ $postPlatform = PostPlatform::factory()->published()->create([
+ 'post_id' => $post->id,
+ 'social_account_id' => $account->id,
+ 'platform' => Platform::Telegram,
+ 'platform_post_id' => '77',
+ ]);
+
+ $update = [
+ 'message_reaction_count' => [
+ 'chat' => ['id' => -1001234567890],
+ 'message_id' => 77,
+ 'reactions' => [['type' => ['type' => 'custom_emoji', 'custom_emoji_id' => '555'], 'total_count' => 3]],
+ ],
+ ];
+
+ $this->withHeader('X-Telegram-Bot-Api-Secret-Token', 'shh-secret')
+ ->postJson(route('telegram.webhook'), $update)
+ ->assertNoContent();
+
+ expect(data_get($postPlatform->fresh()->meta, 'reactions'))
+ ->toBe([['type' => 'Custom', 'count' => 3]]);
+});
+
+it('connects without an avatar when the channel has no photo', function () {
+ Http::fake([
+ '*/botTESTTOKEN/getChat*' => Http::response(['ok' => true, 'result' => []], 200),
+ ]);
+
+ $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();
+
+ expect($account)->not->toBeNull();
+ expect($account->getRawOriginal('avatar_url'))->toBeNull();
+});
+
it('rejects the webhook without the secret token', function () {
$code = TelegramConnectCode::issue($this->workspace->id, now()->addMinutes(15));