Skip to content

Commit

Permalink
fix: Fixed reply buttons send (useTemplateButtons: false) (fix #577)
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Aug 9, 2022
1 parent b09589e commit 28f3682
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/chat/functions/prepareMessageButtons.ts
Expand Up @@ -21,9 +21,11 @@ import {
TemplateButtonCollection,
TemplateButtonModel,
} from '../../whatsapp';
import { DROP_ATTR } from '../../whatsapp/contants';
import { wrapModuleFunction } from '../../whatsapp/exportModule';
import {
createMsgProtobuf,
encodeMaybeMediaType,
mediaTypeFromProtobuf,
typeAttributeFromProtobuf,
} from '../../whatsapp/functions';
Expand Down Expand Up @@ -56,7 +58,7 @@ export interface MessageButtonsOptions {
* Set to use template buttons instead of reply buttons.
* @default: undefined - auto detect
*/
useTemplateButtons?: boolean;
useTemplateButtons?: boolean | null;
/**
* Footer text for buttons
*/
Expand All @@ -81,7 +83,10 @@ export function prepareMessageButtons<T extends RawMessage>(
throw 'Buttons options is not a array';
}

if (typeof options.useTemplateButtons === 'undefined') {
if (
typeof options.useTemplateButtons === 'undefined' ||
options.useTemplateButtons === null
) {
options.useTemplateButtons = options.buttons.some(
(button) => 'phoneNumber' in button || 'url' in button
);
Expand Down Expand Up @@ -307,6 +312,14 @@ webpack.onInjected(() => {
});
}, 100);

wrapModuleFunction(encodeMaybeMediaType, (func, ...args) => {
const [type] = args;
if (type === 'button') {
return DROP_ATTR;
}
return func(...args);
});

wrapModuleFunction(mediaTypeFromProtobuf, (func, ...args) => {
const [proto] = args;
if (proto.templateMessage?.hydratedTemplate) {
Expand All @@ -333,6 +346,14 @@ webpack.onInjected(() => {

return 'text';
}

if (
proto.buttonsMessage?.headerType === 1 ||
proto.buttonsMessage?.headerType === 2
) {
return 'text';
}

return func(...args);
});
});
30 changes: 30 additions & 0 deletions src/whatsapp/contants/DROP_ATTR.ts
@@ -0,0 +1,30 @@
/*!
* Copyright 2021 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 { exportModule } from '../exportModule';

/**
* @whatsapp 831914
*/
export declare const DROP_ATTR: any;

exportModule(
exports,
{
DROP_ATTR: ['DROP_ATTR'],
},
(m) => m.DROP_ATTR
);
17 changes: 17 additions & 0 deletions src/whatsapp/contants/index.ts
@@ -0,0 +1,17 @@
/*!
* Copyright 2021 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.
*/

export * from './DROP_ATTR';
30 changes: 30 additions & 0 deletions src/whatsapp/functions/encodeMaybeMediaType.ts
@@ -0,0 +1,30 @@
/*!
* Copyright 2021 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 { exportModule } from '../exportModule';

/**
* @whatsapp 760210
*/
export declare function encodeMaybeMediaType(type: string): string | undefined;

exportModule(
exports,
{
encodeMaybeMediaType: 'encodeMaybeMediaType',
},
(m) => m.encodeMaybeMediaType
);
1 change: 1 addition & 0 deletions src/whatsapp/functions/index.ts
Expand Up @@ -18,6 +18,7 @@ export * from './addAndSendMsgToChat';
export * from './blockContact';
export * from './createMsgProtobuf';
export * from './createOrUpdateReactions';
export * from './encodeMaybeMediaType';
export * from './encryptAndSendGroupMsg';
export * from './encryptAndSendMsg';
export * from './fetchLinkPreview';
Expand Down
1 change: 1 addition & 0 deletions src/whatsapp/index.ts
Expand Up @@ -15,6 +15,7 @@
*/

export * from './collections';
export * as contants from './contants';
export * as enums from './enums';
export { _moduleIdMap } from './exportModule';
export * as functions from './functions';
Expand Down

0 comments on commit 28f3682

Please sign in to comment.