mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-17 19:26:23 +00:00
feat(provider): added twilio provider
This commit is contained in:
3
TODO.md
3
TODO.md
@@ -2,6 +2,7 @@
|
||||
- [X] __(doc)__ Video de como colaborar PR
|
||||
- [ ] __(doc)__ Video implementación de test y cobertura
|
||||
- [ ] __(doc)__ Video explicacion de github action
|
||||
- [ ] Crear packages list externas
|
||||
|
||||
### @bot-whatsapp/bot
|
||||
- [X] agregar export package
|
||||
@@ -13,7 +14,7 @@
|
||||
- [ ] colocar mensaje esperando conectando whatsapp (provider)
|
||||
- [ ] createDatabase validar implementacion de funciones
|
||||
- [ ] limitar caracteres de mensajes
|
||||
- [ ] cuando envias numeros (5 o 1) se dispara el flujo
|
||||
- [X] cuando envias numeros (5 o 1) se dispara el flujo
|
||||
|
||||
### @bot-whatsapp/database
|
||||
- [X] agregar export package
|
||||
|
||||
@@ -1,24 +1,59 @@
|
||||
const twilio = require('twilio')
|
||||
const { ProviderClass } = require('@bot-whatsapp/bot')
|
||||
|
||||
const WebHookServer = require('./server')
|
||||
const TwilioWebHookServer = require('./server')
|
||||
const { parseNumber } = require('./utils')
|
||||
|
||||
/**
|
||||
* { accountSid, authToken, vendorNumber }
|
||||
*/
|
||||
class TwilioProvider extends ProviderClass {
|
||||
twilioHook
|
||||
vendor
|
||||
vendorNumber
|
||||
constructor({ accountSid, authToken, vendorNumber }) {
|
||||
super()
|
||||
this.vendor = new twilio(accountSid, authToken)
|
||||
this.twilioHook = new TwilioWebHookServer()
|
||||
this.vendorNumber = vendorNumber
|
||||
new WebHookServer().start()
|
||||
|
||||
this.twilioHook.start()
|
||||
const listEvents = this.busEvents()
|
||||
|
||||
for (const { event, func } of listEvents) {
|
||||
this.twilioHook.on(event, func)
|
||||
}
|
||||
}
|
||||
|
||||
sendMessage = async (number, message) => {
|
||||
return this.vendor.messages.create({
|
||||
body: message,
|
||||
from: ['whatsapp:', this.vendorNumber].join(''),
|
||||
to: ['whatsapp:', number].join(''),
|
||||
from: ['whatsapp:+', parseNumber(this.vendorNumber)].join(''),
|
||||
to: ['whatsapp:+', parseNumber(number)].join(''),
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Mapeamos los eventos nativos de whatsapp-web.js a los que la clase Provider espera
|
||||
* para tener un standar de eventos
|
||||
* @returns
|
||||
*/
|
||||
busEvents = () => [
|
||||
{
|
||||
event: 'auth_failure',
|
||||
func: (payload) => this.emit('error', payload),
|
||||
},
|
||||
{
|
||||
event: 'ready',
|
||||
func: () => this.emit('ready', true),
|
||||
},
|
||||
{
|
||||
event: 'message',
|
||||
func: (payload) => {
|
||||
this.emit('message', payload)
|
||||
},
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
module.exports = TwilioProvider
|
||||
|
||||
@@ -1,16 +1,23 @@
|
||||
const { EventEmitter } = require('node:events')
|
||||
const polka = require('polka')
|
||||
const parsePolka = require('@polka/parse')
|
||||
const { urlencoded } = require('body-parser')
|
||||
const { parseNumber } = require('./utils')
|
||||
|
||||
class WebHookServer {
|
||||
incomingMsg = (req, res, next) => {
|
||||
class TwilioWebHookServer extends EventEmitter {
|
||||
incomingMsg = (req, res) => {
|
||||
const { body } = req
|
||||
let json = JSON.stringify({ error: 'Missing CSRF token', body })
|
||||
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(parsePolka.urlencoded({ extended: false }))
|
||||
.use(urlencoded({ extended: true }))
|
||||
.post('/hook', this.incomingMsg)
|
||||
.listen(3000, () => {
|
||||
console.log(`> Running on localhost:3000 /hook`)
|
||||
@@ -18,4 +25,4 @@ class WebHookServer {
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = WebHookServer
|
||||
module.exports = TwilioWebHookServer
|
||||
|
||||
5
packages/provider/src/twilio/utils.js
Normal file
5
packages/provider/src/twilio/utils.js
Normal file
@@ -0,0 +1,5 @@
|
||||
const parseNumber = (number) => {
|
||||
return `${number}`.replace('whatsapp:', '').replace('+', '')
|
||||
}
|
||||
|
||||
module.exports = { parseNumber }
|
||||
Reference in New Issue
Block a user