111 lines
4.2 KiB
PHP
111 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\SortingHistory;
|
|
use App\Models\SortingSession;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class TelegramService
|
|
{
|
|
protected string $botToken;
|
|
protected string $chatId;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->botToken = config('services.telegram.bot_token', env('TELEGRAM_BOT_TOKEN', ''));
|
|
$this->chatId = config('services.telegram.chat_id', env('TELEGRAM_CHAT_ID', ''));
|
|
}
|
|
|
|
/**
|
|
* Kirim pesan ke Telegram Bot
|
|
*/
|
|
public function sendMessage(string $text): bool
|
|
{
|
|
if (empty($this->botToken) || empty($this->chatId)) {
|
|
Log::warning('Telegram bot token atau chat ID belum dikonfigurasi.');
|
|
return false;
|
|
}
|
|
|
|
$url = "https://api.telegram.org/bot{$this->botToken}/sendMessage";
|
|
|
|
try {
|
|
$response = Http::timeout(15)
|
|
->retry(3, 1000)
|
|
->post($url, [
|
|
'chat_id' => $this->chatId,
|
|
'text' => $text,
|
|
'parse_mode' => 'HTML',
|
|
'disable_web_page_preview' => true,
|
|
]);
|
|
|
|
if ($response->successful()) {
|
|
Log::info('Telegram alert berhasil dikirim.');
|
|
return true;
|
|
}
|
|
|
|
Log::error('Telegram API error: ' . $response->body());
|
|
return false;
|
|
} catch (\Exception $e) {
|
|
Log::error('Telegram send failed: ' . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Kirim hasil kalkulasi sesi sortir yang sudah selesai
|
|
*/
|
|
public function sendSessionResult(SortingSession $session): bool
|
|
{
|
|
$message = $this->formatSessionMessage($session);
|
|
return $this->sendMessage($message);
|
|
}
|
|
|
|
/**
|
|
* Format pesan hasil sesi sortir
|
|
*/
|
|
protected function formatSessionMessage(SortingSession $session): string
|
|
{
|
|
$startedAt = Carbon::parse($session->started_at)->timezone(config('app.timezone', 'Asia/Jakarta'))->format('d/m/Y H:i:s');
|
|
$endedAt = Carbon::parse($session->ended_at)->timezone(config('app.timezone', 'Asia/Jakarta'))->format('d/m/Y H:i:s');
|
|
|
|
// Hitung durasi sesi
|
|
$duration = Carbon::parse($session->started_at)->diff(Carbon::parse($session->ended_at));
|
|
$durationStr = '';
|
|
if ($duration->h > 0) $durationStr .= "{$duration->h} jam ";
|
|
if ($duration->i > 0) $durationStr .= "{$duration->i} menit ";
|
|
$durationStr .= "{$duration->s} detik";
|
|
|
|
$totalWeight = ($session->total_raw_banana_weight ?? 0)
|
|
+ ($session->total_ripe_banana_weight ?? 0)
|
|
+ ($session->total_raw_orange_weight ?? 0)
|
|
+ ($session->total_ripe_orange_weight ?? 0);
|
|
|
|
$message = "✅ <b>SORTIR SELESAI!</b>\n";
|
|
$message .= "━━━━━━━━━━━━━━━━━━━━━\n\n";
|
|
|
|
$message .= "📅 <b>Waktu Mulai:</b> {$startedAt}\n";
|
|
$message .= "🏁 <b>Waktu Selesai:</b> {$endedAt}\n";
|
|
$message .= "⏱ <b>Durasi:</b> {$durationStr}\n";
|
|
$message .= "📊 <b>Total Data Masuk:</b> {$session->total_records} record\n\n";
|
|
|
|
$message .= "━━━━━━━━━━━━━━━━━━━━━\n";
|
|
$message .= "📋 <b>HASIL KALKULASI</b>\n";
|
|
$message .= "━━━━━━━━━━━━━━━━━━━━━\n\n";
|
|
|
|
$message .= "🍌 <b>Pisang Mentah:</b> " . number_format($session->total_raw_banana_weight ?? 0, 2) . " gram\n";
|
|
$message .= "🍌 <b>Pisang Matang:</b> " . number_format($session->total_ripe_banana_weight ?? 0, 2) . " gram\n";
|
|
$message .= "🍊 <b>Jeruk Mentah:</b> " . number_format($session->total_raw_orange_weight ?? 0, 2) . " gram\n";
|
|
$message .= "🍊 <b>Jeruk Matang:</b> " . number_format($session->total_ripe_orange_weight ?? 0, 2) . " gram\n\n";
|
|
|
|
$message .= "━━━━━━━━━━━━━━━━━━━━━\n";
|
|
$message .= "⚖️ <b>Total Berat:</b> " . number_format($totalWeight, 2) . " gram\n";
|
|
|
|
$message .= "\n📊 Dashboard: " . config('app.url', 'http://localhost:8000');
|
|
|
|
return $message;
|
|
}
|
|
}
|