bodegAI/commands/weather_command.js

48 lines
1.9 KiB
JavaScript

const weatherService = require('../services/weather_service');
const weatherUtils = require('../utils/weather_utils');
/**
* Maneja el comando de clima.
* @param {Client} client - Instancia del cliente de WhatsApp.
* @param {Object} message - Mensaje recibido.
*/
const handleWeatherCommand = async (client, message) => {
const parts = message.body.trim().split(' ');
const command = parts[0].toLowerCase();
const city = parts.length > 1 ? parts.slice(1).join(' ') : 'Guatemala';
try {
const { latitude, longitude } = await weatherUtils.getCoordinates(city);
const weather = await weatherService.getWeather(latitude, longitude);
const weatherDescription = weatherUtils.translateWeatherCode(weather.weathercode);
const weatherInfo = formatWeatherInfo(city, weather, weatherDescription);
await client.sendMessage(message.from, weatherInfo);
console.log(`📤 Información del clima de ${city} enviada a ${message.from}`);
} catch (error) {
console.error('❌ Error al obtener el clima:', error);
await client.sendMessage(message.from, `❌ Error al obtener el clima: ${error.message}`);
}
};
/**
* Formatea la información del clima para enviarla como mensaje.
* @param {String} city - Nombre de la ciudad.
* @param {Object} weather - Datos del clima obtenidos del servicio.
* @param {String} weatherDescription - Descripción traducida del código de clima.
* @returns {String} - Mensaje formateado con la información del clima.
*/
const formatWeatherInfo = (city, weather, weatherDescription) => {
return `
🌤️ *Clima Actual en ${city}*:
- 🌡️ Temperatura: ${weather.temperature}°C
- 💨 Viento: ${weather.windspeed} km/h
- 🧭 Dirección del viento: ${weather.winddirection}°
- ☁️ Condición: ${weatherDescription}
- ⏰ Hora: ${new Date(weather.time).toLocaleTimeString('es-ES')}
`.trim();
};
module.exports = {
handleWeatherCommand
};