@zero-bot.net/tg-bot-api 1.1.0 → 1.2.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/src/telegram.js CHANGED
@@ -61,7 +61,53 @@ const _messageTypes = [
61
61
  'chat_invite_link',
62
62
  'chat_member_updated',
63
63
  'web_app_data',
64
- 'message_reaction'
64
+ 'message_reaction',
65
+ // Bot API 7.7
66
+ 'refunded_payment',
67
+ // Bot API 9.0
68
+ 'gift',
69
+ 'unique_gift',
70
+ 'paid_message_price_changed',
71
+ 'paid_star_count',
72
+ // Bot API 9.1
73
+ 'checklist',
74
+ 'checklist_tasks_done',
75
+ 'checklist_tasks_added',
76
+ 'direct_message_price_changed',
77
+ // Bot API 9.2
78
+ 'direct_messages_topic',
79
+ 'suggested_post_info',
80
+ 'suggested_post_approved',
81
+ 'suggested_post_approval_failed',
82
+ 'suggested_post_declined',
83
+ 'suggested_post_paid',
84
+ 'suggested_post_refunded',
85
+ // Bot API 9.3
86
+ 'gift_upgrade_sent',
87
+ // Bot API 9.4
88
+ 'chat_owner_left',
89
+ 'chat_owner_changed',
90
+ // Bot API 9.5
91
+ 'sender_tag',
92
+ // Bot API 9.6
93
+ 'managed_bot_created',
94
+ 'poll_option_added',
95
+ 'poll_option_deleted',
96
+ // Bot API 10.0
97
+ 'guest_bot_caller_user',
98
+ 'guest_bot_caller_chat',
99
+ 'guest_query_id',
100
+ 'live_photo',
101
+ // Bot API 10.1
102
+ 'rich_message',
103
+ // Bot API 10.2
104
+ 'ephemeral_message_id',
105
+ 'receiver_user',
106
+ 'community_chat_added',
107
+ 'community_chat_removed',
108
+ // Bot API 10.3
109
+ 'stopped_message_generation',
110
+ 'community_chat_joined',
65
111
  ];
66
112
 
67
113
  const _deprecatedMessageTypes = [
@@ -274,6 +320,34 @@ class TelegramBot extends EventEmitter {
274
320
  }
275
321
  }
276
322
 
323
+ /**
324
+ * Fix JSON-serialized object fields by making them JSON strings if they are still objects.
325
+ * Covers new Bot API 7.4-10.3 fields that accept JSON-serialized objects.
326
+ * @param {Object} obj Object; either 'form' or 'qs'
327
+ * @private
328
+ */
329
+ _fixJsonFields(obj) {
330
+ const jsonFields = [
331
+ 'rich_message',
332
+ 'content',
333
+ 'result',
334
+ 'button',
335
+ 'web_app',
336
+ 'photo',
337
+ 'tasks',
338
+ 'reaction_type',
339
+ 'restricted_channels',
340
+ 'target_business_connection_ids',
341
+ 'accepted_gift_types',
342
+ 'link_preview_options',
343
+ ];
344
+ for (const field of jsonFields) {
345
+ if (obj.hasOwnProperty(field) && typeof obj[field] !== 'string') {
346
+ obj[field] = stringify(obj[field]);
347
+ }
348
+ }
349
+ }
350
+
277
351
  /**
278
352
  * Make request against the API
279
353
  * @param {String} _path API endpoint
@@ -294,10 +368,12 @@ class TelegramBot extends EventEmitter {
294
368
  this._fixReplyMarkup(options.form);
295
369
  this._fixEntitiesField(options.form);
296
370
  this._fixReplyParameters(options.form);
371
+ this._fixJsonFields(options.form);
297
372
  }
298
373
  if (options.qs) {
299
374
  this._fixReplyMarkup(options.qs);
300
375
  this._fixReplyParameters(options.qs);
376
+ this._fixJsonFields(options.qs);
301
377
  }
302
378
 
303
379
  options.method = 'POST';
@@ -3618,6 +3694,8 @@ class TelegramBot extends EventEmitter {
3618
3694
  * @see https://core.telegram.org/bots/api#sendchecklist
3619
3695
  */
3620
3696
  sendChecklist(businessConnectionId, title, tasks, form = {}) {
3697
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3698
+ if (!title) return Promise.reject(new Error('title is required'));
3621
3699
  form.business_connection_id = businessConnectionId;
3622
3700
  form.title = title;
3623
3701
  form.tasks = stringify(tasks);
@@ -3634,6 +3712,8 @@ class TelegramBot extends EventEmitter {
3634
3712
  * @see https://core.telegram.org/bots/api#editmessagechecklist
3635
3713
  */
3636
3714
  editMessageChecklist(businessConnectionId, messageId, form = {}) {
3715
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3716
+ if (!messageId) return Promise.reject(new Error('messageId is required'));
3637
3717
  form.business_connection_id = businessConnectionId;
3638
3718
  form.message_id = messageId;
3639
3719
  if (form.tasks) {
@@ -3667,6 +3747,8 @@ class TelegramBot extends EventEmitter {
3667
3747
  * @see https://core.telegram.org/bots/api#approvesuggestedpost
3668
3748
  */
3669
3749
  approveSuggestedPost(businessConnectionId, messageId, form = {}) {
3750
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3751
+ if (!messageId) return Promise.reject(new Error('messageId is required'));
3670
3752
  form.business_connection_id = businessConnectionId;
3671
3753
  form.message_id = messageId;
3672
3754
  return this._request('approveSuggestedPost', { form });
@@ -3682,6 +3764,8 @@ class TelegramBot extends EventEmitter {
3682
3764
  * @see https://core.telegram.org/bots/api#declinesuggestedpost
3683
3765
  */
3684
3766
  declineSuggestedPost(businessConnectionId, messageId, form = {}) {
3767
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3768
+ if (!messageId) return Promise.reject(new Error('messageId is required'));
3685
3769
  form.business_connection_id = businessConnectionId;
3686
3770
  form.message_id = messageId;
3687
3771
  return this._request('declineSuggestedPost', { form });
@@ -3701,6 +3785,8 @@ class TelegramBot extends EventEmitter {
3701
3785
  * @see https://core.telegram.org/bots/api#sendmessagedraft
3702
3786
  */
3703
3787
  sendMessageDraft(chatId, text, form = {}) {
3788
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
3789
+ if (!text) return Promise.reject(new Error('text is required'));
3704
3790
  form.chat_id = chatId;
3705
3791
  form.text = text;
3706
3792
  if (form.entities) {
@@ -3749,6 +3835,8 @@ class TelegramBot extends EventEmitter {
3749
3835
  * @see https://core.telegram.org/bots/api#repoststory
3750
3836
  */
3751
3837
  repostStory(businessConnectionId, storyId, targetBusinessConnectionIds, form = {}) {
3838
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3839
+ if (!storyId) return Promise.reject(new Error('storyId is required'));
3752
3840
  form.business_connection_id = businessConnectionId;
3753
3841
  form.story_id = storyId;
3754
3842
  form.target_business_connection_ids = stringify(targetBusinessConnectionIds);
@@ -3872,6 +3960,8 @@ class TelegramBot extends EventEmitter {
3872
3960
  * @see https://core.telegram.org/bots/api#answerguestquery
3873
3961
  */
3874
3962
  answerGuestQuery(guestQueryId, text, form = {}) {
3963
+ if (!guestQueryId) return Promise.reject(new Error('guestQueryId is required'));
3964
+ if (!text) return Promise.reject(new Error('text is required'));
3875
3965
  form.guest_query_id = guestQueryId;
3876
3966
  form.text = text;
3877
3967
  return this._request('answerGuestQuery', { form });
@@ -3922,6 +4012,7 @@ class TelegramBot extends EventEmitter {
3922
4012
  * @see https://core.telegram.org/bots/api#sendlivephoto
3923
4013
  */
3924
4014
  sendLivePhoto(chatId, photo, video, form = {}, fileOptions = {}) {
4015
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
3925
4016
  const opts = {
3926
4017
  qs: form,
3927
4018
  };
@@ -3990,6 +4081,8 @@ class TelegramBot extends EventEmitter {
3990
4081
  * @see https://core.telegram.org/bots/api#sendrichmessage
3991
4082
  */
3992
4083
  sendRichMessage(chatId, content, form = {}) {
4084
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4085
+ if (!content) return Promise.reject(new Error('content is required'));
3993
4086
  form.chat_id = chatId;
3994
4087
  form.content = stringify(content);
3995
4088
  return this._request('sendRichMessage', { form });
@@ -4005,6 +4098,8 @@ class TelegramBot extends EventEmitter {
4005
4098
  * @see https://core.telegram.org/bots/api#sendrichmessagedraft
4006
4099
  */
4007
4100
  sendRichMessageDraft(chatId, content, form = {}) {
4101
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4102
+ if (!content) return Promise.reject(new Error('content is required'));
4008
4103
  form.chat_id = chatId;
4009
4104
  form.content = stringify(content);
4010
4105
  return this._request('sendRichMessageDraft', { form });
@@ -4020,6 +4115,8 @@ class TelegramBot extends EventEmitter {
4020
4115
  * @see https://core.telegram.org/bots/api#answerchatjoinrequestquery
4021
4116
  */
4022
4117
  answerChatJoinRequestQuery(chatJoinRequestId, queryId, form = {}) {
4118
+ if (!chatJoinRequestId) return Promise.reject(new Error('chatJoinRequestId is required'));
4119
+ if (!queryId) return Promise.reject(new Error('queryId is required'));
4023
4120
  form.chat_join_request_id = chatJoinRequestId;
4024
4121
  form.query_id = queryId;
4025
4122
  return this._request('answerChatJoinRequestQuery', { form });
@@ -4035,6 +4132,8 @@ class TelegramBot extends EventEmitter {
4035
4132
  * @see https://core.telegram.org/bots/api#sendchatjoinrequestwebapp
4036
4133
  */
4037
4134
  sendChatJoinRequestWebApp(chatJoinRequestId, webApp, form = {}) {
4135
+ if (!chatJoinRequestId) return Promise.reject(new Error('chatJoinRequestId is required'));
4136
+ if (!webApp) return Promise.reject(new Error('webApp is required'));
4038
4137
  form.chat_join_request_id = chatJoinRequestId;
4039
4138
  form.web_app = stringify(webApp);
4040
4139
  return this._request('sendChatJoinRequestWebApp', { form });
@@ -4055,6 +4154,8 @@ class TelegramBot extends EventEmitter {
4055
4154
  * @see https://core.telegram.org/bots/api#editephemeralmessagetext
4056
4155
  */
4057
4156
  editEphemeralMessageText(chatId, ephemeralMessageId, text, form = {}) {
4157
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4158
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4058
4159
  form.chat_id = chatId;
4059
4160
  form.ephemeral_message_id = ephemeralMessageId;
4060
4161
  form.text = text;
@@ -4073,6 +4174,8 @@ class TelegramBot extends EventEmitter {
4073
4174
  * @see https://core.telegram.org/bots/api#editephemeralmessagemedia
4074
4175
  */
4075
4176
  editEphemeralMessageMedia(chatId, ephemeralMessageId, media, form = {}, fileOptions = {}) {
4177
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4178
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4076
4179
  form.chat_id = chatId;
4077
4180
  form.ephemeral_message_id = ephemeralMessageId;
4078
4181
  form.media = stringify(media);
@@ -4089,6 +4192,8 @@ class TelegramBot extends EventEmitter {
4089
4192
  * @see https://core.telegram.org/bots/api#editephemeralmessagecaption
4090
4193
  */
4091
4194
  editEphemeralMessageCaption(chatId, ephemeralMessageId, form = {}) {
4195
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4196
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4092
4197
  form.chat_id = chatId;
4093
4198
  form.ephemeral_message_id = ephemeralMessageId;
4094
4199
  this._fixEntitiesField(form);
@@ -4105,6 +4210,8 @@ class TelegramBot extends EventEmitter {
4105
4210
  * @see https://core.telegram.org/bots/api#editephemeralmessagereplymarkup
4106
4211
  */
4107
4212
  editEphemeralMessageReplyMarkup(chatId, ephemeralMessageId, form = {}) {
4213
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4214
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4108
4215
  form.chat_id = chatId;
4109
4216
  form.ephemeral_message_id = ephemeralMessageId;
4110
4217
  return this._request('editEphemeralMessageReplyMarkup', { form });
@@ -4120,6 +4227,8 @@ class TelegramBot extends EventEmitter {
4120
4227
  * @see https://core.telegram.org/bots/api#deleteephemeralmessage
4121
4228
  */
4122
4229
  deleteEphemeralMessage(chatId, ephemeralMessageId, form = {}) {
4230
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4231
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4123
4232
  form.chat_id = chatId;
4124
4233
  form.ephemeral_message_id = ephemeralMessageId;
4125
4234
  return this._request('deleteEphemeralMessage', { form });