Skip to content

Commit 4d62b85

Browse files
committed
Latest
1 parent d1f76f6 commit 4d62b85

File tree

8 files changed

+497
-56
lines changed

8 files changed

+497
-56
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ All notable changes to `telegram` will be documented in this file
99
* Drop support for Laravel 10.
1010
* Drop support for PHP 8.1.
1111
* Add support for Laravel 12.
12+
* Add `sticker` method to the `TelegramFile` to send sticker file.
1213
* Add `sendWhen` method to conditionally send a message.
1314
* Add ParseMode Enum and refactor parsing mode setting logic.
1415
* Add `buttonWithWebApp` method to open web app from a button.
1516
* Add `onError` method to handle exceptions. Based of https://github.com/laravel-notification-channels/telegram/pull/201
17+
* Refactor `sendFile` to support raw data sending.
1618
* Refactor `escapedLine` method.
1719
* Refactor `HasSharedLogic` trait.
1820
* Refactor classes to use PHP 8.2 features.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ For more information on supported parameters, check out these [docs](https://cor
513513
- `animation(string $file)` - Send animated GIF.
514514
- `voice(string $file)` - Send voice note (OGG/OPUS).
515515
- `videoNote(string $file)` - Send video note (≤1min, rounded square video).
516+
- `sticker(string $file)` - Send sticker (static PNG/WEBP, animated .TGS, or video .WEBM stickers).
516517

517518
### Telegram Contact Methods
518519

src/Enums/FileType.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
namespace NotificationChannels\Telegram\Enums;
4+
5+
/**
6+
* Enum FileType
7+
*
8+
* Represents the different file types supported by Telegram Bot API.
9+
*/
10+
enum FileType: string
11+
{
12+
case Document = 'document';
13+
case Photo = 'photo';
14+
case Audio = 'audio';
15+
case Video = 'video';
16+
case Animation = 'animation';
17+
case Voice = 'voice';
18+
case VideoNote = 'video_note';
19+
case Sticker = 'sticker';
20+
21+
/**
22+
* Get the mime type associated with this file type.
23+
*/
24+
public function getMimeType(): string
25+
{
26+
return match($this) {
27+
self::Document => 'application/octet-stream',
28+
self::Photo => 'image/jpeg',
29+
self::Audio => 'audio/mp3',
30+
self::Video => 'video/mp4',
31+
self::Animation => 'video/mp4',
32+
self::Voice => 'audio/ogg',
33+
self::VideoNote => 'video/mp4',
34+
self::Sticker => 'image/webp',
35+
};
36+
}
37+
38+
/**
39+
* Get allowed file extensions for this type.
40+
*
41+
* @return array<string>
42+
*/
43+
public function getAllowedExtensions(): array
44+
{
45+
return match($this) {
46+
self::Document => [], // Any extension allowed
47+
self::Photo => ['jpg', 'jpeg', 'png', 'webp'],
48+
self::Audio => ['mp3', 'ogg', 'm4a'],
49+
self::Video => ['mp4', 'avi', 'mov', 'mkv'],
50+
self::Animation => ['gif', 'mp4'],
51+
self::Voice => ['ogg', 'mp3'],
52+
self::VideoNote => ['mp4'],
53+
self::Sticker => ['png', 'webp', 'tgs', 'webm'],
54+
};
55+
}
56+
57+
/**
58+
* Check if a file extension is allowed for this type.
59+
*/
60+
public function isExtensionAllowed(string $extension): bool
61+
{
62+
$extensions = $this->getAllowedExtensions();
63+
64+
// Document allows all extensions
65+
if ($this === self::Document || empty($extensions)) {
66+
return true;
67+
}
68+
69+
return in_array(strtolower($extension), $extensions, true);
70+
}
71+
72+
/**
73+
* Get all file types as an array.
74+
*
75+
* @return array<string, string>
76+
*/
77+
public static function toArray(): array
78+
{
79+
return array_column(self::cases(), 'value', 'name');
80+
}
81+
}

src/Exceptions/CouldNotSendNotification.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,20 @@ public static function couldNotCommunicateWithTelegram(string $message): self
4646
{
4747
return new self("The communication with Telegram failed. `{$message}`");
4848
}
49+
50+
/**
51+
* Thrown when the file cannot be opened.
52+
*/
53+
public static function fileAccessFailed(string $file): self
54+
{
55+
return new self("Failed to open file: {$file}");
56+
}
57+
58+
/**
59+
* Thrown when the file identifier is invalid (ID or URL).
60+
*/
61+
public static function invalidFileIdentifier(string $file): self
62+
{
63+
return new self("Invalid file identifier: {$file}");
64+
}
4965
}

0 commit comments

Comments
 (0)