Skip to content

Commit

Permalink
feat: Added onNotificationMessage function for notif. msg. (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed May 7, 2021
1 parent 2760f68 commit 32c395b
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/api/helpers/exposed.enum.ts
Expand Up @@ -19,6 +19,7 @@ export enum ExposedFn {
OnMessage = 'onMessage',
OnAnyMessage = 'onAnyMessage',
onAck = 'onAck',
onNotificationMessage = 'onNotificationMessage',
onParticipantsChanged = 'onParticipantsChanged',
onStateChange = 'onStateChange',
onStreamChange = 'onStreamChange',
Expand Down
14 changes: 14 additions & 0 deletions src/api/layers/listener.layer.ts
Expand Up @@ -109,6 +109,10 @@ export class ListenerLayer extends ProfileLayer {
window.WAPI.onInterfaceChange(window['onInterfaceChange']);
window['onInterfaceChange'].exposed = true;
}
if (!window['onNotificationMessage'].exposed) {
window.WAPI.onNotificationMessage(window['onNotificationMessage']);
window['onNotificationMessage'].exposed = true;
}
})
.catch(() => {});
}
Expand Down Expand Up @@ -149,6 +153,16 @@ export class ListenerLayer extends ProfileLayer {
return this.registerEvent(ExposedFn.OnAnyMessage, callback);
}

/**
* @event Listens to all notification messages, like group changes, join, leave
* @param to callback
* @fires Message
* @returns Disposable object to stop the listening
*/
public onNotificationMessage(callback: (message: Message) => void) {
return this.registerEvent(ExposedFn.onNotificationMessage, callback);
}

/**
* @event Listens List of mobile states
* @returns Disposable object to stop the listening
Expand Down
40 changes: 32 additions & 8 deletions src/api/model/enum/message-type.ts
Expand Up @@ -16,17 +16,41 @@
*/

export enum MessageType {
TEXT = 'chat',
AUDIO = 'audio',
VOICE = 'ptt',
NOTIFICATION = 'notification',
NOTIFICATION_TEMPLATE = 'notification_template',
GROUP_NOTIFICATION = 'group_notification',

/**
* Group data modification, like subtitle or description and group members (join, leave)
* See {@link GroupNotificationType}
*/
GP2 = 'gp2',
BROADCAST_NOTIFICATION = 'broadcast_notification',
E2E_NOTIFICATION = 'e2e_notification',
CALL_LOG = 'call_log',
PROTOCOL = 'protocol',
CHAT = 'chat',
LOCATION = 'location',
PAYMENT = 'payment',
VCARD = 'vcard',
CIPHERTEXT = 'ciphertext',
MULTI_VCARD = 'multi_vcard',
REVOKED = 'revoked',
OVERSIZED = 'oversized',
GROUPS_V4_INVITE = 'groups_v4_invite',
HSM = 'hsm',
TEMPLATE_BUTTON_REPLY = 'template_button_reply',
IMAGE = 'image',
VIDEO = 'video',
DOCUMENT = 'document',
AUDIO = 'audio',
PTT = 'ptt',
STICKER = 'sticker',
LOCATION = 'location',
CONTACT_CARD = 'vcard',
CONTACT_CARD_MULTI = 'multi_vcard',
REVOKED = 'revoked',
DOCUMENT = 'document',
PRODUCT = 'product',
ORDER = 'order',
LIST = 'list',
LIST_RESPONSE = 'list_response',
BUTTONS_RESPONSE = 'buttons_response',
UNKNOWN = 'unknown',
}

Expand Down
7 changes: 6 additions & 1 deletion src/api/model/message.ts
Expand Up @@ -17,11 +17,16 @@

import { Chat } from './chat';
import { Contact } from './contact';
import { MessageType } from './enum';

export interface Message {
id: string;
body: string;
type: string;
type: MessageType;
/**
* When type is GP2: {@link GroupNotificationType}
*/
subtype: string;
t: number;
notifyName: string;
from: string;
Expand Down
31 changes: 31 additions & 0 deletions src/lib/wapi/listeners/add-on-notification-message.js
@@ -0,0 +1,31 @@
/*
* 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/>.
*/

export function addOnNotificationMessage() {
window.WAPI.onNotificationMessage = function (callback) {
window.WAPI.waitForStore('Msg', () => {
window.Store.Msg.on('add', (message) => {
if (!message.isNotification) {
return;
}
const data = WAPI._serializeMessageObj(message);
callback(data);
});
});
return true;
};
}
1 change: 1 addition & 0 deletions src/lib/wapi/listeners/index.js
Expand Up @@ -23,3 +23,4 @@ export { addOnNewAcks } from './add-on-new-ack';
export { addOnLiveLocation } from './add-on-live-location';
export { addOnParticipantsChange } from './add-on-participants-change';
export { addOnAddedToGroup } from './add-on-added-to-group';
export { addOnNotificationMessage } from './add-on-notification-message';
2 changes: 2 additions & 0 deletions src/lib/wapi/wapi.js
Expand Up @@ -135,6 +135,7 @@ import {
addOnAddedToGroup,
addOnLiveLocation,
addOnNewAcks,
addOnNotificationMessage,
addOnParticipantsChange,
addOnStateChange,
addOnStreamChange,
Expand Down Expand Up @@ -566,4 +567,5 @@ if (typeof window.WAPI === 'undefined') {
addOnAddedToGroup();
addOnLiveLocation();
addOnParticipantsChange();
addOnNotificationMessage();
}
1 change: 1 addition & 0 deletions src/types/WAPI.d.ts
Expand Up @@ -110,6 +110,7 @@ interface WAPI {
onIncomingCall: (callback: Function) => any;
onInterfaceChange: (callback: Function) => void;
onLiveLocation: (chatId: string, callback: Function) => any;
onNotificationMessage: (callback: (message: Message) => void) => any;
onParticipantsChanged: (groupId: string, callback: Function) => any;
onStateChange: (callback: Function) => void;
onStreamChange: (callback: Function) => void;
Expand Down

0 comments on commit 32c395b

Please sign in to comment.