Skip to content

Commit

Permalink
Added functions for get image from telegram and generate inline button
Browse files Browse the repository at this point in the history
  • Loading branch information
AUnhurian committed May 9, 2020
1 parent 82fa9e8 commit 8ee3184
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 4 deletions.
32 changes: 30 additions & 2 deletions src/BaseTelegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function methodType($typeMethod = 'sendMessage', $params)
$baseURL = $this->base_url . $this->token .'/';
$baseURL .= $typeMethod;

$url = $baseURL. '?'. http_build_query($params);
$url = $baseURL . '?'. http_build_query($params);

$result = $this->curlQuery($url);
return $result;
Expand All @@ -37,7 +37,8 @@ public function methodType($typeMethod = 'sendMessage', $params)
* @return content string
* @author Andrii_Unhurian
*/
protected function curlQuery($url, $header = false, $fields = null, $typePOST = 'http', $cookieFile = 'cookie.txt'){
protected function curlQuery($url, $header = false, $fields = null, $typePOST = 'http', $cookieFile = 'cookie.txt')
{
$curl = curl_init();

$option = [
Expand Down Expand Up @@ -68,4 +69,31 @@ protected function curlQuery($url, $header = false, $fields = null, $typePOST =
return $content;
}

/**
* Function for getting value in assoc array by key
*
* @param data array
* @param keyData string
* @return array, string
* @author Andrii_Unhurian
*/
public function searchKey($data, $keyData)
{
if (!is_null($data)) {
foreach ($data as $key => $value) {
if ($keyData === $key) {
return $value;
} elseif (is_array($value)) {
$result = $this->searchKey($value, $keyData);

if (!is_null($result)) {
return $result;
}
}
}
}

return null;
}

}
83 changes: 81 additions & 2 deletions src/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

class Telegram extends BaseTelegram
{
public $InlineKeyboardButton = null;

public function __construct($token)
{
Expand Down Expand Up @@ -172,8 +173,8 @@ public function answerCallbackQuery($params)

return $result;
}


/**
* The function sends any method to the telegram for processing
*
Expand Down Expand Up @@ -209,6 +210,84 @@ public function answerInlineQuery($params)
return $result;
}

/**
* Method for make InlineKeyboardMarkup
*/
public function getInlineKeyboardMarkup($data)
{
$InlineKeyboardButton = array();

$InlineKeyboardButton[] = $data;
if (!empty($this->InlineKeyboardButton)) {
$InlineKeyboardButton = $this->InlineKeyboardButton;
}

$InlineKeyboardMarkup = [
'inline_keyboard' => $InlineKeyboardButton
];

return json_encode($InlineKeyboardMarkup);
}

/**
* Key in json `photo` as assoc array
* @param array photos this is key
* @return array
*/
public function getUrlImage($photos)
{
$api_files = 'https://api.telegram.org/file/bot'. $this->token .'/';
$api = 'https://api.telegram.org/bot'. $this->token .'/';

try {
if (empty($photos)) {
throw new \Exception('Sorry, but I get empty array `photos`');
}

$photo = $photos[1];
if (empty($photo)) {
throw new \Exception('Exception with array photo. I think, we have only first photo.');
}

$file_id = $photo['file_id'];
if (empty($file_id)) {
throw new \Exception('File id empty.');
}

$file_size = $photo['file_size'];
if (empty($file_size)) {
throw new \Exception('File size empty.');
}

$url = $api . 'getFile?file_id=' . $file_id;
$path_file = file_get_contents($url);

if (empty($path_file)) {
throw new \Exception('Empty path to file.');
}

$path_file = json_decode($path_file, true);
if (empty($path_file['result']) || empty($path_file['result']['file_path'])) {
throw new \Exception('I have exception with getting path to image.');
}

$file_path = $path_file['result']['file_path'];
$image_url = $api_files . $file_path;

$response = [
'result' => true,
'message' => 'All good',
'url' => $image_url
];

} catch(\Exception $exception) {
$response = [
'result' => false,
'message' => $exception->getMessage()
];
}

return $response;
}

}

0 comments on commit 8ee3184

Please sign in to comment.