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