1 | <?php |
||
21 | class TgLog |
||
22 | { |
||
23 | /** |
||
24 | * @var ClientInterface |
||
25 | */ |
||
26 | protected $httpClient; |
||
27 | |||
28 | /** |
||
29 | * Stores the token |
||
30 | * @var string |
||
31 | */ |
||
32 | private $botToken = ''; |
||
33 | |||
34 | /** |
||
35 | * Contains an instance to a PSR-3 compatible logger |
||
36 | * @var LoggerInterface |
||
37 | */ |
||
38 | protected $logger = null; |
||
39 | |||
40 | /** |
||
41 | * Stores the API URL from Telegram |
||
42 | * @var string |
||
43 | */ |
||
44 | private $apiUrl = ''; |
||
45 | |||
46 | /** |
||
47 | * With this flag we'll know what type of request to send to Telegram |
||
48 | * |
||
49 | * 'application/x-www-form-urlencoded' is the "normal" one, which is simpler and quicker. |
||
50 | * 'multipart/form-data' should be used only when you upload documents, photos, etc. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | private $formType = 'application/x-www-form-urlencoded'; |
||
55 | |||
56 | /** |
||
57 | * Stores the last method name used |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $methodName = ''; |
||
61 | |||
62 | /** |
||
63 | * TelegramLog constructor. |
||
64 | * @param string $botToken |
||
65 | * @param LoggerInterface $logger |
||
66 | * @param Client $client Optional Guzzle object |
||
67 | */ |
||
68 | 36 | public function __construct(string $botToken, LoggerInterface $logger = null, Client $client = null) |
|
86 | |||
87 | /** |
||
88 | * Prepares and sends an API request to Telegram |
||
89 | * |
||
90 | * @param TelegramMethods $method |
||
91 | * @return TelegramTypes |
||
92 | */ |
||
93 | 27 | public function performApiRequest(TelegramMethods $method): TelegramTypes |
|
101 | |||
102 | /** |
||
103 | * Will download a file from the Telegram server. Before calling this function, you have to call the getFile method! |
||
104 | * |
||
105 | * @see unreal4u\TelegramAPI\Telegram\Types\File |
||
106 | * @see unreal4u\TelegramAPI\Telegram\Methods\GetFile |
||
107 | * |
||
108 | * @param File $file |
||
109 | * @return TelegramDocument |
||
110 | */ |
||
111 | public function downloadFile(File $file): TelegramDocument |
||
118 | |||
119 | /** |
||
120 | * Builds up the Telegram API url |
||
121 | * @return TgLog |
||
122 | */ |
||
123 | 36 | final private function constructApiUrl(): TgLog |
|
129 | |||
130 | /** |
||
131 | * This is the method that actually makes the call, which can be easily overwritten so that our unit tests can work |
||
132 | * |
||
133 | * @param TelegramMethods $method |
||
134 | * @param array $formData |
||
135 | * @return TelegramRawData |
||
136 | */ |
||
137 | 21 | protected function sendRequestToTelegram(TelegramMethods $method, array $formData): TelegramRawData |
|
138 | { |
||
139 | 21 | $this->logger->debug('About to perform HTTP call to Telegram\'s API'); |
|
140 | 21 | $response = $this->httpClient->post($this->composeApiMethodUrl($method), $formData); |
|
141 | 21 | $this->logger->debug('Got response back from Telegram, applying json_decode'); |
|
142 | 21 | return new TelegramRawData((string)$response->getBody()); |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * Resets everything to the default values |
||
147 | * |
||
148 | * @return TgLog |
||
149 | */ |
||
150 | 27 | private function resetObjectValues(): TgLog |
|
157 | |||
158 | /** |
||
159 | * Builds up the form elements to be sent to Telegram |
||
160 | * |
||
161 | * @TODO Move this to apart function |
||
162 | * |
||
163 | * @param TelegramMethods $method |
||
164 | * @return array |
||
165 | */ |
||
166 | 27 | private function constructFormData(TelegramMethods $method): array |
|
195 | |||
196 | /** |
||
197 | * Can perform any special checks needed to be performed before sending the actual request to Telegram |
||
198 | * |
||
199 | * This will return an array with data that will be different in each case (for now). This can be changed in the |
||
200 | * future. |
||
201 | * |
||
202 | * @param TelegramMethods $method |
||
203 | * @return array |
||
204 | */ |
||
205 | 27 | private function checkSpecialConditions(TelegramMethods $method): array |
|
226 | |||
227 | /** |
||
228 | * Builds up the URL with which we can work with |
||
229 | * |
||
230 | * All methods in the Bot API are case-insensitive. |
||
231 | * All queries must be made using UTF-8. |
||
232 | * |
||
233 | * @see https://core.telegram.org/bots/api#making-requests |
||
234 | * |
||
235 | * @param TelegramMethods $call |
||
236 | * @return string |
||
237 | */ |
||
238 | 27 | protected function composeApiMethodUrl(TelegramMethods $call): string |
|
246 | |||
247 | /** |
||
248 | * Builds up a multipart form-like array for Guzzle |
||
249 | * |
||
250 | * @param array $data The original object in array form |
||
251 | * @param string $fileKeyName A file handler will be sent instead of a string, state here which field it is |
||
252 | * @param resource $stream The actual file handler |
||
253 | * @return array Returns the actual formdata to be sent |
||
254 | */ |
||
255 | 4 | private function buildMultipartFormData(array $data, string $fileKeyName, $stream): array |
|
280 | } |
||
281 |