From 55ec49ac46d5d4398817148f5d053a50a68a9cb9 Mon Sep 17 00:00:00 2001 From: Alex Craviotto Date: Wed, 6 Sep 2023 18:56:05 +0200 Subject: [PATCH 1/2] feat(index): sendTemplate add --- index.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/index.js b/index.js index d982d0d..99f095f 100644 --- a/index.js +++ b/index.js @@ -121,6 +121,21 @@ class WhatsappCloud { } }; + this._mustHaveTemplateName = (templateName) => { + if (!templateName) { + throw new Error('"templateName" is required in making a request'); + } + }; + this._mustHaveComponents = (components) => { + if (!components) { + throw new Error('"components" is required in making a request'); + } + }; + this._mustHaveLanguageCode = (languageCode) => { + if (!languageCode) { + throw new Error('"languageCode" is required in making a request'); + } + }; this._mustHaveMessageId = (messageId) => { if (!messageId) { throw new Error('"messageId" is required in making a request'); @@ -244,6 +259,34 @@ class WhatsappCloud { return response; } + async sendTemplate({ templateName,languageCode,components,recipientPhone }) { + + this._mustHaverecipientPhone(recipientPhone); + this._mustHaveTemplateName(templateName); + this._mustHaveComponents(components) + this.languageCode(languageCode) + let body = { + "messaging_product": "whatsapp", + "recipient_type": "individual", + "to": recipientPhone, + "type": "template", + "template": { + "name": templateName, + "language": { + "code": languageCode + }, + "components": components + } + }; + + let response = await this._fetchAssistant({ + url: '/messages', + method: 'POST', + body, + }); + + return response; + } async markMessageAsRead({ message_id }) { try { From 40e48696e899cfda8dd5bd0e385b6a644b7fee21 Mon Sep 17 00:00:00 2001 From: Alex Craviotto Date: Wed, 6 Sep 2023 19:07:03 +0200 Subject: [PATCH 2/2] fix(index): check language code --- index.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 99f095f..7c528df 100644 --- a/index.js +++ b/index.js @@ -259,12 +259,11 @@ class WhatsappCloud { return response; } - async sendTemplate({ templateName,languageCode,components,recipientPhone }) { - + async sendTemplate({templateName,languageCode,components,recipientPhone} ) { this._mustHaverecipientPhone(recipientPhone); this._mustHaveTemplateName(templateName); this._mustHaveComponents(components) - this.languageCode(languageCode) + this._mustHaveLanguageCode(languageCode) let body = { "messaging_product": "whatsapp", "recipient_type": "individual",