Skip to content

Commit c599d54

Browse files
committed
Helper for sending InputMedia objects using Request::sendMediaGroup() and Request::editMediaMessage() methods.
1 parent 0f8e027 commit c599d54

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Exclamation symbols (:exclamation:) note something of importance e.g. breaking c
55

66
## [Unreleased]
77
### Added
8+
- Helper for sending `InputMedia` objects using `Request::sendMediaGroup()` and `Request::editMediaMessage()` methods.
89
### Changed
910
### Deprecated
1011
- Botan.io service has been discontinued.

src/Entities/Entity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
use Exception;
1414
use Longman\TelegramBot\Entities\InlineQuery\InlineEntity;
15-
use Longman\TelegramBot\TelegramLog;
15+
use Longman\TelegramBot\Entities\InputMedia\InputMedia;
1616

1717
/**
1818
* Class Entity
@@ -149,7 +149,7 @@ public function __call($method, $args)
149149
}
150150
} elseif ($action === 'set') {
151151
// Limit setters to specific classes.
152-
if ($this instanceof InlineEntity || $this instanceof Keyboard || $this instanceof KeyboardButton) {
152+
if ($this instanceof InlineEntity || $this instanceof InputMedia || $this instanceof Keyboard || $this instanceof KeyboardButton) {
153153
$this->$property_name = $args[0];
154154

155155
return $this;

src/Request.php

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
use GuzzleHttp\Client;
1414
use GuzzleHttp\Exception\RequestException;
15+
use GuzzleHttp\Psr7\Stream;
1516
use Longman\TelegramBot\Entities\File;
17+
use Longman\TelegramBot\Entities\InputMedia\InputMedia;
1618
use Longman\TelegramBot\Entities\ServerResponse;
1719
use Longman\TelegramBot\Exception\InvalidBotTokenException;
1820
use Longman\TelegramBot\Exception\TelegramException;
@@ -324,23 +326,80 @@ private static function setUpRequestParams(array $data)
324326
$has_resource = false;
325327
$multipart = [];
326328

327-
// Convert any nested arrays into JSON strings.
328-
array_walk($data, function (&$item) {
329-
is_array($item) && $item = json_encode($item);
330-
});
329+
foreach ($data as $key => &$item) {
330+
if ($key === 'media') {
331+
// Magical media input helper.
332+
$item = self::mediaInputHelper($item, $has_resource, $multipart);
333+
} elseif (is_array($item)) {
334+
// Convert any nested arrays into JSON strings.
335+
$item = json_encode($item);
336+
}
331337

332-
//Reformat data array in multipart way if it contains a resource
333-
foreach ($data as $key => $item) {
334-
$has_resource |= (is_resource($item) || $item instanceof \GuzzleHttp\Psr7\Stream);
338+
// Reformat data array in multipart way if it contains a resource
339+
$has_resource |= (is_resource($item) || $item instanceof Stream);
335340
$multipart[] = ['name' => $key, 'contents' => $item];
336341
}
342+
337343
if ($has_resource) {
338344
return ['multipart' => $multipart];
339345
}
340346

341347
return ['form_params' => $data];
342348
}
343349

350+
/**
351+
* Magical input media helper to simplify passing media.
352+
*
353+
* This allows the following:
354+
* Request::editMessageMedia([
355+
* ...
356+
* 'media' => new InputMediaPhoto([
357+
* 'caption' => 'Caption!',
358+
* 'media' => Request::encodeFile($local_photo),
359+
* ]),
360+
* ]);
361+
* and
362+
* Request::sendMediaGroup([
363+
* 'media' => [
364+
* new InputMediaPhoto(['media' => Request::encodeFile($photo_1)]),
365+
* new InputMediaPhoto(['media' => Request::encodeFile($photo_2)]),
366+
* new InputMediaVideo(['media' => Request::encodeFile($video_1)]),
367+
* ],
368+
* ]);
369+
*
370+
* @param mixed $item
371+
* @param bool $has_resource
372+
* @param array $multipart
373+
*
374+
* @return mixed
375+
*/
376+
private static function mediaInputHelper($item, &$has_resource, array &$multipart)
377+
{
378+
$was_array = is_array($item);
379+
$was_array || $item = [$item];
380+
381+
foreach ($item as $media_item) {
382+
if (!($media_item instanceof InputMedia)) {
383+
continue;
384+
}
385+
386+
$media = $media_item->getMedia();
387+
if (is_resource($media) || $media instanceof Stream) {
388+
$has_resource = true;
389+
$rnd_key = uniqid('media_', false);
390+
$multipart[] = ['name' => $rnd_key, 'contents' => $media];
391+
392+
// We're literally overwriting the passed media data!
393+
$media_item->media = 'attach://' . $rnd_key;
394+
$media_item->raw_data['media'] = 'attach://' . $rnd_key;
395+
}
396+
}
397+
398+
$was_array || $item = reset($item);
399+
400+
return json_encode($item);
401+
}
402+
344403
/**
345404
* Execute HTTP Request
346405
*

0 commit comments

Comments
 (0)