1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace TgBotApi\BotApiBase; |
6
|
|
|
|
7
|
|
|
use TgBotApi\BotApiBase\Exception\ResponseException; |
8
|
|
|
use TgBotApi\BotApiBase\Method\ExportChatInviteLinkMethod; |
9
|
|
|
use TgBotApi\BotApiBase\Method\Interfaces\MethodInterface; |
10
|
|
|
use TgBotApi\BotApiBase\Method\SendChatActionMethod; |
11
|
|
|
use TgBotApi\BotApiBase\Method\SendMediaGroupMethod; |
12
|
|
|
use TgBotApi\BotApiBase\Method\StopPollMethod; |
13
|
|
|
use TgBotApi\BotApiBase\Traits\AliasMethodTrait; |
14
|
|
|
use TgBotApi\BotApiBase\Traits\GetMethodTrait; |
15
|
|
|
use TgBotApi\BotApiBase\Type\FileType; |
16
|
|
|
use TgBotApi\BotApiBase\Type\MessageType; |
17
|
|
|
use TgBotApi\BotApiBase\Type\PollType; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class BotApi. |
21
|
|
|
*/ |
22
|
|
|
class BotApi implements BotApiInterface |
23
|
|
|
{ |
24
|
|
|
use AliasMethodTrait; |
25
|
|
|
use GetMethodTrait; |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $botKey; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var ApiClientInterface |
33
|
|
|
*/ |
34
|
|
|
private $apiClient; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
private $endPoint; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var NormalizerInterface |
43
|
|
|
*/ |
44
|
|
|
private $normalizer; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* BotApi constructor. |
48
|
|
|
* |
49
|
|
|
* @param string $botKey |
50
|
|
|
* @param ApiClientInterface $apiClient |
51
|
|
|
* @param NormalizerInterface $normalizer |
52
|
|
|
* @param string $endPoint |
53
|
|
|
*/ |
54
|
273 |
|
public function __construct( |
55
|
|
|
string $botKey, |
56
|
|
|
ApiClientInterface $apiClient, |
57
|
|
|
NormalizerInterface $normalizer, |
58
|
|
|
string $endPoint = 'https://api.telegram.org' |
59
|
|
|
) { |
60
|
273 |
|
$this->botKey = $botKey; |
61
|
273 |
|
$this->apiClient = $apiClient; |
62
|
273 |
|
$this->normalizer = $normalizer; |
63
|
273 |
|
$this->endPoint = $endPoint; |
64
|
|
|
|
65
|
273 |
|
$this->apiClient->setBotKey($botKey); |
66
|
273 |
|
$this->apiClient->setEndpoint($endPoint); |
67
|
273 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param $method |
71
|
|
|
* @param string|null $type |
72
|
|
|
* |
73
|
|
|
* @throws ResponseException |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
270 |
|
public function call(MethodInterface $method, string $type = null) |
78
|
|
|
{ |
79
|
270 |
|
$json = $this->apiClient->send($this->getMethodName($method), $this->normalizer->normalize($method)); |
80
|
|
|
|
81
|
270 |
|
if (true !== $json->ok) { |
82
|
3 |
|
throw new ResponseException($json->description); |
83
|
|
|
} |
84
|
|
|
|
85
|
267 |
|
return $type ? $this->normalizer->denormalize($json->result, $type) : $json->result; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param ExportChatInviteLinkMethod $method |
90
|
|
|
* |
91
|
|
|
* @throws ResponseException |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
3 |
|
public function exportChatInviteLink(ExportChatInviteLinkMethod $method): string |
96
|
|
|
{ |
97
|
3 |
|
return $this->call($method); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param SendChatActionMethod $method |
102
|
|
|
* |
103
|
|
|
* @throws ResponseException |
104
|
|
|
* |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
3 |
|
public function sendChatAction(SendChatActionMethod $method): bool |
108
|
|
|
{ |
109
|
3 |
|
return $this->call($method); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param SendMediaGroupMethod $method |
114
|
|
|
* |
115
|
|
|
* @throws ResponseException |
116
|
|
|
* |
117
|
|
|
* @return MessageType[] |
118
|
|
|
*/ |
119
|
6 |
|
public function sendMediaGroup(SendMediaGroupMethod $method): array |
120
|
|
|
{ |
121
|
6 |
|
return $this->call($method, MessageType::class . '[]'); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param StopPollMethod $method |
126
|
|
|
* |
127
|
|
|
* @throws ResponseException |
128
|
|
|
* |
129
|
|
|
* @return PollType |
130
|
|
|
*/ |
131
|
3 |
|
public function stopPoll(StopPollMethod $method): PollType |
132
|
|
|
{ |
133
|
3 |
|
return $this->call($method, PollType::class); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @param FileType $file |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
3 |
|
public function getAbsoluteFilePath(FileType $file): string |
142
|
|
|
{ |
143
|
3 |
|
return \sprintf( |
144
|
3 |
|
'%s/file/bot%s/%s', |
145
|
3 |
|
$this->endPoint, |
146
|
3 |
|
$this->botKey, |
147
|
3 |
|
$file->filePath |
148
|
|
|
); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param $method |
153
|
|
|
* |
154
|
|
|
* @return string |
155
|
|
|
*/ |
156
|
270 |
|
private function getMethodName($method): string |
157
|
|
|
{ |
158
|
270 |
|
return \lcfirst(\substr( |
159
|
270 |
|
\get_class($method), |
160
|
270 |
|
\strrpos(\get_class($method), '\\') + 1, |
161
|
270 |
|
-6 |
162
|
|
|
)); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|