-
Notifications
You must be signed in to change notification settings - Fork 10
/
server.js
146 lines (127 loc) · 3.62 KB
/
server.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import path from 'path'
import fs from 'fs/promises'
import { createReadStream } from 'fs'
import express from 'express'
import bodyParser from 'body-parser'
import config from './config.js'
import * as bot from './bot.js'
import * as actions from './actions.js'
// Create web server
const app = express()
// Middleware to parse incoming request bodies
app.use(bodyParser.json())
// Index route
app.get('/', (req, res) => {
res.send({
name: 'chatbot',
description: 'WhatsApp ChatGPT powered chatbot for Wassenger',
endpoints: {
webhook: {
path: '/webhook',
method: 'POST'
},
sendMessage: {
path: '/message',
method: 'POST'
},
sample: {
path: '/sample',
method: 'GET'
}
}
})
})
// POST route to handle incoming webhook messages
app.post('/webhook', (req, res) => {
const { body } = req
if (!body || !body.event || !body.data) {
return res.status(400).send({ message: 'Invalid payload body' })
}
if (body.event !== 'message:in:new') {
return res.status(202).send({ message: 'Ignore webhook event: only message:in:new is accepted' })
}
// Send immediate response to acknowledge the webhook event
res.send({ ok: true })
// Process message response in background
bot.processMessage(body).catch(err => {
console.error('[error] failed to process inbound message:', body.id, body.data.fromNumber, body.data.body, err)
})
})
// Send message on demand
app.post('/message', (req, res) => {
const { body } = req
if (!body || !body.phone || !body.message) {
return res.status(400).send({ message: 'Invalid payload body' })
}
actions.sendMessage(body).then((data) => {
res.send(data)
}).catch(err => {
res.status(+err.status || 500).send(err.response
? err.response.data
: {
message: 'Failed to send message'
})
})
})
// Send a sample message to your own number, or to a number specified in the query string
app.get('/sample', (req, res) => {
const { phone, message } = req.query
const data = {
phone: phone || app.device.phone,
message: message || 'Hello World from Wassenger!',
device: app.device.id
}
actions.sendMessage(data).then((data) => {
res.send(data)
}).catch(err => {
res.status(+err.status || 500).send(err.response
? err.response.data
: {
message: 'Failed to send sample message'
})
})
})
async function fileExists (filepath) {
try {
await fs.access(filepath, fs.constants.F_OK)
return true
} catch (err) {
return false
}
}
async function fileSize (filepath) {
try {
const stat = await fs.stat(filepath)
return stat.size
} catch (err) {
return -1
}
}
app.get('/files/:id', async (req, res) => {
if (!(/^[a-f0-9]{15,18}$/i.test(req.params.id))) {
return res.status(400).send({ message: 'Invalid ID' })
}
const filename = `${req.params.id}.mp3`
const filepath = path.join(config.tempPath, filename)
if (!(await fileExists(filepath))) {
return res.status(404).send({ message: 'Invalid or deleted file' })
}
const size = await fileSize(filepath)
if (!size) {
return res.status(404).send({ message: 'Invalid or deleted file' })
}
res.set('Content-Length', size)
res.set('Content-Type', 'application/octet-stream')
createReadStream(filepath).pipe(res)
res.once('close', () => {
fs.unlink(filepath).catch(err => {
console.error('[error] failed to delete temp file:', filepath, err.message)
})
})
})
app.use((err, req, res, next) => {
res.status(+err.status || 500).send({
message: `Unexpected error: ${err.message}`
})
})
export default app