diff --git a/src/chat/functions/index.ts b/src/chat/functions/index.ts index 01510da6d2..bba4a2e69d 100644 --- a/src/chat/functions/index.ts +++ b/src/chat/functions/index.ts @@ -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, diff --git a/src/chat/functions/sendScheduledCallMessage.ts b/src/chat/functions/sendScheduledCallMessage.ts new file mode 100644 index 0000000000..abc039704c --- /dev/null +++ b/src/chat/functions/sendScheduledCallMessage.ts @@ -0,0 +1,99 @@ +/*! + * 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: 'video' | 'voice' | SCHEDULED_CALL_TYPE; + 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 { + options = { + ...{ callType: SCHEDULED_CALL_TYPE.VOICE }, + ...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: parseInt(options.scheduledTimestampMs?.toString()), + }; + + return await sendRawMessage(chatId, rawMessage, options); +} + +webpack.onFullReady(() => { + 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); + }); +});