Skip to content

Commit

Permalink
feat: Adicionado recurso de recusar ligação do WhatsApp (close #299)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Jul 14, 2021
1 parent 54b2be7 commit e7ef0b6
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/api/layers/listener.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,11 @@ export class ListenerLayer extends ProfileLayer {
}

/**
* @event Listens to messages received
* @returns Disposable object to stop the listening
* @event Escuta por ligações recebidas, seja de áudio ou vídeo.
*
* Para recusar a ligação, basta chamar o `rejectCall` {@link rejectCall}
*
* @returns Objeto descartável para parar de ouvir
*/
public onIncomingCall(callback: (call: any) => any) {
return this.registerEvent('onIncomingCall', callback);
Expand Down
15 changes: 15 additions & 0 deletions src/api/whatsapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,4 +225,19 @@ export class Whatsapp extends BusinessLayer {
const buff = Buffer.from(res.data, 'binary');
return magix(buff, message.mediaKey, message.type, message.size);
}

/**
* Rejeita uma ligação recebida pelo WhatsApp
* @param callId string ID da ligação, caso não passado, todas ligações serão rejeitadas
* @returns Número de ligações rejeitadas
*/
public async rejectCall(callId?: string) {
return await evaluateAndReturn(
this.page,
({ callId }) => WAPI.rejectCall(callId),
{
callId,
}
);
}
}
1 change: 1 addition & 0 deletions src/lib/wapi/functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export { getListMute, interfaceMute } from './get-list-mute';
export { downloadMedia } from './download-media';
export * from './phoneWatchdog';
export * from './presence';
export * from './reject-call';
export * from './set-group-description';
export * from './set-group-property';
export * from './set-group-subject';
Expand Down
68 changes: 68 additions & 0 deletions src/lib/wapi/functions/reject-call.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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/>.
*/

async function rejectCallByCallId(callId) {
const call = await Store.Call.get(callId);

if (!call) {
throw {
error: true,
code: 'call_not_found',
message: 'Call not found',
};
}

if (call.getState() !== 'INCOMING_RING') {
throw {
error: true,
code: 'call_is_not_incoming_ring',
message: 'Call is not incoming ring',
};
}

return await Store.sendCallSignalingMsg({
common: {
peer_jid: call.peerJid,
},
payload: [
'reject',
{
'call-id': call.id,
'call-creator': call.peerJid.toString({ legacy: true }),
count: '0',
},
null,
],
});
}

export async function rejectCall(callId) {
if (callId) {
await rejectCallByCallId(callId);
return 1;
}

const calls = Store.Call.models.filter(
(c) => c.getState() === 'INCOMING_RING'
);

for (const call of calls) {
await rejectCallByCallId(call.id).catch((e) => null);
}

return calls.lenth;
}
4 changes: 4 additions & 0 deletions src/lib/wapi/store/store-objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,8 @@ export const storeObjects = [
id: 'changeEphemeralDuration',
conditions: (module) => module.changeEphemeralDuration,
},
{
id: 'sendCallSignalingMsg',
conditions: (module) => module.sendCallSignalingMsg,
},
];
4 changes: 4 additions & 0 deletions src/lib/wapi/wapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ import {
subscribePresence,
unsubscribePresence,
getMessages,
rejectCall,
} from './functions';
import {
base64ToFile,
Expand Down Expand Up @@ -358,6 +359,9 @@ if (typeof window.WAPI === 'undefined') {
window.WAPI.getBusinessProfilesProducts = getBusinessProfilesProducts;
window.WAPI.getOrderbyMsg = getOrderbyMsg;

// call functions
window.WAPI.rejectCall = rejectCall;

// Listeners initialization
window.WAPI._newMessagesQueue = [];
window.WAPI._newMessagesBuffer =
Expand Down
1 change: 1 addition & 0 deletions src/types/WAPI.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ interface WAPI {
promoteParticipant: (groupId: string, contactId: string | string[]) => void;
removeParticipant: (groupId: string, contactId: string | string[]) => void;
reply: (to: string, content: string, quotedMsg: string) => Promise<string>;
rejectCall: (callId?: string) => Promise<number>;
revokeGroupInviteLink: (chatId: string) => Promise<string>;
restartService: () => boolean;
sendChatstate: (chatState: string, chatId: string) => void;
Expand Down

0 comments on commit e7ef0b6

Please sign in to comment.