Files
bot-whatsapp/packages/provider/src/twilio/server.js
2022-12-06 21:28:52 +01:00

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