Skip to content

Commit

Permalink
fix: Fixed buttons/list from normal accounts (based on adiwajshing/Ba…
Browse files Browse the repository at this point in the history
…ileys#2674)
  • Loading branch information
edgardmessias committed Apr 1, 2023
1 parent 845bcf6 commit 6ec2d93
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 24 deletions.
78 changes: 64 additions & 14 deletions src/chat/functions/prepareMessageButtons.ts
Expand Up @@ -20,10 +20,12 @@ import {
ReplyButtonModel,
TemplateButtonCollection,
TemplateButtonModel,
websocket,
} from '../../whatsapp';
import { DROP_ATTR } from '../../whatsapp/contants';
import { wrapModuleFunction } from '../../whatsapp/exportModule';
import {
createFanoutMsgStanza,
createMsgProtobuf,
encodeMaybeMediaType,
mediaTypeFromProtobuf,
Expand Down Expand Up @@ -293,21 +295,21 @@ webpack.onInjected(() => {
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;
// proto.viewOnceMessage = {
// message: {
// templateMessage: proto.templateMessage,
// },
// };
// delete proto.templateMessage;
}
// if (proto.buttonsMessage) {
// proto.viewOnceMessage = {
// message: {
// buttonsMessage: proto.buttonsMessage,
// },
// };
// delete proto.buttonsMessage;
// }
return proto;
});
}, 100);
Expand Down Expand Up @@ -356,4 +358,52 @@ webpack.onInjected(() => {

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

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

let buttonNode: websocket.WapNode | null = null;

if (proto.buttonsMessage) {
buttonNode = websocket.smax('buttons');
} else if (proto.listMessage) {
const listType: number = proto.listMessage.listType || 0;

const types = ['unknown', 'single_select', 'product_list'];

buttonNode = websocket.smax('list', {
v: '2',
type: types[listType],
});
}

const node = await func(...args);

if (!buttonNode) {
return node;
}

const content = node.content as websocket.WapNode[];

let bizNode = content.find((c) => c.tag === 'biz');

if (!bizNode) {
bizNode = websocket.smax('biz', {}, null);
content.push(bizNode);
}

let hasButtonNode = false;

if (Array.isArray(bizNode.content)) {
hasButtonNode = !!bizNode.content.find((c) => c.tag === buttonNode?.tag);
} else {
bizNode.content = [];
}

if (!hasButtonNode) {
bizNode.content.push(buttonNode);
}

return node;
});
});
16 changes: 8 additions & 8 deletions src/chat/functions/sendListMessage.ts
Expand Up @@ -127,14 +127,14 @@ webpack.onInjected(() => {
wrapModuleFunction(createMsgProtobuf, (func, ...args) => {
const proto = func(...args);

if (proto.listMessage) {
proto.viewOnceMessage = {
message: {
listMessage: proto.listMessage,
},
};
delete proto.listMessage;
}
// if (proto.listMessage) {
// proto.viewOnceMessage = {
// message: {
// listMessage: proto.listMessage,
// },
// };
// delete proto.listMessage;
// }
return proto;
});
}, 100);
Expand Down
36 changes: 36 additions & 0 deletions src/whatsapp/functions/createFanoutMsgStanza.ts
@@ -0,0 +1,36 @@
/*!
* Copyright 2023 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 { MsgModel, websocket, Wid } from '..';
import { exportModule } from '../exportModule';

/**
*
*/
export declare function createFanoutMsgStanza(
msg: MsgModel,
proto: { [key: string]: any },
devices: Wid[],
options: { [key: string]: any }
): Promise<websocket.WapNode>;

exportModule(
exports,
{
createFanoutMsgStanza: 'createFanoutMsgStanza',
},
(m) => m.createFanoutMsgStanza
);
3 changes: 2 additions & 1 deletion src/whatsapp/functions/index.ts
@@ -1,5 +1,5 @@
/*!
* Copyright 2021 WPPConnect Team
* Copyright 2023 WPPConnect Team
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,7 @@ export * from './calculateFilehashFromBlob';
export * from './canEditMsg';
export * from './canReplyMsg';
export * from './collections';
export * from './createFanoutMsgStanza';
export * from './createGroup';
export * from './createMsgProtobuf';
export * from './createOrUpdateReactions';
Expand Down
2 changes: 1 addition & 1 deletion src/whatsapp/websocket/WapNode.ts
Expand Up @@ -22,7 +22,7 @@ import { exportModule } from '../exportModule';
export declare class WapNode {
public tag: any;
public attrs: { [key: string]: any };
public content: Uint8Array | (any | WapNode)[];
public content: undefined | string | Uint8Array | WapNode[];

constructor(tag: any, attrs?: { [key: string]: any }, content?: any[]);

Expand Down

0 comments on commit 6ec2d93

Please sign in to comment.