mytglib 2.2.0 → 2.2.1

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 +110 -0
  2. package/dist/index.js +1 -1
  3. package/package.json +1 -1
package/README.md CHANGED
@@ -1 +1,111 @@
1
1
  # mytglib
2
+
3
+ Telegram 会话创建、转换和登录后 RPC。Node 使用 `@mtcute/node`,Rust 使用仓库内的 `grammers-core`。
4
+
5
+ ## 使用已有会话
6
+
7
+ ```js
8
+ import { CoreClient } from "mytglib";
9
+
10
+ const core = new CoreClient({
11
+ sessionSource: { type: "native", sessionFilePath: "/absolute/account.session" },
12
+ timeout: 30_000,
13
+ // proxy: { type: "socks5", host: "127.0.0.1", port: 1080 },
14
+ });
15
+ try {
16
+ const client = await core.init();
17
+ const users = await client.call({ _: "users.getUsers", id: [{ _: "inputUserSelf" }] });
18
+ console.log(users);
19
+ console.log(await core.checkAccountStatus());
20
+ } finally {
21
+ await core.destroy();
22
+ }
23
+ ```
24
+
25
+ `native` 指 Node 的 mtcute SQLite 运行文件,必须已经保存授权及连接身份。原生会话直接打开,不复制。`destroy()` 关闭连接和存储,保留文件供下次重开。
26
+
27
+ 首次导入外部格式时,将 `sessionSource` 换为以下一种:
28
+
29
+ ```js
30
+ // Telethon / mtcute .session + 配套 JSON
31
+ const sessionSource = {
32
+ type: "sessionJson",
33
+ sessionFilePath: "/absolute/source.session",
34
+ sessionJsonFilePath: "/absolute/source.json",
35
+ outputSessionPath: "/absolute/new-runtime.session",
36
+ };
37
+
38
+ // 或 tdata(多账号目录需指定 accountIndex)
39
+ const tdataSource = {
40
+ type: "tdata",
41
+ tdataPath: "/absolute/tdata",
42
+ outputSessionPath: "/absolute/new-runtime.session",
43
+ passcode: "",
44
+ // accountIndex: 0,
45
+ };
46
+ ```
47
+
48
+ 输出必须是新文件。导入成功后只需保存运行文件路径,后续使用 `native` 打开。连接失败不会丢掉已经完成的导入;同一个 Core 实例重试时也会直接重开运行文件。
49
+
50
+ 已有会话使用保存的 API、设备、版本与语言,不需要设备 token resolver,也不需要重新登录。tdata 使用现有 Desktop 配置规则生成并持久保存连接身份。不要同时传入注册用的 `clientType`、手机号或身份覆盖配置。
51
+
52
+ ## 新建与登录
53
+
54
+ 新建流程继续使用原来的 `CoreClient({ clientType, phone, sessionDirPath, deviceTokenResolver, ... })`。完整示例覆盖初始化、发码、验证码/密码处理、请求与关闭:
55
+
56
+ ```sh
57
+ node examples/login.js --help
58
+ node examples/account-status.js --help
59
+ ```
60
+
61
+ 登录示例所需的代理和设备 token 提供方由调用方配置。已有会话不经过该新建流程。
62
+
63
+ ## Rust
64
+
65
+ 使用 `CoreClientConfig::from_session(SessionSource)`,再由 `CoreClient::init()` 返回 `CoreRpcClient`。三种来源为:
66
+
67
+ - `SessionSource::Grammers`:打开已有 `.grammers`。
68
+ - `SessionSource::SessionJson`:`.session + .json` 导入指定的新 `.grammers`。
69
+ - `SessionSource::Tdata`:选定 tdata 账号导入指定的新 `.grammers`,`profile` 可用 `DesktopProfile::default()`。
70
+
71
+ 普通请求使用 `rpc.invoke()` / `rpc.call()`,测活使用 `rpc.check_account_status()`,最后调用 `core.destroy().await`。两种外部格式会写入真正的原生 SQLite 授权表和连接身份;移除来源文件后仍能重新打开。
72
+
73
+ ```sh
74
+ cargo run --manifest-path vendor/grammers/Cargo.toml -p grammers-core --example account_status -- --help
75
+ ```
76
+
77
+ ## 行为与迁移
78
+
79
+ - 已有授权失效会返回错误,不自动新建授权替代。一次只能有一个所有者使用同一授权;调用方也应协调其他进程。
80
+ - 来源转换是离线操作,转换成功不代表远端授权仍有效。可用 `users.getUsers(inputUserSelf)` 验证登录后请求。
81
+ - `syncSessionJson()` / `sync_session_json()` 显式刷新导出件;关闭客户端不会自动执行导出。
82
+ - 原独立 `checkAccountStatus(options)` / `check_account_status(options)` 已由 Core 方法替代,属于公共 API 变更。桌面客户端接入另行进行,其原固定 revision 不在本次修改范围。
83
+ - 普通 Core 会处理协议控制包和保存运行状态,不再提供旧测活的只读快照、单业务包连接契约。
84
+
85
+ 完整边界和调用方式见 [已有会话](docs/session-reuse.md) 与 [账号测活](docs/account-status.md)。
86
+
87
+ ## 双向限制检测
88
+
89
+ Node 使用 `core.checkAccountRestriction()`,Rust 使用 `rpc.check_account_restriction()`;两端也提供仅重新解释已留存正文的接口。Core 负责 SpamBot 查询、跨国家缓存匹配、AI 提示词/校验和时间模板。正常检测复用身份查询中的手机号,通过本地 `checkPhone` / `check_phone` 得到国家代码,不再识别回复语言,也不携带 Lingua 模型;业务方负责共享国家目录、首次 TXT 样本及账号状态持久化。
90
+
91
+ AI 分类使用共享自定义 tone:传入 `aiToneSlug` / `ai_tone_slug`,各账号解析后使用自己的 AI 配额,无需安装。提示词保持已验证的 `spambot-classifier-v1` 原文与四字段输出契约,可继续使用原 tone;本地规则命中或协议冻结时不需要 AI 配置。配置、错误处理与恢复示例见 [双向限制检测](docs/account-restriction.md)。
92
+
93
+ 业务端在写锁内使用 `mergeSpamBotRule()` / `merge_spambot_rule()` 合并最新索引并原子保存;每国家每类别只保存第一份原始 TXT。新规则版本为 `spambot-country-v1`,使用 `countryCode`;旧语言库原样搁置,不读取、迁移或删除。规则加载回调一次返回新库全部国家索引(Rust 为 `Vec<SpamBotRuleIndex>`),空库返回空数组。结果以可空 `countryCode` 替代语言及模型字段,属于需要消费方适配的 API 变更。
94
+
95
+ ## 验证
96
+
97
+ ```sh
98
+ npm test
99
+ cargo test --locked --manifest-path vendor/grammers/Cargo.toml -p grammers-core
100
+ ```
101
+
102
+ Node 需使用与本机 `better-sqlite3` ABI 匹配的版本。本仓库当前依赖支持的 Node 版本见 `package.json`。
103
+
104
+ Rust 的本地加密协议集成测试需先构建示例,再通过环境变量启用;未指定时 `npm test` 会跳过这部分用例:
105
+
106
+ ```sh
107
+ cargo build --locked --manifest-path vendor/grammers/Cargo.toml -p grammers-core --example account_status
108
+ TG_ACCOUNT_CHECK_RUST_BIN="$PWD/vendor/grammers/target/debug/examples/account_status" node --test tests/account-status-rust.test.js
109
+ ```
110
+
111
+ 若设置了 `CARGO_TARGET_DIR`,将示例路径改为实际构建输出目录。协议测试使用本地 peer 和合成授权,不连接 Telegram。