feat(provider): 🔥 add twilii (weoking)

This commit is contained in:
Leifer Mendez
2022-12-05 20:45:05 +01:00
parent 30e3d443bb
commit 4350dff22a
8 changed files with 53 additions and 27 deletions

View File

@@ -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

View 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

View File

@@ -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) => {