1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace unreal4u\TelegramAPI\Telegram\Methods; |
6
|
|
|
|
7
|
|
|
use Generator; |
8
|
|
|
use Psr\Log\LoggerInterface; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use unreal4u\TelegramAPI\Abstracts\TelegramMethods; |
11
|
|
|
use unreal4u\TelegramAPI\Abstracts\TelegramTypes; |
12
|
|
|
use unreal4u\TelegramAPI\Exceptions\InvalidMediaType; |
13
|
|
|
use unreal4u\TelegramAPI\InternalFunctionality\TelegramResponse; |
14
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\Custom\InputFile; |
15
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\Custom\MessageArray; |
16
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\InputMedia; |
17
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\InputMedia\Photo; |
18
|
|
|
use unreal4u\TelegramAPI\Telegram\Types\InputMedia\Video; |
19
|
|
|
use function basename; |
20
|
|
|
use function count; |
21
|
|
|
use function is_readable; |
22
|
|
|
use function json_encode; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Use this method to send photos. On success, the sent Message is returned |
26
|
|
|
* |
27
|
|
|
* Objects defined as-is January 2017 |
28
|
|
|
* |
29
|
|
|
* @see https://core.telegram.org/bots/api#sendphoto |
30
|
|
|
*/ |
31
|
|
|
class SendMediaGroup extends TelegramMethods |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Unique identifier for the target chat or username of the target channel (in the format @channelusername) |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
public $chat_id = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* A JSON-serialized array describing photos and videos to be sent |
41
|
|
|
* @var InputMedia[] |
42
|
|
|
*/ |
43
|
|
|
public $media = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var InputFile[] |
47
|
|
|
*/ |
48
|
|
|
private $localFiles = []; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Optional. Sends the message silently. iOS users will not receive a notification, Android users will receive a |
52
|
|
|
* notification with no sound. |
53
|
|
|
* @see https://telegram.org/blog/channels-2-0#silent-messages |
54
|
|
|
* @var bool |
55
|
|
|
*/ |
56
|
|
|
public $disable_notification = false; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Optional. If the message is a reply, ID of the original message |
60
|
|
|
* @var int |
61
|
|
|
*/ |
62
|
|
|
public $reply_to_message_id = 0; |
63
|
|
|
|
64
|
|
|
public function getMandatoryFields(): array |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
'chat_id', |
68
|
|
|
'media', |
69
|
|
|
]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public static function bindToObject(TelegramResponse $data, LoggerInterface $logger): TelegramTypes |
73
|
|
|
{ |
74
|
|
|
return new MessageArray($data->getResult(), $logger); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function performSpecialConditions(): TelegramMethods |
78
|
|
|
{ |
79
|
|
|
$imageQuantity = count($this->media); |
80
|
|
|
if ($imageQuantity < 2) { |
81
|
|
|
throw new RuntimeException('Must include at least 2 images'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
if ($imageQuantity > 10) { |
85
|
|
|
throw new RuntimeException('Can not include more than 10 images'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$this->performSpecialOperationsOnLocalFiles(); |
89
|
|
|
$this->media = json_encode($this->media); |
90
|
|
|
|
91
|
|
|
return parent::performSpecialConditions(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Helper function that performs some special operations should a local file be detected |
96
|
|
|
* |
97
|
|
|
* @return self |
98
|
|
|
*/ |
99
|
|
|
private function performSpecialOperationsOnLocalFiles(): self |
100
|
|
|
{ |
101
|
|
|
foreach ($this->media as $fileLocation) { |
102
|
|
|
if (!$fileLocation instanceof Photo && !$fileLocation instanceof Video) { |
103
|
|
|
throw new InvalidMediaType('To be sent media types can only be Photo or Video'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if (is_readable($fileLocation->media)) { |
107
|
|
|
$this->localFiles[basename($fileLocation->media)] = new InputFile($fileLocation->media); |
108
|
|
|
$fileLocation->media = 'attach://' . basename($fileLocation->media); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @return Generator|InputFile[] |
117
|
|
|
*/ |
118
|
|
|
public function getLocalFiles(): Generator |
119
|
|
|
{ |
120
|
|
|
yield from $this->localFiles; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Will return true if local files are present, false otherwise |
125
|
|
|
* |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
|
|
public function hasLocalFiles(): bool |
129
|
|
|
{ |
130
|
|
|
return $this->localFiles !== []; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|