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.
Files changed (5) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +312 -0
  3. package/index.d.ts +633 -0
  4. package/index.js +276 -0
  5. package/package.json +43 -0
package/index.js ADDED
@@ -0,0 +1,276 @@
1
+ import { File as InputFile } from 'node:buffer';
2
+ export { InputFile };
3
+
4
+ function createFormData(params) {
5
+ const result = new FormData();
6
+ for (const [key, value] of Object.entries(params)) {
7
+ if (value === undefined)
8
+ continue;
9
+ if (typeof value === 'string')
10
+ result.append(key, value)
11
+ else if (value instanceof InputFile)
12
+ result.append(key, value, value.name);
13
+ else
14
+ result.append(key, JSON.stringify(value));
15
+ }
16
+ return result;
17
+ }
18
+
19
+ function createRequestInit(params) {
20
+ /** @type {RequestInit} */
21
+ const result = {};
22
+ if (params !== undefined) {
23
+ const values = Object.values(params);
24
+ if (values.some(value => value instanceof InputFile)) {
25
+ result.body = createFormData(params);
26
+ result.method = 'POST';
27
+ }
28
+ else if (values.length > 0) {
29
+ result.body = JSON.stringify(params);
30
+ result.headers = { 'Content-Type': 'application/json' };
31
+ result.method = 'POST';
32
+ }
33
+ }
34
+ return result;
35
+ }
36
+
37
+ export class ApiError extends Error {
38
+ #code;
39
+ constructor(message, code) {
40
+ super(message);
41
+ this.#code = code;
42
+ }
43
+ get code() {
44
+ return this.#code;
45
+ }
46
+ }
47
+
48
+ export class Api {
49
+
50
+ static call(api, method, params) {
51
+ return api.#call(method, params);
52
+ }
53
+
54
+ static get version() {
55
+ return '9.5';
56
+ }
57
+
58
+ #keepAlive = false;
59
+ #path;
60
+ #timeout = 0;
61
+
62
+ /** @type {import('.').ApiTransport?} */
63
+ #transport = null;
64
+
65
+ async #call(method, params) {
66
+ const init = createRequestInit(params);
67
+ if (this.#keepAlive)
68
+ init.keepalive = true;
69
+ const timeout = this.#timeout;
70
+ if (timeout > 0)
71
+ init.signal = AbortSignal.timeout(timeout);
72
+ const transport = this.#transport ?? fetch;
73
+ const response = await transport(this.#path + method, init);
74
+ const json = await response.json();
75
+ if (json.ok)
76
+ return json.result;
77
+ throw new ApiError(json.description, json.error_code);
78
+ }
79
+
80
+ constructor(botToken) {
81
+ this.#path = `https://api.telegram.org/bot${botToken}/`;
82
+ }
83
+
84
+ addStickerToSet(params) { return this.#call('addStickerToSet', params); }
85
+ answerCallbackQuery(params) { return this.#call('answerCallbackQuery', params); }
86
+ answerInlineQuery(params) { return this.#call('answerInlineQuery', params); }
87
+ answerPreCheckoutQuery(params) { return this.#call('answerPreCheckoutQuery', params); }
88
+ answerShippingQuery(params) { return this.#call('answerShippingQuery', params); }
89
+ answerWebAppQuery(params) { return this.#call('answerWebAppQuery', params); }
90
+ approveChatJoinRequest(params) { return this.#call('approveChatJoinRequest', params); }
91
+ approveSuggestedPost(params) { return this.#call('approveSuggestedPost', params); }
92
+ banChatMember(params) { return this.#call('banChatMember', params); }
93
+ banChatSenderChat(params) { return this.#call('banChatSenderChat', params); }
94
+ close() { return this.#call('close'); }
95
+ closeForumTopic(params) { return this.#call('closeForumTopic', params); }
96
+ closeGeneralForumTopic(params) { return this.#call('closeGeneralForumTopic', params); }
97
+ convertGiftToStars(params) { return this.#call('convertGiftToStars', params); }
98
+ copyMessage(params) { return this.#call('copyMessage', params); }
99
+ copyMessages(params) { return this.#call('copyMessages', params); }
100
+ createChatInviteLink(params) { return this.#call('createChatInviteLink', params); }
101
+ createChatSubscriptionInviteLink(params) { return this.#call('createChatSubscriptionInviteLink', params); }
102
+ createForumTopic(params) { return this.#call('createForumTopic', params); }
103
+ createInvoiceLink(params) { return this.#call('createInvoiceLink', params); }
104
+ createNewStickerSet(params) { return this.#call('createNewStickerSet', params); }
105
+ declineChatJoinRequest(params) { return this.#call('declineChatJoinRequest', params); }
106
+ declineSuggestedPost(params) { return this.#call('declineSuggestedPost', params); }
107
+ deleteBusinessMessages(params) { return this.#call('deleteBusinessMessages', params); }
108
+ deleteChatPhoto(params) { return this.#call('deleteChatPhoto', params); }
109
+ deleteChatStickerSet(params) { return this.#call('deleteChatStickerSet', params); }
110
+ deleteForumTopic(params) { return this.#call('deleteForumTopic', params); }
111
+ deleteMessage(params) { return this.#call('deleteMessage', params); }
112
+ deleteMessages(params) { return this.#call('deleteMessages', params); }
113
+ deleteMyCommands(params) { return this.#call('deleteMyCommands', params); }
114
+ deleteStickerFromSet(params) { return this.#call('deleteStickerFromSet', params); }
115
+ deleteStickerSet(params) { return this.#call('deleteStickerSet', params); }
116
+ deleteStory(params) { return this.#call('deleteStory', params); }
117
+ deleteWebhook(params) { return this.#call('deleteWebhook', params); }
118
+ editChatInviteLink(params) { return this.#call('editChatInviteLink', params); }
119
+ editChatSubscriptionInviteLink(params) { return this.#call('editChatSubscriptionInviteLink', params); }
120
+ editForumTopic(params) { return this.#call('editForumTopic', params); }
121
+ editGeneralForumTopic(params) { return this.#call('editGeneralForumTopic', params); }
122
+ editMessageCaption(params) { return this.#call('editMessageCaption', params); }
123
+ editMessageChecklist(params) { return this.#call('editMessageChecklist', params); }
124
+ editMessageLiveLocation(params) { return this.#call('editMessageLiveLocation', params); }
125
+ editMessageMedia(params) { return this.#call('editMessageMedia', params); }
126
+ editMessageReplyMarkup(params) { return this.#call('editMessageReplyMarkup', params); }
127
+ editMessageText(params) { return this.#call('editMessageText', params); }
128
+ editStory(params) { return this.#call('editStory', params); }
129
+ editUserStarSubscription(params) { return this.#call('editUserStarSubscription', params); }
130
+ exportChatInviteLink(params) { return this.#call('exportChatInviteLink', params); }
131
+ forwardMessage(params) { return this.#call('forwardMessage', params); }
132
+ forwardMessages(params) { return this.#call('forwardMessages', params); }
133
+ getAvailableGifts() { return this.#call('getAvailableGifts'); }
134
+ getBusinessAccountGifts(params) { return this.#call('getBusinessAccountGifts', params); }
135
+ getBusinessAccountStarBalance(params) { return this.#call('getBusinessAccountStarBalance', params); }
136
+ getBusinessConnection(params) { return this.#call('getBusinessConnection', params); }
137
+ getChat(params) { return this.#call('getChat', params); }
138
+ getChatAdministrators(params) { return this.#call('getChatAdministrators', params); }
139
+ getChatGifts(params) { return this.#call('getChatGifts', params); }
140
+ getChatMember(params) { return this.#call('getChatMember', params); }
141
+ getChatMemberCount(params) { return this.#call('getChatMemberCount', params); }
142
+ getChatMenuButton(params) { return this.#call('getChatMenuButton', params); }
143
+ getCustomEmojiStickers(params) { return this.#call('getCustomEmojiStickers', params); }
144
+ getFile(params) { return this.#call('getFile', params); }
145
+ getForumTopicIconStickers() { return this.#call('getForumTopicIconStickers'); }
146
+ getGameHighScores(params) { return this.#call('getGameHighScores', params); }
147
+ getMe() { return this.#call('getMe'); }
148
+ getMyCommands(params) { return this.#call('getMyCommands', params); }
149
+ getMyDefaultAdministratorRights(params) { return this.#call('getMyDefaultAdministratorRights', params); }
150
+ getMyDescription(params) { return this.#call('getMyDescription', params); }
151
+ getMyName(params) { return this.#call('getMyName', params); }
152
+ getMyShortDescription(params) { return this.#call('getMyShortDescription', params); }
153
+ getMyStarBalance() { return this.#call('getMyStarBalance'); }
154
+ getStarTransactions(params) { return this.#call('getStarTransactions', params); }
155
+ getStickerSet(params) { return this.#call('getStickerSet', params); }
156
+ getUpdates(params) { return this.#call('getUpdates', params); }
157
+ getUserChatBoosts(params) { return this.#call('getUserChatBoosts', params); }
158
+ getUserGifts(params) { return this.#call('getUserGifts', params); }
159
+ getUserProfileAudios(params) { return this.#call('getUserProfileAudios', params); }
160
+ getUserProfilePhotos(params) { return this.#call('getUserProfilePhotos', params); }
161
+ getWebhookInfo() { return this.#call('getWebhookInfo'); }
162
+ giftPremiumSubscription(params) { return this.#call('giftPremiumSubscription', params); }
163
+ hideGeneralForumTopic(params) { return this.#call('hideGeneralForumTopic', params); }
164
+ leaveChat(params) { return this.#call('leaveChat', params); }
165
+ logOut() { return this.#call('logOut'); }
166
+ pinChatMessage(params) { return this.#call('pinChatMessage', params); }
167
+ postStory(params) { return this.#call('postStory', params); }
168
+ promoteChatMember(params) { return this.#call('promoteChatMember', params); }
169
+ readBusinessMessage(params) { return this.#call('readBusinessMessage', params); }
170
+ refundStarPayment(params) { return this.#call('refundStarPayment', params); }
171
+ removeBusinessAccountProfilePhoto(params) { return this.#call('removeBusinessAccountProfilePhoto', params); }
172
+ removeChatVerification(params) { return this.#call('removeChatVerification', params); }
173
+ removeMyProfilePhoto() { return this.#call('removeMyProfilePhoto'); }
174
+ removeUserVerification(params) { return this.#call('removeUserVerification', params); }
175
+ reopenForumTopic(params) { return this.#call('reopenForumTopic', params); }
176
+ reopenGeneralForumTopic(params) { return this.#call('reopenGeneralForumTopic', params); }
177
+ replaceStickerInSet(params) { return this.#call('replaceStickerInSet', params); }
178
+ repostStory(params) { return this.#call('repostStory', params); }
179
+ restrictChatMember(params) { return this.#call('restrictChatMember', params); }
180
+ revokeChatInviteLink(params) { return this.#call('revokeChatInviteLink', params); }
181
+ savePreparedInlineMessage(params) { return this.#call('savePreparedInlineMessage', params); }
182
+ sendAnimation(params) { return this.#call('sendAnimation', params); }
183
+ sendAudio(params) { return this.#call('sendAudio', params); }
184
+ sendChatAction(params) { return this.#call('sendChatAction', params); }
185
+ sendChecklist(params) { return this.#call('sendChecklist', params); }
186
+ sendContact(params) { return this.#call('sendContact', params); }
187
+ sendDice(params) { return this.#call('sendDice', params); }
188
+ sendDocument(params) { return this.#call('sendDocument', params); }
189
+ sendGame(params) { return this.#call('sendGame', params); }
190
+ sendGift(params) { return this.#call('sendGift', params); }
191
+ sendInvoice(params) { return this.#call('sendInvoice', params); }
192
+ sendLocation(params) { return this.#call('sendLocation', params); }
193
+ sendMediaGroup(params) { return this.#call('sendMediaGroup', params); }
194
+ sendMessage(params) { return this.#call('sendMessage', params); }
195
+ sendMessageDraft(params) { return this.#call('sendMessageDraft', params); }
196
+ sendPaidMedia(params) { return this.#call('sendPaidMedia', params); }
197
+ sendPhoto(params) { return this.#call('sendPhoto', params); }
198
+ sendPoll(params) { return this.#call('sendPoll', params); }
199
+ sendSticker(params) { return this.#call('sendSticker', params); }
200
+ sendVenue(params) { return this.#call('sendVenue', params); }
201
+ sendVideo(params) { return this.#call('sendVideo', params); }
202
+ sendVideoNote(params) { return this.#call('sendVideoNote', params); }
203
+ sendVoice(params) { return this.#call('sendVoice', params); }
204
+ setBusinessAccountBio(params) { return this.#call('setBusinessAccountBio', params); }
205
+ setBusinessAccountGiftSettings(params) { return this.#call('setBusinessAccountGiftSettings', params); }
206
+ setBusinessAccountName(params) { return this.#call('setBusinessAccountName', params); }
207
+ setBusinessAccountProfilePhoto(params) { return this.#call('setBusinessAccountProfilePhoto', params); }
208
+ setBusinessAccountUsername(params) { return this.#call('setBusinessAccountUsername', params); }
209
+ setChatAdministratorCustomTitle(params) { return this.#call('setChatAdministratorCustomTitle', params); }
210
+ setChatDescription(params) { return this.#call('setChatDescription', params); }
211
+ setChatMemberTag(params) { return this.#call('setChatMemberTag', params); }
212
+ setChatMenuButton(params) { return this.#call('setChatMenuButton', params); }
213
+ setChatPermissions(params) { return this.#call('setChatPermissions', params); }
214
+ setChatPhoto(params) { return this.#call('setChatPhoto', params); }
215
+ setChatStickerSet(params) { return this.#call('setChatStickerSet', params); }
216
+ setChatTitle(params) { return this.#call('setChatTitle', params); }
217
+ setCustomEmojiStickerSetThumbnail(params) { return this.#call('setCustomEmojiStickerSetThumbnail', params); }
218
+ setGameScore(params) { return this.#call('setGameScore', params); }
219
+ setMessageReaction(params) { return this.#call('setMessageReaction', params); }
220
+ setMyCommands(params) { return this.#call('setMyCommands', params); }
221
+ setMyDefaultAdministratorRights(params) { return this.#call('setMyDefaultAdministratorRights', params); }
222
+ setMyDescription(params) { return this.#call('setMyDescription', params); }
223
+ setMyName(params) { return this.#call('setMyName', params); }
224
+ setMyProfilePhoto(params) { return this.#call('setMyProfilePhoto', params); }
225
+ setMyShortDescription(params) { return this.#call('setMyShortDescription', params); }
226
+ setPassportDataErrors(params) { return this.#call('setPassportDataErrors', params); }
227
+ setStickerEmojiList(params) { return this.#call('setStickerEmojiList', params); }
228
+ setStickerKeywords(params) { return this.#call('setStickerKeywords', params); }
229
+ setStickerMaskPosition(params) { return this.#call('setStickerMaskPosition', params); }
230
+ setStickerPositionInSet(params) { return this.#call('setStickerPositionInSet', params); }
231
+ setStickerSetThumbnail(params) { return this.#call('setStickerSetThumbnail', params); }
232
+ setStickerSetTitle(params) { return this.#call('setStickerSetTitle', params); }
233
+ setUserEmojiStatus(params) { return this.#call('setUserEmojiStatus', params); }
234
+ setWebhook(params) { return this.#call('setWebhook', params); }
235
+ stopMessageLiveLocation(params) { return this.#call('stopMessageLiveLocation', params); }
236
+ stopPoll(params) { return this.#call('stopPoll', params); }
237
+ transferBusinessAccountStars(params) { return this.#call('transferBusinessAccountStars', params); }
238
+ transferGift(params) { return this.#call('transferGift', params); }
239
+ unbanChatMember(params) { return this.#call('unbanChatMember', params); }
240
+ unbanChatSenderChat(params) { return this.#call('unbanChatSenderChat', params); }
241
+ unhideGeneralForumTopic(params) { return this.#call('unhideGeneralForumTopic', params); }
242
+ unpinAllChatMessages(params) { return this.#call('unpinAllChatMessages', params); }
243
+ unpinAllForumTopicMessages(params) { return this.#call('unpinAllForumTopicMessages', params); }
244
+ unpinAllGeneralForumTopicMessages(params) { return this.#call('unpinAllGeneralForumTopicMessages', params); }
245
+ unpinChatMessage(params) { return this.#call('unpinChatMessage', params); }
246
+ upgradeGift(params) { return this.#call('upgradeGift', params); }
247
+ uploadStickerFile(params) { return this.#call('uploadStickerFile', params); }
248
+ verifyChat(params) { return this.#call('verifyChat', params); }
249
+ verifyUser(params) { return this.#call('verifyUser', params); }
250
+
251
+ get keepAlive() {
252
+ return this.#keepAlive;
253
+ }
254
+
255
+ set keepAlive(value) {
256
+ this.#keepAlive = Boolean(value);
257
+ }
258
+
259
+ get timeout() {
260
+ return this.#timeout;
261
+ }
262
+
263
+ set timeout(value) {
264
+ value = Math.trunc(value);
265
+ this.#timeout = Number.isFinite(value) && value > 0 ? value : 0;
266
+ }
267
+
268
+ get transport() {
269
+ return this.#transport;
270
+ }
271
+
272
+ set transport(value) {
273
+ this.#transport = value instanceof Function ? value : null;
274
+ }
275
+
276
+ }
package/package.json ADDED
@@ -0,0 +1,43 @@
1
+ {
2
+ "author": "Pavel Zhukov (https://github.com/pavel-zhukov)",
3
+ "bugs": {
4
+ "url": "https://github.com/pavel-zhukov/telegram-bot-raw/issues"
5
+ },
6
+ "description": "A lightweight, low-level client for interacting with the Telegram Bot API",
7
+ "engines": {
8
+ "node": ">=18"
9
+ },
10
+ "exports": {
11
+ ".": {
12
+ "import": "./index.js",
13
+ "types": "./index.d.ts"
14
+ }
15
+ },
16
+ "files": [
17
+ "index.d.ts",
18
+ "index.js"
19
+ ],
20
+ "homepage": "https://github.com/pavel-zhukov/telegram-bot-raw#readme",
21
+ "keywords": [
22
+ "fetch",
23
+ "lightweight",
24
+ "low-level",
25
+ "raw",
26
+ "telegram",
27
+ "telegram api",
28
+ "telegram bot",
29
+ "telegram client",
30
+ "telegram framework",
31
+ "zero dependencies"
32
+ ],
33
+ "license": "MIT",
34
+ "main": "index.js",
35
+ "name": "telegram-bot-raw",
36
+ "repository": {
37
+ "type": "git",
38
+ "url": "git+https://github.com/pavel-zhukov/telegram-bot-raw.git"
39
+ },
40
+ "type": "module",
41
+ "types": "index.d.ts",
42
+ "version": "1.0.1"
43
+ }