-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (32 loc) · 1.14 KB
/
index.js
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
const express = require('express')
const { PORT } = require("./utils/env")
const { getOpenAIResponse } = require("./services/openai")
const { getExtractedInfos, sendMessageToWhatsApp } = require("./services/whatsapp-business")
const app = express()
app.use(express.json())
const processWhatsAppBusinessMessages = async (message) => {
const decodedValue = atob(message.data);
const data = JSON.parse(decodedValue);
const { text, from, to } = await getExtractedInfos(data)
if (!text || !text.length) {
console.error('No text to process')
return
}
const aiChoices = await getOpenAIResponse(text)
for (const choice of aiChoices) {
const aiResponse = choice.message.content
await sendMessageToWhatsApp(aiResponse, from, to)
}
}
app.post('/receive-push', async (req, res) => {
try {
const { body } = req
const { message } = body
await processWhatsAppBusinessMessages(message)
res.sendStatus(200)
} catch (error) {
console.error('Receive push error', error)
res.sendStatus(500)
}
})
app.listen(PORT, () => console.log(`Running in ${PORT}`))