mirror of
https://github.com/cheveguerra/api-whatsapp-ts.git
synced 2026-04-18 03:39:19 +00:00
add meta, twilio
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import LeadExternal from "../domain/lead.external";
|
||||
import LeadExternal from "../domain/lead-external.repository";
|
||||
import LeadRepository from "../domain/lead.repository";
|
||||
|
||||
export class LeadCreate {
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { Lead } from "./lead";
|
||||
|
||||
/**
|
||||
* Esta la interfaz que debe de cumplir el repositorio de infraestructura
|
||||
* mysql o mongo o etc
|
||||
*/
|
||||
export default interface LeadRepository {
|
||||
save({
|
||||
message,
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import { ContainerBuilder } from "node-dependency-injection";
|
||||
import { LeadCreate } from "../application/lead.create";
|
||||
import LeadCtrl from "./controller/lead.ctrl";
|
||||
import MetaRepository from "./repositories/meta.repository";
|
||||
import MockRepository from "./repositories/mock.repository";
|
||||
import TwilioService from "./repositories/twilio.repository";
|
||||
import WsTransporter from "./repositories/ws.external";
|
||||
|
||||
const container = new ContainerBuilder();
|
||||
@@ -9,7 +11,7 @@ const container = new ContainerBuilder();
|
||||
/**
|
||||
* Inicamos servicio de WS / Bot / Twilio
|
||||
*/
|
||||
container.register("ws.transporter", WsTransporter);
|
||||
container.register("ws.transporter", MetaRepository);
|
||||
const wsTransporter = container.get("ws.transporter");
|
||||
|
||||
container.register("db.repository", MockRepository);
|
||||
|
||||
47
src/infrastructure/repositories/meta.repository.ts
Normal file
47
src/infrastructure/repositories/meta.repository.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import "dotenv/config";
|
||||
import axios from "axios";
|
||||
import LeadExternal from "../../domain/lead-external.repository";
|
||||
|
||||
const META_TOKEN = process.env.META_TOKEN || "";
|
||||
const META_ID_NUMBER = process.env.META_ID_NUMBER || "";
|
||||
const URL = `https://graph.facebook.com/v13.0/${META_ID_NUMBER}/messages`;
|
||||
|
||||
export default class MetaRepository implements LeadExternal {
|
||||
async sendMsg({
|
||||
message,
|
||||
phone,
|
||||
}: {
|
||||
message: string;
|
||||
phone: string;
|
||||
}): Promise<any> {
|
||||
try{
|
||||
const body = this.parseBody({message, phone})
|
||||
const response = await axios.post(URL,body, {
|
||||
headers: {
|
||||
Authorization: `Bearer ${META_TOKEN}`,
|
||||
},
|
||||
}) as any;
|
||||
|
||||
return response.data
|
||||
}catch(e){
|
||||
return Promise.resolve(e)
|
||||
}
|
||||
}
|
||||
|
||||
private parseBody ({message, phone}:{message:string,phone:string}){
|
||||
const body = {
|
||||
"messaging_product": "whatsapp",
|
||||
"to": phone,
|
||||
"type": "template",
|
||||
"template": {
|
||||
"name": "hello_world",
|
||||
"language": {
|
||||
"code": "en_US"
|
||||
}
|
||||
}
|
||||
}
|
||||
return body
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,8 +1,31 @@
|
||||
import LeadExternal from "../../domain/lead.external";
|
||||
import "dotenv/config";
|
||||
import { Twilio } from "twilio";
|
||||
import LeadExternal from "../../domain/lead-external.repository";
|
||||
|
||||
export class TwilioService implements LeadExternal {
|
||||
sendMsg({ message, phone }: { message: string; phone: string; }): Promise<any> {
|
||||
return Promise.resolve('SE_ENVIO_CORRECTAMENTE')
|
||||
const accountSid = process.env.TWILIO_SID || "";
|
||||
const authToken = process.env.TWILIO_TOKEN || "";
|
||||
const fromNumber = process.env.TWILIO_FROM || "";
|
||||
|
||||
export default class TwilioService extends Twilio implements LeadExternal {
|
||||
constructor() {
|
||||
super(accountSid, authToken);
|
||||
}
|
||||
async sendMsg({
|
||||
message,
|
||||
phone,
|
||||
}: {
|
||||
message: string;
|
||||
phone: string;
|
||||
}): Promise<any> {
|
||||
try{
|
||||
const parsePhone = `+${phone}`
|
||||
const mapMsg = { body: message, to: parsePhone, from:fromNumber };
|
||||
const response = await this.messages.create(mapMsg);
|
||||
return response
|
||||
}catch(e){
|
||||
console.log(e)
|
||||
return Promise.reject(e)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Client, LocalAuth } from "whatsapp-web.js";
|
||||
import { image as imageQr } from "qr-image";
|
||||
import LeadExternal from "../../domain/lead.external";
|
||||
import LeadExternal from "../../domain/lead-external.repository";
|
||||
|
||||
/**
|
||||
* Extendemos los super poderes de whatsapp-web
|
||||
@@ -28,7 +28,10 @@ class WsTransporter extends Client implements LeadExternal {
|
||||
console.log("LOGIN_FAIL");
|
||||
});
|
||||
|
||||
this.on("qr", (qr) => this.generateImage(qr));
|
||||
this.on("qr", (qr) => {
|
||||
console.log('Escanea el codigo QR que esta en la carepta tmp')
|
||||
this.generateImage(qr)
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user