Skip to content

Commit 8cca79e

Browse files
committed
Allow absolute file paths for InputFile fields.
1 parent a9ceaf3 commit 8cca79e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
66
## [Unreleased]
77
### Added
88
- Helper for sending `InputMedia` objects using `Request::sendMediaGroup()` and `Request::editMediaMessage()` methods.
9+
- Allow passing absolute file path for InputFile fields, instead of `Request::encodeFile($path)`.
910
### Changed
1011
### Deprecated
1112
- Botan.io service has been discontinued.

src/Request.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,30 @@ class Request
222222
'getMe',
223223
];
224224

225+
/**
226+
* Available fields for InputFile helper
227+
*
228+
* This is basically the list of all fields that allow InputFile objects
229+
* for which input can be simplified by providing local path directly as string.
230+
*
231+
* @var array
232+
*/
233+
private static $input_file_fields = [
234+
'setWebhook' => ['certificate'],
235+
'sendPhoto' => ['photo'],
236+
'sendAudio' => ['audio', 'thumb'],
237+
'sendDocument' => ['document', 'thumb'],
238+
'sendVideo' => ['video', 'thumb'],
239+
'sendAnimation' => ['animation', 'thumb'],
240+
'sendVoice' => ['voice', 'thumb'],
241+
'sendVideoNote' => ['video_note', 'thumb'],
242+
'setChatPhoto' => ['photo'],
243+
'sendSticker' => ['sticker'],
244+
'uploadStickerFile' => ['png_sticker'],
245+
'createNewStickerSet' => ['png_sticker'],
246+
'addStickerToSet' => ['png_sticker'],
247+
];
248+
225249
/**
226250
* Initialize
227251
*
@@ -327,6 +351,7 @@ public static function generateGeneralFakeServerResponse(array $data = [])
327351
* @param array $data
328352
*
329353
* @return array
354+
* @throws TelegramException
330355
*/
331356
private static function setUpRequestParams(array $data)
332357
{
@@ -337,6 +362,10 @@ private static function setUpRequestParams(array $data)
337362
if ($key === 'media') {
338363
// Magical media input helper.
339364
$item = self::mediaInputHelper($item, $has_resource, $multipart);
365+
} elseif (array_key_exists(self::$current_action, self::$input_file_fields) && in_array($key, self::$input_file_fields[self::$current_action], true)) {
366+
if (is_string($item) && file_exists($item)) {
367+
$item = new Stream(self::encodeFile($item));
368+
}
340369
} elseif (is_array($item)) {
341370
// Convert any nested arrays into JSON strings.
342371
$item = json_encode($item);

0 commit comments

Comments
 (0)