telegram-bot-raw 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/LICENSE +21 -0
- package/README.md +312 -0
- package/index.d.ts +633 -0
- package/index.js +276 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Pavel Zhukov
|
|
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,312 @@
|
|
|
1
|
+
# [telegram-bot-raw](https://www.npmjs.com/package/telegram-bot-raw) npm package
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+

|
|
5
|
+

|
|
6
|
+

|
|
7
|
+
|
|
8
|
+
This package provides a minimal abstraction over the Telegram Bot API, closely
|
|
9
|
+
follows the official specification without adding extra layers or hidden
|
|
10
|
+
behavior.
|
|
11
|
+
|
|
12
|
+
## Table of Contents
|
|
13
|
+
|
|
14
|
+
- [Features](#features)
|
|
15
|
+
- [Requirements](#requirements)
|
|
16
|
+
- [Supported Bot API](#supported-bot-api)
|
|
17
|
+
- [Installation](#installation)
|
|
18
|
+
- [Quick Start](#quick-start)
|
|
19
|
+
- [Usage](#usage)
|
|
20
|
+
- [Calling API Methods](#calling-api-methods)
|
|
21
|
+
- [Optional Parameters](#optional-parameters)
|
|
22
|
+
- [Methods Without Parameters](#methods-without-parameters)
|
|
23
|
+
- [InputFile Object](#inputfile-object)
|
|
24
|
+
- [Return Values](#return-values)
|
|
25
|
+
- [Error Handling](#error-handling)
|
|
26
|
+
- [Api Class Reference](#api-class-reference)
|
|
27
|
+
- [Constructor](#constructor)
|
|
28
|
+
- [Properties](#properties)
|
|
29
|
+
- [Static Properties](#static-properties)
|
|
30
|
+
- [Static Methods](#static-methods)
|
|
31
|
+
- [License](#license)
|
|
32
|
+
|
|
33
|
+
## Features
|
|
34
|
+
|
|
35
|
+
- Low-level API wrapper with minimal overhead
|
|
36
|
+
- Full control over API requests and parameters
|
|
37
|
+
- Promise-based interface
|
|
38
|
+
- Full TypeScript definitions
|
|
39
|
+
- Zero dependencies
|
|
40
|
+
|
|
41
|
+
## Requirements
|
|
42
|
+
|
|
43
|
+
**Node.js v18** or higher.
|
|
44
|
+
|
|
45
|
+
## Supported Bot API
|
|
46
|
+
|
|
47
|
+
This package supports Telegram Bot API version 9.5 (released March 1, 2026).
|
|
48
|
+
|
|
49
|
+
See the [Telegram Bot API documentation](https://core.telegram.org/bots/api).
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
npm install telegram-bot-raw
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Quick Start
|
|
58
|
+
|
|
59
|
+
```js
|
|
60
|
+
import { Api } from 'telegram-bot-raw';
|
|
61
|
+
|
|
62
|
+
// Create an API client instance
|
|
63
|
+
const botToken = '123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11';
|
|
64
|
+
const api = new Api(botToken);
|
|
65
|
+
|
|
66
|
+
// Get the bot's name (returns a BotName object)
|
|
67
|
+
const botName = await api.getMyName();
|
|
68
|
+
|
|
69
|
+
// Send a message to the user #123456789
|
|
70
|
+
const response = await api.sendMessage({
|
|
71
|
+
chat_id: 123456789,
|
|
72
|
+
text: `Greetings from <b>${botName.name}</b>!`,
|
|
73
|
+
parse_mode: 'HTML'
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Log the sent message object
|
|
77
|
+
console.log('Sent message:', response);
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Usage
|
|
81
|
+
|
|
82
|
+
### Calling API Methods
|
|
83
|
+
|
|
84
|
+
Each Telegram Bot API method is exposed as a method of the `Api` class with the
|
|
85
|
+
same name and parameters. For example, the Bot API method `setMyName` which
|
|
86
|
+
accepts two string parameters `name` and `language_code` can be called as
|
|
87
|
+
follows:
|
|
88
|
+
|
|
89
|
+
```js
|
|
90
|
+
await api.setMyName({
|
|
91
|
+
name: 'マイちゃんボット',
|
|
92
|
+
language_code: 'jp'
|
|
93
|
+
});
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
To call an undocumented Bot API method, use the static `call` method, passing
|
|
97
|
+
your `Api` instance, the name of the API method, and an optional object with the
|
|
98
|
+
API call parameters. The following example uses fictitious undocumented API
|
|
99
|
+
methods:
|
|
100
|
+
|
|
101
|
+
```js
|
|
102
|
+
const api = new Api('123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11');
|
|
103
|
+
const userId = 123456789;
|
|
104
|
+
|
|
105
|
+
if (await Api.call(api, 'isUserWombat', { user_id: userId })) {
|
|
106
|
+
await Api.call(api, 'markUserAsCute', {
|
|
107
|
+
user_id: userId,
|
|
108
|
+
reason: 'wombat'
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Optional Parameters
|
|
114
|
+
|
|
115
|
+
If all parameters are optional, the argument can be omitted. The following calls
|
|
116
|
+
are equivalent:
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
await api.deleteWebhook({ drop_pending_updates: undefined });
|
|
120
|
+
await api.deleteWebhook({});
|
|
121
|
+
await api.deleteWebhook();
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Methods Without Parameters
|
|
125
|
+
|
|
126
|
+
API methods that do not accept parameters should be called without arguments:
|
|
127
|
+
|
|
128
|
+
```js
|
|
129
|
+
await api.logOut();
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### InputFile Object
|
|
133
|
+
|
|
134
|
+
In this library, `InputFile` is an alias of the native Node.js `File` class from
|
|
135
|
+
`node:buffer`:
|
|
136
|
+
|
|
137
|
+
```js
|
|
138
|
+
import { readFileSync } from 'node:fs';
|
|
139
|
+
import { InputFile } from 'telegram-bot-raw';
|
|
140
|
+
|
|
141
|
+
const buffer = readFileSync('photo.png');
|
|
142
|
+
const file = new InputFile([buffer], 'photo.png');
|
|
143
|
+
|
|
144
|
+
await api.setMyProfilePhoto({ photo: file });
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
See the
|
|
148
|
+
[File class documentation](https://nodejs.org/api/buffer.html#class-file).
|
|
149
|
+
|
|
150
|
+
### Return Values
|
|
151
|
+
|
|
152
|
+
All methods return a `Promise` that resolves to the result defined in the
|
|
153
|
+
[Telegram Bot API documentation](https://core.telegram.org/bots/api).
|
|
154
|
+
|
|
155
|
+
### Error Handling
|
|
156
|
+
|
|
157
|
+
If a request fails, an `ApiError` is thrown:
|
|
158
|
+
|
|
159
|
+
```js
|
|
160
|
+
import { ApiError } from 'telegram-bot-raw';
|
|
161
|
+
|
|
162
|
+
const params = {
|
|
163
|
+
user_id: 123456789,
|
|
164
|
+
chat_id: 987654321
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
try {
|
|
168
|
+
await api.forwardMessage(params);
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
if (error instanceof ApiError) {
|
|
172
|
+
console.error('Error code:', error.code);
|
|
173
|
+
console.error('Error message:', error.message);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
`error.code` and `error.message` contain the `error_code` and `description`
|
|
179
|
+
response values, respectively.
|
|
180
|
+
|
|
181
|
+
## `Api` Class Reference
|
|
182
|
+
|
|
183
|
+
### Constructor
|
|
184
|
+
|
|
185
|
+
```ts
|
|
186
|
+
new Api(botToken: string)
|
|
187
|
+
```
|
|
188
|
+
- `botToken`: Telegram bot token.
|
|
189
|
+
|
|
190
|
+
### Properties
|
|
191
|
+
|
|
192
|
+
```ts
|
|
193
|
+
keepAlive: boolean
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
Enables or disables HTTP keep-alive for requests to `api.telegram.org`.
|
|
197
|
+
|
|
198
|
+
The default value is `false`.
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
timeout: number
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
Sets request timeout:
|
|
207
|
+
- `> 0` – timeout in milliseconds,
|
|
208
|
+
- `0` – no timeout,
|
|
209
|
+
- other values are treated as `0`.
|
|
210
|
+
|
|
211
|
+
If the timeout is exceeded, an `AbortError` is thrown:
|
|
212
|
+
|
|
213
|
+
```js
|
|
214
|
+
api.timeout = 5000;
|
|
215
|
+
|
|
216
|
+
const params = {
|
|
217
|
+
message_id: 123456789,
|
|
218
|
+
chat_id: 987654321,
|
|
219
|
+
from_chat_id: -987654321,
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
try {
|
|
223
|
+
await api.banChatMember(params);
|
|
224
|
+
}
|
|
225
|
+
catch (error) {
|
|
226
|
+
if (error.name === 'AbortError')
|
|
227
|
+
console.error('Server is not responding.');
|
|
228
|
+
else
|
|
229
|
+
throw error;
|
|
230
|
+
}
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
The default value is `0`.
|
|
234
|
+
|
|
235
|
+
---
|
|
236
|
+
|
|
237
|
+
```ts
|
|
238
|
+
transport: ApiTransport | null
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Sets a function responsible for making HTTP requests to the Telegram Bot API:
|
|
242
|
+
|
|
243
|
+
```ts
|
|
244
|
+
function(url: string, init: RequestInit): Promise<Response>
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
**Parameters:**
|
|
248
|
+
|
|
249
|
+
- `url` – the full request URL.
|
|
250
|
+
- `init` – request options, including method, headers, etc.
|
|
251
|
+
|
|
252
|
+
**Returns:**
|
|
253
|
+
|
|
254
|
+
- A `Promise` that resolves to a `Response` object.
|
|
255
|
+
|
|
256
|
+
This can be used for logging, simulating server requests, and other tasks:
|
|
257
|
+
|
|
258
|
+
```js
|
|
259
|
+
api.transport = (url, init) => {
|
|
260
|
+
const method = init.method ?? 'GET';
|
|
261
|
+
console.log(`${method}:`, url);
|
|
262
|
+
return fetch(url, init);
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
- If `transport` is a `Function`, it will be used to send all API requests.
|
|
267
|
+
- If `transport` is `null`, the built-in implementation is used.
|
|
268
|
+
- Other values are treated as `null`.
|
|
269
|
+
|
|
270
|
+
The default value is `null`.
|
|
271
|
+
|
|
272
|
+
---
|
|
273
|
+
|
|
274
|
+
### Static Properties
|
|
275
|
+
|
|
276
|
+
```ts
|
|
277
|
+
version: string
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
Returns the version of the Telegram Bot API supported by the library:
|
|
281
|
+
|
|
282
|
+
```js
|
|
283
|
+
console.log(Api.version); // '9.5'
|
|
284
|
+
```
|
|
285
|
+
|
|
286
|
+
### Static Methods
|
|
287
|
+
|
|
288
|
+
```ts
|
|
289
|
+
call<T>(api: Api, method: string, params?: Record<string, any>): Promise<T>
|
|
290
|
+
```
|
|
291
|
+
|
|
292
|
+
Calls an arbitrary API method.
|
|
293
|
+
|
|
294
|
+
**Parameters:**
|
|
295
|
+
|
|
296
|
+
- `api` – an `Api` instance.
|
|
297
|
+
- `method` – the API method name.
|
|
298
|
+
- `params` – the API method parameters, if present.
|
|
299
|
+
|
|
300
|
+
**Returns:**
|
|
301
|
+
|
|
302
|
+
- A `Promise` that resolves to the value returned by the server.
|
|
303
|
+
|
|
304
|
+
See an example of calling undocumented methods in the
|
|
305
|
+
[Calling API Methods](#calling-api-methods) section.
|
|
306
|
+
|
|
307
|
+
The `call` method may throw an exception, see the
|
|
308
|
+
[Error Handling](#error-handling) section.
|
|
309
|
+
|
|
310
|
+
## License
|
|
311
|
+
|
|
312
|
+
- MIT
|