diff --git a/src/TelegramDriver.php b/src/TelegramDriver.php index cee9eef..8fb66a1 100644 --- a/src/TelegramDriver.php +++ b/src/TelegramDriver.php @@ -71,6 +71,44 @@ public function getUser(IncomingMessage $matchingMessage) $userData->get('username'), $responseData['result']); } + /** + * @param IncomingMessage $matchingMessage + * @return array $userProfilePhotoPaths + * @throws TelegramException + */ + public function getUserProfilePhotos(IncomingMessage $matchingMessage) + { + + $profilePhotoParameters = [ + 'user_id' => $matchingMessage->getSender(), + 'limit' => 1, + ]; + + $responseProfilePhoto = $this->http->post($this->buildApiUrl('getUserProfilePhotos'), [], $profilePhotoParameters); + + $responseDataProfilePhotos = json_decode($responseProfilePhoto->getContent(), true); + if ($responseProfilePhoto->getStatusCode() !== 200) { + throw new TelegramException('Error retrieving user photos info: '.$responseDataProfilePhotos['description']); + } + + foreach ($responseDataProfilePhotos['result']['photos'] as $photoArray) { + foreach ($photoArray as $photo) { + $profilePhotoParameters = [ + 'file_id' => $photo['file_id'], + ]; + $responsePhotoFile = $this->http->post($this->buildApiUrl('getFile'), [], $profilePhotoParameters); + + $responseDataPhotoFile = json_decode($responsePhotoFile->getContent(), true); + if ($responsePhotoFile->getStatusCode() !== 200) { + throw new TelegramException('Error retrieving user photos info: '.$responseDataPhotoFile['description']); + } + $userProfilePhotoPaths[] = 'https://api.telegram.org/file/bot'.env('TELEGRAM_TOKEN').'/'.$responseDataPhotoFile['result']['file_path']; + } + } + + return $userProfilePhotoPaths; + } + /** * Determine if the request is for this driver. *