Skip to content

Commit

Permalink
feat: Added WPP.chat.sendGroupInviteMessage function (close #1530) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Dec 29, 2023
1 parent 364fad6 commit caf2595
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/chat/functions/index.ts
Expand Up @@ -71,6 +71,10 @@ export {
StickerMessageOptions,
VideoMessageOptions,
} from './sendFileMessage';
export {
GroupInviteMessage,
sendGroupInviteMessage,
} from './sendGroupInviteMessage';
export { ListMessageOptions, sendListMessage } from './sendListMessage';
export {
LocationMessageOptions,
Expand Down
103 changes: 103 additions & 0 deletions src/chat/functions/sendGroupInviteMessage.ts
@@ -0,0 +1,103 @@
/*!
* 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 { getProfilePictureUrl } from '../../contact';
import { ensureGroup } from '../../group';
import { downloadImage } from '../../util';
import { Wid } from '../../whatsapp';
import {
defaultSendMessageOptions,
RawMessage,
SendMessageOptions,
SendMessageReturn,
} from '..';
import { sendRawMessage } from '.';

export interface GroupInviteMessage extends SendMessageOptions {
jpegThumbnail?: string;
inviteCode: string;
inviteCodeExpiration?: number;
groupId: string | Wid;
groupName?: string;
caption?: string;
}

/**
* Send a group invite message
*
* @example
* ```javascript
* WPP.chat.sendGroupInviteMessage(
* '[number]@c.us',
* {
* inviteCode: '123',
* groupId: '789@g.us'
* }
* );
*
* // After a invite
* const result = await WPP.group.addParticipants('789@g.us', '123@c.us');
* const participant = result['123@c.us'];
* if (participant.invite_code) {
* WPP.chat.sendGroupInviteMessage(
* '123@c.us',
* {
* inviteCode: participant.invite_code,
* inviteCodeExpiration: participant.invite_code_exp,
* groupId: '789@g.us'
* }
* );
* }
* ```
*
* @category Message
*/
export async function sendGroupInviteMessage(
chatId: any,
options: GroupInviteMessage
): Promise<SendMessageReturn> {
options = {
...defaultSendMessageOptions,
...options,
};

if (!options.groupName) {
const group = await ensureGroup(options.groupId);
options.groupName = group.name;
}

if (!options.jpegThumbnail) {
const url = await getProfilePictureUrl(options.groupId, false);
if (url) {
try {
const download = await downloadImage(url);
options.jpegThumbnail = download.data.split(',', 2)[1];
} catch (error) {}
}
}

const rawMessage: RawMessage = {
type: 'groups_v4_invite',
inviteGrpJpegThum: options.jpegThumbnail,
inviteCode: options.inviteCode,
inviteCodeExp: options.inviteCodeExpiration,
inviteGrp: options.groupId,
inviteGrpName: options.groupName,
comment: options.caption,
};

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

0 comments on commit caf2595

Please sign in to comment.