Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added WPP.chat.sendScheduledCallMessage (close #1381) #1389

Merged
merged 5 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/chat/functions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ export {
export { sendOrderMessage } from './sendOrderMessage';
export { sendRawMessage } from './sendRawMessage';
export { sendReactionToMessage } from './sendReactionToMessage';
export { sendScheduledCallMessage } from './sendScheduledCallMessage';
export { sendTextMessage, TextMessageOptions } from './sendTextMessage';
export {
sendVCardContactMessage,
Expand Down
98 changes: 98 additions & 0 deletions src/chat/functions/sendScheduledCallMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*!
* 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 * as webpack from '../../webpack';
import { wrapModuleFunction } from '../../whatsapp/exportModule';
import {
createMsgProtobuf,
typeAttributeFromProtobuf,
} from '../../whatsapp/functions';
import {
defaultSendMessageOptions,
RawMessage,
SendMessageOptions,
SendMessageReturn,
} from '..';
import { sendRawMessage } from '.';

export enum SCHEDULED_CALL_TYPE {
UNKNOWN = 0,
VOICE = 1,
VIDEO = 2,
}

export interface ScheduledCallMessageOptions extends SendMessageOptions {
scheduledTimestampMs: number | string;
callType: string | SCHEDULED_CALL_TYPE;
icleitoncosta marked this conversation as resolved.
Show resolved Hide resolved
title: string;
}

/**
* Send a scheduled call message
*
* @example
* ```javascript
* WPP.chat.sendScheduledCallMessage('[number]@c.us', {
* title: "Title of event"
* callType: 'voice'
* scheduledTimestampMs: 1696084222000
* });
* ```
* @category Message
*/
export async function sendScheduledCallMessage(
chatId: any,
options: ScheduledCallMessageOptions
): Promise<SendMessageReturn> {
options = {
icleitoncosta marked this conversation as resolved.
Show resolved Hide resolved
...defaultSendMessageOptions,
...options,
};

if (typeof options.callType === 'string') {
options.callType = options.callType == 'voice' ? 1 : 2;
}
const rawMessage: RawMessage = {
type: 'scheduled_call',
title: options.title,
callType: options.callType,
scheduledTimestampMs: options.scheduledTimestampMs,
icleitoncosta marked this conversation as resolved.
Show resolved Hide resolved
};

return await sendRawMessage(chatId, rawMessage, options);
}

webpack.onReady(() => {
icleitoncosta marked this conversation as resolved.
Show resolved Hide resolved
wrapModuleFunction(createMsgProtobuf, (func, ...args) => {
const [message] = args;
const r = func(...args);
if (message?.type == 'scheduled_call') {
r.scheduledCallCreationMessage = {
title: message.title,
scheduledTimestampMs: (message as any).scheduledTimestampMs,
callType: (message as any).callType,
};
}
return r;
});
wrapModuleFunction(typeAttributeFromProtobuf, (func, ...args) => {
const [proto] = args;
if (proto.scheduledCallCreationMessage) {
return 'text';
}
return func(...args);
});
});