Skip to content

Instantly share code, notes, and snippets.

@aditagusta
Created August 31, 2023 02:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aditagusta/bb2be6928732aa4156a9eeb2154355e9 to your computer and use it in GitHub Desktop.
Save aditagusta/bb2be6928732aa4156a9eeb2154355e9 to your computer and use it in GitHub Desktop.
Chat Bot Telegram : Feature ( SendMessage , SendPhoto , SendDoc , SendPDF ) reff : gist.github.com/fahrezi-ux
// SendMessages
public function sendTelegramMessage()
{
$message_text ="Sent Message successfuly";
$chat_id = "receiver_id";
$botTelegram = 'https://api.telegram.org/bot"your_bot_token"/sendMessage';
$url = $botTelegram."?parse_mode=markdown&chat_id=".$chat_id. "&text=" . $message_text;
$ch = curl_init();
$optArray = array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true
);
curl_setopt_array($ch, $optArray);
$result = curl_exec($ch);
curl_close($ch);
return "Sukses Mengirim Pesan Ke Telegram";
}
// SendPdf
public function sendTelegramPDF()
{
$data = [
'title' => 'My PDF Document',
'content' => 'This is the content of my PDF document.',
];
$pdf = PDF::loadView('pdf.template', $data);
$pdfData = $pdf->output();
$nameFile = 'HO_'.date('H:i:s').'.pdf';
$tmpFilePath = Storage::put('pdf/'.$nameFile, $pdfData);
// return $pdf->download('document_'.date('Y-m-d H:i:s') .'.pdf');
$path = storage_path("location_file/$nameFile");
$chatId = "receiver_id"; // Chat ID tempat Anda ingin mengirim dokumen
$url = "https://api.telegram.org/bot"your_bot_token"/sendDocument";
$data = [
'chat_id' => $chatId,
'document' => new \CURLFile($path),
'caption' => 'This test send Document'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
Storage::delete("/pdf/$nameFile");
return $response;
}
// SendPhoto
public function sendTelegramPhoto()
{
$chatId = "receiver_id"; // Chat ID tempat Anda ingin mengirim foto
$photoPath = public_path('photos/tv.jpg'); // Ganti dengan path foto Anda
$url = "https://api.telegram.org/bot"your_bot_token"/sendPhoto";
$data = [
'chat_id' => $chatId,
'photo' => new \CURLFile($photoPath),
'caption' => 'This test send Photo'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
// SendDocument
public function sendTelegramDocument()
{
$chatId = "receiver_id"; // Chat ID tempat Anda ingin mengirim dokumen
$docPath = public_path('document/FileName.pdf'); // Ganti dengan path dokumen Anda
$url = "https://api.telegram.org/bot"your_bot_token"/sendDocument";
$data = [
'chat_id' => $chatId,
'document' => new \CURLFile($docPath),
'caption' => 'This test send Document'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment