@mirid-dev/sendkit-core 0.0.0 → 0.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ky-Ling
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # @mirid-dev/sendkit-core
2
+
3
+ Telegram messaging operations and [zod](https://zod.dev) schemas shared by the SendKit CLI and MCP servers.
4
+
5
+ Requires Node.js 20 or later. ESM only.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install @mirid-dev/sendkit-core
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ```ts
16
+ import { sendTelegramMessage } from '@mirid-dev/sendkit-core'
17
+
18
+ const result = await sendTelegramMessage({
19
+ botToken: process.env.TELEGRAM_BOT_TOKEN!,
20
+ chatId: '123456789',
21
+ message: 'Hello from SendKit',
22
+ })
23
+
24
+ console.log(result.messageId, result.chatId, result.sentAt)
25
+ ```
26
+
27
+ `sendTelegramMessage` validates its input, calls the Telegram Bot API `sendMessage` method with a 10-second timeout, and resolves to `{ messageId, chatId, sentAt }`. `chatId` comes back numeric even when you pass an `@channelusername`. Errors never include the bot token.
28
+
29
+ ## Exports
30
+
31
+ - `sendTelegramMessage(options)`
32
+ - Schemas: `telegramMessageInputSchema` (`chatId`, `message`), `telegramMessageOptionsSchema` (adds `botToken`), `telegramMessageOutputSchema`, `telegramSendMessageRequestSchema`, `telegramSendMessageResponseSchema`
33
+ - Types: `TelegramMessageInput`, `TelegramMessageOptions`, `TelegramMessageOutput`
34
+
35
+ ## License
36
+
37
+ MIT
package/package.json CHANGED
@@ -1,8 +1,16 @@
1
1
  {
2
2
  "name": "@mirid-dev/sendkit-core",
3
- "version": "0.0.0",
3
+ "version": "0.0.1",
4
+ "description": "Core Telegram messaging operations and zod schemas for SendKit",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/Ky-Ling/sendkit.git",
9
+ "directory": "packages/core"
10
+ },
4
11
  "files": [
5
- "dist"
12
+ "dist",
13
+ "src"
6
14
  ],
7
15
  "type": "module",
8
16
  "main": "./dist/index.js",
@@ -25,5 +33,8 @@
25
33
  },
26
34
  "dependencies": {
27
35
  "zod": "^4.6.7"
36
+ },
37
+ "engines": {
38
+ "node": ">=20"
28
39
  }
29
40
  }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './operations'
2
+ export * from './schemas'
@@ -0,0 +1,65 @@
1
+ import { z } from 'zod'
2
+ import {
3
+ telegramMessageOutputSchema,
4
+ telegramMessageOptionsSchema,
5
+ telegramSendMessageRequestSchema,
6
+ telegramSendMessageResponseSchema,
7
+ type TelegramMessageOutput,
8
+ } from './schemas'
9
+
10
+ const TELEGRAM_REQUEST_TIMEOUT_MS = 10_000
11
+
12
+ export async function sendTelegramMessage(
13
+ input: z.input<typeof telegramMessageOptionsSchema>,
14
+ ): Promise<TelegramMessageOutput> {
15
+ const { chatId, message, botToken } = telegramMessageOptionsSchema.parse(input)
16
+
17
+ const requestBody = telegramSendMessageRequestSchema.parse({
18
+ chat_id: chatId,
19
+ text: message,
20
+ })
21
+
22
+ let response: Response
23
+
24
+ try {
25
+ response = await fetch(`https://api.telegram.org/bot${botToken}/sendMessage`, {
26
+ method: 'POST',
27
+ headers: {
28
+ 'Content-Type': 'application/json',
29
+ },
30
+ body: JSON.stringify(requestBody),
31
+ signal: AbortSignal.timeout(TELEGRAM_REQUEST_TIMEOUT_MS),
32
+ })
33
+ } catch (error) {
34
+ // Don't rethrow the original error: the request URL contains the bot token
35
+ const reason = error instanceof Error ? error.message : String(error)
36
+ throw new Error(`Could not reach Telegram: ${reason.replaceAll(botToken, '<redacted>')}`, {
37
+ cause: error,
38
+ })
39
+ }
40
+
41
+ // Telegram returns a JSON body even on 4xx errors, but a proxy or outage page may not
42
+ const parsedResponse = telegramSendMessageResponseSchema.safeParse(
43
+ await response.json().catch(() => null),
44
+ )
45
+
46
+ if (!parsedResponse.success) {
47
+ // A 2xx we can't read may still mean the message went out, so warn before the caller resends
48
+ const hint = response.ok ? '; the message may still have been sent' : ''
49
+ throw new Error(`Unexpected response from Telegram (HTTP ${response.status})${hint}`)
50
+ }
51
+
52
+ const data = parsedResponse.data
53
+
54
+ if (!data.ok) {
55
+ throw new Error(
56
+ `Telegram API error (${response.status}): ${data.description || response.statusText || 'Unknown error'}`,
57
+ )
58
+ }
59
+
60
+ return telegramMessageOutputSchema.parse({
61
+ messageId: data.result.message_id,
62
+ chatId: data.result.chat.id,
63
+ sentAt: new Date(data.result.date * 1000),
64
+ })
65
+ }
package/src/schemas.ts ADDED
@@ -0,0 +1,72 @@
1
+ import { z } from 'zod'
2
+
3
+ // Telegram rejects message text longer than this
4
+ const TELEGRAM_MAX_MESSAGE_LENGTH = 4096
5
+
6
+ // Numeric ID (negative for groups) or a public @channelusername
7
+ const telegramChatIdSchema = z
8
+ .string()
9
+ .trim()
10
+ .refine(
11
+ (chatId) => /^-?\d+$/.test(chatId) || /^@\w{5,}$/.test(chatId),
12
+ 'Use a numeric chat ID or @channelusername',
13
+ )
14
+
15
+ export const telegramMessageInputSchema = z.object({
16
+ chatId: telegramChatIdSchema,
17
+ message: z
18
+ .string()
19
+ .refine((message) => message.trim().length > 0, 'Message cannot be empty')
20
+ .refine(
21
+ (message) => message.length <= TELEGRAM_MAX_MESSAGE_LENGTH,
22
+ `Telegram allows at most ${TELEGRAM_MAX_MESSAGE_LENGTH} characters`,
23
+ ),
24
+ })
25
+
26
+ export const telegramMessageOptionsSchema = telegramMessageInputSchema.extend({
27
+ botToken: z
28
+ .string()
29
+ .trim()
30
+ .min(1, 'Telegram bot token is required')
31
+ // Tokens look like 123456789:AAH...; a copied "bot" prefix would otherwise surface as a 404
32
+ .regex(/^\d+:[\w-]+$/, 'Expected a token like 123456789:ABC-DEF..., without the "bot" prefix'),
33
+ })
34
+
35
+ // Body of POST /bot<token>/sendMessage, in Telegram's snake_case
36
+ export const telegramSendMessageRequestSchema = z.object({
37
+ chat_id: z.union([z.string(), z.number().int()]),
38
+ text: z.string().min(1).max(TELEGRAM_MAX_MESSAGE_LENGTH),
39
+ parse_mode: z.enum(['MarkdownV2', 'HTML', 'Markdown']).optional(),
40
+ disable_notification: z.boolean().optional(),
41
+ protect_content: z.boolean().optional(),
42
+ })
43
+
44
+ // Telegram returns a JSON body with `ok` and `description` even on 4xx errors
45
+ export const telegramSendMessageResponseSchema = z.discriminatedUnion('ok', [
46
+ z.object({
47
+ ok: z.literal(true),
48
+ result: z.object({
49
+ message_id: z.number().int(),
50
+ date: z.number().int(),
51
+ chat: z.object({
52
+ id: z.number().int(),
53
+ }),
54
+ }),
55
+ }),
56
+ z.object({
57
+ ok: z.literal(false),
58
+ error_code: z.number().int().optional(),
59
+ description: z.string().optional(),
60
+ }),
61
+ ])
62
+
63
+ export const telegramMessageOutputSchema = z.object({
64
+ messageId: z.number().int(),
65
+ // Always numeric: Telegram resolves @channelusername to the chat's ID
66
+ chatId: z.number().int(),
67
+ sentAt: z.date(),
68
+ })
69
+
70
+ export type TelegramMessageInput = z.infer<typeof telegramMessageInputSchema>
71
+ export type TelegramMessageOptions = z.infer<typeof telegramMessageOptionsSchema>
72
+ export type TelegramMessageOutput = z.infer<typeof telegramMessageOutputSchema>