@mirid-dev/sendkit-core 0.0.0-stage → 0.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/dist/index.d.ts +45 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +73 -0
- package/dist/index.js.map +1 -0
- package/package.json +27 -4
- package/README.md +0 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
export declare const telegramMessageInputSchema: z.ZodObject<{
|
|
3
|
+
chatId: z.ZodString;
|
|
4
|
+
message: z.ZodString;
|
|
5
|
+
}, z.core.$strip>;
|
|
6
|
+
export declare const telegramMessageOptionsSchema: z.ZodObject<{
|
|
7
|
+
chatId: z.ZodString;
|
|
8
|
+
message: z.ZodString;
|
|
9
|
+
botToken: z.ZodString;
|
|
10
|
+
}, z.core.$strip>;
|
|
11
|
+
export declare const telegramSendMessageRequestSchema: z.ZodObject<{
|
|
12
|
+
chat_id: z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>;
|
|
13
|
+
text: z.ZodString;
|
|
14
|
+
parse_mode: z.ZodOptional<z.ZodEnum<{
|
|
15
|
+
HTML: "HTML";
|
|
16
|
+
Markdown: "Markdown";
|
|
17
|
+
MarkdownV2: "MarkdownV2";
|
|
18
|
+
}>>;
|
|
19
|
+
disable_notification: z.ZodOptional<z.ZodBoolean>;
|
|
20
|
+
protect_content: z.ZodOptional<z.ZodBoolean>;
|
|
21
|
+
}, z.core.$strip>;
|
|
22
|
+
export declare const telegramSendMessageResponseSchema: z.ZodDiscriminatedUnion<[z.ZodObject<{
|
|
23
|
+
ok: z.ZodLiteral<true>;
|
|
24
|
+
result: z.ZodObject<{
|
|
25
|
+
message_id: z.ZodNumber;
|
|
26
|
+
date: z.ZodNumber;
|
|
27
|
+
chat: z.ZodObject<{
|
|
28
|
+
id: z.ZodNumber;
|
|
29
|
+
}, z.core.$strip>;
|
|
30
|
+
}, z.core.$strip>;
|
|
31
|
+
}, z.core.$strip>, z.ZodObject<{
|
|
32
|
+
ok: z.ZodLiteral<false>;
|
|
33
|
+
error_code: z.ZodOptional<z.ZodNumber>;
|
|
34
|
+
description: z.ZodOptional<z.ZodString>;
|
|
35
|
+
}, z.core.$strip>], "ok">;
|
|
36
|
+
export declare const telegramMessageOutputSchema: z.ZodObject<{
|
|
37
|
+
messageId: z.ZodNumber;
|
|
38
|
+
chatId: z.ZodNumber;
|
|
39
|
+
sentAt: z.ZodDate;
|
|
40
|
+
}, z.core.$strip>;
|
|
41
|
+
export type TelegramMessageInput = z.infer<typeof telegramMessageInputSchema>;
|
|
42
|
+
export type TelegramMessageOptions = z.infer<typeof telegramMessageOptionsSchema>;
|
|
43
|
+
export type TelegramMessageOutput = z.infer<typeof telegramMessageOutputSchema>;
|
|
44
|
+
export declare function sendTelegramMessage(input: z.input<typeof telegramMessageOptionsSchema>): Promise<TelegramMessageOutput>;
|
|
45
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../src/schemas.ts","../src/operations.ts"],"mappings":";qBAca,4BAA0B,EAAA;;;GASrC,EAAA,KAAA;qBAEW,8BAA4B,EAAA;;;;GAOvC,EAAA,KAAA;qBAGW,kCAAgC,EAAA;;;;;;;;;;GAM3C,EAAA,KAAA;qBAGW,mCAAiC,EAAA,uBAAA,EAAA;;;;;;;;;;;;;GAgB5C,EAAA,KAAA;qBAEW,6BAA2B,EAAA;;;;GAKtC,EAAA,KAAA;YAEU,uBAAuB,EAAE,aAAa;YACtC,yBAAyB,EAAE,aAAa;YACxC,wBAAwB,EAAE,aAAa;wBC5D7B,oBACpB,OAAO,EAAE,aAAa,gCACrB,QAAQ"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
// src/schemas.ts
|
|
3
|
+
const TELEGRAM_MAX_MESSAGE_LENGTH = 4096;
|
|
4
|
+
const telegramChatIdSchema = z.string().trim().refine((chatId) => /^-?\d+$/.test(chatId) || /^@\w{5,}$/.test(chatId), "Use a numeric chat ID or @channelusername");
|
|
5
|
+
const telegramMessageInputSchema = z.object({
|
|
6
|
+
chatId: telegramChatIdSchema,
|
|
7
|
+
message: z.string().refine((message) => message.trim().length > 0, "Message cannot be empty").refine((message) => message.length <= TELEGRAM_MAX_MESSAGE_LENGTH, `Telegram allows at most ${TELEGRAM_MAX_MESSAGE_LENGTH} characters`)
|
|
8
|
+
});
|
|
9
|
+
const telegramMessageOptionsSchema = telegramMessageInputSchema.extend({ botToken: z.string().trim().min(1, "Telegram bot token is required").regex(/^\d+:[\w-]+$/, "Expected a token like 123456789:ABC-DEF..., without the \"bot\" prefix") });
|
|
10
|
+
const telegramSendMessageRequestSchema = z.object({
|
|
11
|
+
chat_id: z.union([z.string(), z.number().int()]),
|
|
12
|
+
text: z.string().min(1).max(TELEGRAM_MAX_MESSAGE_LENGTH),
|
|
13
|
+
parse_mode: z.enum([
|
|
14
|
+
"MarkdownV2",
|
|
15
|
+
"HTML",
|
|
16
|
+
"Markdown"
|
|
17
|
+
]).optional(),
|
|
18
|
+
disable_notification: z.boolean().optional(),
|
|
19
|
+
protect_content: z.boolean().optional()
|
|
20
|
+
});
|
|
21
|
+
const telegramSendMessageResponseSchema = z.discriminatedUnion("ok", [z.object({
|
|
22
|
+
ok: z.literal(true),
|
|
23
|
+
result: z.object({
|
|
24
|
+
message_id: z.number().int(),
|
|
25
|
+
date: z.number().int(),
|
|
26
|
+
chat: z.object({ id: z.number().int() })
|
|
27
|
+
})
|
|
28
|
+
}), z.object({
|
|
29
|
+
ok: z.literal(false),
|
|
30
|
+
error_code: z.number().int().optional(),
|
|
31
|
+
description: z.string().optional()
|
|
32
|
+
})]);
|
|
33
|
+
const telegramMessageOutputSchema = z.object({
|
|
34
|
+
messageId: z.number().int(),
|
|
35
|
+
chatId: z.number().int(),
|
|
36
|
+
sentAt: z.date()
|
|
37
|
+
});
|
|
38
|
+
// src/operations.ts
|
|
39
|
+
const TELEGRAM_REQUEST_TIMEOUT_MS = 1e4;
|
|
40
|
+
async function sendTelegramMessage(input) {
|
|
41
|
+
const { chatId, message, botToken } = telegramMessageOptionsSchema.parse(input);
|
|
42
|
+
const requestBody = telegramSendMessageRequestSchema.parse({
|
|
43
|
+
chat_id: chatId,
|
|
44
|
+
text: message
|
|
45
|
+
});
|
|
46
|
+
let response;
|
|
47
|
+
try {
|
|
48
|
+
response = await fetch(`https://api.telegram.org/bot${botToken}/sendMessage`, {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: { "Content-Type": "application/json" },
|
|
51
|
+
body: JSON.stringify(requestBody),
|
|
52
|
+
signal: AbortSignal.timeout(TELEGRAM_REQUEST_TIMEOUT_MS)
|
|
53
|
+
});
|
|
54
|
+
} catch (error) {
|
|
55
|
+
const reason = error instanceof Error ? error.message : String(error);
|
|
56
|
+
throw new Error(`Could not reach Telegram: ${reason.replaceAll(botToken, "<redacted>")}`, { cause: error });
|
|
57
|
+
}
|
|
58
|
+
const parsedResponse = telegramSendMessageResponseSchema.safeParse(await response.json().catch(() => null));
|
|
59
|
+
if (!parsedResponse.success) {
|
|
60
|
+
const hint = response.ok ? "; the message may still have been sent" : "";
|
|
61
|
+
throw new Error(`Unexpected response from Telegram (HTTP ${response.status})${hint}`);
|
|
62
|
+
}
|
|
63
|
+
const data = parsedResponse.data;
|
|
64
|
+
if (!data.ok) throw new Error(`Telegram API error (${response.status}): ${data.description || response.statusText || "Unknown error"}`);
|
|
65
|
+
return telegramMessageOutputSchema.parse({
|
|
66
|
+
messageId: data.result.message_id,
|
|
67
|
+
chatId: data.result.chat.id,
|
|
68
|
+
sentAt: /* @__PURE__ */ new Date(data.result.date * 1e3)
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
export { sendTelegramMessage, telegramMessageInputSchema, telegramMessageOptionsSchema, telegramMessageOutputSchema, telegramSendMessageRequestSchema, telegramSendMessageResponseSchema };
|
|
72
|
+
|
|
73
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/schemas.ts","../src/operations.ts"],"sourcesContent":["import { z } from 'zod'\n\n// Telegram rejects message text longer than this\nconst TELEGRAM_MAX_MESSAGE_LENGTH = 4096\n\n// Numeric ID (negative for groups) or a public @channelusername\nconst telegramChatIdSchema = z\n .string()\n .trim()\n .refine(\n (chatId) => /^-?\\d+$/.test(chatId) || /^@\\w{5,}$/.test(chatId),\n 'Use a numeric chat ID or @channelusername',\n )\n\nexport const telegramMessageInputSchema = z.object({\n chatId: telegramChatIdSchema,\n message: z\n .string()\n .refine((message) => message.trim().length > 0, 'Message cannot be empty')\n .refine(\n (message) => message.length <= TELEGRAM_MAX_MESSAGE_LENGTH,\n `Telegram allows at most ${TELEGRAM_MAX_MESSAGE_LENGTH} characters`,\n ),\n})\n\nexport const telegramMessageOptionsSchema = telegramMessageInputSchema.extend({\n botToken: z\n .string()\n .trim()\n .min(1, 'Telegram bot token is required')\n // Tokens look like 123456789:AAH...; a copied \"bot\" prefix would otherwise surface as a 404\n .regex(/^\\d+:[\\w-]+$/, 'Expected a token like 123456789:ABC-DEF..., without the \"bot\" prefix'),\n})\n\n// Body of POST /bot<token>/sendMessage, in Telegram's snake_case\nexport const telegramSendMessageRequestSchema = z.object({\n chat_id: z.union([z.string(), z.number().int()]),\n text: z.string().min(1).max(TELEGRAM_MAX_MESSAGE_LENGTH),\n parse_mode: z.enum(['MarkdownV2', 'HTML', 'Markdown']).optional(),\n disable_notification: z.boolean().optional(),\n protect_content: z.boolean().optional(),\n})\n\n// Telegram returns a JSON body with `ok` and `description` even on 4xx errors\nexport const telegramSendMessageResponseSchema = z.discriminatedUnion('ok', [\n z.object({\n ok: z.literal(true),\n result: z.object({\n message_id: z.number().int(),\n date: z.number().int(),\n chat: z.object({\n id: z.number().int(),\n }),\n }),\n }),\n z.object({\n ok: z.literal(false),\n error_code: z.number().int().optional(),\n description: z.string().optional(),\n }),\n])\n\nexport const telegramMessageOutputSchema = z.object({\n messageId: z.number().int(),\n // Always numeric: Telegram resolves @channelusername to the chat's ID\n chatId: z.number().int(),\n sentAt: z.date(),\n})\n\nexport type TelegramMessageInput = z.infer<typeof telegramMessageInputSchema>\nexport type TelegramMessageOptions = z.infer<typeof telegramMessageOptionsSchema>\nexport type TelegramMessageOutput = z.infer<typeof telegramMessageOutputSchema>\n","import { z } from 'zod'\nimport {\n telegramMessageOutputSchema,\n telegramMessageOptionsSchema,\n telegramSendMessageRequestSchema,\n telegramSendMessageResponseSchema,\n type TelegramMessageOutput,\n} from './schemas'\n\nconst TELEGRAM_REQUEST_TIMEOUT_MS = 10_000\n\nexport async function sendTelegramMessage(\n input: z.input<typeof telegramMessageOptionsSchema>,\n): Promise<TelegramMessageOutput> {\n const { chatId, message, botToken } = telegramMessageOptionsSchema.parse(input)\n\n const requestBody = telegramSendMessageRequestSchema.parse({\n chat_id: chatId,\n text: message,\n })\n\n let response: Response\n\n try {\n response = await fetch(`https://api.telegram.org/bot${botToken}/sendMessage`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify(requestBody),\n signal: AbortSignal.timeout(TELEGRAM_REQUEST_TIMEOUT_MS),\n })\n } catch (error) {\n // Don't rethrow the original error: the request URL contains the bot token\n const reason = error instanceof Error ? error.message : String(error)\n throw new Error(`Could not reach Telegram: ${reason.replaceAll(botToken, '<redacted>')}`, {\n cause: error,\n })\n }\n\n // Telegram returns a JSON body even on 4xx errors, but a proxy or outage page may not\n const parsedResponse = telegramSendMessageResponseSchema.safeParse(\n await response.json().catch(() => null),\n )\n\n if (!parsedResponse.success) {\n // A 2xx we can't read may still mean the message went out, so warn before the caller resends\n const hint = response.ok ? '; the message may still have been sent' : ''\n throw new Error(`Unexpected response from Telegram (HTTP ${response.status})${hint}`)\n }\n\n const data = parsedResponse.data\n\n if (!data.ok) {\n throw new Error(\n `Telegram API error (${response.status}): ${data.description || response.statusText || 'Unknown error'}`,\n )\n }\n\n return telegramMessageOutputSchema.parse({\n messageId: data.result.message_id,\n chatId: data.result.chat.id,\n sentAt: new Date(data.result.date * 1000),\n })\n}\n"],"mappings":";;AAGA,MAAM,8BAA8B;AAGpC,MAAM,uBAAuB,EAC1B,OAAO,CAAC,CACR,KAAK,CAAC,CACN,QACE,WAAW,UAAU,KAAK,MAAM,KAAK,YAAY,KAAK,MAAM,GAC7D,2CACF;AAEF,MAAa,6BAA6B,EAAE,OAAO;CACjD,QAAQ;CACR,SAAS,EACN,OAAO,CAAC,CACR,QAAQ,YAAY,QAAQ,KAAK,CAAC,CAAC,SAAS,GAAG,yBAAyB,CAAC,CACzE,QACE,YAAY,QAAQ,UAAU,6BAC/B,2BAA2B,4BAA4B,YACzD;AACJ,CAAC;AAED,MAAa,+BAA+B,2BAA2B,OAAO,EAC5E,UAAU,EACP,OAAO,CAAC,CACR,KAAK,CAAC,CACN,IAAI,GAAG,gCAAgC,CAAC,CAExC,MAAM,gBAAgB,wEAAsE,EACjG,CAAC;AAGD,MAAa,mCAAmC,EAAE,OAAO;CACvD,SAAS,EAAE,MAAM,CAAC,EAAE,OAAO,GAAG,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC;CAC/C,MAAM,EAAE,OAAO,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,2BAA2B;CACvD,YAAY,EAAE,KAAK;EAAC;EAAc;EAAQ;CAAU,CAAC,CAAC,CAAC,SAAS;CAChE,sBAAsB,EAAE,QAAQ,CAAC,CAAC,SAAS;CAC3C,iBAAiB,EAAE,QAAQ,CAAC,CAAC,SAAS;AACxC,CAAC;AAGD,MAAa,oCAAoC,EAAE,mBAAmB,MAAM,CAC1E,EAAE,OAAO;CACP,IAAI,EAAE,QAAQ,IAAI;CAClB,QAAQ,EAAE,OAAO;EACf,YAAY,EAAE,OAAO,CAAC,CAAC,IAAI;EAC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e;CAE5B,IAAI,CAAC,KAAK,IACR,MAAM,IAAI,MACR,uBAAuB,SAAS,OAAO,KAAK,KAAK,eAAe,SAAS,cAAc,iBACzF;CAGF,OAAO,4BAA4B,MAAM;EACvC,WAAW,KAAK,OAAO;EACvB,QAAQ,KAAK,OAAO,KAAK;EACzB,wBAAQ,IAAI,KAAK,KAAK,OAAO,OAAO,GAAI;CAC1C,CAAC;AACH"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mirid-dev/sendkit-core",
|
|
3
|
-
"version": "0.0.0
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"files": [
|
|
5
|
+
"dist"
|
|
6
|
+
],
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"source": "./src/index.ts",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsdown",
|
|
23
|
+
"pack:dry": "bun run build && bun pm pack --dry-run",
|
|
24
|
+
"prepublishOnly": "bun run build"
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"zod": "^4.6.7"
|
|
28
|
+
}
|
|
29
|
+
}
|
package/README.md
DELETED