Skip to content

Commit

Permalink
feat: creating new startup template
Browse files Browse the repository at this point in the history
  • Loading branch information
jonalan7 committed May 15, 2023
1 parent e326986 commit 066b189
Show file tree
Hide file tree
Showing 12 changed files with 369 additions and 67 deletions.
260 changes: 208 additions & 52 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "venom-bot",
"version": "5.0.1",
"description": "Venom is a high-performance system developed with JavaScript to create a bot for WhatsApp, support for creating any interaction, such as customer service, media sending, sentence recognition based on artificial intelligence and all types of design architecture for WhatsApp. ",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"main": "index.js",
"types": "index.d.ts",
"scripts": {
"build:counter": "cd src/lib/counter/ && gulp",
"build:docs": "typedoc && git add docs/*",
Expand Down Expand Up @@ -124,7 +124,7 @@
"futoin-hkdf": "^1.5.1",
"i": "^0.3.7",
"latest-lib": "^0.2.1",
"latest-version": "^7.0.0",
"latest-version": "^5.1.0",
"mime-types": "^2.1.35",
"ora": "^6.1.2",
"puppeteer": "^20.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/config/create-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { puppeteerConfig } from './puppeteer.config';

// Server config
export interface CreateConfig {
session?: string;
/** folder name when saving tokens
* @default 'tokens'
*/
Expand Down
1 change: 1 addition & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { options, defaultOptions } from './options';
108 changes: 108 additions & 0 deletions src/config/options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import { LaunchOptions } from 'puppeteer';
import { puppeteerConfig } from './puppeteer.config';

// Server config
export interface options {
/**
* logs info updates automatically in terminal
* @default true
*/
updatesLog?: boolean;
/**
* name of the token to be generated, a folder with all customer information will be created
* @default 'session'
*/
session?: string;
/** folder name when saving tokens
* @default 'tokens'
*/
folderNameToken?: string;
/**
* folder directory tokens, just inside the venom folder, example: { mkdirFolderToken: '/node_modules', } //will save the tokens folder in the node_modules directory
* @default 'null'
*/
mkdirFolderToken?: string;
/**
* Headless chrome
* @default true
*/
headless?: boolean | 'new';
/**
* If you want to use browserWSEndpoint
*/
browserWS?: string;
/**
* Parameters to be added into the chrome browser instance
*/
browserArgs?: string[];
/**
* Add broserArgs without overwriting the project's original
*/
addBrowserArgs?: string[];
/**
* Will be passed to puppeteer.launch
*/
puppeteerOptions?: LaunchOptions;
/**
* Logs QR automatically in terminal
* @default true
*/
logQR?: boolean;
/**
* Will disable the welcoming message which appears in the beginning
* @default false
*/
disableWelcome?: boolean;
/**
* Automatically closes the venom-bot only when scanning the QR code (default 60000 miliseconds, if you want to turn it off, assign 0 or false)
* @default 60000
*/
autoClose?: number;
/**
* Creates a folder when inserting an object in the client's browser, to work it is necessary to pass the parameters in the function create browserSessionToken
* @default true
*/
createPathFileToken?: boolean;
/**
* automatically download Chromium browser
* @default true
*/
BrowserFetcher?: boolean;
/**
* Add proxy server
* @default null
*/
addProxy?: string[];
/**
* Proxy username
* @default null
*/
userProxy?: string;
/**
* Proxy password
* @default null
*/
userPass?: string;
/**
* Open devtools by default
* @default false
*/
devtools?: boolean;
}

export const defaultOptions: options = {
session: 'name-session',
folderNameToken: 'tokens',
disableWelcome: false,
BrowserFetcher: true,
updatesLog: true,
headless: true,
logQR: true,
devtools: false,
mkdirFolderToken: '',
browserWS: '',
browserArgs: puppeteerConfig.chromiumArgs,
addBrowserArgs: [],
autoClose: 120000,
addProxy: []
};
12 changes: 6 additions & 6 deletions src/controllers/browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as fs from 'fs';
import * as path from 'path';
import { Browser, BrowserContext, Page, LaunchOptions } from 'puppeteer';
import puppeteer from 'puppeteer-extra';
import { options } from '../config';
import { CreateConfig } from '../config/create-config';
import { puppeteerConfig } from '../config/puppeteer.config';
import StealthPlugin from 'puppeteer-extra-plugin-stealth';
Expand All @@ -11,7 +12,7 @@ import { sleep } from '../utils/sleep';
import * as Spinnies from 'spinnies';

export async function initWhatsapp(
options: CreateConfig,
options: options | CreateConfig,
browser: Browser
): Promise<Page | false> {
const waPage = await getWhatsappPage(browser);
Expand Down Expand Up @@ -74,13 +75,13 @@ export async function getWhatsappPage(
}
}

export function folderSession(options: CreateConfig, session: string) {
export function folderSession(options: options | CreateConfig) {
const folderSession = path.join(
path.resolve(
process.cwd(),
options.mkdirFolderToken,
options.folderNameToken,
session
options.session
)
);

Expand Down Expand Up @@ -116,11 +117,10 @@ export function folderSession(options: CreateConfig, session: string) {
}

export async function initBrowser(
session: string,
options: CreateConfig
options: options | CreateConfig
): Promise<Browser | false> {
try {
folderSession(options, session);
folderSession(options);

// Set the executable path to the path of the Chrome binary or the executable path provided
const executablePath = getChrome() ?? puppeteer.executablePath();
Expand Down
1 change: 0 additions & 1 deletion src/controllers/check-up-to-date.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require = require('esm')(module);
import latestVersion from 'latest-version';
import { upToDate } from '../utils/semver';
import boxen from 'boxen';
Expand Down
32 changes: 32 additions & 0 deletions src/controllers/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Browser, Page } from 'puppeteer';
import { checkUpdates } from './check-up-to-date';
import { options, defaultOptions } from '../config';
import { initWhatsapp, initBrowser, statusLog } from './browser';

export async function connect(options?: options);

export async function connect(options?: options) {
const mergeOptionsDefault = { ...defaultOptions, ...options };

if (!!mergeOptionsDefault.session && mergeOptionsDefault.session.length) {
const sessionName = mergeOptionsDefault.session;
const replaceSession = sessionName.replace(/[^0-9a-zA-Zs]/g, '');
if (replaceSession.length) {
mergeOptionsDefault.session = replaceSession;
} else {
mergeOptionsDefault.session = defaultOptions.session;
}
}

if (mergeOptionsDefault.updatesLog) {
await checkUpdates();
}

const wpage: Browser | boolean = await initBrowser(mergeOptionsDefault);
if (typeof wpage !== 'boolean') {
const page: boolean | Page = await initWhatsapp(mergeOptionsDefault, wpage);
if (typeof page !== 'boolean') {
console.log('New option');
}
}
}
5 changes: 1 addition & 4 deletions src/controllers/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,7 @@ export async function create(
});
}

const browser: Browser | boolean = await initBrowser(
session,
mergedOptions
);
const browser: Browser | boolean = await initBrowser(mergedOptions);

if (typeof browser === 'boolean') {
spinnies.fail(`browser-${session}`, {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export {
} from './api/model/enum';
export { Whatsapp } from './api/whatsapp';
export { CreateConfig } from './config/create-config';
export { connect } from './controllers/init';
export {
create,
CatchQR,
Expand Down
7 changes: 7 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@
// "experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
// "emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
},
"typedocOptions": {
"entryPoints": [
"src/index.ts",
"./package.json"
],
"out": "api-docs",
},
"include": ["src/**/*"],
"exclude": ["src/middleware/middleware.ts", "img/*"]
}
2 changes: 1 addition & 1 deletion typedoc.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"disableSources": true,
"entryPoints": "src/index.ts",
"entryPoints": ["src/index.ts"],
"excludeExternals": true,
"excludePrivate": true,
"excludeProtected": true,
Expand Down

0 comments on commit 066b189

Please sign in to comment.