sendkit-core 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.
- package/dist/index.d.ts +2 -0
- package/dist/index.js +93 -0
- package/dist/operations.d.ts +9 -0
- package/dist/schemas.d.ts +29 -0
- package/package.json +22 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
// src/schemas.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
var telegramMessageInputSchema = z.object({
|
|
4
|
+
chatId: z.string().min(1, "Chat ID is required"),
|
|
5
|
+
message: z.string().min(1, "Message is required")
|
|
6
|
+
});
|
|
7
|
+
var telegramMessageOptionsSchema = telegramMessageInputSchema.extend({
|
|
8
|
+
botToken: z.string().min(1, "Telegram bot token is required")
|
|
9
|
+
});
|
|
10
|
+
var telegramSendMessageRequestSchema = z.object({
|
|
11
|
+
chat_id: z.string().min(1),
|
|
12
|
+
text: z.string().min(1)
|
|
13
|
+
});
|
|
14
|
+
var telegramSendMessageResponseSchema = z.object({
|
|
15
|
+
ok: z.boolean(),
|
|
16
|
+
result: z.object({
|
|
17
|
+
message_id: z.number()
|
|
18
|
+
}).optional(),
|
|
19
|
+
description: z.string().optional()
|
|
20
|
+
});
|
|
21
|
+
var telegramMessageOutputSchema = z.object({
|
|
22
|
+
ok: z.literal(true),
|
|
23
|
+
chatId: z.string(),
|
|
24
|
+
messageId: z.number()
|
|
25
|
+
});
|
|
26
|
+
// src/operations.ts
|
|
27
|
+
async function sendTelegramMessage(options) {
|
|
28
|
+
const parsedOptions = telegramMessageOptionsSchema.safeParse(options);
|
|
29
|
+
if (!parsedOptions.success) {
|
|
30
|
+
return {
|
|
31
|
+
success: false,
|
|
32
|
+
error: parsedOptions.error.issues[0]?.message ?? "Invalid input"
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
const { chatId, message, botToken } = parsedOptions.data;
|
|
36
|
+
const parsedRequestBody = telegramSendMessageRequestSchema.safeParse({
|
|
37
|
+
chat_id: chatId,
|
|
38
|
+
text: message
|
|
39
|
+
});
|
|
40
|
+
if (!parsedRequestBody.success) {
|
|
41
|
+
return {
|
|
42
|
+
success: false,
|
|
43
|
+
error: parsedRequestBody.error.issues[0]?.message ?? "Invalid request body"
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
try {
|
|
47
|
+
const res = await fetch(`https://api.telegram.org/bot${botToken}/sendMessage`, {
|
|
48
|
+
method: "POST",
|
|
49
|
+
headers: { "Content-Type": "application/json" },
|
|
50
|
+
body: JSON.stringify(parsedRequestBody.data)
|
|
51
|
+
});
|
|
52
|
+
let rawData;
|
|
53
|
+
try {
|
|
54
|
+
rawData = await res.json();
|
|
55
|
+
} catch {
|
|
56
|
+
return {
|
|
57
|
+
success: false,
|
|
58
|
+
error: "Failed to parse Telegram API response (invalid JSON)."
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const parsedResponse = telegramSendMessageResponseSchema.safeParse(rawData);
|
|
62
|
+
if (!parsedResponse.success) {
|
|
63
|
+
return {
|
|
64
|
+
success: false,
|
|
65
|
+
error: "Unexpected response shape from Telegram API."
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
const data = parsedResponse.data;
|
|
69
|
+
if (!res.ok || !data.ok || !data.result) {
|
|
70
|
+
return {
|
|
71
|
+
success: false,
|
|
72
|
+
error: data.description ?? `Telegram API error (HTTP ${res.status})`
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
const output = telegramMessageOutputSchema.parse({
|
|
76
|
+
ok: true,
|
|
77
|
+
chatId,
|
|
78
|
+
messageId: data.result.message_id
|
|
79
|
+
});
|
|
80
|
+
return { success: true, data: output };
|
|
81
|
+
} catch (err) {
|
|
82
|
+
const msg = err instanceof Error ? err.message : "Unknown error";
|
|
83
|
+
return { success: false, error: `Network error: ${msg}` };
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
export {
|
|
87
|
+
telegramSendMessageResponseSchema,
|
|
88
|
+
telegramSendMessageRequestSchema,
|
|
89
|
+
telegramMessageOutputSchema,
|
|
90
|
+
telegramMessageOptionsSchema,
|
|
91
|
+
telegramMessageInputSchema,
|
|
92
|
+
sendTelegramMessage
|
|
93
|
+
};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type TelegramMessageOptions, type TelegramMessageOutput } from "./schemas";
|
|
2
|
+
export type SendTelegramMessageResult = {
|
|
3
|
+
success: true;
|
|
4
|
+
data: TelegramMessageOutput;
|
|
5
|
+
} | {
|
|
6
|
+
success: false;
|
|
7
|
+
error: string;
|
|
8
|
+
};
|
|
9
|
+
export declare function sendTelegramMessage(options: TelegramMessageOptions): Promise<SendTelegramMessageResult>;
|
|
@@ -0,0 +1,29 @@
|
|
|
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.ZodString;
|
|
13
|
+
text: z.ZodString;
|
|
14
|
+
}, z.core.$strip>;
|
|
15
|
+
export declare const telegramSendMessageResponseSchema: z.ZodObject<{
|
|
16
|
+
ok: z.ZodBoolean;
|
|
17
|
+
result: z.ZodOptional<z.ZodObject<{
|
|
18
|
+
message_id: z.ZodNumber;
|
|
19
|
+
}, z.core.$strip>>;
|
|
20
|
+
description: z.ZodOptional<z.ZodString>;
|
|
21
|
+
}, z.core.$strip>;
|
|
22
|
+
export declare const telegramMessageOutputSchema: z.ZodObject<{
|
|
23
|
+
ok: z.ZodLiteral<true>;
|
|
24
|
+
chatId: z.ZodString;
|
|
25
|
+
messageId: z.ZodNumber;
|
|
26
|
+
}, z.core.$strip>;
|
|
27
|
+
export type TelegramMessageInput = z.infer<typeof telegramMessageInputSchema>;
|
|
28
|
+
export type TelegramMessageOptions = z.infer<typeof telegramMessageOptionsSchema>;
|
|
29
|
+
export type TelegramMessageOutput = z.infer<typeof telegramMessageOutputSchema>;
|
package/package.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "sendkit-core",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"import": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"dist"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "bun build ./src/index.ts --outdir ./dist --target node --format esm --external zod && tsc"
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"zod": "^4.4.3"
|
|
21
|
+
}
|
|
22
|
+
}
|