/** * @FIXME this function name will probably change in the future! * This will download a file and offer it to the user */ private function downloadSticker(Message $message) : unreal4uBot { $this->logger->debug(sprintf('Got a sticker request, downloading sticker')); $sendMessage = new SendMessage(); $sendMessage->chat_id = $message->chat->id; try { $getFile = new GetFile(); $getFile->file_id = $message->sticker->file_id; $tgLog = new TgLog($this->token, $this->logger); $file = $tgLog->performApiRequest($getFile); $tgDocument = $tgLog->downloadFile($file); $this->logger->debug('Downloaded sticker, sending it to temporary directory'); file_put_contents(sprintf('media/%s', basename($file->file_path)), (string) $tgDocument); #$image = \imagecreatefromwebp(sprintf('media/%s', basename($file->file_path))); #\imagepng($image, 'media/'.basename($file->file_path).'.png'); #\imagedestroy($image); $sendMessage->text = sprintf('Download link for sticker: http://media.unreal4u.com/%s', basename($file->file_path)); } catch (\Exception $e) { $this->logger->error('Problem downloading sticker: ' . $e->getMessage()); $sendMessage->text = sprintf('There was a problem downloading your sticker, please retry later'); } try { $tgLog->performApiRequest($sendMessage); $this->logger->debug('Sent message to user'); } catch (\Exception $e) { $this->logger->warning('Caught exception while trying to send message to user: ' . $e->getMessage()); } return $this; }
<?php include 'basics.php'; use GuzzleHttp\Exception\ClientException; use unreal4u\TelegramAPI\TgLog; use unreal4u\TelegramAPI\Telegram\Methods\GetFile; $tgLog = new TgLog(BOT_TOKEN); $getFile = new GetFile(); // You can get a file id from an update, webhook or any other message object $getFile->file_id = A_FILE_ID; try { $file = $tgLog->performApiRequest($getFile); } catch (ClientException $e) { // Do whatever you want, function below contains exact JSON output from Telegram echo '<pre>'; print_r(json_decode((string) $e->getResponse()->getBody())); echo '</pre>'; } // Don't do anything if we have already output (as we can't modify those headers) if (!headers_sent()) { $tgDocument = $tgLog->downloadFile($file); header(sprintf('Content-Type: %s', $tgDocument->mime_type)); header(sprintf('Content-Length: %d', $tgDocument->file_size)); header(sprintf('Content-Disposition: inline; filename="%s"', basename($file->file_path))); print $tgDocument; }