mtcute/packages/node/index.ts

106 lines
3.1 KiB
TypeScript
Raw Normal View History

import { createRequire } from 'module'
import { createInterface, Interface as RlInterface } from 'readline'
2023-10-23 12:25:39 +03:00
import { TelegramClient, TelegramClientOptions } from '@mtcute/client'
2021-08-05 20:38:24 +03:00
import { HtmlMessageEntityParser } from '@mtcute/html-parser'
import { MarkdownMessageEntityParser } from '@mtcute/markdown-parser'
import { SqliteStorage } from '@mtcute/sqlite'
2021-06-06 00:37:44 +03:00
2021-08-05 20:38:24 +03:00
export * from '@mtcute/client'
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) {}
export interface NodeTelegramClientOptions extends Omit<TelegramClientOptions, 'storage'> {
2022-08-29 16:22:57 +03:00
/**
* Default parse mode to use.
*
* Both HTML and Markdown parse modes are
* registered automatically.
*
* Defaults to `html`
2022-08-29 16:22:57 +03:00
*/
defaultParseMode?: 'html' | 'markdown'
/**
* 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
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 automatically sets the parse modes, native
* crypto addon and defaults to SQLite session.
2022-08-29 16:22:57 +03:00
*
* Documentation for this class only contains the
* difference between {@link TelegramClient} and {@link NodeTelegramClient}.
* For the complete documentation, please refer to {@link TelegramClient}.
2021-06-06 00:37:44 +03:00
*/
export class NodeTelegramClient extends TelegramClient {
constructor(opts: NodeTelegramClientOptions) {
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
})
this.registerParseMode(new HtmlMessageEntityParser())
this.registerParseMode(new MarkdownMessageEntityParser())
if (opts.defaultParseMode) {
this.setDefaultParseMode(opts.defaultParseMode)
}
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()
}
2021-06-06 00:37:44 +03:00
}