diff --git a/README.md b/README.md index aac5b5a3..c6397c8d 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@

- mtcute logo
+ mtcute logo

modern typescript library for mtproto
@@ -41,12 +41,8 @@ dp.onNewMessage(filters.chat('private'), async (msg) => { await msg.replyText('hiiii from mtcute! 🌸') }) -tg.run({ - phone: () => tg.input('phone > '), - code: () => tg.input('code > '), - password: () => tg.input('password > ') -}, async (self) => { - console.log(`logged in as ${self.displayName}`) +tg.run({ /* optional params */ }, async (self) => { + console.log(`✨ logged in as ${self.displayName}`) }) ``` diff --git a/packages/create-bot/src/utils.ts b/packages/create-bot/src/utils.ts index 3820a0d8..f808f4a6 100644 --- a/packages/create-bot/src/utils.ts +++ b/packages/create-bot/src/utils.ts @@ -14,7 +14,7 @@ export function exec(cwd: string, ...cmd: string[]) { if (code === 0) { resolve() } else { - reject(new Error(`Process exited with code ${code}`)) + reject(new Error(`Process "${cmd.join(' ')}" exited with code ${code}`)) } }) }) diff --git a/packages/create-bot/template/src/index.js.hbs b/packages/create-bot/template/src/index.js.hbs index 51da314f..b2ee63d2 100644 --- a/packages/create-bot/template/src/index.js.hbs +++ b/packages/create-bot/template/src/index.js.hbs @@ -30,12 +30,6 @@ dp.onNewMessage(filters.start, async (msg) => { tg.run( {{#if botToken}} { botToken: env.BOT_TOKEN }, - {{else}} - { - phone: () => tg.input('Phone > '), - code: () => tg.input('Code > '), - password: () => tg.input('2FA password > '), - }, {{/if}} (user) => { console.log('Logged in as', user.username) diff --git a/packages/create-bot/template/src/index.ts.hbs b/packages/create-bot/template/src/index.ts.hbs index 2d79c657..18415ac2 100644 --- a/packages/create-bot/template/src/index.ts.hbs +++ b/packages/create-bot/template/src/index.ts.hbs @@ -30,12 +30,6 @@ dp.onNewMessage(filters.start, async (msg) => { tg.run( {{#if botToken}} { botToken: env.BOT_TOKEN }, - {{else}} - { - phone: () => tg.input('Phone > '), - code: () => tg.input('Code > '), - password: () => tg.input('2FA password > '), - }, {{/if}} (user) => { console.log('Logged in as', user.username) diff --git a/packages/node/README.md b/packages/node/README.md index fbfec035..fe07677a 100644 --- a/packages/node/README.md +++ b/packages/node/README.md @@ -4,7 +4,7 @@ All-in-one package for NodeJS. Includes support for native crypto addon (must be installed separately, `@mtcute/crypto-node`), terminal I/O via -`readline` and comes with pre-installed HTML and Markdown parsers. +`readline` and includes HTML and Markdown parsers. ## Usage @@ -16,4 +16,8 @@ const tg = new NodeTelegramClient({ apiHash: 'abcdef', storage: 'my-account' }) + +tg.run(async (user) => { + console.log(`✨ logged in as ${user.displayName}`) +}) ``` diff --git a/packages/node/index.ts b/packages/node/index.ts index a2dbc97f..6a9a18af 100644 --- a/packages/node/index.ts +++ b/packages/node/index.ts @@ -1,7 +1,7 @@ import { createRequire } from 'module' import { createInterface, Interface as RlInterface } from 'readline' -import { TelegramClient, TelegramClientOptions } from '@mtcute/client' +import { TelegramClient, TelegramClientOptions, User } from '@mtcute/client' import { SqliteStorage } from '@mtcute/sqlite' export * from '@mtcute/client' @@ -20,31 +20,15 @@ try { nativeCrypto = require('@mtcute/crypto-node').NodeNativeCryptoProvider } catch (e) {} -export interface NodeTelegramClientOptions extends Omit { - /** - * Storage to use. - * - * You can pass a file name as a simple string, - * which will be passed directly to `SqliteStorage` - * - * Defaults to SQLite storage in `client.session` file in - * current working directory - */ - storage?: TelegramClientOptions['storage'] | string -} - /** * Tiny wrapper over {@link TelegramClient} for usage inside Node JS. * - * This automatically sets the parse modes, native - * crypto addon and defaults to SQLite session. - * - * Documentation for this class only contains the - * difference between {@link TelegramClient} and {@link NodeTelegramClient}. - * For the complete documentation, please refer to {@link TelegramClient}. + * This class automatically manages native + * crypto addon and defaults to SQLite session (unlike `TelegarmClient`, + * which defaults to a JSON file on Node). */ export class NodeTelegramClient extends TelegramClient { - constructor(opts: NodeTelegramClientOptions) { + constructor(opts: TelegramClientOptions) { super({ // eslint-disable-next-line crypto: nativeCrypto ? () => new nativeCrypto() : undefined, @@ -83,4 +67,38 @@ export class NodeTelegramClient extends TelegramClient { return super.close() } + + start(params: Parameters[0] = {}): Promise { + if (!params.botToken) { + if (!params.phone) params.phone = () => this.input('phone > ') + if (!params.code) params.code = () => this.input('code > ') + + if (!params.password) { + params.password = () => this.input('2fa password > ') + } + } + + return super.start(params).then((user) => { + if (this._rl) { + this._rl.close() + delete this._rl + } + + return user + }) + } + + run( + params: Parameters[0] | ((user: User) => void | Promise), + then?: (user: User) => void | Promise, + ): void { + if (typeof params === 'function') { + then = params + params = {} + } + + this.start(params) + .then(then) + .catch((err) => this._emitError(err)) + } }