Skip to content

Commit

Permalink
Merge branch 'v2'
Browse files Browse the repository at this point in the history
  • Loading branch information
unreal4u committed Jan 8, 2018
2 parents b496ca8 + d02a102 commit bc17698
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 25 deletions.
43 changes: 21 additions & 22 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.
51 changes: 51 additions & 0 deletions examples/send-media-group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types = 1);

include __DIR__.'/basics.php';

use React\EventLoop\Factory;
use unreal4u\TelegramAPI\HttpClientRequestHandler;
use unreal4u\TelegramAPI\Telegram\Methods\SendMediaGroup;
use unreal4u\TelegramAPI\Telegram\Types\InputMedia\Photo;
use unreal4u\TelegramAPI\TgLog;

$loop = Factory::create();
$tgLog = new TgLog(BOT_TOKEN, new HttpClientRequestHandler($loop));

$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;
}

$promise = $tgLog->performApiRequest($sendMediaGroup);

$promise->then(
function ($response) {
echo '<pre>';
$imageCounter = 0;
foreach ($response->traverseObject() as $message) {
$imageCounter++;
}
var_dump('Sent ' . $imageCounter . ' images');
echo '</pre>';
},
function (\Exception $exception) {
// Onoes, an exception occurred...
echo 'Exception ' . get_class($exception) . ' caught, message: ' . $exception->getMessage();
}
);

$loop->run();
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(TelegramResponse $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 @@ -6,6 +6,7 @@
use Psr\Log\LoggerInterface;
use unreal4u\TelegramAPI\Abstracts\TraversableCustomType;
use unreal4u\TelegramAPI\Telegram\Types\Message;
use unreal4u\TelegramAPI\Telegram\Types\Update;

/**
* Used for methods that will return an array of messages
Expand All @@ -14,7 +15,7 @@ class MessageArray extends TraversableCustomType
{
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
2 changes: 1 addition & 1 deletion src/TgLog.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function __construct(string $botToken, RequestHandlerInterface $handler,
*/
public function performApiRequest(TelegramMethods $method): PromiseInterface
{
$this->logger->debug('Request for async API call, resetting internal values', [get_class($method)]);
$this->logger->debug('Request for async API call, resetting internal values', [\get_class($method)]);
$this->resetObjectValues();
$option = $this->formConstructor->constructOptions($method);
return $this->sendRequestToTelegram($method, $option)
Expand Down

0 comments on commit bc17698

Please sign in to comment.