2023-11-17 01:44:01 -05:00
|
|
|
|
<?php
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
2023-12-04 07:40:49 -05:00
|
|
|
|
use App\Exceptions\ApiException;
|
2023-11-17 01:44:01 -05:00
|
|
|
|
use App\Jobs\SendTelegramJob;
|
|
|
|
|
use App\Models\User;
|
|
|
|
|
use \Curl\Curl;
|
|
|
|
|
|
|
|
|
|
class TelegramService {
|
|
|
|
|
protected $api;
|
|
|
|
|
|
|
|
|
|
public function __construct($token = '')
|
|
|
|
|
{
|
|
|
|
|
$this->api = 'https://api.telegram.org/bot' . admin_setting('telegram_bot_token', $token) . '/';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function sendMessage(int $chatId, string $text, string $parseMode = '')
|
|
|
|
|
{
|
|
|
|
|
if ($parseMode === 'markdown') {
|
|
|
|
|
$text = str_replace('_', '\_', $text);
|
|
|
|
|
}
|
|
|
|
|
$this->request('sendMessage', [
|
|
|
|
|
'chat_id' => $chatId,
|
|
|
|
|
'text' => $text,
|
|
|
|
|
'parse_mode' => $parseMode
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function approveChatJoinRequest(int $chatId, int $userId)
|
|
|
|
|
{
|
|
|
|
|
$this->request('approveChatJoinRequest', [
|
|
|
|
|
'chat_id' => $chatId,
|
|
|
|
|
'user_id' => $userId
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function declineChatJoinRequest(int $chatId, int $userId)
|
|
|
|
|
{
|
|
|
|
|
$this->request('declineChatJoinRequest', [
|
|
|
|
|
'chat_id' => $chatId,
|
|
|
|
|
'user_id' => $userId
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getMe()
|
|
|
|
|
{
|
|
|
|
|
return $this->request('getMe');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setWebhook(string $url)
|
|
|
|
|
{
|
|
|
|
|
return $this->request('setWebhook', [
|
|
|
|
|
'url' => $url
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function request(string $method, array $params = [])
|
|
|
|
|
{
|
|
|
|
|
$curl = new Curl();
|
|
|
|
|
$curl->get($this->api . $method . '?' . http_build_query($params));
|
|
|
|
|
$response = $curl->response;
|
|
|
|
|
$curl->close();
|
2023-12-06 15:01:32 -05:00
|
|
|
|
if (!isset($response->ok)) throw new ApiException('请求失败');
|
2023-11-17 01:44:01 -05:00
|
|
|
|
if (!$response->ok) {
|
2023-12-06 15:01:32 -05:00
|
|
|
|
throw new ApiException('来自TG的错误:' . $response->description);
|
2023-11-17 01:44:01 -05:00
|
|
|
|
}
|
|
|
|
|
return $response;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function sendMessageWithAdmin($message, $isStaff = false)
|
|
|
|
|
{
|
|
|
|
|
if (!admin_setting('telegram_bot_enable', 0)) return;
|
|
|
|
|
$users = User::where(function ($query) use ($isStaff) {
|
|
|
|
|
$query->where('is_admin', 1);
|
|
|
|
|
if ($isStaff) {
|
|
|
|
|
$query->orWhere('is_staff', 1);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
->where('telegram_id', '!=', NULL)
|
|
|
|
|
->get();
|
|
|
|
|
foreach ($users as $user) {
|
|
|
|
|
SendTelegramJob::dispatch($user->telegram_id, $message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|