Skip to content

Commit

Permalink
feat: Added option to create from object
Browse files Browse the repository at this point in the history
  • Loading branch information
edgardmessias committed Dec 9, 2020
1 parent 7bff0b2 commit 6e9d557
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 16 deletions.
80 changes: 65 additions & 15 deletions src/controllers/initializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,79 @@ import { checkUpdates, welcomeScreen } from './welcome';
import { getSpinnies } from '../utils/spinnies';
import { SocketState } from '../api/model/enum';
import { deleteFiles } from '../api/helpers';
import { tokenSession } from '../config/tokenSession.config';

/**
* A callback will be received, informing the status of the qrcode
*/
export type CatchQR = (
qrCode: string,
asciiQR: string,
attempt: number,
urlCode?: string
) => void;

/**
* A callback will be received, informing the customer's status
*/
export type StatusFind = (statusGet: string, session: string) => void;

export interface CreateOptions extends CreateConfig {
/**
* You must pass a string type parameter, this parameter will be the name of the client's session. If the parameter is not passed, the section name will be "session".
*/
session: string;
/**
* A callback will be received, informing the status of the qrcode
*/
catchQR?: CatchQR;
/**
* A callback will be received, informing the customer's status
*/
statusFind?: StatusFind;
/**
* Pass the session token information you can receive this token with the await client.getSessionTokenBrowser () function
*/
browserSessionToken?: tokenSession;
}

/**
* Start the bot
* @param session, You must pass a string type parameter, this parameter will be the name of the client's session. If the parameter is not passed, the section name will be "session".
* @param catchQR, A callback will be received, informing the status of the qrcode
* @param statusFind, A callback will be received, informing the customer's status
* @param options, Pass an object with the bot settings
* @param browserSessionToken, Pass the session token information you can receive this token with the await client.getSessionTokenBrowser () function
* @returns Whatsapp page, with this parameter you will be able to access the bot functions
*/

export async function create(createOption: CreateOptions): Promise<Whatsapp>;
/**
* Start the bot
* You must pass a string type parameter, this parameter will be the name of the client's session. If the parameter is not passed, the section name will be "session".
* @returns Whatsapp page, with this parameter you will be able to access the bot functions
*/
export async function create(
session = 'session',
catchQR?: (
qrCode: string,
asciiQR: string,
attempt: number,
urlCode?: string
) => void,
statusFind?: (statusGet: string, session: string) => void,
sessionName: string,
catchQR?: CatchQR,
statusFind?: StatusFind,
options?: CreateConfig,
browserSessionToken?: object
browserSessionToken?: tokenSession
): Promise<Whatsapp>;
export async function create(
sessionOrOption: string | CreateOptions,
catchQR?: CatchQR,
statusFind?: StatusFind,
options?: CreateConfig,
browserSessionToken?: tokenSession
): Promise<Whatsapp> {
let session = 'session';

if (typeof sessionOrOption === 'string') {
session = sessionOrOption;
} else if (typeof sessionOrOption === 'object') {
session = sessionOrOption.session;
catchQR = sessionOrOption.catchQR || catchQR;
statusFind = sessionOrOption.statusFind || statusFind;
browserSessionToken =
sessionOrOption.browserSessionToken || browserSessionToken;
options = sessionOrOption;
}

let browserToken: any;

const spinnies = getSpinnies({
Expand Down
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { tokenSession } from './config/tokenSession.config';

/*
NNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNNN
MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM
Expand Down Expand Up @@ -63,4 +65,9 @@ export {
} from './api/model/enum';
export { Whatsapp } from './api/whatsapp';
export { CreateConfig } from './config/create-config';
export { create } from './controllers/initializer';
export {
create,
CatchQR,
CreateOptions,
StatusFind,
} from './controllers/initializer';

0 comments on commit 6e9d557

Please sign in to comment.