Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add getUserProfilePhotos method #35

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/TelegramDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,43 @@ 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'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Environment variable 'TELEGRAM_TOKEN' is hardcoded here. Maybe it is better to retrieve the token from $this->config?

}
}

return $userProfilePhotoPaths;
}

/**
* Determine if the request is for this driver.
*
Expand Down