mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-21 13:09:16 +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
|
- [X] __(doc)__ Video de como colaborar PR
|
||||||
- [ ] __(doc)__ Video implementación de test y cobertura
|
- [ ] __(doc)__ Video implementación de test y cobertura
|
||||||
- [ ] __(doc)__ Video explicacion de github action
|
- [ ] __(doc)__ Video explicacion de github action
|
||||||
|
- [ ] Crear packages list externas
|
||||||
|
|
||||||
### @bot-whatsapp/bot
|
### @bot-whatsapp/bot
|
||||||
- [X] agregar export package
|
- [X] agregar export package
|
||||||
@@ -13,7 +14,7 @@
|
|||||||
- [ ] colocar mensaje esperando conectando whatsapp (provider)
|
- [ ] colocar mensaje esperando conectando whatsapp (provider)
|
||||||
- [ ] createDatabase validar implementacion de funciones
|
- [ ] createDatabase validar implementacion de funciones
|
||||||
- [ ] limitar caracteres de mensajes
|
- [ ] 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
|
### @bot-whatsapp/database
|
||||||
- [X] agregar export package
|
- [X] agregar export package
|
||||||
|
|||||||
@@ -1,24 +1,59 @@
|
|||||||
const twilio = require('twilio')
|
const twilio = require('twilio')
|
||||||
const { ProviderClass } = require('@bot-whatsapp/bot')
|
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 {
|
class TwilioProvider extends ProviderClass {
|
||||||
|
twilioHook
|
||||||
vendor
|
vendor
|
||||||
vendorNumber
|
vendorNumber
|
||||||
constructor({ accountSid, authToken, vendorNumber }) {
|
constructor({ accountSid, authToken, vendorNumber }) {
|
||||||
super()
|
super()
|
||||||
this.vendor = new twilio(accountSid, authToken)
|
this.vendor = new twilio(accountSid, authToken)
|
||||||
|
this.twilioHook = new TwilioWebHookServer()
|
||||||
this.vendorNumber = vendorNumber
|
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) => {
|
sendMessage = async (number, message) => {
|
||||||
return this.vendor.messages.create({
|
return this.vendor.messages.create({
|
||||||
body: message,
|
body: message,
|
||||||
from: ['whatsapp:', this.vendorNumber].join(''),
|
from: ['whatsapp:+', parseNumber(this.vendorNumber)].join(''),
|
||||||
to: ['whatsapp:', number].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
|
module.exports = TwilioProvider
|
||||||
|
|||||||
@@ -1,16 +1,23 @@
|
|||||||
|
const { EventEmitter } = require('node:events')
|
||||||
const polka = require('polka')
|
const polka = require('polka')
|
||||||
const parsePolka = require('@polka/parse')
|
const { urlencoded } = require('body-parser')
|
||||||
|
const { parseNumber } = require('./utils')
|
||||||
|
|
||||||
class WebHookServer {
|
class TwilioWebHookServer extends EventEmitter {
|
||||||
incomingMsg = (req, res, next) => {
|
incomingMsg = (req, res) => {
|
||||||
const { body } = req
|
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)
|
res.end(json)
|
||||||
}
|
}
|
||||||
|
|
||||||
start = () => {
|
start = () => {
|
||||||
polka()
|
polka()
|
||||||
.use(parsePolka.urlencoded({ extended: false }))
|
.use(urlencoded({ extended: true }))
|
||||||
.post('/hook', this.incomingMsg)
|
.post('/hook', this.incomingMsg)
|
||||||
.listen(3000, () => {
|
.listen(3000, () => {
|
||||||
console.log(`> Running on localhost:3000 /hook`)
|
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