Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit bc17698

Browse files
committed
Merge branch 'v2'
2 parents b496ca8 + d02a102 commit bc17698

File tree

7 files changed

+92
-25
lines changed

7 files changed

+92
-25
lines changed

composer.lock

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

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(TelegramResponse $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
@@ -6,6 +6,7 @@
66
use Psr\Log\LoggerInterface;
77
use unreal4u\TelegramAPI\Abstracts\TraversableCustomType;
88
use unreal4u\TelegramAPI\Telegram\Types\Message;
9+
use unreal4u\TelegramAPI\Telegram\Types\Update;
910

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

src/TgLog.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function __construct(string $botToken, RequestHandlerInterface $handler,
8383
*/
8484
public function performApiRequest(TelegramMethods $method): PromiseInterface
8585
{
86-
$this->logger->debug('Request for async API call, resetting internal values', [get_class($method)]);
86+
$this->logger->debug('Request for async API call, resetting internal values', [\get_class($method)]);
8787
$this->resetObjectValues();
8888
$option = $this->formConstructor->constructOptions($method);
8989
return $this->sendRequestToTelegram($method, $option)

0 commit comments

Comments
 (0)