Skip to content

Commit

Permalink
Merge pull request #1227 from icleitoncosta/feat/setGroupIcon
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias authored Jul 19, 2022
2 parents 6410913 + edbec84 commit b45c0b5
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
61 changes: 60 additions & 1 deletion src/api/layers/group.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@

import { Page } from 'puppeteer';
import { CreateConfig } from '../../config/create-config';
import { evaluateAndReturn } from '../helpers';
import {
evaluateAndReturn,
base64MimeType,
fileToBase64,
downloadFileToBase64,
} from '../helpers';
import { Id } from '../model';
import { GroupProperty } from '../model/enum';
import { RetrieverLayer } from './retriever.layer';
Expand Down Expand Up @@ -298,4 +303,58 @@ export class GroupLayer extends RetrieverLayer {
{ groupId, property, value }
);
}

/**
* Set group subject (if allowed)
* @category Group
* @param groupId Group ID ('000000-000000@g.us')
* @param base64 Image in base64 ( data:image/jpeg;base64,..... )
* @returns empty object
*/
public async setGroupIcon(groupId: string, pathOrBase64: string) {
let base64: string = '';
if (pathOrBase64.startsWith('data:')) {
base64 = pathOrBase64;
} else {
let fileContent = await downloadFileToBase64(pathOrBase64, [
'image/gif',
'image/png',
'image/jpg',
'image/jpeg',
'image/webp',
]);
if (!fileContent) {
fileContent = await fileToBase64(pathOrBase64);
}
if (fileContent) {
base64 = fileContent;
}
}

if (!base64) {
const error = new Error('Empty or invalid file or base64');
Object.assign(error, {
code: 'empty_file',
});
throw error;
}

const mimeInfo = base64MimeType(base64);

if (!mimeInfo || !mimeInfo.includes('image')) {
const error = new Error(
'Not an image, allowed formats png, jpeg and webp'
);
Object.assign(error, {
code: 'invalid_image',
});
throw error;
}

return await evaluateAndReturn(
this.page,
({ groupId, base64 }) => WPP.group.setIcon(groupId, base64),
{ groupId, base64 }
);
}
}
25 changes: 25 additions & 0 deletions src/api/layers/sender.layer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1212,4 +1212,29 @@ export class SenderLayer extends ListenerLayer {
{ chatState, chatId }
);
}

/**
* Send reaction to message
* @example
* ```javascript
* // For send Reaction, just to send emoji
* await client.sendReactionToMessage('[number]@c.us', '🤯');
*
* // to remove reacition
* await client.startRecording('[number]@c.us', false);
* ```
* @category Chat
* @param to Chat Id
* @param duration Duration um miliseconds
*/
public async sendReactionToMessage(msgId: string, reaction: string | false) {
return evaluateAndReturn(
this.page,
({ msgId, reaction }) => WPP.chat.sendReactionToMessage(msgId, reaction),
{
msgId,
reaction,
}
);
}
}

0 comments on commit b45c0b5

Please sign in to comment.