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.

Type Telegram media kinds with a TelegramMediaType enum

Paulo Castellano80b68088a05d
diff --git a/app/Services/Social/TelegramMediaType.php b/app/Services/Social/TelegramMediaType.php
new file mode 100644
index 000000000..b4f192111
--- /dev/null
+++ b/app/Services/Social/TelegramMediaType.php
@@ -0,0 +1,39 @@
+<?php
+
+declare(strict_types=1);
+
+namespace App\Services\Social;
+
+use App\DataTransferObjects\MediaItem;
+
+/**
+ * Telegram's media kinds, as used both in the `sendMediaGroup` `type` field and
+ * to pick the matching single-media Bot API method.
+ */
+enum TelegramMediaType: string
+{
+ case Photo = 'photo';
+ case Video = 'video';
+ case Document = 'document';
+
+ public static function for(MediaItem $media): self
+ {
+ return match (true) {
+ $media->isImage() => self::Photo,
+ $media->isVideo() => self::Video,
+ default => self::Document,
+ };
+ }
+
+ /**
+ * The Bot API method that sends a single media of this type.
+ */
+ public function sendMethod(): string
+ {
+ return match ($this) {
+ self::Photo => 'sendPhoto',
+ self::Video => 'sendVideo',
+ self::Document => 'sendDocument',
+ };
+ }
+}
diff --git a/app/Services/Social/TelegramPublisher.php b/app/Services/Social/TelegramPublisher.php
index 073471f41..997d1fb87 100644
--- a/app/Services/Social/TelegramPublisher.php
+++ b/app/Services/Social/TelegramPublisher.php
@@ -79,19 +79,15 @@ class TelegramPublisher
}
/**
- * @param array{type: string, url: string} $item
+ * @param array{type: TelegramMediaType, url: string} $item
*/
private function sendSingleMedia(string $chatId, array $item, string $caption): int
{
- $method = match ($item['type']) {
- 'photo' => 'sendPhoto',
- 'video' => 'sendVideo',
- default => 'sendDocument',
- };
+ $type = $item['type'];
- $response = $this->call($method, [
+ $response = $this->call($type->sendMethod(), [
'chat_id' => $chatId,
- $item['type'] => $item['url'],
+ $type->value => $item['url'],
'caption' => $caption,
'parse_mode' => 'HTML',
]);
@@ -100,14 +96,14 @@ class TelegramPublisher
}
/**
- * @param array<int, array{type: string, url: string}> $items
+ * @param array<int, array{type: TelegramMediaType, url: string}> $items
*/
private function sendMediaGroup(string $chatId, array $items, string $caption): int
{
$group = [];
foreach ($items as $index => $item) {
- $entry = ['type' => $item['type'], 'media' => $item['url']];
+ $entry = ['type' => $item['type']->value, 'media' => $item['url']];
if ($index === 0 && $caption !== '') {
$entry['caption'] = $caption;
@@ -126,17 +122,11 @@ class TelegramPublisher
}
/**
- * @return array{type: string, url: string}
+ * @return array{type: TelegramMediaType, url: string}
*/
private function telegramMedia(MediaItem $media): array
{
- $type = match (true) {
- $media->isImage() => 'photo',
- $media->isVideo() => 'video',
- default => 'document',
- };
-
- return ['type' => $type, 'url' => $media->url];
+ return ['type' => TelegramMediaType::for($media), 'url' => $media->url];
}
private function call(string $method, array $payload): Response