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