|
12 | 12 |
|
13 | 13 | use GuzzleHttp\Client; |
14 | 14 | use GuzzleHttp\Exception\RequestException; |
| 15 | +use GuzzleHttp\Psr7\Stream; |
15 | 16 | use Longman\TelegramBot\Entities\File; |
| 17 | +use Longman\TelegramBot\Entities\InputMedia\InputMedia; |
16 | 18 | use Longman\TelegramBot\Entities\ServerResponse; |
17 | 19 | use Longman\TelegramBot\Exception\InvalidBotTokenException; |
18 | 20 | use Longman\TelegramBot\Exception\TelegramException; |
@@ -324,23 +326,80 @@ private static function setUpRequestParams(array $data) |
324 | 326 | $has_resource = false; |
325 | 327 | $multipart = []; |
326 | 328 |
|
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 | + } |
331 | 337 |
|
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); |
335 | 340 | $multipart[] = ['name' => $key, 'contents' => $item]; |
336 | 341 | } |
| 342 | + |
337 | 343 | if ($has_resource) { |
338 | 344 | return ['multipart' => $multipart]; |
339 | 345 | } |
340 | 346 |
|
341 | 347 | return ['form_params' => $data]; |
342 | 348 | } |
343 | 349 |
|
| 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 | + |
344 | 403 | /** |
345 | 404 | * Execute HTTP Request |
346 | 405 | * |
|
0 commit comments