-
-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1228 from icleitoncosta/feature/newFunctions
Feature/new functions
- Loading branch information
Showing
3 changed files
with
269 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/* | ||
* This file is part of WPPConnect. | ||
* | ||
* WPPConnect is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* WPPConnect is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with WPPConnect. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { Page } from 'puppeteer'; | ||
import { CreateConfig } from '../../config/create-config'; | ||
import { HostLayer } from './host.layer'; | ||
import { | ||
evaluateAndReturn, | ||
base64MimeType, | ||
fileToBase64, | ||
downloadFileToBase64, | ||
} from '../helpers'; | ||
|
||
export class LabelsLayer extends HostLayer { | ||
constructor(public page: Page, session?: string, options?: CreateConfig) { | ||
super(page, session, options); | ||
} | ||
/** | ||
* Create New Label | ||
* | ||
* @example | ||
* ```javascript | ||
* client.addNewLabel(`Name of label`); | ||
* //or | ||
* client.addNewLabel(`Name of label`, { labelColor: '#dfaef0' }); | ||
* //or | ||
* client.addNewLabel(`Name of label`, { labelColor: 4292849392 }); | ||
* ``` | ||
* @param name Name of label | ||
* @param options options of label | ||
*/ | ||
public async addNewLabel(name: string, options?: string) { | ||
return await evaluateAndReturn( | ||
this.page, | ||
({ name, options }) => { | ||
WPP.labels.addNewLabel(name, options); | ||
}, | ||
{ name, options } | ||
); | ||
} | ||
/** | ||
* Add or delete label of chatId | ||
* | ||
* @example | ||
* ```javascript | ||
* client.addOrRemoveLabels(['[number]@c.us','[number]@c.us'], | ||
* [{labelId:'76', type:'add'},{labelId:'75', type:'remove'}]); | ||
* //or | ||
* ``` | ||
* @param name Name of label | ||
* @param options options to remove or add | ||
*/ | ||
public async addOrRemoveLabels(chatIds: string, options: string) { | ||
return await evaluateAndReturn( | ||
this.page, | ||
({ chatIds, options }) => { | ||
WPP.labels.addOrRemoveLabels(chatIds, options); | ||
}, | ||
{ chatIds, options } | ||
); | ||
} | ||
/** | ||
* Get all Labels | ||
* | ||
* @example | ||
* ```javascript | ||
* client.getAllLabels(); | ||
* ``` | ||
*/ | ||
public async getAllLabels() { | ||
return await evaluateAndReturn(this.page, () => { | ||
WPP.labels.getAllLabels(); | ||
}); | ||
} | ||
/** | ||
* Delete all Labels | ||
* | ||
* @example | ||
* ```javascript | ||
* client.deleteAllLabels(); | ||
* ``` | ||
*/ | ||
public async deleteAllLabels() { | ||
return await evaluateAndReturn(this.page, () => { | ||
WPP.labels.deleteAllLabels(); | ||
}); | ||
} | ||
/** | ||
* Add or delete label of chatId | ||
* | ||
* @example | ||
* ```javascript | ||
* client.deleteLabel(); | ||
* ``` | ||
* @param id Id or string to labels to delete | ||
*/ | ||
public async deleteLabel(id: string | string[]) { | ||
return await evaluateAndReturn( | ||
this.page, | ||
({ id }) => { | ||
WPP.labels.deleteLabel(id); | ||
}, | ||
{ id } | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
/* | ||
* This file is part of WPPConnect. | ||
* | ||
* WPPConnect is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Lesser General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* WPPConnect is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with WPPConnect. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import { Page } from 'puppeteer'; | ||
import { CreateConfig } from '../../config/create-config'; | ||
import { BusinessLayer } from './business.layer'; | ||
import { LabelsLayer } from './labels.layer'; | ||
import { | ||
evaluateAndReturn, | ||
base64MimeType, | ||
fileToBase64, | ||
downloadFileToBase64, | ||
} from '../helpers'; | ||
|
||
export class StatusLayer extends LabelsLayer { | ||
constructor(public page: Page, session?: string, options?: CreateConfig) { | ||
super(page, session, options); | ||
} | ||
/** | ||
* Send a image message to status stories | ||
* | ||
* @example | ||
* ```javascript | ||
* client.sendImageStatus('data:image/jpeg;base64,<a long base64 file...>'); | ||
* ``` | ||
* @param pathOrBase64 Path or base 64 image | ||
*/ | ||
public async sendImageStatus(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, | ||
({ base64 }) => { | ||
WPP.status.sendImageStatus(base64); | ||
}, | ||
{ base64 } | ||
); | ||
} | ||
/** | ||
* Send a video message to status stories | ||
* | ||
* @example | ||
* ```javascript | ||
* client.sendVideoStatus('data:video/mp4;base64,<a long base64 file...>'); | ||
* ``` | ||
* @param pathOrBase64 Path or base 64 image | ||
*/ | ||
public async sendVideoStatus(pathOrBase64: string) { | ||
let base64: string = ''; | ||
if (pathOrBase64.startsWith('data:')) { | ||
base64 = pathOrBase64; | ||
} else { | ||
let fileContent = await downloadFileToBase64(pathOrBase64); | ||
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; | ||
} | ||
|
||
return await evaluateAndReturn( | ||
this.page, | ||
({ base64 }) => { | ||
WPP.status.sendVideoStatus(base64); | ||
}, | ||
{ base64 } | ||
); | ||
} | ||
|
||
/** | ||
* Send a text to status stories | ||
* | ||
* @example | ||
* ```javascript | ||
* client.sendTextStatus(`Bootstrap primary color: #0275d8`, { backgroundColor: '#0275d8', font: 2}); | ||
* ``` | ||
* @param pathOrBase64 Path or base 64 image | ||
*/ | ||
public async sendTextStatus(text: string, options: string) { | ||
return await evaluateAndReturn( | ||
this.page, | ||
({ text, options }) => { | ||
WPP.status.sendTextStatus(text, options); | ||
}, | ||
{ text, options } | ||
); | ||
} | ||
} |