Feat: Finalizada integracion interactiva de Telegram con botones y manejo de errores
This commit is contained in:
@@ -63,60 +63,89 @@ class TelegramBotService
|
||||
|
||||
private function handleBotState(TelegramAccount $account, string $text, string $chatId): array
|
||||
{
|
||||
$state = $account->bot_state;
|
||||
$data = $account->bot_data ?? [];
|
||||
try {
|
||||
$state = $account->bot_state;
|
||||
$data = $account->bot_data ?? [];
|
||||
|
||||
if (strtolower(trim($text)) === '/cancelar' || $text === '❌ Cancelar') {
|
||||
// Permitir cancelar
|
||||
if (strtolower(trim($text)) === '/cancelar' || $text === '❌ Cancelar') {
|
||||
$account->update(['bot_state' => null, 'bot_data' => null]);
|
||||
$this->sendMenu($chatId, "❌ Operación cancelada.");
|
||||
return ['ok' => true];
|
||||
}
|
||||
|
||||
switch ($state) {
|
||||
// --- FLUJO VENTA ---
|
||||
case 'AWAITING_SALE_DATE':
|
||||
$data['date'] = $this->parseDate($text);
|
||||
$account->update(['bot_state' => 'AWAITING_SALE_USER_AMOUNT', 'bot_data' => $data]);
|
||||
$this->sendMessage($chatId, "💰 Ingresa el monto vendido por el *usuario*:", $this->cancelKeyboard());
|
||||
break;
|
||||
|
||||
case 'AWAITING_SALE_USER_AMOUNT':
|
||||
if (!is_numeric($text)) return $this->sendInvalidNumeric($chatId);
|
||||
$data['user_sales'] = (float) $text;
|
||||
$account->update(['bot_state' => 'AWAITING_SALE_SYSTEM_AMOUNT', 'bot_data' => $data]);
|
||||
$this->sendMessage($chatId, "🖥️ Ingresa el monto vendido según el *sistema*:", $this->cancelKeyboard());
|
||||
break;
|
||||
|
||||
case 'AWAITING_SALE_SYSTEM_AMOUNT':
|
||||
if (!is_numeric($text)) return $this->sendInvalidNumeric($chatId);
|
||||
$data['system_sales'] = (float) $text;
|
||||
|
||||
$user = $account->user;
|
||||
$month = $user->getCurrentMonth();
|
||||
if (!$month) return $this->sendNoOpenMonth($chatId, $account);
|
||||
|
||||
$month->dailySales()->create([
|
||||
'user_id' => $user->id,
|
||||
'date' => $data['date'],
|
||||
'user_sales' => $data['user_sales'],
|
||||
'system_sales' => $data['system_sales'],
|
||||
]);
|
||||
|
||||
$account->update(['bot_state' => null, 'bot_data' => null]);
|
||||
$this->sendMenu($chatId, "✅ *Venta registrada!*\n\n📅 {$data['date']}\n👤 U: $" . number_format($data['user_sales'], 2) . "\n🖥️ S: $" . number_format($data['system_sales'], 2));
|
||||
break;
|
||||
|
||||
// --- FLUJO GASTO ---
|
||||
case 'AWAITING_EXPENSE_DATE':
|
||||
$data['date'] = $this->parseDate($text);
|
||||
$account->update(['bot_state' => 'AWAITING_EXPENSE_AMOUNT', 'bot_data' => $data]);
|
||||
$this->sendMessage($chatId, "💸 Ingresa el monto del gasto:", $this->cancelKeyboard());
|
||||
break;
|
||||
|
||||
case 'AWAITING_EXPENSE_AMOUNT':
|
||||
if (!is_numeric($text)) return $this->sendInvalidNumeric($chatId);
|
||||
$data['amount'] = (float) $text;
|
||||
$account->update(['bot_state' => 'AWAITING_EXPENSE_DESC', 'bot_data' => $data]);
|
||||
$this->sendMessage($chatId, "📝 Describe el gasto:", $this->cancelKeyboard());
|
||||
break;
|
||||
|
||||
case 'AWAITING_EXPENSE_DESC':
|
||||
$data['description'] = $text;
|
||||
$user = $account->user;
|
||||
$month = $user->getCurrentMonth();
|
||||
if (!$month) return $this->sendNoOpenMonth($chatId, $account);
|
||||
|
||||
$month->expenses()->create([
|
||||
'user_id' => $user->id,
|
||||
'date' => $data['date'],
|
||||
'amount' => $data['amount'],
|
||||
'description' => $data['description'],
|
||||
'type' => 'other',
|
||||
]);
|
||||
|
||||
$account->update(['bot_state' => null, 'bot_data' => null]);
|
||||
$this->sendMenu($chatId, "✅ *Gasto registrado!*\n\n📅 {$data['date']}\n💰 $" . number_format($data['amount'], 2) . "\n📝 {$data['description']}");
|
||||
break;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
Log::error('Bot interaction error: ' . $e->getMessage());
|
||||
$account->update(['bot_state' => null, 'bot_data' => null]);
|
||||
$this->sendMenu($chatId, "Operación cancelada.");
|
||||
return ['ok' => true];
|
||||
$this->sendMenu($chatId, "❌ Ocurrió un error al guardar los datos. Por favor intenta de nuevo.");
|
||||
}
|
||||
|
||||
switch ($state) {
|
||||
case 'AWAITING_SALE_DATE':
|
||||
case 'AWAITING_EXPENSE_DATE':
|
||||
$data['date'] = $this->parseDate($text);
|
||||
$nextState = ($state === 'AWAITING_SALE_DATE') ? 'AWAITING_SALE_USER_AMOUNT' : 'AWAITING_EXPENSE_AMOUNT';
|
||||
$nextMsg = ($state === 'AWAITING_SALE_DATE') ? "💰 Monto vendido por *usuario*:" : "💸 Monto del gasto:";
|
||||
$account->update(['bot_state' => $nextState, 'bot_data' => $data]);
|
||||
$this->sendMessage($chatId, $nextMsg, $this->cancelKeyboard());
|
||||
break;
|
||||
|
||||
case 'AWAITING_SALE_USER_AMOUNT':
|
||||
if (!is_numeric($text)) return $this->sendInvalidNumeric($chatId);
|
||||
$data['user_sales'] = (float) $text;
|
||||
$account->update(['bot_state' => 'AWAITING_SALE_SYSTEM_AMOUNT', 'bot_data' => $data]);
|
||||
$this->sendMessage($chatId, "🖥️ Monto vendido según *sistema*:", $this->cancelKeyboard());
|
||||
break;
|
||||
|
||||
case 'AWAITING_SALE_SYSTEM_AMOUNT':
|
||||
if (!is_numeric($text)) return $this->sendInvalidNumeric($chatId);
|
||||
$data['system_sales'] = (float) $text;
|
||||
$month = $account->user->getCurrentMonth();
|
||||
if (!$month) return $this->sendNoOpenMonth($chatId, $account);
|
||||
|
||||
$month->dailySales()->create(['date' => $data['date'], 'user_sales' => $data['user_sales'], 'system_sales' => $data['system_sales']]);
|
||||
$account->update(['bot_state' => null, 'bot_data' => null]);
|
||||
$this->sendMenu($chatId, "✅ *Venta registrada!*\n\n📅 {$data['date']}\n👤 U: $" . number_format($data['user_sales'], 2) . "\n🖥️ S: $" . number_format($data['system_sales'], 2));
|
||||
break;
|
||||
|
||||
case 'AWAITING_EXPENSE_AMOUNT':
|
||||
if (!is_numeric($text)) return $this->sendInvalidNumeric($chatId);
|
||||
$data['amount'] = (float) $text;
|
||||
$account->update(['bot_state' => 'AWAITING_EXPENSE_DESC', 'bot_data' => $data]);
|
||||
$this->sendMessage($chatId, "📝 Describe el gasto:", $this->cancelKeyboard());
|
||||
break;
|
||||
|
||||
case 'AWAITING_EXPENSE_DESC':
|
||||
$data['description'] = $text;
|
||||
$month = $account->user->getCurrentMonth();
|
||||
if (!$month) return $this->sendNoOpenMonth($chatId, $account);
|
||||
|
||||
$month->expenses()->create(['date' => $data['date'], 'amount' => $data['amount'], 'description' => $data['description'], 'type' => 'other']);
|
||||
$account->update(['bot_state' => null, 'bot_data' => null]);
|
||||
$this->sendMenu($chatId, "✅ *Gasto registrado!*\n\n📅 {$data['date']}\n💰 $" . number_format($data['amount'], 2) . "\n📝 {$data['description']}");
|
||||
break;
|
||||
}
|
||||
return ['ok' => true];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user