UNPKG

@brighthustle/adonisjs-whatsapp

Version:

Connect your WhatsApp Cloud API with AdonisJS

236 lines (235 loc) 7.85 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WhatsAppCloudApi = void 0; const fs = __importStar(require("fs")); const mime_types_1 = __importDefault(require("mime-types")); const Helpers_1 = __importDefault(require("./Helpers")); const form_data_1 = __importDefault(require("form-data")); const WhatsAppClient_1 = __importDefault(require("./WhatsAppClient")); class WhatsAppCloudApi { constructor(config, drive, emitter, db) { this.config = config; this.drive = drive; this.emitter = emitter; this.db = db; this.client = new WhatsAppClient_1.default(this.config, this.db); } async sendText(to, text, options, from) { return await this.client.send({ to, from, type: 'text', text: { preview_url: options?.preview_url || false, body: text, }, }); } async sendImage(to, media, options, from) { return await this.client.send({ to, from, type: 'image', image: { ...(Helpers_1.default.isUrl(media) ? { link: media } : { id: media }), ...options, }, }); } async sendDocument(to, media, options, from) { return await this.client.send({ to, from, type: 'document', document: { ...(Helpers_1.default.isUrl(media) ? { link: media } : { id: media }), ...options, }, }); } async sendAudio(to, media, from) { return await this.client.send({ to, type: 'audio', from, audio: { ...(Helpers_1.default.isUrl(media) ? { link: media } : { id: media }), }, }); } async sendVideo(to, media, options, from) { return await this.client.send({ to, from, type: 'video', video: { ...(Helpers_1.default.isUrl(media) ? { link: media } : { id: media }), ...options, }, }); } async sendSticker(to, media, from) { return await this.client.send({ to, from, type: 'sticker', sticker: { ...(Helpers_1.default.isUrl(media) ? { link: media } : { id: media }), }, }); } async sendLocation(to, coordinate, options, from) { return await this.client.send({ to, from, type: 'location', location: { ...coordinate, ...options, }, }); } async sendTemplate(to, template, language, components, from) { return await this.client.send({ to, from, type: 'template', template: { name: template, language: { code: language, }, components, }, }); } async sendContact(to, contacts, from) { return await this.client.send({ to, from, type: 'contacts', contacts, }); } async sendButtons(to, text, buttons, options, from) { return await this.client.send({ to, from, type: 'interactive', interactive: { type: 'button', body: { text }, ...(options?.footer ? { footer: { text: options?.footer } } : {}), header: options?.header, action: { buttons: Object.keys(buttons).map((key) => { return { type: 'reply', reply: { id: key, title: buttons[key], }, }; }), }, }, }); } async sendList(to, text, button, sections, options, from) { return await this.client.send({ to, from, type: 'interactive', interactive: { type: 'list', body: { text }, ...(options?.footer ? { footer: { text: options?.footer } } : {}), header: options?.header, action: { button, sections, }, }, }); } async markAsRead(wamid) { const response = await this.client.send({ status: 'read', message_id: wamid, }, false); return response?.success || false; } on(event, handler) { this.emitter.on(`wa:${event}`, (message) => { handler(message); }); } async uploadMedia(source, from) { source = typeof source !== 'string' ? source.tmpPath : source; const form = new form_data_1.default(); form.append('messaging_product', 'whatsapp'); form.append('type', mime_types_1.default.contentType(source)); form.append('file', fs.readFileSync(source), { filename: source }); const response = await this.client.upload(form, from); return response.id || false; } async downloadMedia(media, options, from) { const response = await this.client.media(media, from); if (!response.url || !response.mime_type) return false; const ext = mime_types_1.default.extension(response.mime_type); const filename = options?.filename || media + (ext ? '.' + ext : ''); const filepath = options?.folder ? options.folder + '/' + filename : filename; const file = await this.client.download(response.url, from); if (options?.disk) { const disk = this.drive.use(options.disk); await disk.put(filepath, file); } else { await this.drive.put(filepath, file); } return filename; } async createTemplate(category, name, language, components, from) { return await this.client.createTemplate({ category, name, language, components, from, }); } async getTemplates(options, from) { return await this.client.getTemplates(options, from); } async deleteTemplate(name, from) { return await this.client.deleteTemplate(name, from); } } exports.WhatsAppCloudApi = WhatsAppCloudApi;