-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
73 lines (62 loc) · 2.15 KB
/
app.js
File metadata and controls
73 lines (62 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import express from 'express'
import 'dotenv/config'
import mongoose from 'mongoose'
import { bot, webhookCallback } from './config/telegraf/telegraf.js'
import { startReminderScheduler } from './services/schedulers/reminderScheduler.js'
import { debugLog } from './utils/logUtils/debugLog.js'
// ====================================
// 🔰 Verifico .env
// ====================================
const mongoURI = process.env.MONGO_URI
const port = process.env.PORT || 3000
const domain = process.env.WEBHOOK_DOMAIN
if (!mongoURI) {
throw new Error('🪧 MONGO_URI no está definido en el archivo .env')
}
if (!domain) {
throw new Error('🪧 WEBHOOK_DOMAIN no está definido en el archivo .env')
}
// =====================
// 🔰 Conecto a MongoDB
// =====================
mongoose
.connect(mongoURI)
.then(() => {
console.info('👾 Conectado a MongoDB correctamente')
// ======================
// 🔰 Inicializo Scheduler
// ======================
startReminderScheduler()
// ======================
// 🔰 Inicializo Express
// ======================
const app = express()
// =======================
// 🔰 Webhook de Telegraf
// =======================
const path = '/telegraf/tuttobot-path-seguro'
app.post(path, express.json(), (req, res, next) => {
debugLog('📩 Petición recibida en webhook') // Necesito forzar a render a mostrarme
webhookCallback(req, res, next)
})
bot.telegram.setWebhook(`${domain}${path}`)
console.info(`🤖 Webhook activo en: ${domain}${path}`)
// =========================================
// 🔰 Ruta raíz para mantener render activo
// =========================================
app.get('/', (req, res) => {
res
.status(200)
.send('🤖 TuttoFatto está despierto y funcionando correctamente.')
})
// =======================
// 🔰 Levanto el servidor
// =======================
app.listen(port, () => {
console.info(`🛫 Servidor escuchando en http://localhost:${port}`)
})
})
.catch((err) => {
console.error('🦽 Error al conectar a MongoDB:', err.message)
process.exit(1)
})