UNPKG

@brighthustle/adonisjs-whatsapp

Version:

Connect your WhatsApp Cloud API with AdonisJS

90 lines (89 loc) 3.25 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class Helpers { static isUrl(string) { const pattern = new RegExp('^(https?:\\/\\/)?' + '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + '((\\d{1,3}\\.){3}\\d{1,3}))' + '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + '(\\?[;&a-z\\d%_.~+=-]*)?' + '(\\#[-a-z\\d_]*)?$', 'i'); return !!pattern.test(string); } static translateType(type) { const types = { button_reply: 'button', list_reply: 'list', }; return types[type] || type; } static translateInteractive(message) { if (!message || !message.type || message.type !== 'interactive') return null; return { type: message.interactive.type, data: message.interactive[message.interactive.type], }; } static translateInteractiveMsg91(message) { if (!message || !message.content_type || message.content_type !== 'interactive') return null; return { type: message.interactive.type, data: message.interactive[message.interactive.type], }; } static transformToMsg91SendTemplate(integrated_number, name, language, components, to) { const toAndComponents = { to: [`91${to.toString()}`], components: {}, }; components.forEach((component) => { component.parameters.forEach((parameter, index) => { const componentKey = `${component.type}_${index + 1}`; toAndComponents.components[componentKey] = { type: parameter.type, value: this.extractParameterValue(parameter), ...(component.type === 'button' && component.sub_type ? { sub_type: component.sub_type } : {}), }; }); }); return { integrated_number, content_type: 'template', payload: { type: 'template', messaging_product: 'whatsapp', template: { name, language: { code: language, policy: 'deterministic', // Adjust as needed }, to_and_components: [toAndComponents], }, }, }; } static extractParameterValue(parameter) { switch (parameter.type) { case 'text': return parameter.text || ''; case 'payload': return parameter.payload || ''; case 'currency': return `${parameter.currency?.amount_1000} ${parameter.currency?.code}` || ''; case 'date_time': return `${parameter.date_time?.fallback_value}` || ''; case 'image': case 'document': case 'video': return parameter[parameter.type]?.link || ''; // Assuming you want to extract the URL of the media default: return ''; } } } exports.default = Helpers;