Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Glebsky committed Aug 21, 2021
2 parents 8d654df + 4dd989c commit 3c0d366
Showing 1 changed file with 45 additions and 44 deletions.
89 changes: 45 additions & 44 deletions src/SimpleTelegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class SimpleTelegram

public function __construct(string $botToken, $recipient = null)
{
$this->botToken = $botToken;
$this->botToken = $botToken;
$this->recipient = $recipient;
}

Expand All @@ -24,6 +24,7 @@ public function setRecipient($recipient)
throw new \Exception('Recipient must be a valid ID.');
}
$this->recipient = $recipient;

return $this;
}

Expand All @@ -38,6 +39,7 @@ public function sendMessage(string $msg)
if ($result !== false) {
return true;
}

return false;
}

Expand All @@ -56,44 +58,44 @@ public function sendDocument(string $localPath, string $caption = '')

$file_contents = file_get_contents($filename);

$content = "--".$MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"".$FORM_FIELD."\"; filename=\"".basename($filename)."\"\r\n".
$content = '--'.$MULTIPART_BOUNDARY."\r\n".
'Content-Disposition: form-data; name="'.$FORM_FIELD.'"; filename="'.basename($filename)."\"\r\n".
"Content-Type: application/zip\r\n\r\n".
$file_contents."\r\n";

$content .= "--".$MULTIPART_BOUNDARY."\r\n".
$content .= '--'.$MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"caption\"\r\n\r\n".
"$caption\r\n";

$content .= "--".$MULTIPART_BOUNDARY."\r\n".
$content .= '--'.$MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"chat_id\"\r\n\r\n".
"$this->recipient\r\n";

$content .= "--".$MULTIPART_BOUNDARY."--\r\n";
$content .= '--'.$MULTIPART_BOUNDARY."--\r\n";

$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => $header,
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => $header,
'content' => $content,
)
));
],
]);

$url = "https://api.telegram.org/bot$this->botToken/sendDocument?chat_id=$this->recipient";
$result = file_get_contents($url, false, $context);

if ($result !== false) {
return true;
return true;
}

return false;
}

public function sendPhoto(string $localPath, string $caption = '')
{
$filename = realpath($localPath);
$type = mime_content_type($filename);
if (!strstr($type, 'image/'))
{
if (!strstr($type, 'image/')) {
return false;
}

Expand All @@ -102,47 +104,46 @@ public function sendPhoto(string $localPath, string $caption = '')

$header = 'Content-Type: multipart/form-data; boundary='.$MULTIPART_BOUNDARY;


$file_contents = file_get_contents($filename);

$content = "--".$MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"".$FORM_FIELD."\"; filename=\"".basename($filename)."\"\r\n".
$content = '--'.$MULTIPART_BOUNDARY."\r\n".
'Content-Disposition: form-data; name="'.$FORM_FIELD.'"; filename="'.basename($filename)."\"\r\n".
"Content-Type: application/zip\r\n\r\n".
$file_contents."\r\n";

$content .= "--".$MULTIPART_BOUNDARY."\r\n".
$content .= '--'.$MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"caption\"\r\n\r\n".
"$caption\r\n";

$content .= "--".$MULTIPART_BOUNDARY."\r\n".
$content .= '--'.$MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"chat_id\"\r\n\r\n".
"$this->recipient\r\n";

$content .= "--".$MULTIPART_BOUNDARY."--\r\n";
$content .= '--'.$MULTIPART_BOUNDARY."--\r\n";

$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => $header,
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => $header,
'content' => $content,
)
));
],
]);

$url = "https://api.telegram.org/bot$this->botToken/sendPhoto?chat_id=$this->recipient";
$result = file_get_contents($url, false, $context);

if ($result !== false) {
return true;
return true;
}

return false;
}

public function sendAudio(string $localPath, string $caption = '')
{
$filename = realpath($localPath);
$type = mime_content_type($filename);
if (!strstr($type, 'audio/mpeg'))
{
if (!strstr($type, 'audio/mpeg')) {
return false;
}

Expand All @@ -151,38 +152,38 @@ public function sendAudio(string $localPath, string $caption = '')

$header = 'Content-Type: multipart/form-data; boundary='.$MULTIPART_BOUNDARY;


$file_contents = file_get_contents($filename);

$content = "--".$MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"".$FORM_FIELD."\"; filename=\"".basename($filename)."\"\r\n".
$content = '--'.$MULTIPART_BOUNDARY."\r\n".
'Content-Disposition: form-data; name="'.$FORM_FIELD.'"; filename="'.basename($filename)."\"\r\n".
"Content-Type: application/zip\r\n\r\n".
$file_contents."\r\n";

$content .= "--".$MULTIPART_BOUNDARY."\r\n".
$content .= '--'.$MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"caption\"\r\n\r\n".
"$caption\r\n";

$content .= "--".$MULTIPART_BOUNDARY."\r\n".
$content .= '--'.$MULTIPART_BOUNDARY."\r\n".
"Content-Disposition: form-data; name=\"chat_id\"\r\n\r\n".
"$this->recipient\r\n";

$content .= "--".$MULTIPART_BOUNDARY."--\r\n";
$content .= '--'.$MULTIPART_BOUNDARY."--\r\n";

$context = stream_context_create(array(
'http' => array(
'method' => 'POST',
'header' => $header,
$context = stream_context_create([
'http' => [
'method' => 'POST',
'header' => $header,
'content' => $content,
)
));
],
]);

$url = "https://api.telegram.org/bot$this->botToken/sendAudio?chat_id=$this->recipient";
$result = file_get_contents($url, false, $context);

if ($result !== false) {
return true;
return true;
}

return false;
}
}
}

0 comments on commit 3c0d366

Please sign in to comment.