Skip to content

Commit

Permalink
feat: Added onOrderStatusUpdate event
Browse files Browse the repository at this point in the history
  • Loading branch information
icleitoncosta committed Nov 15, 2023
1 parent 76cd217 commit 149cd83
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 1 deletion.
4 changes: 3 additions & 1 deletion 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.
Expand Down
4 changes: 4 additions & 0 deletions examples/orders/.gitignore
@@ -0,0 +1,4 @@
node_modules
tokens
package-lock.json
debug.log
13 changes: 13 additions & 0 deletions 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.
51 changes: 51 additions & 0 deletions 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 <https://www.gnu.org/licenses/>.
*/
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) + '```');
});
});
}
11 changes: 11 additions & 0 deletions 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"
}
33 changes: 33 additions & 0 deletions src/api/layers/listener.layer.ts
Expand Up @@ -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 {
Expand Down Expand Up @@ -88,6 +89,7 @@ export class ListenerLayer extends ProfileLayer {
'onReactionMessage',
'onPollResponse',
'onUpdateLabel',
'onOrderStatusUpdate',
];

for (const func of functions) {
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 149cd83

Please sign in to comment.