62 lines
1.6 KiB
PHP
62 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Models\Setting;
|
|
|
|
class TelegramService
|
|
{
|
|
private $token;
|
|
private $chatId;
|
|
private $settingModel;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->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 = "<b>Nova Ordem Cadastrada!</b>\n\n<b>Título:</b> {title}\n<b>Tipo:</b> {type}\n<b>Domínios:</b> {total_domains}";
|
|
}
|
|
|
|
$message = str_replace(
|
|
['{title}', '{type}', '{total_domains}', '{order_id}'],
|
|
[$order['title'], $order['type'], $domainCount, $order['id']],
|
|
$template
|
|
);
|
|
|
|
return $this->sendMessage($message);
|
|
}
|
|
}
|