telegram-private-logger 1.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 +16 -0
- package/dist/index.js +44 -0
- package/package.json +31 -0
package/dist/index.d.ts
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
export interface LoggerOptions {
|
2
|
+
title: string;
|
3
|
+
icon: string;
|
4
|
+
source: string;
|
5
|
+
content?: string;
|
6
|
+
tags?: Record<string, string>;
|
7
|
+
}
|
8
|
+
export interface TelegramPrivateLoggerConfig {
|
9
|
+
token: string;
|
10
|
+
chatId: string;
|
11
|
+
}
|
12
|
+
export interface TelegramPrivateLoggerClient {
|
13
|
+
track: (options: LoggerOptions) => Promise<void>;
|
14
|
+
}
|
15
|
+
export declare function init(config: TelegramPrivateLoggerConfig): TelegramPrivateLoggerClient;
|
16
|
+
export default init;
|
package/dist/index.js
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
export function init(config) {
|
2
|
+
const { token, chatId } = config;
|
3
|
+
if (!token) {
|
4
|
+
throw new Error("Token is required. Please provide a valid token.");
|
5
|
+
}
|
6
|
+
if (!chatId) {
|
7
|
+
throw new Error("ChatId is required. Please provide a valid chatId.");
|
8
|
+
}
|
9
|
+
const track = async (options) => {
|
10
|
+
const { title, icon, content, source, tags } = options;
|
11
|
+
const _icon = icon ?? "🔔";
|
12
|
+
const _title = title && title !== "" ? escapeMarkdownV2(title) : "New Event";
|
13
|
+
const _source = source && source !== "" ? escapeMarkdownV2(source) : "Unknown";
|
14
|
+
const _tags = parseTags(tags ?? {});
|
15
|
+
const message = JSON.stringify({
|
16
|
+
chat_id: chatId,
|
17
|
+
text: `${_icon} ${_title}:\nSource: ${_source}\n${escapeMarkdownV2(content ?? "")}\n\n${_tags}`,
|
18
|
+
parse_mode: "MarkdownV2",
|
19
|
+
});
|
20
|
+
const response = await fetch(`https://api.telegram.org/bot${token}/sendMessage`, {
|
21
|
+
method: "POST",
|
22
|
+
headers: {
|
23
|
+
"Content-Type": "application/json",
|
24
|
+
},
|
25
|
+
body: message,
|
26
|
+
});
|
27
|
+
if (!response.ok) {
|
28
|
+
console.error(JSON.stringify(await response.json(), null, 2));
|
29
|
+
}
|
30
|
+
};
|
31
|
+
return {
|
32
|
+
track,
|
33
|
+
};
|
34
|
+
}
|
35
|
+
// Default export for the init function
|
36
|
+
export default init;
|
37
|
+
function parseTags(tags) {
|
38
|
+
return Object.entries(tags)
|
39
|
+
?.map(([key, value]) => `${key}: *${escapeMarkdownV2(value)}*`)
|
40
|
+
.join("\n");
|
41
|
+
}
|
42
|
+
function escapeMarkdownV2(text) {
|
43
|
+
return text.replace(/[_*[\]()~`>#+\-=|{}.!]/g, "\\$&");
|
44
|
+
}
|
package/package.json
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
{
|
2
|
+
"name": "telegram-private-logger",
|
3
|
+
"version": "1.0.0",
|
4
|
+
"license": "MIT",
|
5
|
+
"author": "",
|
6
|
+
"type": "module",
|
7
|
+
"main": "./dist/index.js",
|
8
|
+
"types": "./dist/index.d.ts",
|
9
|
+
"exports": {
|
10
|
+
".": {
|
11
|
+
"import": "./dist/index.js",
|
12
|
+
"types": "./dist/index.d.ts"
|
13
|
+
}
|
14
|
+
},
|
15
|
+
"files": [
|
16
|
+
"dist"
|
17
|
+
],
|
18
|
+
"scripts": {
|
19
|
+
"build": "tsc",
|
20
|
+
"dev": "tsc --watch",
|
21
|
+
"prepublishOnly": "npm run build",
|
22
|
+
"test": "echo \"Error: no test specified\" && exit 1"
|
23
|
+
},
|
24
|
+
"devDependencies": {
|
25
|
+
"@types/node": "^24.1.0",
|
26
|
+
"typescript": "^5.0.0"
|
27
|
+
},
|
28
|
+
"engines": {
|
29
|
+
"node": ">=18.0.0"
|
30
|
+
}
|
31
|
+
}
|