chore(node): allow omitting input params
This commit is contained in:
parent
38358622e7
commit
b261a661d2
6 changed files with 48 additions and 42 deletions
10
README.md
10
README.md
|
@ -1,6 +1,6 @@
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://github.com/mtcute/mtcute/">
|
<a href="https://github.com/mtcute/mtcute/">
|
||||||
<img src="./.github/logo.svg" alt="mtcute logo" title="mtcute" width="480" /><br/>
|
<img src="https://raw.githubusercontent.com/mtcute/mtcute/master/.github/logo.svg" alt="mtcute logo" title="mtcute" width="480" /><br/>
|
||||||
</a><br/>
|
</a><br/>
|
||||||
<b>modern typescript library for mtproto</b>
|
<b>modern typescript library for mtproto</b>
|
||||||
<br>
|
<br>
|
||||||
|
@ -41,12 +41,8 @@ dp.onNewMessage(filters.chat('private'), async (msg) => {
|
||||||
await msg.replyText('hiiii from mtcute! 🌸')
|
await msg.replyText('hiiii from mtcute! 🌸')
|
||||||
})
|
})
|
||||||
|
|
||||||
tg.run({
|
tg.run({ /* optional params */ }, async (self) => {
|
||||||
phone: () => tg.input('phone > '),
|
console.log(`✨ logged in as ${self.displayName}`)
|
||||||
code: () => tg.input('code > '),
|
|
||||||
password: () => tg.input('password > ')
|
|
||||||
}, async (self) => {
|
|
||||||
console.log(`logged in as ${self.displayName}`)
|
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -14,7 +14,7 @@ export function exec(cwd: string, ...cmd: string[]) {
|
||||||
if (code === 0) {
|
if (code === 0) {
|
||||||
resolve()
|
resolve()
|
||||||
} else {
|
} else {
|
||||||
reject(new Error(`Process exited with code ${code}`))
|
reject(new Error(`Process "${cmd.join(' ')}" exited with code ${code}`))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -30,12 +30,6 @@ dp.onNewMessage(filters.start, async (msg) => {
|
||||||
tg.run(
|
tg.run(
|
||||||
{{#if botToken}}
|
{{#if botToken}}
|
||||||
{ botToken: env.BOT_TOKEN },
|
{ botToken: env.BOT_TOKEN },
|
||||||
{{else}}
|
|
||||||
{
|
|
||||||
phone: () => tg.input('Phone > '),
|
|
||||||
code: () => tg.input('Code > '),
|
|
||||||
password: () => tg.input('2FA password > '),
|
|
||||||
},
|
|
||||||
{{/if}}
|
{{/if}}
|
||||||
(user) => {
|
(user) => {
|
||||||
console.log('Logged in as', user.username)
|
console.log('Logged in as', user.username)
|
||||||
|
|
|
@ -30,12 +30,6 @@ dp.onNewMessage(filters.start, async (msg) => {
|
||||||
tg.run(
|
tg.run(
|
||||||
{{#if botToken}}
|
{{#if botToken}}
|
||||||
{ botToken: env.BOT_TOKEN },
|
{ botToken: env.BOT_TOKEN },
|
||||||
{{else}}
|
|
||||||
{
|
|
||||||
phone: () => tg.input('Phone > '),
|
|
||||||
code: () => tg.input('Code > '),
|
|
||||||
password: () => tg.input('2FA password > '),
|
|
||||||
},
|
|
||||||
{{/if}}
|
{{/if}}
|
||||||
(user) => {
|
(user) => {
|
||||||
console.log('Logged in as', user.username)
|
console.log('Logged in as', user.username)
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
All-in-one package for NodeJS. Includes support for native crypto addon
|
All-in-one package for NodeJS. Includes support for native crypto addon
|
||||||
(must be installed separately, `@mtcute/crypto-node`), terminal I/O via
|
(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
|
## Usage
|
||||||
|
|
||||||
|
@ -16,4 +16,8 @@ const tg = new NodeTelegramClient({
|
||||||
apiHash: 'abcdef',
|
apiHash: 'abcdef',
|
||||||
storage: 'my-account'
|
storage: 'my-account'
|
||||||
})
|
})
|
||||||
|
|
||||||
|
tg.run(async (user) => {
|
||||||
|
console.log(`✨ logged in as ${user.displayName}`)
|
||||||
|
})
|
||||||
```
|
```
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import { createRequire } from 'module'
|
import { createRequire } from 'module'
|
||||||
import { createInterface, Interface as RlInterface } from 'readline'
|
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'
|
import { SqliteStorage } from '@mtcute/sqlite'
|
||||||
|
|
||||||
export * from '@mtcute/client'
|
export * from '@mtcute/client'
|
||||||
|
@ -20,31 +20,15 @@ try {
|
||||||
nativeCrypto = require('@mtcute/crypto-node').NodeNativeCryptoProvider
|
nativeCrypto = require('@mtcute/crypto-node').NodeNativeCryptoProvider
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
|
|
||||||
export interface NodeTelegramClientOptions extends Omit<TelegramClientOptions, 'storage'> {
|
|
||||||
/**
|
|
||||||
* 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.
|
* Tiny wrapper over {@link TelegramClient} for usage inside Node JS.
|
||||||
*
|
*
|
||||||
* This automatically sets the parse modes, native
|
* This class automatically manages native
|
||||||
* crypto addon and defaults to SQLite session.
|
* crypto addon and defaults to SQLite session (unlike `TelegarmClient`,
|
||||||
*
|
* which defaults to a JSON file on Node).
|
||||||
* Documentation for this class only contains the
|
|
||||||
* difference between {@link TelegramClient} and {@link NodeTelegramClient}.
|
|
||||||
* For the complete documentation, please refer to {@link TelegramClient}.
|
|
||||||
*/
|
*/
|
||||||
export class NodeTelegramClient extends TelegramClient {
|
export class NodeTelegramClient extends TelegramClient {
|
||||||
constructor(opts: NodeTelegramClientOptions) {
|
constructor(opts: TelegramClientOptions) {
|
||||||
super({
|
super({
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
crypto: nativeCrypto ? () => new nativeCrypto() : undefined,
|
crypto: nativeCrypto ? () => new nativeCrypto() : undefined,
|
||||||
|
@ -83,4 +67,38 @@ export class NodeTelegramClient extends TelegramClient {
|
||||||
|
|
||||||
return super.close()
|
return super.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
start(params: Parameters<TelegramClient['start']>[0] = {}): Promise<User> {
|
||||||
|
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<TelegramClient['start']>[0] | ((user: User) => void | Promise<void>),
|
||||||
|
then?: (user: User) => void | Promise<void>,
|
||||||
|
): void {
|
||||||
|
if (typeof params === 'function') {
|
||||||
|
then = params
|
||||||
|
params = {}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.start(params)
|
||||||
|
.then(then)
|
||||||
|
.catch((err) => this._emitError(err))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue