telegram-private-logger 1.0.0 โ 1.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/README.md +317 -0
- package/package.json +1 -1
package/README.md
ADDED
@@ -0,0 +1,317 @@
|
|
1
|
+
# Telegram Private Logger
|
2
|
+
|
3
|
+
A lightweight, typed logger for sending logs directly to Telegram chats using the official Telegram Bot API with full TypeScript support.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```bash
|
8
|
+
npm install telegram-private-logger
|
9
|
+
```
|
10
|
+
|
11
|
+
## Quick Start
|
12
|
+
|
13
|
+
```typescript
|
14
|
+
import { init } from "telegram-private-logger";
|
15
|
+
|
16
|
+
// Initialize the logger client
|
17
|
+
const logger = init({
|
18
|
+
token: "your-telegram-bot-token",
|
19
|
+
chatId: "your-chat-id",
|
20
|
+
});
|
21
|
+
|
22
|
+
// Send a log message
|
23
|
+
await logger.track({
|
24
|
+
title: "User Login",
|
25
|
+
icon: "๐",
|
26
|
+
source: "auth-service",
|
27
|
+
content: "User successfully logged in",
|
28
|
+
tags: {
|
29
|
+
userId: "12345",
|
30
|
+
method: "email",
|
31
|
+
},
|
32
|
+
});
|
33
|
+
```
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
|
37
|
+
### Basic Setup
|
38
|
+
|
39
|
+
Create a logger client in your project:
|
40
|
+
|
41
|
+
```typescript
|
42
|
+
// logger.ts
|
43
|
+
import { init } from "telegram-private-logger";
|
44
|
+
|
45
|
+
export const telegramLogger = init({
|
46
|
+
token: process.env.PRIVATE_LOGGER_TOKEN!,
|
47
|
+
chatId: process.env.PRIVATE_LOGGER_CHAT_ID!,
|
48
|
+
});
|
49
|
+
```
|
50
|
+
|
51
|
+
### Using the Logger
|
52
|
+
|
53
|
+
```typescript
|
54
|
+
// app.ts
|
55
|
+
import { telegramLogger } from "./logger";
|
56
|
+
|
57
|
+
// Track user actions
|
58
|
+
await telegramLogger.track({
|
59
|
+
title: "Dashboard Loaded",
|
60
|
+
icon: "๐",
|
61
|
+
source: "dashboard-service",
|
62
|
+
content: "User dashboard loaded successfully",
|
63
|
+
tags: {
|
64
|
+
userId: "12345",
|
65
|
+
organizationId: "67890",
|
66
|
+
},
|
67
|
+
});
|
68
|
+
|
69
|
+
// Track errors
|
70
|
+
await telegramLogger.track({
|
71
|
+
title: "API Error",
|
72
|
+
icon: "โ",
|
73
|
+
source: "api-service",
|
74
|
+
content: "Failed to fetch user data from database",
|
75
|
+
tags: {
|
76
|
+
endpoint: "/api/users",
|
77
|
+
statusCode: "500",
|
78
|
+
error: "database_connection_failed",
|
79
|
+
},
|
80
|
+
});
|
81
|
+
|
82
|
+
// Track simple events
|
83
|
+
await telegramLogger.track({
|
84
|
+
title: "User Action",
|
85
|
+
icon: "๐ค",
|
86
|
+
source: "user-service",
|
87
|
+
});
|
88
|
+
```
|
89
|
+
|
90
|
+
### Environment Variables
|
91
|
+
|
92
|
+
```bash
|
93
|
+
# .env
|
94
|
+
PRIVATE_LOGGER_TOKEN=your_bot_token_here
|
95
|
+
PRIVATE_LOGGER_CHAT_ID=your_chat_id_here
|
96
|
+
```
|
97
|
+
|
98
|
+
## API Reference
|
99
|
+
|
100
|
+
### `init(config: TelegramPrivateLoggerConfig): TelegramPrivateLoggerClient`
|
101
|
+
|
102
|
+
Initializes a new telegram logger client.
|
103
|
+
|
104
|
+
#### Parameters
|
105
|
+
|
106
|
+
- `config` (TelegramPrivateLoggerConfig): Configuration object
|
107
|
+
- `token` (string): Your Telegram bot authentication token
|
108
|
+
- `chatId` (string): The chat ID where logs will be sent
|
109
|
+
|
110
|
+
#### Returns
|
111
|
+
|
112
|
+
- `TelegramPrivateLoggerClient`: A client instance with tracking capabilities
|
113
|
+
|
114
|
+
### `TelegramPrivateLoggerClient.track(options: LoggerOptions): Promise<void>`
|
115
|
+
|
116
|
+
Sends a log entry to your Telegram chat.
|
117
|
+
|
118
|
+
#### Parameters
|
119
|
+
|
120
|
+
- `options` (LoggerOptions): Logging options object
|
121
|
+
- `title` (string): Title for the log entry
|
122
|
+
- `icon` (string): Emoji or icon (e.g., "๐", "โ", "โ
")
|
123
|
+
- `source` (string): Source identifier (e.g., "auth-service", "api-gateway")
|
124
|
+
- `content?` (string): Optional detailed content/message
|
125
|
+
- `tags?` (Record<string, string>): Optional key-value pairs for categorization
|
126
|
+
|
127
|
+
#### Returns
|
128
|
+
|
129
|
+
- `Promise<void>`: Resolves when the log is sent successfully
|
130
|
+
|
131
|
+
## TypeScript Support
|
132
|
+
|
133
|
+
This package is written in TypeScript and provides full type definitions. All parameters are properly typed and will show up in your IDE with autocomplete and type checking.
|
134
|
+
|
135
|
+
```typescript
|
136
|
+
import {
|
137
|
+
init,
|
138
|
+
LoggerOptions,
|
139
|
+
TelegramPrivateLoggerConfig,
|
140
|
+
} from "telegram-private-logger";
|
141
|
+
|
142
|
+
// Full type safety
|
143
|
+
const config: TelegramPrivateLoggerConfig = {
|
144
|
+
token: process.env.PRIVATE_LOGGER_TOKEN!,
|
145
|
+
chatId: process.env.PRIVATE_LOGGER_CHAT_ID!,
|
146
|
+
};
|
147
|
+
|
148
|
+
const logData: LoggerOptions = {
|
149
|
+
title: "Database Connection",
|
150
|
+
icon: "๐พ",
|
151
|
+
source: "database-service",
|
152
|
+
content: "Successfully connected to PostgreSQL",
|
153
|
+
tags: {
|
154
|
+
database: "postgres",
|
155
|
+
connection: "pool",
|
156
|
+
},
|
157
|
+
};
|
158
|
+
|
159
|
+
const client = init(config);
|
160
|
+
await client.track(logData);
|
161
|
+
```
|
162
|
+
|
163
|
+
## Examples
|
164
|
+
|
165
|
+
### Error Tracking
|
166
|
+
|
167
|
+
```typescript
|
168
|
+
const logError = async (error: Error, context: Record<string, string>) => {
|
169
|
+
await telegramLogger.track({
|
170
|
+
title: "Application Error",
|
171
|
+
icon: "๐จ",
|
172
|
+
source: "error-handler",
|
173
|
+
content: error.message,
|
174
|
+
tags: {
|
175
|
+
errorType: error.name,
|
176
|
+
stack: error.stack?.split("\n")[0] || "",
|
177
|
+
...context,
|
178
|
+
},
|
179
|
+
});
|
180
|
+
};
|
181
|
+
```
|
182
|
+
|
183
|
+
### User Activity Tracking
|
184
|
+
|
185
|
+
```typescript
|
186
|
+
const trackUserAction = async (
|
187
|
+
action: string,
|
188
|
+
userId: string,
|
189
|
+
details?: string
|
190
|
+
) => {
|
191
|
+
await telegramLogger.track({
|
192
|
+
title: `User Action: ${action}`,
|
193
|
+
icon: "๐ค",
|
194
|
+
source: "user-service",
|
195
|
+
content: details,
|
196
|
+
tags: {
|
197
|
+
userId,
|
198
|
+
action,
|
199
|
+
timestamp: new Date().toISOString(),
|
200
|
+
},
|
201
|
+
});
|
202
|
+
};
|
203
|
+
```
|
204
|
+
|
205
|
+
### API Request Logging
|
206
|
+
|
207
|
+
```typescript
|
208
|
+
const logApiRequest = async (
|
209
|
+
method: string,
|
210
|
+
endpoint: string,
|
211
|
+
statusCode: number
|
212
|
+
) => {
|
213
|
+
await telegramLogger.track({
|
214
|
+
title: `API ${method} ${endpoint}`,
|
215
|
+
icon: statusCode >= 400 ? "โ" : "โ
",
|
216
|
+
source: "api-gateway",
|
217
|
+
content: `HTTP ${method} ${endpoint} returned ${statusCode}`,
|
218
|
+
tags: {
|
219
|
+
method,
|
220
|
+
endpoint,
|
221
|
+
statusCode: statusCode.toString(),
|
222
|
+
},
|
223
|
+
});
|
224
|
+
};
|
225
|
+
```
|
226
|
+
|
227
|
+
## Configuration
|
228
|
+
|
229
|
+
### Required Parameters
|
230
|
+
|
231
|
+
- **token**: Your Telegram bot token (get it from [@BotFather](https://t.me/botfather))
|
232
|
+
- **chatId**: The chat ID where logs will be sent (can be a user chat, group, or channel)
|
233
|
+
|
234
|
+
### Required Log Fields
|
235
|
+
|
236
|
+
The following fields are required for each log entry:
|
237
|
+
|
238
|
+
- **title**: The main title of the log entry
|
239
|
+
- **icon**: An emoji or icon to represent the log type
|
240
|
+
- **source**: The source/service identifier
|
241
|
+
|
242
|
+
### Optional Log Fields
|
243
|
+
|
244
|
+
- **content**: Detailed description of what happened
|
245
|
+
- **tags**: Key-value pairs for additional context
|
246
|
+
|
247
|
+
```typescript
|
248
|
+
// Minimal required log
|
249
|
+
await telegramLogger.track({
|
250
|
+
title: "Simple event",
|
251
|
+
icon: "๐",
|
252
|
+
source: "service-name",
|
253
|
+
});
|
254
|
+
|
255
|
+
// Full log with all fields
|
256
|
+
await telegramLogger.track({
|
257
|
+
title: "Complex event",
|
258
|
+
icon: "๐ฏ",
|
259
|
+
source: "service-name",
|
260
|
+
content: "Detailed description of what happened",
|
261
|
+
tags: {
|
262
|
+
category: "user-action",
|
263
|
+
priority: "high",
|
264
|
+
environment: "production",
|
265
|
+
},
|
266
|
+
});
|
267
|
+
```
|
268
|
+
|
269
|
+
## Getting Your Telegram Bot Token and Chat ID
|
270
|
+
|
271
|
+
### 1. Create a Bot
|
272
|
+
|
273
|
+
1. Message [@BotFather](https://t.me/botfather) on Telegram
|
274
|
+
2. Send `/newbot` and follow the instructions
|
275
|
+
3. Save the bot token you receive
|
276
|
+
|
277
|
+
### 2. Get Your Chat ID
|
278
|
+
|
279
|
+
1. Start a chat with your bot
|
280
|
+
2. Send any message to the bot
|
281
|
+
3. Visit: `https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates`
|
282
|
+
4. Find your chat ID in the response
|
283
|
+
|
284
|
+
## Error Handling
|
285
|
+
|
286
|
+
The logger will automatically log errors to the console if the request fails:
|
287
|
+
|
288
|
+
```typescript
|
289
|
+
try {
|
290
|
+
await telegramLogger.track({
|
291
|
+
title: "Test log",
|
292
|
+
icon: "๐งช",
|
293
|
+
source: "test-service",
|
294
|
+
});
|
295
|
+
} catch (error) {
|
296
|
+
// The logger will automatically console.error the response
|
297
|
+
// You can also handle errors here if needed
|
298
|
+
console.error("Failed to send log:", error);
|
299
|
+
}
|
300
|
+
```
|
301
|
+
|
302
|
+
## Message Format
|
303
|
+
|
304
|
+
The logger sends formatted messages to Telegram using MarkdownV2 format:
|
305
|
+
|
306
|
+
```
|
307
|
+
๐ User Login:
|
308
|
+
Source: auth-service
|
309
|
+
User successfully logged in
|
310
|
+
|
311
|
+
userId: *12345*
|
312
|
+
method: *email*
|
313
|
+
```
|
314
|
+
|
315
|
+
## License
|
316
|
+
|
317
|
+
MIT
|