Skip to content

Commit

Permalink
Allow media group to be sent through HTTP method
Browse files Browse the repository at this point in the history
  • Loading branch information
unreal4u committed Jan 8, 2018
1 parent 40e823d commit d02a102
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 19 deletions.
33 changes: 16 additions & 17 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/binary-test-data/futurama-seymour.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions examples/send-media-group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types = 1);

include 'basics.php';

use unreal4u\TelegramAPI\Telegram\Methods\SendMediaGroup;
use unreal4u\TelegramAPI\Telegram\Types\InputMedia\Photo;
use unreal4u\TelegramAPI\TgLog;
use GuzzleHttp\Exception\ClientException;

$tgLog = new TgLog(BOT_TOKEN);

$sendMediaGroup = new SendMediaGroup();
$sendMediaGroup->chat_id = A_USER_CHAT_ID;
#$photos = glob(__DIR__ . '/binary-test-data/*.jpg');

$photos = [
'https://cdn.pixabay.com/photo/2018/01/04/19/43/desktop-background-3061483_960_720.jpg',
'https://cdn.pixabay.com/photo/2017/02/20/19/59/sunset-2083771_960_720.jpg',
'https://cdn.pixabay.com/photo/2017/09/07/15/37/space-2725697_960_720.jpg',
];

foreach ($photos as $photoLocation) {
$inputMedia = new Photo();
$inputMedia->media = $photoLocation;
$inputMedia->caption = basename($photoLocation);
$sendMediaGroup->media[] = $inputMedia;
}

try {
$messageArray = $tgLog->performApiRequest($sendMediaGroup);
$imagesSent = 0;
echo '<pre>';
foreach ($messageArray->traverseObject() as $message) {
$imagesSent++;
}
var_dump('Sent ' . $imagesSent . ' images');
echo '</pre>';
} catch (ClientException $e) {
echo '<pre>';
var_dump($e->getMessage());
var_dump($e->getRequest());
#var_dump($e->getTrace());
echo '</pre>';
}
18 changes: 17 additions & 1 deletion src/Telegram/Methods/SendMediaGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,22 @@ public function getMandatoryFields(): array

public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes
{
return new MessageArray($data, $logger);
return new MessageArray($data->getResult(), $logger);
}

public function performSpecialConditions(): TelegramMethods
{
$imageQuantity = \count($this->media);
if ($imageQuantity < 2) {
throw new \RuntimeException('Must include at least 2 images');
}

if ($imageQuantity > 10) {
throw new \RuntimeException('Can not include more than 10 images');
}

$this->media = json_encode($this->media);

return parent::performSpecialConditions();
}
}
3 changes: 2 additions & 1 deletion src/Telegram/Types/Custom/MessageArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Psr\Log\LoggerInterface;
use unreal4u\TelegramAPI\Interfaces\CustomArrayType;
use unreal4u\TelegramAPI\Telegram\Types\Message;
use unreal4u\TelegramAPI\Telegram\Types\Update;

/**
* Used for methods that will return an array of messages
Expand All @@ -15,7 +16,7 @@ class MessageArray extends CustomType implements CustomArrayType
{
public function __construct(array $data = null, LoggerInterface $logger = null)
{
if (count($data) !== 0) {
if (\count($data) !== 0) {
foreach ($data as $telegramResponse) {
// Create an actual Update object and fill the array
$this->data[] = new Message($telegramResponse, $logger);
Expand Down

0 comments on commit d02a102

Please sign in to comment.