settingModel = new Setting();
$this->token = $this->settingModel->get('telegram_bot_token');
$this->chatId = $this->settingModel->get('telegram_chat_id');
}
public function sendMessage($message)
{
if (empty($this->token) || empty($this->chatId)) {
return false;
}
$url = "https://api.telegram.org/bot{$this->token}/sendMessage";
$data = [
'chat_id' => $this->chatId,
'text' => $message,
'parse_mode' => 'HTML'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
}
public function sendOrderNotification($order, $domainCount)
{
$template = $this->settingModel->get('telegram_message_template');
if (empty($template)) {
$template = "Nova Ordem Cadastrada!\n\nTítulo: {title}\nTipo: {type}\nDomínios: {total_domains}";
}
$message = str_replace(
['{title}', '{type}', '{total_domains}', '{order_id}'],
[$order['title'], $order['type'], $domainCount, $order['id']],
$template
);
return $this->sendMessage($message);
}
}