mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-17 19:26:23 +00:00
29 lines
797 B
JavaScript
29 lines
797 B
JavaScript
const { EventEmitter } = require('node:events')
|
|
const polka = require('polka')
|
|
const { urlencoded } = require('body-parser')
|
|
const { parseNumber } = require('./utils')
|
|
|
|
class TwilioWebHookServer extends EventEmitter {
|
|
incomingMsg = (req, res) => {
|
|
const { body } = req
|
|
this.emit('message', {
|
|
from: parseNumber(body.From),
|
|
to: parseNumber(body.To),
|
|
body: body.Body,
|
|
})
|
|
const json = JSON.stringify({ body })
|
|
res.end(json)
|
|
}
|
|
|
|
start = () => {
|
|
polka()
|
|
.use(urlencoded({ extended: true }))
|
|
.post('/hook', this.incomingMsg)
|
|
.listen(3000, () => {
|
|
console.log(`> Running on localhost:3000 /hook`)
|
|
})
|
|
}
|
|
}
|
|
|
|
module.exports = TwilioWebHookServer
|