diff --git a/examples/basic/README.md b/examples/basic/README.md index 1db83d7a5..a642373a7 100644 --- a/examples/basic/README.md +++ b/examples/basic/README.md @@ -1,4 +1,6 @@ -# Simple example to start the Wppconnect +# Simple example to start the Wppconnect and send a order message + +Is necessary to login with a business account > 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. diff --git a/examples/orders/.gitignore b/examples/orders/.gitignore new file mode 100644 index 000000000..27826cf8a --- /dev/null +++ b/examples/orders/.gitignore @@ -0,0 +1,4 @@ +node_modules +tokens +package-lock.json +debug.log \ No newline at end of file diff --git a/examples/orders/README.md b/examples/orders/README.md new file mode 100644 index 000000000..1db83d7a5 --- /dev/null +++ b/examples/orders/README.md @@ -0,0 +1,13 @@ +# Simple example to start the Wppconnect + +> 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/orders/index.js b/examples/orders/index.js new file mode 100644 index 000000000..c5b551930 --- /dev/null +++ b/examples/orders/index.js @@ -0,0 +1,51 @@ +/* + * 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'); + +const froms = []; +wppconnect + .create() + .then((client) => start(client)) + .catch((erro) => { + console.log(erro); + }); + +function start(client) { + client.onMessage(async (message) => { + if (message.body === 'new order' && message.isGroupMsg === false) { + const order = await client.sendOrderMessage(message.from, [ + { type: 'custom', name: 'Item with cost test', price: 120000, qnt: 2 }, + ]); + froms.push(message.from); + client.sendText( + message.from, + `Please, save your order id, and get order for test porpouse` + ); + client.sendText(message.from, `${order.id}`); + } + if (message?.body?.includes('order id=') && message?.isGroupMsg === false) { + const id = message?.body.split('=')[1]; + const order = await client.getOrder(id); + client.sendText(message.from, '```' + JSON.stringify(order) + '```'); + } + }); + client.onOrderStatusUpdate((data) => { + froms.forEach((from) => { + client.sendText(from, '```' + JSON.stringify(data) + '```'); + }); + }); +} diff --git a/examples/orders/package.json b/examples/orders/package.json new file mode 100644 index 000000000..ec7411cb3 --- /dev/null +++ b/examples/orders/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/listener.layer.ts b/src/api/layers/listener.layer.ts index fcca8d9da..bf661851b 100644 --- a/src/api/layers/listener.layer.ts +++ b/src/api/layers/listener.layer.ts @@ -34,6 +34,7 @@ import { InterfaceMode } from '../model/enum/interface-mode'; import { InterfaceState } from '../model/enum/interface-state'; import { ProfileLayer } from './profile.layer'; import { Label } from '../model/label'; +import { MsgKey } from '@wppconnect/wa-js/dist/whatsapp'; declare global { interface Window { @@ -88,6 +89,7 @@ export class ListenerLayer extends ProfileLayer { 'onReactionMessage', 'onPollResponse', 'onUpdateLabel', + 'onOrderStatusUpdate', ]; for (const func of functions) { @@ -287,6 +289,22 @@ export class ListenerLayer extends ProfileLayer { } catch (error) { console.error(error); } + try { + if (!window['onOrderStatusUpdate'].exposed) { + WPP.on('order.payment_status', (data) => { + const eventData = { + method: data.method, + timestamp: data.timestamp, + reference_id: data.reference_id, + msgId: data.msgId, + }; + window['onOrderStatusUpdate'](eventData); + }); + window['onOrderStatusUpdate'].exposed = true; + } + } catch (error) { + console.error(error); + } try { if (!window['onParticipantsChanged'].exposed) { WPP.on('group.participant_changed', (participantChangedEvent) => { @@ -646,4 +664,19 @@ export class ListenerLayer extends ProfileLayer { ) { return this.registerEvent('onUpdateLabel', callback); } + + /** + * @event Listens to update order status + * @returns Disposable object to stop the listening + */ + public onOrderStatusUpdate( + callback: (data: { + method: string; + timestamp: number; + reference_id: string; + msgId: MsgKey; + }) => any + ) { + return this.registerEvent('onOrderStatusUpdate', callback); + } }