telegram-bot-api-nodejs 1.0.1 → 1.0.2

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 (4) hide show
  1. package/index.d.ts +15 -13
  2. package/index.js +10 -7
  3. package/index.ts +20 -19
  4. package/package.json +3 -2
package/index.d.ts CHANGED
@@ -45,9 +45,6 @@ export interface AnswerCallbackQueryOptions {
45
45
  url?: string;
46
46
  cache_time?: number;
47
47
  }
48
- export interface GetChatOptions {
49
- chat_id: string;
50
- }
51
48
  interface SetMyCommandsOptions {
52
49
  commands: BotCommand[];
53
50
  }
@@ -63,11 +60,6 @@ interface EditMessageReplyMarkupOptions {
63
60
  message_id?: number | string;
64
61
  inline_message_id?: string;
65
62
  }
66
- export interface GetUserProfilePhotosOptions {
67
- user_id: number | string;
68
- offset?: number;
69
- limit?: number;
70
- }
71
63
  interface PassportFile {
72
64
  file_id: string;
73
65
  file_size: number;
@@ -517,10 +509,18 @@ export declare function setMessageReaction(token: string, payload: {
517
509
  reaction?: ReactionType[];
518
510
  is_big?: boolean;
519
511
  }, signal: AbortSignal): Promise<Boolean>;
520
- export declare function getUserProfilePhotos(token: string, payload: GetUserProfilePhotosOptions, signal: AbortSignal): Promise<UserProfilePhotos>;
521
- export declare function getFile(token: string, file_id: string, signal: AbortSignal): Promise<File>;
512
+ export declare function getUserProfilePhotos(token: string, payload: {
513
+ user_id: number | string;
514
+ offset?: number;
515
+ limit?: number;
516
+ }, signal: AbortSignal): Promise<UserProfilePhotos>;
517
+ export declare function getFile(token: string, payload: {
518
+ file_id: string;
519
+ }, signal: AbortSignal): Promise<File>;
522
520
  export declare function answerCallbackQuery(token: string, payload: AnswerCallbackQueryOptions, signal: AbortSignal): Promise<File>;
523
- export declare function getChat(token: string, payload: GetChatOptions, signal: AbortSignal): Promise<Chat>;
521
+ export declare function getChat(token: string, payload: {
522
+ chat_id: string;
523
+ }, signal: AbortSignal): Promise<Chat>;
524
524
  export declare function getChatMember(token: string, payload: {
525
525
  chat_id: string;
526
526
  user_id: string;
@@ -619,9 +619,11 @@ export declare function parsePollOption(v: PollOption): {
619
619
  * @see https://core.telegram.org/bots/api#callbackquery
620
620
  */
621
621
  export declare function parseCallbackQuery(v?: CallbackQuery): CallbackQuery | undefined;
622
+ export declare function parseResponse<T>(response: Response): Promise<T>;
622
623
  export declare class TelegramError extends Error {
623
- status: number;
624
- constructor(message: string, status: number);
624
+ error_code: number;
625
+ parameters: Record<string, any>;
626
+ constructor(message: string, error_code: number, parameters?: Record<string, any>);
625
627
  }
626
628
  /**
627
629
  * Helpers
package/index.js CHANGED
@@ -97,10 +97,10 @@ export function getUserProfilePhotos(token, payload, signal) {
97
97
  signal
98
98
  }).then((parseResponse));
99
99
  }
100
- export function getFile(token, file_id, signal) {
100
+ export function getFile(token, payload, signal) {
101
101
  return fetch(`https://api.telegram.org/bot${token}/getFile`, {
102
102
  method: "POST",
103
- body: JSON.stringify({ file_id }),
103
+ body: JSON.stringify(payload),
104
104
  headers,
105
105
  signal
106
106
  }).then((parseResponse));
@@ -450,7 +450,7 @@ function parseReactionCount(reaction) {
450
450
  total_count: Number(reaction.total_count),
451
451
  };
452
452
  }
453
- function parseResponse(response) {
453
+ export function parseResponse(response) {
454
454
  return response.text().then(function (text) {
455
455
  let data;
456
456
  try {
@@ -465,14 +465,17 @@ function parseResponse(response) {
465
465
  }
466
466
  const error_code = Number(data.error_code ?? 0);
467
467
  const description = String(data.description ?? "");
468
- throw new TelegramError(description, response.status);
468
+ const parameters = data.parameters || {};
469
+ throw new TelegramError(description, error_code, parameters);
469
470
  });
470
471
  }
471
472
  export class TelegramError extends Error {
472
- status;
473
- constructor(message, status) {
473
+ error_code;
474
+ parameters;
475
+ constructor(message, error_code, parameters = {}) {
474
476
  super(message);
475
- this.status = status;
477
+ this.error_code = error_code;
478
+ this.parameters = parameters;
476
479
  }
477
480
  }
478
481
  /**
package/index.ts CHANGED
@@ -217,10 +217,6 @@ export interface AnswerCallbackQueryOptions {
217
217
  cache_time?: number
218
218
  }
219
219
 
220
- export interface GetChatOptions {
221
- chat_id: string
222
- }
223
-
224
220
  interface SetMyCommandsOptions {
225
221
  commands: BotCommand[]
226
222
  }
@@ -243,12 +239,6 @@ interface EditMessageReplyMarkupOptions {
243
239
  inline_message_id?: string;
244
240
  }
245
241
 
246
- export interface GetUserProfilePhotosOptions {
247
- user_id: number | string
248
- offset?: number;
249
- limit?: number;
250
- }
251
-
252
242
  interface SetGameScoreOptions {
253
243
  force?: boolean;
254
244
  disable_edit_message?: boolean;
@@ -1158,7 +1148,11 @@ export function setMessageReaction(token: string, payload: {
1158
1148
  }).then(parseResponse<Boolean>)
1159
1149
  }
1160
1150
 
1161
- export function getUserProfilePhotos(token: string, payload: GetUserProfilePhotosOptions, signal: AbortSignal) {
1151
+ export function getUserProfilePhotos(token: string, payload: {
1152
+ user_id: number | string
1153
+ offset?: number;
1154
+ limit?: number;
1155
+ }, signal: AbortSignal) {
1162
1156
  return fetch(`https://api.telegram.org/bot${token}/getUserProfilePhotos`, {
1163
1157
  method: "POST",
1164
1158
  body: JSON.stringify(payload),
@@ -1167,10 +1161,12 @@ export function getUserProfilePhotos(token: string, payload: GetUserProfilePhoto
1167
1161
  }).then(parseResponse<UserProfilePhotos>)
1168
1162
  }
1169
1163
 
1170
- export function getFile(token: string, file_id: string, signal: AbortSignal) {
1164
+ export function getFile(token: string, payload: {
1165
+ file_id: string
1166
+ }, signal: AbortSignal) {
1171
1167
  return fetch(`https://api.telegram.org/bot${token}/getFile`, {
1172
1168
  method: "POST",
1173
- body: JSON.stringify({ file_id }),
1169
+ body: JSON.stringify(payload),
1174
1170
  headers,
1175
1171
  signal
1176
1172
  }).then(parseResponse<File>)
@@ -1185,7 +1181,9 @@ export function answerCallbackQuery(token: string, payload: AnswerCallbackQueryO
1185
1181
  }).then(parseResponse<File>)
1186
1182
  }
1187
1183
 
1188
- export function getChat(token: string, payload: GetChatOptions, signal: AbortSignal) {
1184
+ export function getChat(token: string, payload: {
1185
+ chat_id: string
1186
+ }, signal: AbortSignal) {
1189
1187
  return fetch(`https://api.telegram.org/bot${token}/getChat`, {
1190
1188
  method: "POST",
1191
1189
  body: JSON.stringify(payload),
@@ -1569,7 +1567,7 @@ function parseReactionCount(reaction: ReactionCount): ReactionCount {
1569
1567
  }
1570
1568
  }
1571
1569
 
1572
- function parseResponse<T>(response: Response) {
1570
+ export function parseResponse<T>(response: Response) {
1573
1571
  return response.text().then(function (text) {
1574
1572
  let data: any
1575
1573
 
@@ -1588,18 +1586,21 @@ function parseResponse<T>(response: Response) {
1588
1586
 
1589
1587
  const error_code = Number(data.error_code ?? 0)
1590
1588
  const description = String(data.description ?? "")
1589
+ const parameters = data.parameters || {}
1591
1590
 
1592
- throw new TelegramError(description, response.status)
1591
+ throw new TelegramError(description, error_code, parameters)
1593
1592
  })
1594
1593
  }
1595
1594
 
1596
1595
  export class TelegramError extends Error {
1597
- status: number
1596
+ error_code: number
1597
+ parameters: Record<string, any>
1598
1598
 
1599
- constructor(message: string, status: number) {
1599
+ constructor(message: string, error_code: number, parameters: Record<string, any> = {}) {
1600
1600
  super(message)
1601
1601
 
1602
- this.status = status
1602
+ this.error_code = error_code
1603
+ this.parameters = parameters
1603
1604
  }
1604
1605
  }
1605
1606
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "telegram-bot-api-nodejs",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "Telegram Bot API client for nodejs",
5
5
  "type": "module",
6
6
  "module": "index.js",
@@ -8,7 +8,8 @@
8
8
  "scripts": {
9
9
  "prepublishOnly": "npm run build",
10
10
  "build": "tsc",
11
- "test": "echo \"Error: no test specified\" && exit 1"
11
+ "pretest": "npm run build",
12
+ "test": "node --test"
12
13
  },
13
14
  "author": "",
14
15
  "license": "ISC",