Repository navigation
Expand file tree
/
Copy pathhandler.js
More file actions
51 lines (40 loc) · 1.68 KB
/
Copy pathhandler.js
File metadata and controls
51 lines (40 loc) · 1.68 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
// handler.js
const { getOrCreateUser } = require('./db')
const { checkBalanceAndMaybeWarn } = require('./balance')
const { sendDM } = require('./nostr')
const { routeToModule } = require('./router') // handles /gratitude, etc.
const { parseEvent } = require('./utils')
const { handleCommand } = require('./commands')
exports.handler = async (event) => {
console.log('🔔 Lambda triggered with event:', JSON.stringify(event))
try {
const { pubkey, message } = parseEvent(event)
console.log(`📩 Parsed event - pubkey: ${pubkey}, message: "${message}"`)
const user = await getOrCreateUser(pubkey)
console.log(`👤 Fetched user:`, user)
if (user.is_paused) {
console.log('⏸️ User is paused. Skipping processing.')
return { statusCode: 200, body: 'User is paused.' }
}
const commandReply = await handleCommand(pubkey, message)
if (commandReply) {
console.log('💬 Responding to command with:', commandReply)
await sendDM(pubkey, commandReply)
return { statusCode: 200, body: 'Command processed' }
}
const reply = await routeToModule({ message, user })
console.log('🧠 Module reply generated:', reply)
await sendDM(pubkey, reply)
console.log('📤 Reply sent to user.')
const warnMessage = await checkBalanceAndMaybeWarn(user)
if (warnMessage) {
console.log('⚠️ Sending balance warning:', warnMessage)
await sendDM(pubkey, warnMessage)
}
console.log('✅ Lambda completed successfully.')
return { statusCode: 200, body: 'Message processed' }
} catch (err) {
console.error('❌ Uncaught error in handler:', err)
return { statusCode: 500, body: 'Error handling message' }
}
}