clawgram 2.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.
- package/LICENSE +22 -0
- package/README.md +589 -0
- package/dist/channel.js +1233 -0
- package/dist/clawgram-cli.js +17 -0
- package/dist/cli-core.js +236 -0
- package/dist/cli.js +25 -0
- package/dist/constants.js +7 -0
- package/dist/gramjs-client.js +437 -0
- package/dist/group-reply-address.js +79 -0
- package/dist/group-visible-reply-guard.js +48 -0
- package/dist/helpers.js +495 -0
- package/dist/history.js +286 -0
- package/dist/index.js +20 -0
- package/dist/joins.js +147 -0
- package/dist/normalize.js +125 -0
- package/dist/proxy-config.js +106 -0
- package/dist/types.js +2 -0
- package/dist/update-config.js +381 -0
- package/openclaw.plugin.json +253 -0
- package/package.json +68 -0
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const config_runtime_1 = require("openclaw/plugin-sdk/config-runtime");
|
|
5
|
+
const cli_core_1 = require("./cli-core");
|
|
6
|
+
async function main() {
|
|
7
|
+
const { snapshot } = await (0, config_runtime_1.readConfigFileSnapshotForWrite)();
|
|
8
|
+
const exitCode = await (0, cli_core_1.runTelegramUserbotStandaloneCli)(process.argv.slice(2), snapshot.config);
|
|
9
|
+
if (exitCode !== 0) {
|
|
10
|
+
process.exitCode = exitCode;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
main().catch((error) => {
|
|
14
|
+
const message = error instanceof Error ? error.stack || error.message : String(error);
|
|
15
|
+
console.error(message);
|
|
16
|
+
process.exitCode = 1;
|
|
17
|
+
});
|
package/dist/cli-core.js
ADDED
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.runTelegramUserbotCliFlags = runTelegramUserbotCliFlags;
|
|
7
|
+
exports.runTelegramUserbotStandaloneCli = runTelegramUserbotStandaloneCli;
|
|
8
|
+
const node_readline_1 = __importDefault(require("node:readline"));
|
|
9
|
+
const telegram_1 = require("telegram");
|
|
10
|
+
const sessions_1 = require("telegram/sessions");
|
|
11
|
+
const config_runtime_1 = require("openclaw/plugin-sdk/config-runtime");
|
|
12
|
+
const constants_1 = require("./constants");
|
|
13
|
+
const update_config_1 = require("./update-config");
|
|
14
|
+
function printRestartNotice() {
|
|
15
|
+
console.log("");
|
|
16
|
+
console.log("After applying config changes, restart OpenClaw:");
|
|
17
|
+
console.log("openclaw gateway restart");
|
|
18
|
+
}
|
|
19
|
+
function createPrompt() {
|
|
20
|
+
const rl = node_readline_1.default.createInterface({
|
|
21
|
+
input: process.stdin,
|
|
22
|
+
output: process.stdout,
|
|
23
|
+
});
|
|
24
|
+
const ask = (question) => new Promise((resolve) => {
|
|
25
|
+
rl.question(question, resolve);
|
|
26
|
+
});
|
|
27
|
+
return {
|
|
28
|
+
ask,
|
|
29
|
+
async askRequired(question) {
|
|
30
|
+
for (;;) {
|
|
31
|
+
const answer = (await ask(question)).trim();
|
|
32
|
+
if (answer) {
|
|
33
|
+
return answer;
|
|
34
|
+
}
|
|
35
|
+
console.log("Value is required.");
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
async askPositiveInteger(question) {
|
|
39
|
+
for (;;) {
|
|
40
|
+
const answer = (await ask(question)).trim();
|
|
41
|
+
if (!/^[1-9]\d*$/.test(answer)) {
|
|
42
|
+
console.log("Enter a positive integer.");
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
const parsed = Number(answer);
|
|
46
|
+
if (!Number.isSafeInteger(parsed)) {
|
|
47
|
+
console.log("Number is too large.");
|
|
48
|
+
continue;
|
|
49
|
+
}
|
|
50
|
+
return parsed;
|
|
51
|
+
}
|
|
52
|
+
},
|
|
53
|
+
async askYesNo(question, defaultValue = false) {
|
|
54
|
+
for (;;) {
|
|
55
|
+
const answer = (await ask(question)).trim().toLowerCase();
|
|
56
|
+
if (!answer) {
|
|
57
|
+
return defaultValue;
|
|
58
|
+
}
|
|
59
|
+
if (["y", "yes", "да", "д"].includes(answer)) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
if (["n", "no", "нет", "н"].includes(answer)) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
console.log("Please answer yes or no.");
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
close() {
|
|
69
|
+
rl.close();
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function resolveDefaultAccountId(config) {
|
|
74
|
+
const accounts = config?.channels?.[constants_1.CHANNEL_ID]?.accounts;
|
|
75
|
+
if (!accounts || typeof accounts !== "object") {
|
|
76
|
+
return "default";
|
|
77
|
+
}
|
|
78
|
+
const firstAccountId = Object.keys(accounts).find((accountId) => accountId.trim());
|
|
79
|
+
return firstAccountId?.trim() || "default";
|
|
80
|
+
}
|
|
81
|
+
function buildAccountConfigFragment(auth) {
|
|
82
|
+
return {
|
|
83
|
+
enabled: true,
|
|
84
|
+
apiId: auth.apiId,
|
|
85
|
+
apiHash: auth.apiHash,
|
|
86
|
+
sessionString: auth.sessionString,
|
|
87
|
+
allowFrom: ["*"],
|
|
88
|
+
groups: {
|
|
89
|
+
"*": {
|
|
90
|
+
enabled: true,
|
|
91
|
+
groupPolicy: "mention",
|
|
92
|
+
allowFrom: ["*"],
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
function buildConfigFragment(accountId, auth) {
|
|
98
|
+
return {
|
|
99
|
+
channels: {
|
|
100
|
+
[constants_1.CHANNEL_ID]: {
|
|
101
|
+
accounts: {
|
|
102
|
+
[accountId]: buildAccountConfigFragment(auth),
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
async function runTelegramAuthorization(prompt) {
|
|
109
|
+
const apiId = await prompt.askPositiveInteger("Please enter your apiId: ");
|
|
110
|
+
const apiHash = await prompt.askRequired("Please enter your apiHash: ");
|
|
111
|
+
const client = new telegram_1.TelegramClient(new sessions_1.StringSession(""), apiId, apiHash, {
|
|
112
|
+
connectionRetries: 5,
|
|
113
|
+
});
|
|
114
|
+
try {
|
|
115
|
+
await client.start({
|
|
116
|
+
phoneNumber: async () => await prompt.askRequired("Please enter your number: "),
|
|
117
|
+
password: async () => await prompt.askRequired("Please enter your password: "),
|
|
118
|
+
phoneCode: async () => await prompt.askRequired("Please enter the code you received: "),
|
|
119
|
+
onError: (error) => {
|
|
120
|
+
console.log(error);
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
return {
|
|
124
|
+
apiId,
|
|
125
|
+
apiHash,
|
|
126
|
+
sessionString: String(client.session.save()),
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
finally {
|
|
130
|
+
await client.destroy().catch(() => undefined);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
async function runTelegramUserbotAuth(config) {
|
|
134
|
+
const prompt = createPrompt();
|
|
135
|
+
try {
|
|
136
|
+
console.log("Starting Clawgram authorization...");
|
|
137
|
+
const auth = await runTelegramAuthorization(prompt);
|
|
138
|
+
console.log("Telegram authorization completed successfully.");
|
|
139
|
+
console.log("");
|
|
140
|
+
console.log("Session string:");
|
|
141
|
+
console.log(auth.sessionString);
|
|
142
|
+
console.log("");
|
|
143
|
+
const defaultAccountId = resolveDefaultAccountId(config);
|
|
144
|
+
const rawAccountId = await prompt.ask(`Enter account id for config [${defaultAccountId}]: `);
|
|
145
|
+
const accountId = rawAccountId.trim() || defaultAccountId;
|
|
146
|
+
const shouldUpdateConfig = await prompt.askYesNo("Update OpenClaw config automatically? [y/N]: ", false);
|
|
147
|
+
const { snapshot } = await (0, config_runtime_1.readConfigFileSnapshotForWrite)();
|
|
148
|
+
if (!shouldUpdateConfig) {
|
|
149
|
+
console.log("");
|
|
150
|
+
console.log("JSON fragment for manual insertion:");
|
|
151
|
+
console.log(JSON.stringify(buildConfigFragment(accountId, auth), null, 2));
|
|
152
|
+
printRestartNotice();
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
if (!snapshot.exists || !snapshot.path) {
|
|
156
|
+
console.log("");
|
|
157
|
+
console.log("Automatic config update is unavailable because openclaw.json was not found.");
|
|
158
|
+
console.log("");
|
|
159
|
+
console.log("JSON fragment for manual insertion:");
|
|
160
|
+
console.log(JSON.stringify(buildConfigFragment(accountId, auth), null, 2));
|
|
161
|
+
printRestartNotice();
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
try {
|
|
165
|
+
const backupPath = await (0, update_config_1.createConfigBackup)(snapshot.path);
|
|
166
|
+
await (0, update_config_1.updateConfigFileDirectly)(snapshot.path, accountId, auth);
|
|
167
|
+
console.log("");
|
|
168
|
+
console.log(`OpenClaw config updated: ${snapshot.path}`);
|
|
169
|
+
console.log(`Configured account id: ${accountId}`);
|
|
170
|
+
if (backupPath) {
|
|
171
|
+
console.log(`Config backup created: ${backupPath}`);
|
|
172
|
+
}
|
|
173
|
+
printRestartNotice();
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
178
|
+
console.log("");
|
|
179
|
+
console.log(`Automatic config update failed: ${message}`);
|
|
180
|
+
if (snapshot.issues.length > 0) {
|
|
181
|
+
console.log("Current config validation issues:");
|
|
182
|
+
for (const issue of snapshot.issues) {
|
|
183
|
+
console.log(`- ${issue.path || "<root>"}: ${issue.message}`);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
console.log("");
|
|
187
|
+
console.log("JSON fragment for manual insertion:");
|
|
188
|
+
console.log(JSON.stringify(buildConfigFragment(accountId, auth), null, 2));
|
|
189
|
+
printRestartNotice();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
finally {
|
|
193
|
+
prompt.close();
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
async function runTelegramUserbotCliFlags(config, options) {
|
|
197
|
+
const enabledFlags = [options.hello, options.auth].filter(Boolean).length;
|
|
198
|
+
if (enabledFlags === 0) {
|
|
199
|
+
console.log("Specify one flag: --hello or --auth");
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
if (enabledFlags > 1) {
|
|
203
|
+
console.log("Use only one flag at a time: --hello or --auth");
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
if (options.hello) {
|
|
207
|
+
console.log("Hello from clawgram");
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
if (options.auth) {
|
|
211
|
+
await runTelegramUserbotAuth(config);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
async function runTelegramUserbotStandaloneCli(argv, config) {
|
|
215
|
+
const flags = new Set(argv);
|
|
216
|
+
const hasHelp = flags.has("-h") || flags.has("--help") || flags.has("help");
|
|
217
|
+
if (argv.length === 0 || hasHelp) {
|
|
218
|
+
console.log("Usage: clawgram-cli <--hello|--auth>");
|
|
219
|
+
return 0;
|
|
220
|
+
}
|
|
221
|
+
const unsupportedArgs = argv.filter((arg) => !["--hello", "--auth"].includes(arg));
|
|
222
|
+
if (unsupportedArgs.length > 0) {
|
|
223
|
+
console.log(`Unknown argument(s): ${unsupportedArgs.join(", ")}`);
|
|
224
|
+
console.log("Usage: clawgram-cli <--hello|--auth>");
|
|
225
|
+
return 1;
|
|
226
|
+
}
|
|
227
|
+
if (flags.has("--hello") || flags.has("--auth")) {
|
|
228
|
+
await runTelegramUserbotCliFlags(config, {
|
|
229
|
+
hello: flags.has("--hello"),
|
|
230
|
+
auth: flags.has("--auth"),
|
|
231
|
+
});
|
|
232
|
+
return 0;
|
|
233
|
+
}
|
|
234
|
+
console.log("Specify one flag: --hello or --auth");
|
|
235
|
+
return 1;
|
|
236
|
+
}
|
package/dist/cli.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.registerTelegramUserbotCli = registerTelegramUserbotCli;
|
|
4
|
+
exports.getTelegramUserbotCliDescriptors = getTelegramUserbotCliDescriptors;
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
const cli_core_1 = require("./cli-core");
|
|
7
|
+
function registerTelegramUserbotCli(program, config) {
|
|
8
|
+
program
|
|
9
|
+
.command(constants_1.CLI_COMMAND)
|
|
10
|
+
.description("Clawgram CLI utilities")
|
|
11
|
+
.option("--hello", "Print a greeting from the clawgram plugin")
|
|
12
|
+
.option("--auth", "Authorize a Telegram account for clawgram")
|
|
13
|
+
.action(async (options) => {
|
|
14
|
+
await (0, cli_core_1.runTelegramUserbotCliFlags)(config, options);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
function getTelegramUserbotCliDescriptors() {
|
|
18
|
+
return [
|
|
19
|
+
{
|
|
20
|
+
name: constants_1.CLI_COMMAND,
|
|
21
|
+
description: "Clawgram CLI utilities",
|
|
22
|
+
hasSubcommands: false,
|
|
23
|
+
},
|
|
24
|
+
];
|
|
25
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CLI_COMMAND = exports.CHANNEL_ID = void 0;
|
|
4
|
+
const CHANNEL_ID = "clawgram";
|
|
5
|
+
exports.CHANNEL_ID = CHANNEL_ID;
|
|
6
|
+
const CLI_COMMAND = "clawgram";
|
|
7
|
+
exports.CLI_COMMAND = CLI_COMMAND;
|