From 64ed54a3a5c14502e162cb20a02f4f660ce7f49a Mon Sep 17 00:00:00 2001 From: MARCELO DOS SANTOS DE OLIVEIRA <69150213+marcelo386@users.noreply.github.com> Date: Tue, 14 Nov 2023 22:47:32 -0300 Subject: [PATCH] feat: Added newsletter functions (#1893) * feat: Added createNewsletter functions * fix: Fixed Version Default wajs 2.27.0 * feat: Added createNewsletter functions * fix: Fixed create newsletter functions and added more function --------- Co-authored-by: marcelo-megaonline <81028641+marcelo-megaonline@users.noreply.github.com> Co-authored-by: Cleiton Carvalho --- examples/basic/index.js | 6 +- examples/newsletter/.gitignore | 4 ++ examples/newsletter/README.md | 13 ++++ examples/newsletter/index.js | 42 +++++++++++ examples/newsletter/package.json | 11 +++ src/api/layers/community.layer.ts | 4 +- src/api/layers/newsletter.layer.ts | 112 +++++++++++++++++++++++++++++ 7 files changed, 186 insertions(+), 6 deletions(-) create mode 100644 examples/newsletter/.gitignore create mode 100644 examples/newsletter/README.md create mode 100644 examples/newsletter/index.js create mode 100644 examples/newsletter/package.json create mode 100644 src/api/layers/newsletter.layer.ts diff --git a/examples/basic/index.js b/examples/basic/index.js index 58ad33de2..b2c1eb0ad 100644 --- a/examples/basic/index.js +++ b/examples/basic/index.js @@ -28,12 +28,10 @@ function start(client) { if (message.body === 'Hi' && message.isGroupMsg === false) { client .sendText(message.from, 'Welcome Wppconnect') - .then((result) => { - console.log('Result: ', result); //return object success - }) + .then((result) => {}) .catch((erro) => { console.error('Error when sending: ', erro); //return object error }); } }); -} \ No newline at end of file +} diff --git a/examples/newsletter/.gitignore b/examples/newsletter/.gitignore new file mode 100644 index 000000000..27826cf8a --- /dev/null +++ b/examples/newsletter/.gitignore @@ -0,0 +1,4 @@ +node_modules +tokens +package-lock.json +debug.log \ No newline at end of file diff --git a/examples/newsletter/README.md b/examples/newsletter/README.md new file mode 100644 index 000000000..1238961d9 --- /dev/null +++ b/examples/newsletter/README.md @@ -0,0 +1,13 @@ +# Simple example to use functions of newsletter + +> Wppconnect is a high-performance system developed with JavaScript to create a bot for WhatsApp, support for creating any interaction, such as customer service, media sending, sentence recognition based on artificial intelligence and all types of design architecture for WhatsApp. + +## Maintainers + +Maintainers are needed, I cannot keep with all the updates by myself. If you are +interested please open a Pull Request. + +## Contributing + +Pull requests are welcome. For major changes, please open an issue first to +discuss what you would like to change. diff --git a/examples/newsletter/index.js b/examples/newsletter/index.js new file mode 100644 index 000000000..988a88dbc --- /dev/null +++ b/examples/newsletter/index.js @@ -0,0 +1,42 @@ +/* + * This file is part of WPPConnect. + * + * WPPConnect is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * WPPConnect is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with WPPConnect. If not, see . + */ +const wppconnect = require('../../dist'); + +wppconnect + .create({ + session: 'teste', + }) + .then((client) => start(client)) + .catch((erro) => { + console.log(erro); + }); + +function start(client) { + client.onMessage(async (message) => { + if (message.body === 'create newsletter' && message.isGroupMsg === false) { + const t = await client.createNewsletter('WPP Test Newsletter2', { + description: 'test', + }); + console.log(t); + await client.sendText(message.from, '```' + JSON.stringify(t) + '```'); + await client.sendText( + message.from, + 'Check the channels of connected device' + ); + } + }); +} diff --git a/examples/newsletter/package.json b/examples/newsletter/package.json new file mode 100644 index 000000000..ec7411cb3 --- /dev/null +++ b/examples/newsletter/package.json @@ -0,0 +1,11 @@ +{ + "name": "wppconnect-functions", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "start": "node index.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "license": "ISC" +} diff --git a/src/api/layers/community.layer.ts b/src/api/layers/community.layer.ts index ea9175585..101f5cace 100644 --- a/src/api/layers/community.layer.ts +++ b/src/api/layers/community.layer.ts @@ -19,9 +19,9 @@ import { Page } from 'puppeteer'; import { CreateConfig } from '../../config/create-config'; import { evaluateAndReturn } from '../helpers'; import { Wid } from '../model'; -import { HostLayer } from './host.layer'; +import { NewsletterLayer } from './newsletter.layer'; -export class CommunityLayer extends HostLayer { +export class CommunityLayer extends NewsletterLayer { constructor(public page: Page, session?: string, options?: CreateConfig) { super(page, session, options); } diff --git a/src/api/layers/newsletter.layer.ts b/src/api/layers/newsletter.layer.ts new file mode 100644 index 000000000..6f6133694 --- /dev/null +++ b/src/api/layers/newsletter.layer.ts @@ -0,0 +1,112 @@ +/* + * This file is part of WPPConnect. + * + * WPPConnect is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * WPPConnect is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with WPPConnect. If not, see . + */ + +import { Page } from 'puppeteer'; +import { CreateConfig } from '../../config/create-config'; +import { evaluateAndReturn } from '../helpers'; +import { HostLayer } from './host.layer'; + +export class NewsletterLayer extends HostLayer { + constructor(public page: Page, session?: string, options?: CreateConfig) { + super(page, session, options); + } + + /** + * Create Newsletter + * @category Newsletter + * + * @example + * ```javascript + * client.createNewsletter('Name for your newsletter', {description: 'Description for that', picture: ' WPP.newsletter.create(name, options), + name, + options + ); + } + + /** + * Destroy a Newsletter + * @category Newsletter + * + * @example + * ```javascript + * client.destroyNewsletter('[newsletter-id]@newsletter'); + * ``` + * @param name id of Newsletter + */ + public async destroyNewsletter(id: string) { + return evaluateAndReturn(this.page, (id) => WPP.newsletter.destroy(id), id); + } + + /** + * Edit a Newsletter + * @category Newsletter + * + * @example + * ```javascript + * client.editNewsletter('[newsletter-id]@newsletter', { + description: 'new description'; + name: 'new name'; + picture: ''; + }); + * ``` + * @param name id of Newsletter + */ + public async editNewsletter( + id: string, + opts?: { + description?: string; + name?: string; + picture?: string | null; + } + ) { + return evaluateAndReturn( + this.page, + (id, opts) => WPP.newsletter.edit(id, opts), + id, + opts + ); + } + + /** + * Mute a Newsletter + * @category Newsletter + * + * @example + * ```javascript + * client.muteNewsletter('[newsletter-id]@newsletter'); + * ``` + * @param name id of Newsletter + */ + public async muteNesletter(id: string) { + return evaluateAndReturn(this.page, (id) => WPP.newsletter.mute(id), id); + } +}