mytglib 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +121 -0
  2. package/dist/index.js +1 -0
  3. package/package.json +48 -0
package/README.md ADDED
@@ -0,0 +1,121 @@
1
+ # mytglib
2
+
3
+ `mytglib` 是对 `@mtcute/node` 登录流程的轻量封装,提供 Android/iOS 客户端配置、手机号与代理检查、验证码登录、结果消息解析和设备模型生成。
4
+
5
+ ## 环境
6
+
7
+ - Node.js 20+
8
+ - 执行登录或代理检查时,需要一个可连接 Telegram 主 DC 的 HTTP 或 SOCKS5 代理
9
+
10
+ 安装当前仓库依赖:
11
+
12
+ ```bash
13
+ npm install
14
+ ```
15
+
16
+ ## 构建
17
+
18
+ 生成供发布和包根入口使用的 ESM 产物:
19
+
20
+ ```bash
21
+ npm run build
22
+ ```
23
+
24
+ 构建结果位于 `dist/index.js`。Webpack 会打包本仓库源码,使用 Terser 压缩,并对标识符和字符串数组进行混淆;`@mtcute/node` 等运行时依赖保持为外部 ESM 依赖,不会重复打入产物。构建不生成 source map,发布包仅包含 `dist`、`README.md`、`package.json` 和许可证文件。
25
+
26
+ ## 离线示例
27
+
28
+ 无需网络即可检查手机号解析、随机设备参数、验证码设置和消息映射:
29
+
30
+ ```bash
31
+ npm run example:inspect
32
+ npm run example:inspect -- "+1 (202) 555-0123"
33
+ ```
34
+
35
+ 实现位于 `examples/inspect-config.js`。
36
+
37
+ ## 初始化与登录
38
+
39
+ 完整示例位于 `examples/login.js`。先查看所需环境变量:
40
+
41
+ ```bash
42
+ npm run example:login -- --help
43
+ ```
44
+
45
+ 以 HTTP 代理运行 Android 登录:
46
+
47
+ ```bash
48
+ TG_PHONE="+12025550123" \
49
+ TG_PROXY_HOST="127.0.0.1" \
50
+ TG_PROXY_PORT="8080" \
51
+ TG_PROXY_TYPE="http" \
52
+ TG_DEVICE_TOKEN="your-device-token" \
53
+ npm run example:login
54
+ ```
55
+
56
+ 示例按服务端响应处理邮箱设置、邮箱验证码、手机验证码和新账号注册。reCAPTCHA 出现时会显示 `action` 与 `siteKey`,并等待输入外部移动端求解器返回的 token。
57
+
58
+ ## 基本 API
59
+
60
+ 下面是常见的短信验证码登录路径;邮箱设置、新账号注册等完整分支见 `examples/login.js`。
61
+
62
+ ```js
63
+ import { resolve } from "node:path";
64
+ import { CoreClient, getCallMessage } from "mytglib";
65
+
66
+ const core = new CoreClient({
67
+ clientType: "android",
68
+ phone: "+12025550123",
69
+ proxy: { type: "http", host: "127.0.0.1", port: 8080 },
70
+ sessionDirPath: resolve("telegram-sessions"),
71
+ deviceTokenResolver: async () => "your-device-token",
72
+ recaptchaMobileTokenResolver: async () => process.env.TG_RECAPTCHA_TOKEN,
73
+ });
74
+
75
+ try {
76
+ await core.init();
77
+ let response = await core.sendCode();
78
+ console.log(getCallMessage(response));
79
+
80
+ if (response._ === "auth.sentCode") {
81
+ response = await core.verifyPhoneCode({ phoneCode: "12345" });
82
+ console.log(getCallMessage(response));
83
+ } else if (response._ === "auth.sentCodeSuccess") {
84
+ response = response.authorization;
85
+ }
86
+
87
+ if (response._ !== "auth.authorization") {
88
+ throw new Error(`Login did not complete: ${response._}`);
89
+ }
90
+
91
+ const config = await core.client.call({ _: "help.getConfig" });
92
+ console.log(config.thisDc);
93
+ } finally {
94
+ await core.client?.destroy();
95
+ }
96
+ ```
97
+
98
+ `sessionDirPath` 是必传的会话主目录。路径已存在时必须是目录;路径不存在时会在初始化过程中递归创建。`init()` 会按照 `<主目录>/<+手机号>/<+手机号>.session` 创建 SQLite 会话文件,然后依次解析设备 token 并连接客户端。登录相关公共异步方法均返回原始 Telegram TL 响应;原始请求可通过已连接的 `core.client.call()` 发送。无论成功或失败,都应在 `finally` 中调用 `destroy()`,永久关闭连接、定时器和存储资源。
99
+
100
+ ## 代理检查
101
+
102
+ ```js
103
+ import { checkProxy } from "mytglib";
104
+
105
+ const result = await checkProxy({
106
+ clientType: "android",
107
+ proxy: "socks5://user:password@127.0.0.1:1080",
108
+ timeoutMs: 12_000,
109
+ });
110
+
111
+ console.log(result);
112
+ ```
113
+
114
+ `checkProxy` 会并行检查所选平台的全部五个 Telegram 主 DC,并关闭每个已建立的连接。
115
+
116
+ ## 测试
117
+
118
+ ```bash
119
+ npm test
120
+ npm run test:coverage
121
+ ```