@prajwalshete/telegram-plugin 0.0.0-stage → 0.1.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 (295) hide show
  1. package/CHANGELOG.md +87 -0
  2. package/LICENSE +21 -0
  3. package/README.md +109 -2
  4. package/dist/adapters/clock.d.ts +3 -0
  5. package/dist/adapters/clock.js +4 -0
  6. package/dist/adapters/console-log.d.ts +13 -0
  7. package/dist/adapters/console-log.js +41 -0
  8. package/dist/adapters/d1-db.d.ts +5 -0
  9. package/dist/adapters/d1-db.js +37 -0
  10. package/dist/adapters/ids.d.ts +5 -0
  11. package/dist/adapters/ids.js +25 -0
  12. package/dist/adapters/index.d.ts +7 -0
  13. package/dist/adapters/keyring.d.ts +50 -0
  14. package/dist/adapters/keyring.js +174 -0
  15. package/dist/adapters/metrics.d.ts +28 -0
  16. package/dist/adapters/metrics.js +48 -0
  17. package/dist/adapters/r2-files.d.ts +20 -0
  18. package/dist/adapters/r2-files.js +53 -0
  19. package/dist/bin/cli.d.ts +1 -0
  20. package/dist/bin/cli.js +45 -0
  21. package/dist/client/broadcasts.d.ts +35 -0
  22. package/dist/client/commands.d.ts +23 -0
  23. package/dist/client/endpoint.d.ts +34 -0
  24. package/dist/client/events.d.ts +33 -0
  25. package/dist/client/index.d.ts +11 -0
  26. package/dist/client/index.js +3 -0
  27. package/dist/client/schedules.d.ts +42 -0
  28. package/dist/client/schema.d.ts +181 -0
  29. package/dist/client/send.d.ts +15 -0
  30. package/dist/client/templates.d.ts +47 -0
  31. package/dist/contracts/bots.d.ts +129 -0
  32. package/dist/contracts/bots.js +70 -0
  33. package/dist/contracts/broadcasts.d.ts +116 -0
  34. package/dist/contracts/broadcasts.js +63 -0
  35. package/dist/contracts/chats.d.ts +97 -0
  36. package/dist/contracts/chats.js +66 -0
  37. package/dist/contracts/commands.d.ts +52 -0
  38. package/dist/contracts/commands.js +53 -0
  39. package/dist/contracts/common.d.ts +38 -0
  40. package/dist/contracts/common.js +41 -0
  41. package/dist/contracts/deliveries.d.ts +103 -0
  42. package/dist/contracts/deliveries.js +74 -0
  43. package/dist/contracts/destinations.d.ts +24 -0
  44. package/dist/contracts/destinations.js +28 -0
  45. package/dist/contracts/events.d.ts +165 -0
  46. package/dist/contracts/events.js +98 -0
  47. package/dist/contracts/features.d.ts +20 -0
  48. package/dist/contracts/features.js +21 -0
  49. package/dist/contracts/health.d.ts +55 -0
  50. package/dist/contracts/health.js +58 -0
  51. package/dist/contracts/index.d.ts +22 -0
  52. package/dist/contracts/index.js +22 -0
  53. package/dist/contracts/labels.d.ts +38 -0
  54. package/dist/contracts/labels.js +52 -0
  55. package/dist/contracts/links.d.ts +51 -0
  56. package/dist/contracts/links.js +50 -0
  57. package/dist/contracts/meta.d.ts +132 -0
  58. package/dist/contracts/meta.js +85 -0
  59. package/dist/contracts/permissions.d.ts +24 -0
  60. package/dist/contracts/permissions.js +27 -0
  61. package/dist/contracts/problems.d.ts +106 -0
  62. package/dist/contracts/problems.js +101 -0
  63. package/dist/contracts/recipients.d.ts +36 -0
  64. package/dist/contracts/recipients.js +22 -0
  65. package/dist/contracts/schedules.d.ts +276 -0
  66. package/dist/contracts/schedules.js +143 -0
  67. package/dist/contracts/send.d.ts +126 -0
  68. package/dist/contracts/send.js +69 -0
  69. package/dist/contracts/subscriptions.d.ts +38 -0
  70. package/dist/contracts/subscriptions.js +31 -0
  71. package/dist/contracts/templates.d.ts +358 -0
  72. package/dist/contracts/templates.js +174 -0
  73. package/dist/contracts/users.d.ts +15 -0
  74. package/dist/contracts/users.js +19 -0
  75. package/dist/core/guards.d.ts +22 -0
  76. package/dist/core/guards.js +47 -0
  77. package/dist/core/html/escape.d.ts +2 -0
  78. package/dist/core/html/escape.js +36 -0
  79. package/dist/core/html/tokenize.js +65 -0
  80. package/dist/core/index.d.ts +18 -0
  81. package/dist/core/pacing.d.ts +1 -0
  82. package/dist/core/pacing.js +86 -0
  83. package/dist/core/random.d.ts +2 -0
  84. package/dist/core/random.js +38 -0
  85. package/dist/core/recipients.d.ts +1 -0
  86. package/dist/core/recipients.js +86 -0
  87. package/dist/core/schedule/describe.d.ts +3 -0
  88. package/dist/core/schedule/describe.js +74 -0
  89. package/dist/core/schedule/index.d.ts +4 -0
  90. package/dist/core/schedule/policy.d.ts +1 -0
  91. package/dist/core/schedule/policy.js +37 -0
  92. package/dist/core/schedule/rule.d.ts +25 -0
  93. package/dist/core/schedule/rule.js +142 -0
  94. package/dist/core/schedule/tz.d.ts +2 -0
  95. package/dist/core/schedule/tz.js +59 -0
  96. package/dist/core/split/plain.d.ts +2 -0
  97. package/dist/core/split/plain.js +23 -0
  98. package/dist/core/split/split.d.ts +15 -0
  99. package/dist/core/split/split.js +158 -0
  100. package/dist/core/template/ast.d.ts +58 -0
  101. package/dist/core/template/ast.js +5 -0
  102. package/dist/core/template/expr.js +108 -0
  103. package/dist/core/template/filters.d.ts +20 -0
  104. package/dist/core/template/filters.js +127 -0
  105. package/dist/core/template/format.d.ts +17 -0
  106. package/dist/core/template/format.js +88 -0
  107. package/dist/core/template/index.d.ts +20 -0
  108. package/dist/core/template/index.js +29 -0
  109. package/dist/core/template/parse.d.ts +8 -0
  110. package/dist/core/template/parse.js +369 -0
  111. package/dist/core/template/render.d.ts +17 -0
  112. package/dist/core/template/render.js +118 -0
  113. package/dist/core/template/shape.d.ts +28 -0
  114. package/dist/core/template/shape.js +151 -0
  115. package/dist/core/template/tags.js +105 -0
  116. package/dist/core/template/validate.d.ts +10 -0
  117. package/dist/core/template/validate.js +144 -0
  118. package/dist/core/template/variables.d.ts +15 -0
  119. package/dist/core/template/variables.js +41 -0
  120. package/dist/do/index.d.ts +3 -0
  121. package/dist/do/index.js +3 -0
  122. package/dist/do/telegram-bot-do.d.ts +45 -0
  123. package/dist/do/telegram-bot-do.js +219 -0
  124. package/dist/engine/actions/callback.js +256 -0
  125. package/dist/engine/actions/tokens.d.ts +2 -0
  126. package/dist/engine/actions/tokens.js +51 -0
  127. package/dist/engine/bot-api.js +20 -0
  128. package/dist/engine/bot-features.js +39 -0
  129. package/dist/engine/chat-state.js +66 -0
  130. package/dist/engine/content/audience.js +54 -0
  131. package/dist/engine/content/buttons.d.ts +1 -0
  132. package/dist/engine/content/buttons.js +113 -0
  133. package/dist/engine/content/fanout.js +284 -0
  134. package/dist/engine/content/intersect.d.ts +6 -0
  135. package/dist/engine/content/intersect.js +26 -0
  136. package/dist/engine/content/providers.d.ts +6 -0
  137. package/dist/engine/content/providers.js +142 -0
  138. package/dist/engine/content/recipients.js +128 -0
  139. package/dist/engine/content/render.js +138 -0
  140. package/dist/engine/content/reply.js +156 -0
  141. package/dist/engine/content/report-files.js +78 -0
  142. package/dist/engine/content/template-rows.js +74 -0
  143. package/dist/engine/dialogs/dialog.d.ts +2 -0
  144. package/dist/engine/dialogs/dialog.js +375 -0
  145. package/dist/engine/dispatch/builtins.js +198 -0
  146. package/dist/engine/dispatch/commands.js +194 -0
  147. package/dist/engine/dispatch/context.d.ts +2 -0
  148. package/dist/engine/dispatch/context.js +194 -0
  149. package/dist/engine/dispatch/dispatcher.js +66 -0
  150. package/dist/engine/dispatch/handlers.d.ts +1 -0
  151. package/dist/engine/dispatch/handlers.js +103 -0
  152. package/dist/engine/dispatch/lifecycle.js +85 -0
  153. package/dist/engine/dispatch/link-chat.js +31 -0
  154. package/dist/engine/dispatch/linking.js +273 -0
  155. package/dist/engine/errors.d.ts +60 -0
  156. package/dist/engine/errors.js +72 -0
  157. package/dist/engine/events/filters.d.ts +5 -0
  158. package/dist/engine/events/filters.js +82 -0
  159. package/dist/engine/events/kick.js +30 -0
  160. package/dist/engine/events/payload.d.ts +3 -0
  161. package/dist/engine/events/payload.js +16 -0
  162. package/dist/engine/events/tick.js +208 -0
  163. package/dist/engine/i18n.d.ts +12 -0
  164. package/dist/engine/i18n.js +55 -0
  165. package/dist/engine/inbound/files.js +121 -0
  166. package/dist/engine/inbound/handler.js +46 -0
  167. package/dist/engine/jobs.d.ts +32 -0
  168. package/dist/engine/jobs.js +85 -0
  169. package/dist/engine/link-codes.js +46 -0
  170. package/dist/engine/metrics.js +12 -0
  171. package/dist/engine/options.d.ts +369 -0
  172. package/dist/engine/options.js +81 -0
  173. package/dist/engine/outbox/dead-letter.js +78 -0
  174. package/dist/engine/outbox/do-registry.d.ts +10 -0
  175. package/dist/engine/outbox/do-registry.js +10 -0
  176. package/dist/engine/outbox/do-transport.d.ts +33 -0
  177. package/dist/engine/outbox/do-transport.js +73 -0
  178. package/dist/engine/outbox/drain.d.ts +13 -0
  179. package/dist/engine/outbox/drain.js +381 -0
  180. package/dist/engine/outbox/enqueue.d.ts +1 -0
  181. package/dist/engine/outbox/enqueue.js +63 -0
  182. package/dist/engine/outbox/select.js +24 -0
  183. package/dist/engine/outbox/send.js +362 -0
  184. package/dist/engine/outbox/transport.d.ts +35 -0
  185. package/dist/engine/outbox/transport.js +175 -0
  186. package/dist/engine/outbox/wake.js +28 -0
  187. package/dist/engine/registry.d.ts +502 -0
  188. package/dist/engine/registry.js +258 -0
  189. package/dist/engine/rows.d.ts +25 -0
  190. package/dist/engine/rows.js +131 -0
  191. package/dist/engine/runtime.d.ts +95 -0
  192. package/dist/engine/runtime.js +149 -0
  193. package/dist/engine/schedules/fire.js +208 -0
  194. package/dist/engine/schedules/resume.js +75 -0
  195. package/dist/engine/schedules/rows.js +69 -0
  196. package/dist/engine/schedules/tick.js +30 -0
  197. package/dist/engine/schedules/wake.js +50 -0
  198. package/dist/engine/state.d.ts +11 -0
  199. package/dist/engine/state.js +78 -0
  200. package/dist/locales/en.js +339 -0
  201. package/dist/locales/en.json +341 -0
  202. package/dist/locales/mr.js +339 -0
  203. package/dist/locales/mr.json +341 -0
  204. package/dist/ports/db.d.ts +22 -0
  205. package/dist/ports/index.d.ts +4 -0
  206. package/dist/ports/ports.d.ts +257 -0
  207. package/dist/ports/sql.d.ts +39 -0
  208. package/dist/ports/sql.js +76 -0
  209. package/dist/renderers/csv.d.ts +14 -0
  210. package/dist/renderers/csv.js +51 -0
  211. package/dist/renderers/html.d.ts +44 -0
  212. package/dist/renderers/html.js +81 -0
  213. package/dist/renderers/index.d.ts +5 -0
  214. package/dist/renderers/index.js +4 -0
  215. package/dist/renderers/table.d.ts +30 -0
  216. package/dist/renderers/table.js +114 -0
  217. package/dist/routes/admin.js +146 -0
  218. package/dist/routes/app.d.ts +3111 -0
  219. package/dist/routes/app.js +9 -0
  220. package/dist/routes/broadcasts.js +37 -0
  221. package/dist/routes/commands.js +35 -0
  222. package/dist/routes/content.js +12 -0
  223. package/dist/routes/events.js +36 -0
  224. package/dist/routes/http.d.ts +30 -0
  225. package/dist/routes/http.js +223 -0
  226. package/dist/routes/me.js +59 -0
  227. package/dist/routes/schedules.js +62 -0
  228. package/dist/routes/send.js +24 -0
  229. package/dist/routes/templates.js +70 -0
  230. package/dist/routes/webhook.js +147 -0
  231. package/dist/schema/columns.js +307 -0
  232. package/dist/schema/ddl.d.ts +7 -0
  233. package/dist/schema/ddl.js +45 -0
  234. package/dist/schema/index.d.ts +4255 -0
  235. package/dist/schema/index.js +44 -0
  236. package/dist/schema/reference.sql +359 -0
  237. package/dist/schema/tables.d.ts +4243 -0
  238. package/dist/schema/tables.js +327 -0
  239. package/dist/schema/version.d.ts +5 -0
  240. package/dist/schema/version.js +7 -0
  241. package/dist/server/index.d.ts +55 -0
  242. package/dist/server/index.js +46 -0
  243. package/dist/server/plugin.d.ts +81 -0
  244. package/dist/server/plugin.js +247 -0
  245. package/dist/server/version.d.ts +2 -0
  246. package/dist/server/version.js +4 -0
  247. package/dist/services/ai-tool.d.ts +26 -0
  248. package/dist/services/ai-tool.js +148 -0
  249. package/dist/services/bots.js +300 -0
  250. package/dist/services/broadcasts.js +390 -0
  251. package/dist/services/chats.js +83 -0
  252. package/dist/services/cleanup.js +83 -0
  253. package/dist/services/commands.js +140 -0
  254. package/dist/services/deliveries.js +53 -0
  255. package/dist/services/destinations.js +91 -0
  256. package/dist/services/event-rules.js +229 -0
  257. package/dist/services/events.js +20 -0
  258. package/dist/services/idempotency.js +64 -0
  259. package/dist/services/links.js +200 -0
  260. package/dist/services/menu.js +95 -0
  261. package/dist/services/ops.js +90 -0
  262. package/dist/services/rekey.js +63 -0
  263. package/dist/services/reports.js +87 -0
  264. package/dist/services/schedules.js +214 -0
  265. package/dist/services/send.d.ts +14 -0
  266. package/dist/services/send.js +421 -0
  267. package/dist/services/subscriptions.js +28 -0
  268. package/dist/services/templates.js +259 -0
  269. package/dist/telegram/classify.d.ts +30 -0
  270. package/dist/telegram/classify.js +164 -0
  271. package/dist/telegram/client.d.ts +73 -0
  272. package/dist/telegram/client.js +135 -0
  273. package/dist/telegram/errors.d.ts +37 -0
  274. package/dist/telegram/errors.js +62 -0
  275. package/dist/telegram/inbound.d.ts +40 -0
  276. package/dist/telegram/inbound.js +72 -0
  277. package/dist/telegram/index.d.ts +8 -0
  278. package/dist/telegram/multipart.d.ts +5 -0
  279. package/dist/telegram/multipart.js +47 -0
  280. package/dist/telegram/types.d.ts +271 -0
  281. package/dist/telegram/update.d.ts +833 -0
  282. package/dist/telegram/update.js +129 -0
  283. package/dist/testing/fake-html.d.ts +9 -0
  284. package/dist/testing/fake-html.js +83 -0
  285. package/dist/testing/fake-telegram.d.ts +206 -0
  286. package/dist/testing/fake-telegram.js +856 -0
  287. package/dist/testing/index.d.ts +7 -0
  288. package/dist/testing/index.js +7 -0
  289. package/dist/testing/reference-sql.d.ts +4 -0
  290. package/dist/testing/reference-sql.js +8 -0
  291. package/dist/testing/test-plugin.d.ts +126 -0
  292. package/dist/testing/test-plugin.js +182 -0
  293. package/dist/testing/updates.d.ts +100 -0
  294. package/dist/testing/updates.js +212 -0
  295. package/package.json +113 -3
@@ -0,0 +1,48 @@
1
+ // src/adapters/metrics.ts
2
+ /** Discards everything (the default when `ports.metrics` is not set). */
3
+ const noopMetrics = {
4
+ count() {},
5
+ timing() {}
6
+ };
7
+ /** Tag keys that would raise cardinality or leak personal data; they are dropped. */
8
+ const BLOCKED_TAG = /chat|user|text|token|phone|email/i;
9
+ const MAX_BLOB = 96;
10
+ const clean = (v) => (v ?? "").slice(0, MAX_BLOB);
11
+ /**
12
+ * Writes one data point per call. Layout (query with SQL over the dataset):
13
+ * - `index1` = metric name (the sampling key)
14
+ * - `blob1` = name, `blob2` = `count` | `timing`, `blob3` = botId, `blob4` = outcome, `blob5` = code,
15
+ * `blob6` = other tags as sorted `k=v;k=v`
16
+ * - `double1` = value (count n, or ms)
17
+ *
18
+ * Example: `SELECT blob4 AS outcome, SUM(_sample_interval * double1) AS n FROM tg_metrics
19
+ * WHERE blob1 = 'tg.send' AND timestamp > NOW() - INTERVAL '1' DAY GROUP BY outcome`.
20
+ */
21
+ function analyticsEngineMetrics(dataset, opts = {}) {
22
+ const write = (type, name, value, tags = {}) => {
23
+ const { botId, outcome, code, ...rest } = {
24
+ ...opts.tags,
25
+ ...tags
26
+ };
27
+ const extra = Object.keys(rest).filter((k) => !BLOCKED_TAG.test(k)).sort().map((k) => `${k}=${rest[k]}`).join(";");
28
+ try {
29
+ dataset.writeDataPoint({
30
+ indexes: [clean(name)],
31
+ blobs: [
32
+ clean(name),
33
+ type,
34
+ clean(botId),
35
+ clean(outcome),
36
+ clean(code),
37
+ extra.slice(0, 256)
38
+ ],
39
+ doubles: [Number.isFinite(value) ? value : 0]
40
+ });
41
+ } catch {}
42
+ };
43
+ return {
44
+ count: (name, value = 1, tags) => write("count", name, value, tags),
45
+ timing: (name, ms, tags) => write("timing", name, ms, tags)
46
+ };
47
+ }
48
+ export { analyticsEngineMetrics, noopMetrics };
@@ -0,0 +1,20 @@
1
+ import { FilesPort } from "../ports/ports.js";
2
+ /** Options of `r2Files`. */
3
+ export interface R2FilesOptions {
4
+ /** Key prefix. Default `telegram/`. */
5
+ prefix?: string;
6
+ /** Builds a link to a stored file (for files over Telegram's 50 MB limit). */
7
+ url?: (ref: string) => string | null;
8
+ /** Key ids (default `crypto.randomUUID()`). */
9
+ id?: () => string;
10
+ /**
11
+ * Read check for file sends by ref (`{ file: { source: "files", ref } }`). Default: deny every ref. The plugin's
12
+ * own re-opens (report files, queued documents) never call it.
13
+ */
14
+ canRead?: (actor: unknown, ref: string) => boolean | Promise<boolean>;
15
+ }
16
+ /**
17
+ * Minimal `FilesPort` over an R2 bucket: `put` buffers the body in memory and writes `<prefix><id>/<name>` with the
18
+ * tags as custom metadata; `open` only serves keys under `prefix`. File sends by ref need `opts.canRead` (default deny).
19
+ */
20
+ export declare function r2Files(bucket: R2Bucket, opts?: R2FilesOptions): FilesPort<unknown>;
@@ -0,0 +1,53 @@
1
+ // src/adapters/r2-files.ts
2
+ function safeName(name) {
3
+ const n = [...name].map((ch) => ch === "/" || ch === "\\" || ch.charCodeAt(0) < 32 ? "_" : ch).join("").trim();
4
+ return n === "" ? "file" : n.slice(0, 128);
5
+ }
6
+ async function toBytes(body) {
7
+ if (body instanceof Uint8Array) return body;
8
+ if (body instanceof ArrayBuffer) return new Uint8Array(body);
9
+ return new Uint8Array(await new Response(body).arrayBuffer());
10
+ }
11
+ /**
12
+ * Minimal `FilesPort` over an R2 bucket: `put` buffers the body in memory and writes `<prefix><id>/<name>` with the
13
+ * tags as custom metadata; `open` only serves keys under `prefix`. File sends by ref need `opts.canRead` (default deny).
14
+ */
15
+ function r2Files(bucket, opts = {}) {
16
+ const prefix = opts.prefix ?? "telegram/";
17
+ const id = opts.id ?? (() => crypto.randomUUID());
18
+ return {
19
+ async put(input) {
20
+ const name = safeName(input.name);
21
+ const key = `${prefix}${id()}/${name}`;
22
+ const bytes = await toBytes(input.body);
23
+ await bucket.put(key, bytes, {
24
+ httpMetadata: { contentType: input.mime },
25
+ customMetadata: {
26
+ ...input.tags,
27
+ name
28
+ }
29
+ });
30
+ return {
31
+ ref: key,
32
+ name,
33
+ mime: input.mime,
34
+ size: bytes.byteLength
35
+ };
36
+ },
37
+ async open(ref) {
38
+ if (!ref.startsWith(prefix)) return null;
39
+ const obj = await bucket.get(ref);
40
+ if (!obj) return null;
41
+ return {
42
+ ref,
43
+ name: obj.customMetadata?.name ?? ref.slice(ref.lastIndexOf("/") + 1),
44
+ mime: obj.httpMetadata?.contentType ?? "application/octet-stream",
45
+ size: obj.size,
46
+ body: obj.body
47
+ };
48
+ },
49
+ canRead: async (actor, ref) => ref.startsWith(prefix) && (opts.canRead ? await opts.canRead(actor, ref) === true : false),
50
+ ...opts.url ? { url: opts.url } : {}
51
+ };
52
+ }
53
+ export { r2Files };
@@ -0,0 +1 @@
1
+ export {}
@@ -0,0 +1,45 @@
1
+ #!/usr/bin/env node
2
+ import { KeyringError, generateKeyringSecret } from "../adapters/keyring.js";
3
+ // src/bin/cli.ts
4
+ const proc = globalThis.process;
5
+ const USAGE = [
6
+ "Usage:",
7
+ " telegram-plugin keyring new [--kid <id>]",
8
+ " telegram-plugin keyring rotate [--kid <id>] < current-keyring.json",
9
+ "",
10
+ "Store the output with: wrangler secret put TELEGRAM_KEYRING --env <env>"
11
+ ].join("\n");
12
+ async function readStdin() {
13
+ const dec = new TextDecoder();
14
+ let text = "";
15
+ for await (const chunk of proc.stdin) text += typeof chunk === "string" ? chunk : dec.decode(chunk, { stream: true });
16
+ return text + dec.decode();
17
+ }
18
+ async function main(args) {
19
+ const [group, cmd, ...rest] = args;
20
+ let kid;
21
+ for (let i = 0; i < rest.length; i++) if (rest[i] === "--kid") kid = rest[++i];
22
+ if (group !== "keyring" || cmd !== "new" && cmd !== "rotate") {
23
+ console.error(USAGE);
24
+ return group === void 0 || group === "--help" || group === "-h" ? 0 : 1;
25
+ }
26
+ try {
27
+ if (cmd === "new") console.log(generateKeyringSecret(kid === void 0 ? {} : { kid }));
28
+ else {
29
+ const existing = (await readStdin()).trim();
30
+ if (existing === "") {
31
+ console.error(USAGE);
32
+ return 1;
33
+ }
34
+ console.log(generateKeyringSecret(kid === void 0 ? { existing } : {
35
+ kid,
36
+ existing
37
+ }));
38
+ }
39
+ return 0;
40
+ } catch (e) {
41
+ console.error(e instanceof KeyringError ? e.message : "failed to generate keyring");
42
+ return 1;
43
+ }
44
+ }
45
+ proc.exitCode = await main(proc.argv.slice(2));
@@ -0,0 +1,35 @@
1
+ import { BroadcastDto, BroadcastPreviewRequest, BroadcastSave, BroadcastStats, BroadcastStatus } from "../contracts/broadcasts.js";
2
+ import { Page } from "../contracts/common.js";
3
+ import { TemplatePreviewResponse } from "../contracts/templates.js";
4
+ import { Endpoint, Id, Ok, PageIn } from "./endpoint.js";
5
+ /** Hono schema of the `/admin/broadcasts` routes (permission `telegram.broadcasts.send`). */
6
+ export type BroadcastsSchema = {
7
+ "/admin/broadcasts": {
8
+ $get: Endpoint<{
9
+ query: PageIn & {
10
+ status?: BroadcastStatus;
11
+ };
12
+ }, Page<BroadcastDto>>;
13
+ $post: Endpoint<{
14
+ json: BroadcastSave;
15
+ }, BroadcastDto, 201>;
16
+ };
17
+ "/admin/broadcasts/:id": {
18
+ $get: Endpoint<Id, BroadcastDto>;
19
+ $put: Endpoint<Id & {
20
+ json: BroadcastSave;
21
+ }, BroadcastDto>;
22
+ $delete: Endpoint<Id, Ok>;
23
+ };
24
+ "/admin/broadcasts/:id/preview": {
25
+ $post: Endpoint<Id & {
26
+ json: BroadcastPreviewRequest;
27
+ }, TemplatePreviewResponse>;
28
+ };
29
+ "/admin/broadcasts/:id/send": {
30
+ $post: Endpoint<Id, BroadcastDto>;
31
+ };
32
+ "/admin/broadcasts/:id/report": {
33
+ $get: Endpoint<Id, BroadcastStats>;
34
+ };
35
+ };
@@ -0,0 +1,23 @@
1
+ import { CommandConfigDto, CommandConfigSave, CommandSyncResult } from "../contracts/commands.js";
2
+ import { Endpoint, Id } from "./endpoint.js";
3
+ /** Hono schema of the per-bot command routes (permission `telegram.commands.manage`). */
4
+ export type CommandsSchema = {
5
+ "/admin/bots/:id/commands": {
6
+ $get: Endpoint<Id, {
7
+ items: CommandConfigDto[];
8
+ }>;
9
+ };
10
+ "/admin/bots/:id/commands/sync": {
11
+ $post: Endpoint<Id, CommandSyncResult>;
12
+ };
13
+ "/admin/bots/:id/commands/:command": {
14
+ $put: Endpoint<{
15
+ param: {
16
+ id: string;
17
+ command: string;
18
+ };
19
+ } & {
20
+ json: CommandConfigSave;
21
+ }, CommandConfigDto>;
22
+ };
23
+ };
@@ -0,0 +1,34 @@
1
+ /** No input / empty Hono env. */
2
+ export type Empty = Record<never, never>;
3
+ /** One Hono schema endpoint: input `I`, JSON output `O`, HTTP status `S` (default 200). */
4
+ export type Endpoint<I, O, S extends number = 200> = {
5
+ input: I;
6
+ output: O;
7
+ outputFormat: "json";
8
+ status: S;
9
+ };
10
+ /**
11
+ * An endpoint that answers with raw bytes (not JSON): read it with `res.blob()` / `res.arrayBuffer()`; the
12
+ * `content-type` / `content-disposition` headers carry the file's mime and name.
13
+ */
14
+ export type BinaryEndpoint<I> = {
15
+ input: I;
16
+ output: Record<never, never>;
17
+ outputFormat: "binary";
18
+ status: 200;
19
+ };
20
+ /** `:id` path param input. */
21
+ export type Id = {
22
+ param: {
23
+ id: string;
24
+ };
25
+ };
26
+ /** `?cursor=&limit=` query input (strings, as sent on the wire). */
27
+ export type PageIn = {
28
+ cursor?: string;
29
+ limit?: string;
30
+ };
31
+ /** `{ ok: true }` result of deletes. */
32
+ export type Ok = {
33
+ ok: true;
34
+ };
@@ -0,0 +1,33 @@
1
+ import { Page } from "../contracts/common.js";
2
+ import { EventRuleDto, EventRuleSave, EventRuleTestRequest, EventRuleTestResult, RegisteredEventDto } from "../contracts/events.js";
3
+ import { Empty, Endpoint, Id, Ok, PageIn } from "./endpoint.js";
4
+ /** Hono schema of the event and event-rule routes (permission `telegram.events.manage`). */
5
+ export type EventsSchema = {
6
+ "/admin/events": {
7
+ $get: Endpoint<Empty, {
8
+ items: RegisteredEventDto[];
9
+ }>;
10
+ };
11
+ "/admin/event-rules": {
12
+ $get: Endpoint<{
13
+ query: PageIn & {
14
+ event?: string;
15
+ };
16
+ }, Page<EventRuleDto>>;
17
+ $post: Endpoint<{
18
+ json: EventRuleSave;
19
+ }, EventRuleDto, 201>;
20
+ };
21
+ "/admin/event-rules/:id": {
22
+ $get: Endpoint<Id, EventRuleDto>;
23
+ $put: Endpoint<Id & {
24
+ json: EventRuleSave;
25
+ }, EventRuleDto>;
26
+ $delete: Endpoint<Id, Ok>;
27
+ };
28
+ "/admin/event-rules/:id/test": {
29
+ $post: Endpoint<Id & {
30
+ json: EventRuleTestRequest;
31
+ }, EventRuleTestResult>;
32
+ };
33
+ };
@@ -0,0 +1,11 @@
1
+ import { BotDto } from "../contracts/bots.js";
2
+ import { ChatDto } from "../contracts/chats.js";
3
+ import { DestinationDto } from "../contracts/destinations.js";
4
+ import { labelKey } from "../contracts/labels.js";
5
+ import { PluginMeta } from "../contracts/meta.js";
6
+ import { PROBLEM_CONTENT_TYPE, TelegramProblem } from "../contracts/problems.js";
7
+ import { ScheduleDto } from "../contracts/schedules.js";
8
+ import { TemplateDto } from "../contracts/templates.js";
9
+ import { UserDto } from "../contracts/users.js";
10
+ import { TelegramAdminApp, TelegramAdminClient, TelegramAdminSchema } from "./schema.js";
11
+ export { type BotDto, type ChatDto, type DestinationDto, PROBLEM_CONTENT_TYPE, type PluginMeta, type ScheduleDto, type TelegramAdminApp, type TelegramAdminClient, type TelegramAdminSchema, type TelegramProblem, type TemplateDto, type UserDto, labelKey };
@@ -0,0 +1,3 @@
1
+ import { PROBLEM_CONTENT_TYPE } from "../contracts/problems.js";
2
+ import { labelKey } from "../contracts/labels.js";
3
+ export { PROBLEM_CONTENT_TYPE, labelKey };
@@ -0,0 +1,42 @@
1
+ import { Page } from "../contracts/common.js";
2
+ import { NextRunsRequest, NextRunsResponse, ScheduleDto, SchedulePause, ScheduleRunDto, ScheduleSave } from "../contracts/schedules.js";
3
+ import { Endpoint, Id, Ok, PageIn } from "./endpoint.js";
4
+ /** Hono schema of the `/admin/schedules` routes (permission `telegram.schedules.manage`). */
5
+ export type SchedulesSchema = {
6
+ "/admin/schedules": {
7
+ $get: Endpoint<{
8
+ query: PageIn;
9
+ }, Page<ScheduleDto>>;
10
+ $post: Endpoint<{
11
+ json: ScheduleSave;
12
+ }, ScheduleDto, 201>;
13
+ };
14
+ "/admin/schedules/next-runs": {
15
+ $post: Endpoint<{
16
+ json: NextRunsRequest;
17
+ }, NextRunsResponse>;
18
+ };
19
+ "/admin/schedules/:id": {
20
+ $get: Endpoint<Id, ScheduleDto>;
21
+ $put: Endpoint<Id & {
22
+ json: ScheduleSave;
23
+ }, ScheduleDto>;
24
+ $delete: Endpoint<Id, Ok>;
25
+ };
26
+ "/admin/schedules/:id/pause": {
27
+ $post: Endpoint<Id & {
28
+ json?: SchedulePause;
29
+ }, ScheduleDto>;
30
+ };
31
+ "/admin/schedules/:id/resume": {
32
+ $post: Endpoint<Id, ScheduleDto>;
33
+ };
34
+ "/admin/schedules/:id/run-now": {
35
+ $post: Endpoint<Id, ScheduleRunDto>;
36
+ };
37
+ "/admin/schedules/:id/runs": {
38
+ $get: Endpoint<Id & {
39
+ query: PageIn;
40
+ }, Page<ScheduleRunDto>>;
41
+ };
42
+ };
@@ -0,0 +1,181 @@
1
+ import { BotCreate, BotDto, BotPatch, BotRotateToken, BotTestResult } from "../contracts/bots.js";
2
+ import { ChatDto, ChatKind, ChatPatch, ChatStatus } from "../contracts/chats.js";
3
+ import { Page } from "../contracts/common.js";
4
+ import { DeliveryDto, DeliverySource, DeliveryStatus } from "../contracts/deliveries.js";
5
+ import { DestinationDto, DestinationSave } from "../contracts/destinations.js";
6
+ import { HealthDto, StatsDto } from "../contracts/health.js";
7
+ import { LinkCreate, LinkResult, PendingLinkDto, SelfLinkCreate } from "../contracts/links.js";
8
+ import { PluginMeta } from "../contracts/meta.js";
9
+ import { SubscriptionDto, SubscriptionSave } from "../contracts/subscriptions.js";
10
+ import { UserDto } from "../contracts/users.js";
11
+ import { Empty, Endpoint, Id, Ok, PageIn } from "./endpoint.js";
12
+ import { BroadcastsSchema } from "./broadcasts.js";
13
+ import { CommandsSchema } from "./commands.js";
14
+ import { EventsSchema } from "./events.js";
15
+ import { SchedulesSchema } from "./schedules.js";
16
+ import { SendSchema } from "./send.js";
17
+ import { TemplatesSchema } from "./templates.js";
18
+ import { Hono } from "hono";
19
+ import { hc } from "hono/client";
20
+ /** Phase 2 routes (bots, chats, links, destinations, deliveries, ops, self-service). */
21
+ type CoreSchema = {
22
+ "/admin/meta": {
23
+ $get: Endpoint<Empty, PluginMeta>;
24
+ };
25
+ "/admin/bots": {
26
+ $get: Endpoint<{
27
+ query: PageIn;
28
+ }, Page<BotDto>>;
29
+ $post: Endpoint<{
30
+ json: BotCreate;
31
+ }, BotDto, 201>;
32
+ };
33
+ "/admin/bots/:id": {
34
+ $get: Endpoint<Id, BotDto>;
35
+ $patch: Endpoint<Id & {
36
+ json: BotPatch;
37
+ }, BotDto>;
38
+ $delete: Endpoint<Id, Ok>;
39
+ };
40
+ "/admin/bots/:id/test": {
41
+ $post: Endpoint<Id, BotTestResult>;
42
+ };
43
+ "/admin/bots/:id/rotate-token": {
44
+ $post: Endpoint<Id & {
45
+ json: BotRotateToken;
46
+ }, BotDto>;
47
+ };
48
+ "/admin/bots/:id/webhook/reset": {
49
+ $post: Endpoint<Id, BotDto>;
50
+ };
51
+ "/admin/chats": {
52
+ $get: Endpoint<{
53
+ query: PageIn & {
54
+ bot?: string;
55
+ kind?: ChatKind;
56
+ status?: ChatStatus;
57
+ user?: string;
58
+ };
59
+ }, Page<ChatDto>>;
60
+ };
61
+ "/admin/chats/:id": {
62
+ $get: Endpoint<Id, ChatDto>;
63
+ $patch: Endpoint<Id & {
64
+ json: ChatPatch;
65
+ }, ChatDto>;
66
+ $delete: Endpoint<Id, Ok>;
67
+ };
68
+ "/admin/chats/:id/subscriptions": {
69
+ $get: Endpoint<Id, {
70
+ items: SubscriptionDto[];
71
+ }>;
72
+ $put: Endpoint<Id & {
73
+ json: SubscriptionSave;
74
+ }, {
75
+ items: SubscriptionDto[];
76
+ }>;
77
+ };
78
+ "/admin/users": {
79
+ $get: Endpoint<{
80
+ query: {
81
+ q: string;
82
+ limit?: string;
83
+ };
84
+ }, {
85
+ items: UserDto[];
86
+ }>;
87
+ };
88
+ "/admin/links": {
89
+ $post: Endpoint<{
90
+ json: LinkCreate;
91
+ }, LinkResult, 201>;
92
+ };
93
+ "/admin/destinations": {
94
+ $get: Endpoint<{
95
+ query: PageIn;
96
+ }, Page<DestinationDto>>;
97
+ $post: Endpoint<{
98
+ json: DestinationSave;
99
+ }, DestinationDto, 201>;
100
+ };
101
+ "/admin/destinations/:id": {
102
+ $get: Endpoint<Id, DestinationDto>;
103
+ $put: Endpoint<Id & {
104
+ json: DestinationSave;
105
+ }, DestinationDto>;
106
+ $delete: Endpoint<Id, Ok>;
107
+ };
108
+ "/admin/deliveries": {
109
+ $get: Endpoint<{
110
+ query: PageIn & {
111
+ status?: DeliveryStatus;
112
+ source?: DeliverySource;
113
+ bot?: string;
114
+ };
115
+ }, Page<DeliveryDto>>;
116
+ };
117
+ "/admin/deliveries/:id": {
118
+ $get: Endpoint<Id, DeliveryDto>;
119
+ };
120
+ "/admin/deliveries/:id/retry": {
121
+ $post: Endpoint<Id, DeliveryDto>;
122
+ };
123
+ "/admin/deliveries/:id/cancel": {
124
+ $post: Endpoint<Id, DeliveryDto>;
125
+ };
126
+ "/admin/health": {
127
+ $get: Endpoint<Empty, HealthDto>;
128
+ };
129
+ "/admin/stats": {
130
+ $get: Endpoint<{
131
+ query: {
132
+ from: string;
133
+ to: string;
134
+ };
135
+ }, StatsDto>;
136
+ };
137
+ "/me/link": {
138
+ $post: Endpoint<{
139
+ json: SelfLinkCreate;
140
+ }, LinkResult, 201>;
141
+ };
142
+ "/me/links/pending": {
143
+ $get: Endpoint<Empty, {
144
+ items: PendingLinkDto[];
145
+ }>;
146
+ };
147
+ "/me/links/pending/:id/confirm": {
148
+ $post: Endpoint<Id, {
149
+ chat: ChatDto;
150
+ }>;
151
+ };
152
+ "/me/links/pending/:id/decline": {
153
+ $post: Endpoint<Id, Ok>;
154
+ };
155
+ "/me/chats": {
156
+ $get: Endpoint<Empty, Page<ChatDto>>;
157
+ };
158
+ "/me/chats/:id": {
159
+ $delete: Endpoint<Id, Ok>;
160
+ };
161
+ "/me/subscriptions": {
162
+ $get: Endpoint<Empty, {
163
+ items: SubscriptionDto[];
164
+ }>;
165
+ $put: Endpoint<{
166
+ json: SubscriptionSave;
167
+ }, {
168
+ items: SubscriptionDto[];
169
+ }>;
170
+ };
171
+ };
172
+ /** Every admin and self-service route (`/admin/*`, `/me/*`) as one Hono schema type. */
173
+ export type TelegramAdminSchema = CoreSchema & TemplatesSchema & CommandsSchema & SchedulesSchema & EventsSchema & BroadcastsSchema & SendSchema;
174
+ /** The plugin routes as a Hono app type (type-only). */
175
+ export type TelegramAdminApp = Hono<Empty, TelegramAdminSchema, "/">;
176
+ /**
177
+ * Typed `hc` client for the admin API: `hc<TelegramAdminApp>("/api/telegram") as TelegramAdminClient`,
178
+ * then `client.admin.bots.$get({ query: {} })`.
179
+ */
180
+ export type TelegramAdminClient = ReturnType<typeof hc<TelegramAdminApp>>;
181
+ export {};
@@ -0,0 +1,15 @@
1
+ import { SendIntent, SendPreview, SendResult } from "../contracts/send.js";
2
+ import { Endpoint } from "./endpoint.js";
3
+ /** Hono schema of the send routes (permission `telegram.send`). */
4
+ export type SendSchema = {
5
+ "/admin/send/preview": {
6
+ $post: Endpoint<{
7
+ json: SendIntent;
8
+ }, SendPreview>;
9
+ };
10
+ "/admin/send": {
11
+ $post: Endpoint<{
12
+ json: SendIntent;
13
+ }, SendResult>;
14
+ };
15
+ };
@@ -0,0 +1,47 @@
1
+ import { Page } from "../contracts/common.js";
2
+ import { ReportRenderRequest, TemplateDto, TemplateKind, TemplatePreviewRequest, TemplatePreviewResponse, TemplateSave, TemplateVersionDto } from "../contracts/templates.js";
3
+ import { BinaryEndpoint, Endpoint, Id, Ok, PageIn } from "./endpoint.js";
4
+ /** Hono schema of the template routes (`telegram.templates.manage`) and report download (`telegram.reports.run`). */
5
+ export type TemplatesSchema = {
6
+ "/admin/templates": {
7
+ $get: Endpoint<{
8
+ query: PageIn & {
9
+ kind?: TemplateKind;
10
+ };
11
+ }, Page<TemplateDto>>;
12
+ $post: Endpoint<{
13
+ json: TemplateSave;
14
+ }, TemplateDto, 201>;
15
+ };
16
+ "/admin/templates/preview": {
17
+ $post: Endpoint<{
18
+ json: TemplatePreviewRequest;
19
+ }, TemplatePreviewResponse>;
20
+ };
21
+ "/admin/templates/:id": {
22
+ $get: Endpoint<Id, TemplateDto>;
23
+ $put: Endpoint<Id & {
24
+ json: TemplateSave;
25
+ }, TemplateDto>;
26
+ $delete: Endpoint<Id, Ok>;
27
+ };
28
+ "/admin/templates/:id/versions": {
29
+ $get: Endpoint<Id, {
30
+ items: TemplateVersionDto[];
31
+ }>;
32
+ };
33
+ "/admin/templates/:id/restore/:version": {
34
+ $post: Endpoint<{
35
+ param: {
36
+ id: string;
37
+ version: string;
38
+ };
39
+ }, TemplateDto>;
40
+ };
41
+ /** Answers with the file bytes (`content-type` = the file's mime): use `res.blob()`, not `res.json()`. */
42
+ "/admin/reports/render": {
43
+ $post: BinaryEndpoint<{
44
+ json: ReportRenderRequest;
45
+ }>;
46
+ };
47
+ };