mtcute/packages/node/index.ts

111 lines
3.1 KiB
TypeScript
Raw Normal View History

import { createRequire } from 'module'
import { createInterface, Interface as RlInterface } from 'readline'
2024-02-02 04:12:02 +03:00
import { TelegramClient, TelegramClientOptions, User } from '@mtcute/core'
2021-08-05 20:38:24 +03:00
import { SqliteStorage } from '@mtcute/sqlite'
2021-06-06 00:37:44 +03:00
2024-02-02 04:12:02 +03:00
export * from '@mtcute/core'
2021-08-05 20:38:24 +03:00
export * from '@mtcute/html-parser'
export * from '@mtcute/markdown-parser'
export { SqliteStorage }
2021-06-06 00:37:44 +03:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let nativeCrypto: any
2021-06-07 23:59:17 +03:00
try {
// @only-if-esm
const require = createRequire(import.meta.url)
// @/only-if-esm
// eslint-disable-next-line
2021-08-05 20:38:24 +03:00
nativeCrypto = require('@mtcute/crypto-node').NodeNativeCryptoProvider
2021-06-07 23:59:17 +03:00
} catch (e) {}
2021-06-06 00:37:44 +03:00
/**
2022-08-29 16:22:57 +03:00
* Tiny wrapper over {@link TelegramClient} for usage inside Node JS.
2021-06-06 00:37:44 +03:00
*
* This class automatically manages native
* crypto addon and defaults to SQLite session (unlike `TelegarmClient`,
* which defaults to a JSON file on Node).
2021-06-06 00:37:44 +03:00
*/
export class NodeTelegramClient extends TelegramClient {
constructor(opts: TelegramClientOptions) {
2024-02-02 04:12:02 +03:00
if ('client' in opts) {
super(opts)
return
}
2021-06-06 00:37:44 +03:00
super({
// eslint-disable-next-line
crypto: nativeCrypto ? () => new nativeCrypto() : undefined,
2021-06-06 00:37:44 +03:00
...opts,
storage:
typeof opts.storage === 'string' ?
new SqliteStorage(opts.storage) :
opts.storage ?? new SqliteStorage('client.session'),
2021-06-06 00:37:44 +03:00
})
}
private _rl?: RlInterface
/**
* Tiny wrapper over Node `readline` package
* for simpler user input for `.run()` method.
*
* Associated `readline` interface is closed
* after `run()` returns, or with the client.
*
* @param text Text of the question
*/
input(text: string): Promise<string> {
if (!this._rl) {
this._rl = createInterface({
input: process.stdin,
output: process.stdout,
})
}
return new Promise((res) => this._rl?.question(text, res))
}
close(): Promise<void> {
this._rl?.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)
2024-02-02 04:12:02 +03:00
.catch((err) => this.emitError(err))
}
2021-06-06 00:37:44 +03:00
}