313 lines
8.3 KiB
PHP
313 lines
8.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class TelegramController extends Controller
|
|
{
|
|
protected string $token;
|
|
protected string $baseUrl;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->token = env('TELEGRAM_BOT_TOKEN');
|
|
$this->baseUrl = "https://api.telegram.org/bot{$this->token}";
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| WEBHOOK (Telegram -> Laravel)
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
public function webhook(Request $request)
|
|
{
|
|
$update = $request->all();
|
|
|
|
Log::info('Telegram Update', $update);
|
|
|
|
if (isset($update['message'])) {
|
|
return $this->handleMessage($update['message']);
|
|
}
|
|
|
|
if (isset($update['callback_query'])) {
|
|
return $this->handleCallback($update['callback_query']);
|
|
}
|
|
|
|
return response()->json(['success' => true]);
|
|
}
|
|
|
|
protected function handleMessage(array $message)
|
|
{
|
|
$chatId = $message['chat']['id'];
|
|
$text = trim($message['text'] ?? '');
|
|
|
|
return $this->sendMessage($chatId, "Recibido: {$text}");
|
|
}
|
|
|
|
protected function handleCallback(array $callback)
|
|
{
|
|
$chatId = $callback['message']['chat']['id'];
|
|
$data = $callback['data'];
|
|
|
|
$this->answerCallbackQuery($callback['id'], 'OK');
|
|
|
|
return $this->sendMessage($chatId, "Botón: {$data}");
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| CORE REQUEST
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
protected function request(string $method, array $payload = [])
|
|
{
|
|
return Http::timeout(60)
|
|
->post("{$this->baseUrl}/{$method}", $payload)
|
|
->json();
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| MENSAJES BASE (TELEGRAM API)
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
public function sendMessage($chatId, $message, array $extra = [])
|
|
{
|
|
return $this->request('sendMessage', array_merge([
|
|
'chat_id' => $chatId,
|
|
'text' => $message,
|
|
'parse_mode' => 'HTML'
|
|
], $extra));
|
|
}
|
|
|
|
public function sendPhotoFile($chatId, $path, $caption = '')
|
|
{
|
|
// 🔥 CASO 1: URL (internet)
|
|
if (filter_var($path, FILTER_VALIDATE_URL)) {
|
|
|
|
return $this->request('sendPhoto', [
|
|
'chat_id' => $chatId,
|
|
'photo' => $path,
|
|
'caption' => $caption
|
|
]);
|
|
}
|
|
|
|
// 🔥 CASO 2: archivo local
|
|
if (!file_exists($path)) {
|
|
return response()->json([
|
|
'error' => 'Archivo no existe'
|
|
], 400);
|
|
}
|
|
|
|
return Http::attach(
|
|
'photo',
|
|
fopen($path, 'r'),
|
|
basename($path)
|
|
)->post("{$this->baseUrl}/sendPhoto", [
|
|
'chat_id' => $chatId,
|
|
'caption' => $caption
|
|
])->json();
|
|
}
|
|
|
|
public function sendAudioFile($chatId, $path, $caption = '')
|
|
{
|
|
return Http::attach(
|
|
'audio',
|
|
fopen($path, 'r'),
|
|
basename($path)
|
|
)->post("{$this->baseUrl}/sendAudio", [
|
|
'chat_id' => $chatId,
|
|
'caption' => $caption
|
|
])->json();
|
|
}
|
|
|
|
public function sendVideoFile($chatId, $path, $caption = '')
|
|
{
|
|
// 🔥 CASO 1: URL (internet)
|
|
if (filter_var($path, FILTER_VALIDATE_URL)) {
|
|
|
|
return $this->request('sendVideo', [
|
|
'chat_id' => $chatId,
|
|
'video' => $path,
|
|
'caption' => $caption
|
|
]);
|
|
}
|
|
|
|
// 🔥 CASO 2: archivo local
|
|
if (!file_exists($path)) {
|
|
return response()->json([
|
|
'error' => 'Archivo no existe'
|
|
], 400);
|
|
}
|
|
|
|
return Http::attach(
|
|
'video',
|
|
fopen($path, 'r'),
|
|
basename($path)
|
|
)->post("{$this->baseUrl}/sendVideo", [
|
|
'chat_id' => $chatId,
|
|
'caption' => $caption
|
|
])->json();
|
|
}
|
|
|
|
public function sendDocumentFile($chatId, $path, $caption = '')
|
|
{
|
|
return Http::attach(
|
|
'document',
|
|
fopen($path, 'r'),
|
|
basename($path)
|
|
)->post("{$this->baseUrl}/sendDocument", [
|
|
'chat_id' => $chatId,
|
|
'caption' => $caption
|
|
])->json();
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| LOGS
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
public function sendLaravelLog(Request $request)
|
|
{
|
|
$file = storage_path('logs/laravel.log');
|
|
|
|
return $this->sendDocumentFile(
|
|
$request->chat_id,
|
|
$file,
|
|
'Laravel Log'
|
|
);
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| CALLBACKS
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
public function answerCallbackQuery($id, $text = '')
|
|
{
|
|
return $this->request('answerCallbackQuery', [
|
|
'callback_query_id' => $id,
|
|
'text' => $text
|
|
]);
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| BOT INFO
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
public function getMe()
|
|
{
|
|
return $this->request('getMe');
|
|
}
|
|
|
|
/*
|
|
|--------------------------------------------------------------------------
|
|
| WRAPPERS (TUS RUTAS API)
|
|
|--------------------------------------------------------------------------
|
|
*/
|
|
|
|
public function sendText(Request $request)
|
|
{
|
|
return $this->sendMessage($request->chat_id, $request->message);
|
|
}
|
|
|
|
public function sendImage(Request $request)
|
|
{
|
|
return $this->sendPhotoFile(
|
|
$request->chat_id,
|
|
$request->path,
|
|
$request->caption ?? ''
|
|
);
|
|
}
|
|
|
|
public function sendAudioApi(Request $request)
|
|
{
|
|
return $this->sendAudioFile(
|
|
$request->chat_id,
|
|
$request->path,
|
|
$request->caption ?? ''
|
|
);
|
|
}
|
|
|
|
public function sendVideoApi(Request $request)
|
|
{
|
|
return $this->sendVideoFile(
|
|
$request->chat_id,
|
|
$request->path,
|
|
$request->caption ?? ''
|
|
);
|
|
}
|
|
|
|
public function sendDocumentApi(Request $request)
|
|
{
|
|
return $this->sendDocumentFile(
|
|
$request->chat_id,
|
|
$request->path,
|
|
$request->caption ?? ''
|
|
);
|
|
}
|
|
|
|
public function sendLog(Request $request)
|
|
{
|
|
return $this->sendLaravelLog($request);
|
|
}
|
|
|
|
public function notify(Request $request)
|
|
{
|
|
return $this->sendMessage(
|
|
$request->chat_id,
|
|
$request->message
|
|
);
|
|
}
|
|
|
|
public function botInfo()
|
|
{
|
|
return $this->getMe();
|
|
}
|
|
|
|
public function getChatId(Request $request)
|
|
{
|
|
$updates = $this->request('getUpdates');
|
|
|
|
if (!$updates || !isset($updates['result'])) {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'No hay mensajes'
|
|
]);
|
|
}
|
|
|
|
$chats = [];
|
|
|
|
foreach ($updates['result'] as $update) {
|
|
|
|
if (isset($update['message'])) {
|
|
$message = $update['message'];
|
|
|
|
$chats[] = [
|
|
'chat_id' => $message['chat']['id'] ?? null,
|
|
'type' => $message['chat']['type'] ?? null,
|
|
'username' => $message['chat']['username'] ?? null,
|
|
'first_name' => $message['chat']['first_name'] ?? null,
|
|
'text' => $message['text'] ?? null,
|
|
'date' => $message['date'] ?? null,
|
|
];
|
|
}
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'chats' => $chats
|
|
]);
|
|
}
|
|
} |