Skip to content

Commit d02a102

Browse files
committed
Allow media group to be sent through HTTP method
1 parent 40e823d commit d02a102

File tree

6 files changed

+81
-19
lines changed

6 files changed

+81
-19
lines changed

composer.lock

Lines changed: 16 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
20.9 KB
Loading
18.9 KB
Loading

examples/send-media-group.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types = 1);
4+
5+
include 'basics.php';
6+
7+
use unreal4u\TelegramAPI\Telegram\Methods\SendMediaGroup;
8+
use unreal4u\TelegramAPI\Telegram\Types\InputMedia\Photo;
9+
use unreal4u\TelegramAPI\TgLog;
10+
use GuzzleHttp\Exception\ClientException;
11+
12+
$tgLog = new TgLog(BOT_TOKEN);
13+
14+
$sendMediaGroup = new SendMediaGroup();
15+
$sendMediaGroup->chat_id = A_USER_CHAT_ID;
16+
#$photos = glob(__DIR__ . '/binary-test-data/*.jpg');
17+
18+
$photos = [
19+
'https://cdn.pixabay.com/photo/2018/01/04/19/43/desktop-background-3061483_960_720.jpg',
20+
'https://cdn.pixabay.com/photo/2017/02/20/19/59/sunset-2083771_960_720.jpg',
21+
'https://cdn.pixabay.com/photo/2017/09/07/15/37/space-2725697_960_720.jpg',
22+
];
23+
24+
foreach ($photos as $photoLocation) {
25+
$inputMedia = new Photo();
26+
$inputMedia->media = $photoLocation;
27+
$inputMedia->caption = basename($photoLocation);
28+
$sendMediaGroup->media[] = $inputMedia;
29+
}
30+
31+
try {
32+
$messageArray = $tgLog->performApiRequest($sendMediaGroup);
33+
$imagesSent = 0;
34+
echo '<pre>';
35+
foreach ($messageArray->traverseObject() as $message) {
36+
$imagesSent++;
37+
}
38+
var_dump('Sent ' . $imagesSent . ' images');
39+
echo '</pre>';
40+
} catch (ClientException $e) {
41+
echo '<pre>';
42+
var_dump($e->getMessage());
43+
var_dump($e->getRequest());
44+
#var_dump($e->getTrace());
45+
echo '</pre>';
46+
}

src/Telegram/Methods/SendMediaGroup.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,22 @@ public function getMandatoryFields(): array
5656

5757
public static function bindToObject(TelegramRawData $data, LoggerInterface $logger): TelegramTypes
5858
{
59-
return new MessageArray($data, $logger);
59+
return new MessageArray($data->getResult(), $logger);
60+
}
61+
62+
public function performSpecialConditions(): TelegramMethods
63+
{
64+
$imageQuantity = \count($this->media);
65+
if ($imageQuantity < 2) {
66+
throw new \RuntimeException('Must include at least 2 images');
67+
}
68+
69+
if ($imageQuantity > 10) {
70+
throw new \RuntimeException('Can not include more than 10 images');
71+
}
72+
73+
$this->media = json_encode($this->media);
74+
75+
return parent::performSpecialConditions();
6076
}
6177
}

src/Telegram/Types/Custom/MessageArray.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Psr\Log\LoggerInterface;
88
use unreal4u\TelegramAPI\Interfaces\CustomArrayType;
99
use unreal4u\TelegramAPI\Telegram\Types\Message;
10+
use unreal4u\TelegramAPI\Telegram\Types\Update;
1011

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

0 commit comments

Comments
 (0)