create-bunspace 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +181 -0
- package/dist/bin.js +5755 -0
- package/dist/templates/monorepo/CLAUDE.md +164 -0
- package/dist/templates/monorepo/LICENSE +21 -0
- package/dist/templates/monorepo/MUST-FOLLOW-GUIDELINES.md +269 -0
- package/dist/templates/monorepo/README.md +74 -0
- package/dist/templates/monorepo/SYNC_VERIFICATION.md +1 -0
- package/dist/templates/monorepo/apps/example/package.json +19 -0
- package/dist/templates/monorepo/apps/example/src/index.ts +23 -0
- package/dist/templates/monorepo/apps/example/src/types/index.ts +7 -0
- package/dist/templates/monorepo/apps/example/src/utils/index.ts +7 -0
- package/dist/templates/monorepo/core/packages/main/package.json +41 -0
- package/dist/templates/monorepo/core/packages/main/rolldown.config.ts +24 -0
- package/dist/templates/monorepo/core/packages/main/src/index.ts +80 -0
- package/dist/templates/monorepo/core/packages/main/src/types/constants.ts +15 -0
- package/dist/templates/monorepo/core/packages/main/src/types/index.ts +8 -0
- package/dist/templates/monorepo/core/packages/main/src/types/main.types.ts +25 -0
- package/dist/templates/monorepo/core/packages/main/src/utils/index.ts +5 -0
- package/dist/templates/monorepo/core/packages/utils/package.json +43 -0
- package/dist/templates/monorepo/core/packages/utils/rolldown.config.ts +34 -0
- package/dist/templates/monorepo/core/packages/utils/src/index.ts +2 -0
- package/dist/templates/monorepo/core/packages/utils/src/logger.ts +68 -0
- package/dist/templates/monorepo/core/packages/utils/src/result.ts +146 -0
- package/dist/templates/monorepo/core/packages/utils/src/types/constants.ts +15 -0
- package/dist/templates/monorepo/core/packages/utils/src/types/index.ts +8 -0
- package/dist/templates/monorepo/core/packages/utils/src/types/utils.types.ts +32 -0
- package/dist/templates/monorepo/core/packages/utils/src/utils/index.ts +5 -0
- package/dist/templates/monorepo/oxlint.json +14 -0
- package/dist/templates/monorepo/package.json +39 -0
- package/dist/templates/monorepo/tsconfig.json +35 -0
- package/dist/templates/telegram-bot/.oxlintrc.json +33 -0
- package/dist/templates/telegram-bot/.prettierignore +5 -0
- package/dist/templates/telegram-bot/.prettierrc +26 -0
- package/dist/templates/telegram-bot/CLAUDE.deploy.md +356 -0
- package/dist/templates/telegram-bot/CLAUDE.dev.md +266 -0
- package/dist/templates/telegram-bot/CLAUDE.md +280 -0
- package/dist/templates/telegram-bot/Dockerfile +46 -0
- package/dist/templates/telegram-bot/README.md +245 -0
- package/dist/templates/telegram-bot/apps/.gitkeep +0 -0
- package/dist/templates/telegram-bot/bun.lock +208 -0
- package/dist/templates/telegram-bot/core/.env.example +71 -0
- package/dist/templates/telegram-bot/core/README.md +1067 -0
- package/dist/templates/telegram-bot/core/package.json +15 -0
- package/dist/templates/telegram-bot/core/src/config/env.ts +131 -0
- package/dist/templates/telegram-bot/core/src/config/index.ts +97 -0
- package/dist/templates/telegram-bot/core/src/config/logging.ts +110 -0
- package/dist/templates/telegram-bot/core/src/handlers/control.ts +85 -0
- package/dist/templates/telegram-bot/core/src/handlers/health.ts +83 -0
- package/dist/templates/telegram-bot/core/src/handlers/logs.ts +126 -0
- package/dist/templates/telegram-bot/core/src/index.ts +161 -0
- package/dist/templates/telegram-bot/core/src/middleware/auth.ts +41 -0
- package/dist/templates/telegram-bot/core/src/middleware/error-handler.ts +41 -0
- package/dist/templates/telegram-bot/core/src/middleware/logging.ts +1 -0
- package/dist/templates/telegram-bot/core/src/middleware/topics.ts +55 -0
- package/dist/templates/telegram-bot/core/src/types/bot.ts +92 -0
- package/dist/templates/telegram-bot/core/src/types/constants.ts +50 -0
- package/dist/templates/telegram-bot/core/src/types/result.ts +1 -0
- package/dist/templates/telegram-bot/core/src/utils/bot-manager.test.ts +111 -0
- package/dist/templates/telegram-bot/core/src/utils/bot-manager.ts +201 -0
- package/dist/templates/telegram-bot/core/src/utils/commands.ts +63 -0
- package/dist/templates/telegram-bot/core/src/utils/formatters.ts +82 -0
- package/dist/templates/telegram-bot/core/src/utils/instance-manager.ts +189 -0
- package/dist/templates/telegram-bot/core/src/utils/memory.ts +33 -0
- package/dist/templates/telegram-bot/core/src/utils/result.ts +26 -0
- package/dist/templates/telegram-bot/core/src/utils/telegram.ts +31 -0
- package/dist/templates/telegram-bot/core/src/utils/type-guards.ts +71 -0
- package/dist/templates/telegram-bot/core/tsconfig.json +9 -0
- package/dist/templates/telegram-bot/docker-compose.yml +37 -0
- package/dist/templates/telegram-bot/docs/cli-commands.md +377 -0
- package/dist/templates/telegram-bot/docs/development.md +363 -0
- package/dist/templates/telegram-bot/docs/environment.md +460 -0
- package/dist/templates/telegram-bot/docs/examples/middleware-auth.md +335 -0
- package/dist/templates/telegram-bot/docs/examples/simple-command.md +207 -0
- package/dist/templates/telegram-bot/docs/examples/webhook-setup.md +362 -0
- package/dist/templates/telegram-bot/docs/getting-started.md +223 -0
- package/dist/templates/telegram-bot/docs/troubleshooting.md +489 -0
- package/dist/templates/telegram-bot/package.json +49 -0
- package/dist/templates/telegram-bot/packages/utils/package.json +12 -0
- package/dist/templates/telegram-bot/packages/utils/src/index.ts +2 -0
- package/dist/templates/telegram-bot/packages/utils/src/logger.ts +72 -0
- package/dist/templates/telegram-bot/packages/utils/src/result.ts +80 -0
- package/dist/templates/telegram-bot/tools/README.md +47 -0
- package/dist/templates/telegram-bot/tools/commands/doctor.ts +460 -0
- package/dist/templates/telegram-bot/tools/commands/index.ts +35 -0
- package/dist/templates/telegram-bot/tools/commands/ngrok.ts +207 -0
- package/dist/templates/telegram-bot/tools/commands/setup.ts +368 -0
- package/dist/templates/telegram-bot/tools/commands/status.ts +140 -0
- package/dist/templates/telegram-bot/tools/index.ts +16 -0
- package/dist/templates/telegram-bot/tools/package.json +12 -0
- package/dist/templates/telegram-bot/tools/utils/index.ts +13 -0
- package/dist/templates/telegram-bot/tsconfig.json +22 -0
- package/dist/templates/telegram-bot/vitest.config.ts +29 -0
- package/package.json +35 -0
- package/templates/monorepo/CLAUDE.md +164 -0
- package/templates/monorepo/LICENSE +21 -0
- package/templates/monorepo/MUST-FOLLOW-GUIDELINES.md +269 -0
- package/templates/monorepo/README.md +74 -0
- package/templates/monorepo/apps/example/package.json +19 -0
- package/templates/monorepo/apps/example/src/index.ts +23 -0
- package/templates/monorepo/apps/example/src/types/index.ts +7 -0
- package/templates/monorepo/apps/example/src/utils/index.ts +7 -0
- package/templates/monorepo/core/packages/main/package.json +41 -0
- package/templates/monorepo/core/packages/main/rolldown.config.ts +24 -0
- package/templates/monorepo/core/packages/main/src/index.ts +80 -0
- package/templates/monorepo/core/packages/main/src/types/constants.ts +15 -0
- package/templates/monorepo/core/packages/main/src/types/index.ts +8 -0
- package/templates/monorepo/core/packages/main/src/types/main.types.ts +25 -0
- package/templates/monorepo/core/packages/main/src/utils/index.ts +5 -0
- package/templates/monorepo/core/packages/utils/package.json +43 -0
- package/templates/monorepo/core/packages/utils/rolldown.config.ts +34 -0
- package/templates/monorepo/core/packages/utils/src/index.ts +2 -0
- package/templates/monorepo/core/packages/utils/src/logger.ts +68 -0
- package/templates/monorepo/core/packages/utils/src/result.ts +146 -0
- package/templates/monorepo/core/packages/utils/src/types/constants.ts +15 -0
- package/templates/monorepo/core/packages/utils/src/types/index.ts +8 -0
- package/templates/monorepo/core/packages/utils/src/types/utils.types.ts +32 -0
- package/templates/monorepo/core/packages/utils/src/utils/index.ts +5 -0
- package/templates/monorepo/oxlint.json +14 -0
- package/templates/monorepo/package.json +39 -0
- package/templates/monorepo/tsconfig.json +35 -0
- package/templates/telegram-bot/.oxlintrc.json +33 -0
- package/templates/telegram-bot/.prettierignore +5 -0
- package/templates/telegram-bot/.prettierrc +26 -0
- package/templates/telegram-bot/CLAUDE.deploy.md +356 -0
- package/templates/telegram-bot/CLAUDE.dev.md +266 -0
- package/templates/telegram-bot/CLAUDE.md +280 -0
- package/templates/telegram-bot/Dockerfile +46 -0
- package/templates/telegram-bot/README.md +245 -0
- package/templates/telegram-bot/apps/.gitkeep +0 -0
- package/templates/telegram-bot/bun.lock +208 -0
- package/templates/telegram-bot/core/.env.example +71 -0
- package/templates/telegram-bot/core/README.md +1067 -0
- package/templates/telegram-bot/core/package.json +15 -0
- package/templates/telegram-bot/core/src/config/env.ts +131 -0
- package/templates/telegram-bot/core/src/config/index.ts +97 -0
- package/templates/telegram-bot/core/src/config/logging.ts +110 -0
- package/templates/telegram-bot/core/src/handlers/control.ts +85 -0
- package/templates/telegram-bot/core/src/handlers/health.ts +83 -0
- package/templates/telegram-bot/core/src/handlers/logs.ts +126 -0
- package/templates/telegram-bot/core/src/index.ts +161 -0
- package/templates/telegram-bot/core/src/middleware/auth.ts +41 -0
- package/templates/telegram-bot/core/src/middleware/error-handler.ts +41 -0
- package/templates/telegram-bot/core/src/middleware/logging.ts +1 -0
- package/templates/telegram-bot/core/src/middleware/topics.ts +55 -0
- package/templates/telegram-bot/core/src/types/bot.ts +92 -0
- package/templates/telegram-bot/core/src/types/constants.ts +50 -0
- package/templates/telegram-bot/core/src/types/result.ts +1 -0
- package/templates/telegram-bot/core/src/utils/bot-manager.test.ts +111 -0
- package/templates/telegram-bot/core/src/utils/bot-manager.ts +201 -0
- package/templates/telegram-bot/core/src/utils/commands.ts +63 -0
- package/templates/telegram-bot/core/src/utils/formatters.ts +82 -0
- package/templates/telegram-bot/core/src/utils/instance-manager.ts +189 -0
- package/templates/telegram-bot/core/src/utils/memory.ts +33 -0
- package/templates/telegram-bot/core/src/utils/result.ts +26 -0
- package/templates/telegram-bot/core/src/utils/telegram.ts +31 -0
- package/templates/telegram-bot/core/src/utils/type-guards.ts +71 -0
- package/templates/telegram-bot/core/tsconfig.json +9 -0
- package/templates/telegram-bot/docker-compose.yml +37 -0
- package/templates/telegram-bot/docs/cli-commands.md +377 -0
- package/templates/telegram-bot/docs/development.md +363 -0
- package/templates/telegram-bot/docs/environment.md +460 -0
- package/templates/telegram-bot/docs/examples/middleware-auth.md +335 -0
- package/templates/telegram-bot/docs/examples/simple-command.md +207 -0
- package/templates/telegram-bot/docs/examples/webhook-setup.md +362 -0
- package/templates/telegram-bot/docs/getting-started.md +223 -0
- package/templates/telegram-bot/docs/troubleshooting.md +489 -0
- package/templates/telegram-bot/package.json +49 -0
- package/templates/telegram-bot/packages/utils/package.json +12 -0
- package/templates/telegram-bot/packages/utils/src/index.ts +2 -0
- package/templates/telegram-bot/packages/utils/src/logger.ts +72 -0
- package/templates/telegram-bot/packages/utils/src/result.ts +80 -0
- package/templates/telegram-bot/tools/README.md +47 -0
- package/templates/telegram-bot/tools/commands/doctor.ts +460 -0
- package/templates/telegram-bot/tools/commands/index.ts +35 -0
- package/templates/telegram-bot/tools/commands/ngrok.ts +207 -0
- package/templates/telegram-bot/tools/commands/setup.ts +368 -0
- package/templates/telegram-bot/tools/commands/status.ts +140 -0
- package/templates/telegram-bot/tools/index.ts +16 -0
- package/templates/telegram-bot/tools/package.json +12 -0
- package/templates/telegram-bot/tools/utils/index.ts +13 -0
- package/templates/telegram-bot/tsconfig.json +22 -0
- package/templates/telegram-bot/vitest.config.ts +29 -0
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
# CLAUDE.dev - Guía de Desarrollo
|
|
2
|
+
|
|
3
|
+
> **Parte de**: [CLAUDE.md](./CLAUDE.md) | Ver también: [CLAUDE.deploy.md](./CLAUDE.deploy.md)
|
|
4
|
+
|
|
5
|
+
Guía de desarrollo para extender y modificar el bot template.
|
|
6
|
+
|
|
7
|
+
**Documentación relacionada**:
|
|
8
|
+
- [docs/development/setup.mdx](./docs/development/setup.mdx)
|
|
9
|
+
- [docs/development/patterns.mdx](./docs/development/patterns.mdx)
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Development Standards
|
|
14
|
+
|
|
15
|
+
### Code Style
|
|
16
|
+
|
|
17
|
+
- TypeScript strict mode enabled
|
|
18
|
+
- `noUncheckedIndexedAccess: true` - arrays need undefined checks
|
|
19
|
+
- Semi: false, singleQuote: true
|
|
20
|
+
- Result type pattern para error handling
|
|
21
|
+
- Better Logger para logging (no console.*)
|
|
22
|
+
|
|
23
|
+
### Before Commit Checklist
|
|
24
|
+
|
|
25
|
+
- [ ] `bun run typecheck` → 0 errores
|
|
26
|
+
- [ ] `bun run lint` → 0 warnings, 0 errors
|
|
27
|
+
- [ ] `bun test` → all tests pass
|
|
28
|
+
- [ ] No `console.*` statements
|
|
29
|
+
- [ ] Código formateado (`bun run format:check`)
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
## Agregando Commands
|
|
34
|
+
|
|
35
|
+
### 1. Crear Handler
|
|
36
|
+
|
|
37
|
+
**Archivo**: `core/src/handlers/mycommand.ts`
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import type { Context } from 'telegraf'
|
|
41
|
+
|
|
42
|
+
export async function handleMyCommand(ctx: Context): Promise<void> {
|
|
43
|
+
await ctx.reply('Respuesta del comando')
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### 2. Registrar en Bot
|
|
48
|
+
|
|
49
|
+
**Archivo**: `core/src/index.ts`
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { handleMyCommand } from './handlers/mycommand.js'
|
|
53
|
+
bot.command('mycommand', handleMyCommand)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
## Agregando Middleware
|
|
59
|
+
|
|
60
|
+
### 1. Crear Middleware
|
|
61
|
+
|
|
62
|
+
**Archivo**: `core/src/middleware/custom.ts`
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import type { Context, Middleware } from 'telegraf'
|
|
66
|
+
|
|
67
|
+
export function customMiddleware(): Middleware<Context> {
|
|
68
|
+
return async (ctx, next) => {
|
|
69
|
+
// Pre-processing
|
|
70
|
+
await next()
|
|
71
|
+
// Post-processing
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### 2. Registrar Middleware
|
|
77
|
+
|
|
78
|
+
**Archivo**: `core/src/index.ts`
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { customMiddleware } from './middleware/custom.js'
|
|
82
|
+
bot.use(customMiddleware())
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## Patterns
|
|
88
|
+
|
|
89
|
+
### Result Type Pattern
|
|
90
|
+
|
|
91
|
+
Railway-oriented programming para error handling sin excepciones:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { ok, err, type Result, botError } from './types/result.js'
|
|
95
|
+
|
|
96
|
+
function divide(a: number, b: number): Result<number> {
|
|
97
|
+
if (b === 0) {
|
|
98
|
+
return err(botError('INVALID_ARGS', 'Cannot divide by zero'))
|
|
99
|
+
}
|
|
100
|
+
return ok(a / b)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const result = divide(10, 2)
|
|
104
|
+
if (result.ok) {
|
|
105
|
+
console.log(result.value) // 5
|
|
106
|
+
} else {
|
|
107
|
+
console.error(result.error.message)
|
|
108
|
+
}
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
**Utilities disponibles**: `ok`, `err`, `isOk`, `isErr`, `unwrap`, `unwrapOr`, `map`, `mapErr`, `flatMap`, `tap`, `tapErr`, `match`, `collect`, `all`
|
|
112
|
+
|
|
113
|
+
### Singleton Pattern
|
|
114
|
+
|
|
115
|
+
- `BotManager` - Lifecycle y stats
|
|
116
|
+
- `LogStreamer` - Buffer de logs
|
|
117
|
+
- `InstanceManager` - Lock management
|
|
118
|
+
- `Config` - Cached configuration
|
|
119
|
+
|
|
120
|
+
---
|
|
121
|
+
|
|
122
|
+
## Logging System
|
|
123
|
+
|
|
124
|
+
### Usar Better Logger
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
import { botLogger, kv, badge, colorText, colors } from './middleware/logging.js'
|
|
128
|
+
|
|
129
|
+
botLogger.info('Información')
|
|
130
|
+
botLogger.success('Operación exitosa')
|
|
131
|
+
botLogger.warn('Advertencia')
|
|
132
|
+
botLogger.error('Error', error)
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Crear Logger Especializado
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
import { component } from '@mks2508/better-logger'
|
|
139
|
+
|
|
140
|
+
const myLogger = component('MyModule')
|
|
141
|
+
myLogger.info('Módulo iniciado')
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
### Formatting Avanzado
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
// Key-Value formatting
|
|
148
|
+
botLogger.info(kv({ user: '123', action: 'start' }))
|
|
149
|
+
|
|
150
|
+
// Badge
|
|
151
|
+
botLogger.info(badge('CMD', 'rounded'))
|
|
152
|
+
|
|
153
|
+
// Colored text
|
|
154
|
+
botLogger.success(colorText('Success', colors.success))
|
|
155
|
+
|
|
156
|
+
// Duration
|
|
157
|
+
const { formatDuration } = await import('./middleware/logging.js')
|
|
158
|
+
botLogger.info(formatDuration(1234)) // "1.2s"
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
## Testing
|
|
164
|
+
|
|
165
|
+
### Escribiendo Tests
|
|
166
|
+
|
|
167
|
+
Usa `bun test` con sintaxis similar a Jest:
|
|
168
|
+
|
|
169
|
+
```typescript
|
|
170
|
+
import { describe, test, expect } from 'bun:test'
|
|
171
|
+
|
|
172
|
+
describe('Mi Módulo', () => {
|
|
173
|
+
test('debe hacer algo', () => {
|
|
174
|
+
const result = miFuncion()
|
|
175
|
+
expect(result).toBe('expected')
|
|
176
|
+
})
|
|
177
|
+
})
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Archivos de Test
|
|
181
|
+
|
|
182
|
+
- **Nombre**: `*.test.ts` o `*.spec.ts`
|
|
183
|
+
- **Ubicación**: Junto al archivo que testean
|
|
184
|
+
- **Ejemplo**: `core/src/types/result.test.ts`
|
|
185
|
+
|
|
186
|
+
### Ejecutar Tests
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
bun test # Ejecutar todos
|
|
190
|
+
bun test --verbose # Output detallado
|
|
191
|
+
bun test --watch # Watch mode
|
|
192
|
+
bun test path/to/test.ts # Test específico
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
---
|
|
196
|
+
|
|
197
|
+
## Environment Variables (Desarrollo)
|
|
198
|
+
|
|
199
|
+
### Archivo: `core/.env.example`
|
|
200
|
+
|
|
201
|
+
Variables requeridas para desarrollo:
|
|
202
|
+
|
|
203
|
+
| Variable | Descripción |
|
|
204
|
+
| -------- | ----------- |
|
|
205
|
+
| `TG_BOT_TOKEN` | Token from @BotFather |
|
|
206
|
+
| `TG_MODE` | `polling` o `webhook` |
|
|
207
|
+
| `LOG_LEVEL` | `debug`, `info`, `warn`, `error` |
|
|
208
|
+
| `TG_DEBUG` | Habilitar debug mode |
|
|
209
|
+
| `TG_INSTANCE_CHECK` | Habilitar detección de instancias |
|
|
210
|
+
|
|
211
|
+
---
|
|
212
|
+
|
|
213
|
+
## Troubleshooting Development
|
|
214
|
+
|
|
215
|
+
### Typecheck Errors
|
|
216
|
+
|
|
217
|
+
```bash
|
|
218
|
+
# Ejecutar typecheck detallado
|
|
219
|
+
tsgo --traceback
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Tests Fallan
|
|
223
|
+
|
|
224
|
+
```bash
|
|
225
|
+
# Ejecutar tests con output detallado
|
|
226
|
+
bun test --verbose
|
|
227
|
+
|
|
228
|
+
# Ejecutar solo un archivo de test
|
|
229
|
+
bun test core/src/types/result.test.ts
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Bot No Responde
|
|
233
|
+
|
|
234
|
+
1. Verificar `TG_BOT_TOKEN` es válido
|
|
235
|
+
2. Chequear `/health` command
|
|
236
|
+
3. Verificar `TG_AUTHORIZED_USER_IDS` para control commands
|
|
237
|
+
|
|
238
|
+
---
|
|
239
|
+
|
|
240
|
+
## Quick Reference
|
|
241
|
+
|
|
242
|
+
### Comandos de Desarrollo
|
|
243
|
+
|
|
244
|
+
```bash
|
|
245
|
+
bun run dev # Watch mode
|
|
246
|
+
bun run typecheck # Type check
|
|
247
|
+
bun run lint # Lint check
|
|
248
|
+
bun run format # Format code
|
|
249
|
+
bun test # Run tests
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### Archivos Clave para Desarrollo
|
|
253
|
+
|
|
254
|
+
- `core/src/index.ts` - Entry point, command registration
|
|
255
|
+
- `core/src/handlers/` - Command handlers
|
|
256
|
+
- `core/src/middleware/` - Telegraf middleware
|
|
257
|
+
- `core/src/config/` - Configuration and env validation
|
|
258
|
+
- `packages/utils/src/` - Shared utilities
|
|
259
|
+
|
|
260
|
+
---
|
|
261
|
+
|
|
262
|
+
## Ver También
|
|
263
|
+
|
|
264
|
+
- [CLAUDE.md](./CLAUDE.md) - Entry point principal
|
|
265
|
+
- [CLAUDE.deploy.md](./CLAUDE.deploy.md) - Deployment y entornos
|
|
266
|
+
- [README.md](./README.md) - Quick start del proyecto
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
# CLAUDE.md - mks-telegram-bot
|
|
2
|
+
|
|
3
|
+
> **Template**: Monorepo para bots de Telegram con Bun, Telegraf y TypeScript
|
|
4
|
+
> **Versión**: 0.1.0
|
|
5
|
+
|
|
6
|
+
Template monorepo diseñado para ser usado como `bun create mks2508/mks-telegram-bot` o como GitHub template repository.
|
|
7
|
+
|
|
8
|
+
**Objetivo**: Template reutilizable para crear bots de Telegram con las mejores prácticas, tipado estricto y arquitectura modular.
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## Quick Start
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
# Clonar o usar como template
|
|
16
|
+
bun create mks2508/mks-telegram-bot my-bot
|
|
17
|
+
|
|
18
|
+
# Instalar dependencias
|
|
19
|
+
bun install
|
|
20
|
+
|
|
21
|
+
# Configurar variables de entorno
|
|
22
|
+
cp core/.env.example core/.env
|
|
23
|
+
|
|
24
|
+
# Desarrollo
|
|
25
|
+
bun run dev
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
---
|
|
29
|
+
|
|
30
|
+
## Contextos Especializados
|
|
31
|
+
|
|
32
|
+
Este template tiene contextos separados por dominio:
|
|
33
|
+
|
|
34
|
+
| Archivo | Propósito |
|
|
35
|
+
| ------- | --------- |
|
|
36
|
+
| [CLAUDE.dev.md](./CLAUDE.dev.md) | Desarrollo, patterns, testing |
|
|
37
|
+
| [CLAUDE.deploy.md](./CLAUDE.deploy.md) | Deployment, Docker, entornos |
|
|
38
|
+
| [docs/](./docs/) | Documentación web (MDX, futuro) |
|
|
39
|
+
|
|
40
|
+
### CLAUDE.dev.md
|
|
41
|
+
|
|
42
|
+
Ver [CLAUDE.dev.md](./CLAUDE.dev.md) para:
|
|
43
|
+
- Development standards y code style
|
|
44
|
+
- Cómo agregar commands y middleware
|
|
45
|
+
- Testing patterns
|
|
46
|
+
- Result type pattern y Singleton pattern
|
|
47
|
+
- Better Logger usage
|
|
48
|
+
|
|
49
|
+
### CLAUDE.deploy.md
|
|
50
|
+
|
|
51
|
+
Ver [CLAUDE.deploy.md](./CLAUDE.deploy.md) para:
|
|
52
|
+
- Multi-environment (local, staging, production)
|
|
53
|
+
- Multi-instance management
|
|
54
|
+
- Docker deployment
|
|
55
|
+
- ngrok integration
|
|
56
|
+
- VPS deployment guide
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## Critical Files for Claude
|
|
61
|
+
|
|
62
|
+
When modifying this codebase, prioritize understanding:
|
|
63
|
+
|
|
64
|
+
- `core/src/index.ts` - Bot entry point, command registration
|
|
65
|
+
- `core/src/config/env.ts` - Environment schema validation
|
|
66
|
+
- `core/src/utils/instance-manager.ts` - Multi-instance locking
|
|
67
|
+
- `packages/utils/src/` - Shared utilities (logger, result)
|
|
68
|
+
|
|
69
|
+
---
|
|
70
|
+
|
|
71
|
+
## Session Context Template
|
|
72
|
+
|
|
73
|
+
When starting a new session, include:
|
|
74
|
+
- **User's goal**: developing vs deploying vs extending
|
|
75
|
+
- **Current environment**: local/staging/production
|
|
76
|
+
- **Any errors or issues encountered**: Paste error messages
|
|
77
|
+
|
|
78
|
+
---
|
|
79
|
+
|
|
80
|
+
## Monorepo Structure
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
mks-telegram-bot/
|
|
84
|
+
├── core/ # @mks2508/telegram-bot-core
|
|
85
|
+
│ ├── .env.local # Local development
|
|
86
|
+
│ ├── .env.staging # Staging environment
|
|
87
|
+
│ ├── .env.production # Production environment
|
|
88
|
+
│ ├── tmp/ # Instance lock files
|
|
89
|
+
│ └── src/ # Bot source code
|
|
90
|
+
├── packages/ # Shared code
|
|
91
|
+
│ └── utils/ # @mks2508/telegram-bot-utils
|
|
92
|
+
├── tools/ # CLI tools
|
|
93
|
+
│ └── commands/ # ngrok, status
|
|
94
|
+
├── apps/ # Future: examples, docs
|
|
95
|
+
├── docs/ # Future MDX site
|
|
96
|
+
├── Dockerfile # Multi-stage build
|
|
97
|
+
└── docker-compose.yml # Local dev containers
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
---
|
|
101
|
+
|
|
102
|
+
## Commands
|
|
103
|
+
|
|
104
|
+
```bash
|
|
105
|
+
# Development
|
|
106
|
+
bun run dev # Hot reload (watch mode)
|
|
107
|
+
bun run start # Production mode
|
|
108
|
+
|
|
109
|
+
# CLI tools
|
|
110
|
+
bun run status # Show running instances
|
|
111
|
+
bun run ngrok # Start ngrok tunnel
|
|
112
|
+
|
|
113
|
+
# Quality
|
|
114
|
+
bun run typecheck # Type check (tsgo ~10x faster)
|
|
115
|
+
bun run lint # Lint (oxlint)
|
|
116
|
+
bun run format # Format (prettier)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## Environment Variables
|
|
122
|
+
|
|
123
|
+
### Multi-Environment Support
|
|
124
|
+
|
|
125
|
+
El proyecto soporta múltiples entornos con archivos `.env` separados:
|
|
126
|
+
|
|
127
|
+
| Archivo | Uso |
|
|
128
|
+
| ------- | --- |
|
|
129
|
+
| `core/.env.local` | Desarrollo local (polling mode) |
|
|
130
|
+
| `core/.env.staging` | Testing con bot de test |
|
|
131
|
+
| `core/.env.production` | Producción con bot real |
|
|
132
|
+
|
|
133
|
+
La variable `TG_ENV` (default: `local`) determina cuál archivo cargar.
|
|
134
|
+
|
|
135
|
+
### Required Variables
|
|
136
|
+
|
|
137
|
+
| Variable | Tipo | Description |
|
|
138
|
+
| -------- | ---- | ----------- |
|
|
139
|
+
| `TG_BOT_TOKEN` | string | Bot token from @BotFather |
|
|
140
|
+
| `TG_MODE` | enum | `polling` or `webhook` |
|
|
141
|
+
|
|
142
|
+
### Instance Detection Variables
|
|
143
|
+
|
|
144
|
+
| Variable | Default | Description |
|
|
145
|
+
| -------- | ------- | ----------- |
|
|
146
|
+
| `TG_ENV` | `local` | Environment: local/staging/production |
|
|
147
|
+
| `TG_INSTANCE_NAME` | `mks-bot` | Unique instance name for locking |
|
|
148
|
+
| `TG_INSTANCE_CHECK` | `true` | Enable instance conflict detection |
|
|
149
|
+
|
|
150
|
+
---
|
|
151
|
+
|
|
152
|
+
## Technology Stack
|
|
153
|
+
|
|
154
|
+
| Tool | Purpose | Version |
|
|
155
|
+
| ---- | ------- | ------- |
|
|
156
|
+
| **Bun** | Runtime & package manager | 1.3+ |
|
|
157
|
+
| **Telegraf** | Telegram Bot API | 4.16+ |
|
|
158
|
+
| **TypeScript** | Language | 5.9+ |
|
|
159
|
+
| **tsgo** | Type-checking | native-preview |
|
|
160
|
+
| **Zod** | Schema validation | 3.24+ |
|
|
161
|
+
| **Better Logger** | Logging | 4.0.0 |
|
|
162
|
+
| **@mks2508/no-throw** | Result type | 0.1.0 |
|
|
163
|
+
| **oxlint** | Linting | latest |
|
|
164
|
+
| **prettier** | Formatting | 3.4+ |
|
|
165
|
+
| **commander** | CLI framework | 14.0+ |
|
|
166
|
+
|
|
167
|
+
---
|
|
168
|
+
|
|
169
|
+
## Architecture Overview
|
|
170
|
+
|
|
171
|
+
```
|
|
172
|
+
┌─────────────────────────────────────────────────────────────┐
|
|
173
|
+
│ Telegram API (Telegraf) │
|
|
174
|
+
└─────────────────────────┬───────────────────────────────────┘
|
|
175
|
+
│
|
|
176
|
+
┌─────────────────┼─────────────────┐
|
|
177
|
+
│ │ │
|
|
178
|
+
┌────▼────┐ ┌───────▼───────┐ ┌──────▼──────┐
|
|
179
|
+
│Middleware│ │ Handlers │ │ BotManager │
|
|
180
|
+
│ • auth │ │ • health │ │ InstanceMgr │
|
|
181
|
+
│ • error │ │ • control │ │ • stats │
|
|
182
|
+
│ • topics │ │ • logs │ │ • lifecycle │
|
|
183
|
+
└──────────┘ └──────────────┘ └─────────────┘
|
|
184
|
+
│ │
|
|
185
|
+
└────────────┬───────────────────┘
|
|
186
|
+
│
|
|
187
|
+
┌────────▼────────┐
|
|
188
|
+
│ InstanceManager │
|
|
189
|
+
│ • acquireLock │
|
|
190
|
+
│ • releaseLock │
|
|
191
|
+
└─────────────────┘
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
---
|
|
195
|
+
|
|
196
|
+
## Bot Commands
|
|
197
|
+
|
|
198
|
+
### Public Commands
|
|
199
|
+
|
|
200
|
+
| Command | Handler | Description |
|
|
201
|
+
| ------- | ------- | ----------- |
|
|
202
|
+
| `/start` | inline | Welcome message |
|
|
203
|
+
| `/health` | handleHealth | Bot health status |
|
|
204
|
+
| `/uptime` | handleUptime | Uptime information |
|
|
205
|
+
| `/stats` | handleStats | Bot statistics |
|
|
206
|
+
| `/logs` | handleLogs | Log streaming status |
|
|
207
|
+
|
|
208
|
+
### Control Commands (requires auth)
|
|
209
|
+
|
|
210
|
+
| Command | Handler | Description |
|
|
211
|
+
| ------- | ------- | ----------- |
|
|
212
|
+
| `/stop` | handleStop | Graceful shutdown |
|
|
213
|
+
| `/restart` | handleRestart | Restart with stats reset |
|
|
214
|
+
| `/mode` | handleMode | Switch polling/webhook |
|
|
215
|
+
| `/webhook` | handleWebhook | Show webhook config |
|
|
216
|
+
|
|
217
|
+
---
|
|
218
|
+
|
|
219
|
+
## Development Workflow
|
|
220
|
+
|
|
221
|
+
### Before Commit
|
|
222
|
+
|
|
223
|
+
- [ ] `bun run typecheck` → 0 errores
|
|
224
|
+
- [ ] `bun run lint` → 0 warnings, 0 errors
|
|
225
|
+
- [ ] `bun test` → all tests pass (if tests exist)
|
|
226
|
+
- [ ] No `console.*` (use Better Logger)
|
|
227
|
+
|
|
228
|
+
### Code Style
|
|
229
|
+
|
|
230
|
+
- TypeScript strict mode enabled
|
|
231
|
+
- `noUncheckedIndexedAccess: true` - arrays need undefined checks
|
|
232
|
+
- Semi: false, singleQuote: true
|
|
233
|
+
- Result type pattern para error handling
|
|
234
|
+
|
|
235
|
+
---
|
|
236
|
+
|
|
237
|
+
## Documentation Web
|
|
238
|
+
|
|
239
|
+
Ver [docs/](./docs/) para documentación completa (futuro MDX site con Astro/Starlight).
|
|
240
|
+
|
|
241
|
+
---
|
|
242
|
+
|
|
243
|
+
## Future Apps
|
|
244
|
+
|
|
245
|
+
El directorio `apps/` está preparado para:
|
|
246
|
+
- `apps/example/` - Ejemplo de bot completo
|
|
247
|
+
- `apps/docs/` - Documentación interactiva
|
|
248
|
+
- `apps/cli/` - CLI extendida para gestión
|
|
249
|
+
|
|
250
|
+
---
|
|
251
|
+
|
|
252
|
+
## Deployment
|
|
253
|
+
|
|
254
|
+
### Docker
|
|
255
|
+
|
|
256
|
+
```bash
|
|
257
|
+
# Build image
|
|
258
|
+
docker build -t mks-telegram-bot .
|
|
259
|
+
|
|
260
|
+
# Run with docker-compose
|
|
261
|
+
docker-compose up bot-production
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
### Multi-Instance
|
|
265
|
+
|
|
266
|
+
El template soporta múltiples instancias simultáneas con detección de conflictos:
|
|
267
|
+
|
|
268
|
+
```bash
|
|
269
|
+
# Ver instancias corriendo
|
|
270
|
+
bun run status
|
|
271
|
+
|
|
272
|
+
# Iniciar en entorno específico
|
|
273
|
+
TG_ENV=staging bun run start
|
|
274
|
+
```
|
|
275
|
+
|
|
276
|
+
---
|
|
277
|
+
|
|
278
|
+
## License
|
|
279
|
+
|
|
280
|
+
MIT
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Build stage
|
|
2
|
+
FROM oven/bun:1.3 AS builder
|
|
3
|
+
WORKDIR /app
|
|
4
|
+
|
|
5
|
+
# Copy package files
|
|
6
|
+
COPY package.json bun.lock ./
|
|
7
|
+
COPY core/package.json core/
|
|
8
|
+
|
|
9
|
+
# Install dependencies
|
|
10
|
+
RUN bun install --frozen-lockfile
|
|
11
|
+
|
|
12
|
+
# Copy TypeScript config and source
|
|
13
|
+
COPY tsconfig.json ./
|
|
14
|
+
COPY core/tsconfig.json core/
|
|
15
|
+
COPY core/src core/src
|
|
16
|
+
|
|
17
|
+
# Production stage
|
|
18
|
+
FROM oven/bun:1.3 AS production
|
|
19
|
+
WORKDIR /app
|
|
20
|
+
|
|
21
|
+
# Copy node_modules from builder
|
|
22
|
+
COPY --from=builder /app/node_modules ./node_modules
|
|
23
|
+
COPY --from=builder /app/core/node_modules ./core/node_modules
|
|
24
|
+
|
|
25
|
+
# Copy source and config files
|
|
26
|
+
COPY --from=builder /app/core/src ./core/src
|
|
27
|
+
COPY --from=builder /app/core/package.json ./core/
|
|
28
|
+
COPY --from=builder /app/core/tsconfig.json ./core/
|
|
29
|
+
|
|
30
|
+
# Create necessary directories
|
|
31
|
+
RUN mkdir -p /app/core/tmp /app/core/logs
|
|
32
|
+
|
|
33
|
+
# Set environment variables
|
|
34
|
+
ENV TG_ENV=production
|
|
35
|
+
ENV NODE_ENV=production
|
|
36
|
+
ENV TG_MODE=webhook
|
|
37
|
+
|
|
38
|
+
# Healthcheck
|
|
39
|
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
|
40
|
+
CMD bun run cli status || exit 1
|
|
41
|
+
|
|
42
|
+
# Expose webhook port
|
|
43
|
+
EXPOSE 3000
|
|
44
|
+
|
|
45
|
+
# Run the bot
|
|
46
|
+
CMD ["bun", "run", "start"]
|