mirror of
https://github.com/cheveguerra/bot-whatsapp.git
synced 2026-04-17 19:26:23 +00:00
feat(provider): 🔥 add twilii (weoking)
This commit is contained in:
8
.vscode/settings.json
vendored
8
.vscode/settings.json
vendored
@@ -1,3 +1,9 @@
|
||||
{
|
||||
"conventionalCommits.scopes": ["hook", "contributing", "cli", "bot"]
|
||||
"conventionalCommits.scopes": [
|
||||
"hook",
|
||||
"contributing",
|
||||
"cli",
|
||||
"bot",
|
||||
"provider"
|
||||
]
|
||||
}
|
||||
|
||||
2
TODO.md
2
TODO.md
@@ -18,7 +18,7 @@
|
||||
- [X] agregar export package
|
||||
- [X] __(doc):__ Video para explicar como implementar nuevos database
|
||||
- [X] Mongo adapter
|
||||
- [ ] MySQL adapter
|
||||
- [X] MySQL adapter
|
||||
- [ ] JsonFile adapter
|
||||
|
||||
### @bot-whatsapp/provider
|
||||
|
||||
@@ -25,6 +25,10 @@ class CoreClass {
|
||||
* Manejador de eventos
|
||||
*/
|
||||
listenerBusEvents = () => [
|
||||
{
|
||||
event: 'preinit',
|
||||
func: () => printer('Iniciando provider espere...'),
|
||||
},
|
||||
{
|
||||
event: 'require_action',
|
||||
func: ({ instructions, title = '⚡⚡ ACCION REQUERIDA ⚡⚡' }) =>
|
||||
|
||||
@@ -25,8 +25,8 @@ const createFlow = (args) => {
|
||||
* @param {*} args
|
||||
* @returns
|
||||
*/
|
||||
const createProvider = (providerClass = class {}) => {
|
||||
const providerInstance = new providerClass()
|
||||
const createProvider = (providerClass = class {}, args = null) => {
|
||||
const providerInstance = new providerClass(args)
|
||||
if (!providerClass.prototype instanceof ProviderClass)
|
||||
throw new Error('El provider no implementa ProviderClass')
|
||||
return providerInstance
|
||||
|
||||
@@ -1,24 +1,18 @@
|
||||
require('dotenv').config()
|
||||
const mysql = require('mysql2')
|
||||
|
||||
const DB_NAME = process.env.DB_NAME || 'db_bot'
|
||||
const DB_HOST = process.env.DB_HOST || 'localhost'
|
||||
const DB_USER = process.env.DB_USER || 'root'
|
||||
|
||||
class MyslAdapter {
|
||||
db
|
||||
listHistory = []
|
||||
credentials = { host: null, user: null, database: null }
|
||||
|
||||
constructor() {
|
||||
constructor(_credentials) {
|
||||
this.credentials = _credentials
|
||||
this.init().then()
|
||||
}
|
||||
|
||||
async init() {
|
||||
this.db = mysql.createConnection({
|
||||
host: DB_HOST,
|
||||
user: DB_USER,
|
||||
database: DB_NAME,
|
||||
})
|
||||
this.db = mysql.createConnection(this.credentials)
|
||||
|
||||
await this.db.connect((error) => {
|
||||
if (!error) {
|
||||
|
||||
@@ -1,19 +1,24 @@
|
||||
const twilio = require('twilio')
|
||||
const { ProviderClass } = require('@bot-whatsapp/bot')
|
||||
|
||||
const TwilioVendor = new twilio(accountSid, authToken)
|
||||
|
||||
const WebHookServer = require('./server')
|
||||
class TwilioProvider extends ProviderClass {
|
||||
constructor() {
|
||||
super(TwilioVendor)
|
||||
vendor
|
||||
vendorNumber
|
||||
constructor({ accountSid, authToken, vendorNumber }) {
|
||||
super()
|
||||
this.vendor = new twilio(accountSid, authToken)
|
||||
this.vendorNumber = vendorNumber
|
||||
new WebHookServer().start()
|
||||
}
|
||||
|
||||
sendMessage = (message) =>
|
||||
this.vendor.messages.create({
|
||||
sendMessage = async (number, message) => {
|
||||
return this.vendor.messages.create({
|
||||
body: message,
|
||||
to: '+12345678901', // Text this number
|
||||
from: '+12345678901', // From a valid Twilio number
|
||||
from: ['whatsapp:', this.vendorNumber].join(''),
|
||||
to: ['whatsapp:', number].join(''),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = TwilioProvider
|
||||
|
||||
21
packages/provider/src/twilio/server.js
Normal file
21
packages/provider/src/twilio/server.js
Normal file
@@ -0,0 +1,21 @@
|
||||
const polka = require('polka')
|
||||
const parsePolka = require('@polka/parse')
|
||||
|
||||
class WebHookServer {
|
||||
incomingMsg = (req, res, next) => {
|
||||
const { body } = req
|
||||
let json = JSON.stringify({ error: 'Missing CSRF token', body })
|
||||
res.end(json)
|
||||
}
|
||||
|
||||
start = () => {
|
||||
polka()
|
||||
.use(parsePolka.urlencoded({ extended: false }))
|
||||
.post('/hook', this.incomingMsg)
|
||||
.listen(3000, () => {
|
||||
console.log(`> Running on localhost:3000 /hook`)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = WebHookServer
|
||||
@@ -32,7 +32,7 @@ class WebWhatsappProvider extends ProviderClass {
|
||||
for (const { event, func } of listEvents) {
|
||||
this.vendor.on(event, func)
|
||||
}
|
||||
|
||||
this.vendor.emit('preinit')
|
||||
this.vendor.initialize().catch((e) => {
|
||||
logger.log(e)
|
||||
this.emit('require_action', {
|
||||
@@ -72,10 +72,6 @@ class WebWhatsappProvider extends ProviderClass {
|
||||
event: 'ready',
|
||||
func: () => this.emit('ready', true),
|
||||
},
|
||||
{
|
||||
event: 'authenticated',
|
||||
func: () => this.emit('ready', true),
|
||||
},
|
||||
{
|
||||
event: 'message',
|
||||
func: (payload) => {
|
||||
|
||||
Reference in New Issue
Block a user