forked from codigoencasa/bot-whatsapp
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(provider): meta provider is added
- Loading branch information
1 parent
0105dab
commit 438607c
Showing
4 changed files
with
189 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
const { ProviderClass } = require('@bot-whatsapp/bot') | ||
const axios = require('axios') | ||
const MetaWebHookServer = require('./server') | ||
const URL = `https://graph.facebook.com/v15.0` | ||
|
||
/** | ||
* 鈿欙笍MetaProvider: Es un provedor que te ofrece enviar | ||
* mensaje a Whatsapp via API | ||
* info: https://developers.facebook.com/docs/whatsapp/cloud-api/reference/messages | ||
* | ||
* | ||
* Necesitas las siguientes tokens y valores | ||
* { token, numberId, vendorNumber, verify_token } | ||
*/ | ||
|
||
class MetaProvider extends ProviderClass { | ||
metHook | ||
token | ||
numberId | ||
constructor({ token, numberId, verifyToken }, _port = 3000) { | ||
super() | ||
this.token = token | ||
this.numberId = numberId | ||
this.metHook = new MetaWebHookServer(verifyToken, _port) | ||
this.metHook.start() | ||
|
||
const listEvents = this.busEvents() | ||
|
||
for (const { event, func } of listEvents) { | ||
this.metHook.on(event, func) | ||
} | ||
} | ||
|
||
/** | ||
* 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) | ||
}, | ||
}, | ||
] | ||
|
||
async sendMessageMeta(body) { | ||
try { | ||
const response = await axios.post( | ||
`${URL}/${this.numberId}/messages`, | ||
body, | ||
{ | ||
headers: { | ||
Authorization: `Bearer ${this.token}`, | ||
}, | ||
} | ||
) | ||
return response.data | ||
} catch (error) { | ||
return Promise.resolve(error) | ||
} | ||
} | ||
|
||
async sendtext(number, message) { | ||
const body = { | ||
messaging_product: 'whatsapp', | ||
to: number, | ||
type: 'text', | ||
text: { | ||
preview_url: false, | ||
body: message, | ||
}, | ||
} | ||
await this.sendMessageMeta(body) | ||
} | ||
|
||
async sendMedia(number, message, mediaInput = null) { | ||
if (!mediaInput) throw new Error(`MEDIA_INPUT_NULL_: ${mediaInput}`) | ||
const body = { | ||
messaging_product: 'whatsapp', | ||
to: number, | ||
type: 'image', | ||
image: { | ||
link: mediaInput, | ||
}, | ||
} | ||
await this.sendMessageMeta(body) | ||
} | ||
|
||
/** | ||
* | ||
* @param {*} userId | ||
* @param {*} message | ||
* @param {*} param2 | ||
* @returns | ||
*/ | ||
sendMessage = async (number, message, { options }) => { | ||
if (options?.buttons?.length) return console.log('Envio de botones') | ||
if (options?.media) | ||
return this.sendMedia(number, message, options.media) | ||
|
||
this.sendtext(number, message) | ||
} | ||
} | ||
|
||
module.exports = MetaProvider |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
const { EventEmitter } = require('node:events') | ||
const polka = require('polka') | ||
const { urlencoded } = require('body-parser') | ||
|
||
class MetaWebHookServer extends EventEmitter { | ||
metaServer | ||
metaPort | ||
verifyToken | ||
constructor(_verifyToken, _metaPort) { | ||
super() | ||
this.metaServer = this.buildHTTPServer() | ||
this.metaPort = _metaPort | ||
this.verifyToken = _verifyToken | ||
} | ||
|
||
/** | ||
* Mensaje entrante | ||
* emit: 'message' | ||
* @param {*} req | ||
* @param {*} res | ||
*/ | ||
incomingMsg = (req, res) => { | ||
const { body } = req | ||
const message = body.entry[0].changes[0].value.messages[0] | ||
const to = body.entry[0].changes[0].value.metadata.display_phone_number | ||
this.emit('message', { | ||
from: message.from, | ||
to, | ||
body: message.text.body, | ||
}) | ||
const json = JSON.stringify({ body }) | ||
res.end(json) | ||
} | ||
|
||
/** | ||
* Contruir HTTP Server | ||
* @returns | ||
*/ | ||
buildHTTPServer = () => { | ||
return polka() | ||
.use(urlencoded({ extended: true })) | ||
.post('/meta-hook', this.incomingMsg) | ||
} | ||
|
||
/** | ||
* Puerto del HTTP | ||
* @param {*} port default 3000 | ||
*/ | ||
start = () => { | ||
this.metaServer.listen(this.metaPort, () => { | ||
console.log(``) | ||
console.log(`[meta]: Agregar esta url "WHEN A MESSAGE COMES IN"`) | ||
console.log( | ||
`[meta]: POST http://localhost:${this.metaPort}/meta-hook` | ||
) | ||
console.log(`[meta]: M谩s informaci贸n en la documentacion`) | ||
console.log(``) | ||
}) | ||
this.emit('ready') | ||
} | ||
} | ||
|
||
module.exports = MetaWebHookServer |