Skip to content

Commit

Permalink
fix: Fixed buttons for latest WhatsApp (2.22.16.75) (fix #571)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Aug 4, 2022
1 parent 0c09002 commit abfc9ad
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
25 changes: 25 additions & 0 deletions src/chat/functions/prepareMessageButtons.ts
Expand Up @@ -279,6 +279,31 @@ webpack.onInjected(() => {
return r;
});

// Delayed register to ensure is after the common protobuf
setTimeout(() => {
wrapModuleFunction(createMsgProtobuf, (func, ...args) => {
const proto = func(...args);

if (proto.templateMessage) {
proto.viewOnceMessage = {
message: {
templateMessage: proto.templateMessage,
},
};
delete proto.templateMessage;
}
if (proto.buttonsMessage) {
proto.viewOnceMessage = {
message: {
buttonsMessage: proto.buttonsMessage,
},
};
delete proto.buttonsMessage;
}
return proto;
});
}, 100);

wrapModuleFunction(mediaTypeFromProtobuf, (func, ...args) => {
const [proto] = args;
if (proto.templateMessage?.hydratedTemplate) {
Expand Down
1 change: 1 addition & 0 deletions src/chat/index.ts
Expand Up @@ -15,6 +15,7 @@
*/

import './events';
import './patch';

export * from './defaultSendMessageOptions';
export * from './functions';
Expand Down
66 changes: 66 additions & 0 deletions src/chat/patch.ts
@@ -0,0 +1,66 @@
/*!
* Copyright 2022 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as webpack from '../webpack';
import { wrapModuleFunction } from '../whatsapp/exportModule';
import {
mediaTypeFromProtobuf,
typeAttributeFromProtobuf,
} from '../whatsapp/functions';

webpack.onInjected(() => {
// Delay the register to ensure that is the last wrapped function
setTimeout(applyPatch, 1000);
});

function applyPatch() {
wrapModuleFunction(mediaTypeFromProtobuf, (func, ...args) => {
const [proto] = args;
if (proto.deviceSentMessage) {
const { message: n } = proto.deviceSentMessage;
return n ? mediaTypeFromProtobuf(n) : null;
}
if (proto.ephemeralMessage) {
const { message: n } = proto.ephemeralMessage;
return n ? mediaTypeFromProtobuf(n) : null;
}
if (proto.viewOnceMessage) {
const { message: n } = proto.viewOnceMessage;
return n ? mediaTypeFromProtobuf(n) : null;
}

return func(...args);
});

wrapModuleFunction(typeAttributeFromProtobuf, (func, ...args) => {
const [proto] = args;

if (proto.ephemeralMessage) {
const { message: n } = proto.ephemeralMessage;
return n ? typeAttributeFromProtobuf(n) : 'text';
}
if (proto.deviceSentMessage) {
const { message: n } = proto.deviceSentMessage;
return n ? typeAttributeFromProtobuf(n) : 'text';
}
if (proto.viewOnceMessage) {
const { message: n } = proto.viewOnceMessage;
return n ? typeAttributeFromProtobuf(n) : 'text';
}

return func(...args);
});
}
2 changes: 1 addition & 1 deletion src/whatsapp/functions/mediaTypeFromProtobuf.ts
Expand Up @@ -22,7 +22,7 @@ import { exportModule } from '../exportModule';
*/
export declare function mediaTypeFromProtobuf(protoMessage: {
[key: string]: any;
}): string;
}): string | null;

exportModule(
exports,
Expand Down

0 comments on commit abfc9ad

Please sign in to comment.