@zero-bot.net/tg-bot-api 1.1.0 → 1.3.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,69 @@ 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',
111
+ // Bot API 8.3+
112
+ 'giveaway_created',
113
+ 'giveaway',
114
+ 'giveaway_winners',
115
+ 'giveaway_completed',
116
+ 'chat_boost_added',
117
+ 'story',
118
+ 'chat_background_set',
119
+ 'forum_topic_created',
120
+ 'forum_topic_closed',
121
+ 'forum_topic_reopened',
122
+ 'forum_topic_edited',
123
+ 'general_forum_topic_hidden',
124
+ 'general_forum_topic_unhidden',
125
+ 'write_access_allowed',
126
+ 'boost',
65
127
  ];
66
128
 
67
129
  const _deprecatedMessageTypes = [
@@ -274,6 +336,34 @@ class TelegramBot extends EventEmitter {
274
336
  }
275
337
  }
276
338
 
339
+ /**
340
+ * Fix JSON-serialized object fields by making them JSON strings if they are still objects.
341
+ * Covers new Bot API 7.4-10.3 fields that accept JSON-serialized objects.
342
+ * @param {Object} obj Object; either 'form' or 'qs'
343
+ * @private
344
+ */
345
+ _fixJsonFields(obj) {
346
+ const jsonFields = [
347
+ 'rich_message',
348
+ 'content',
349
+ 'result',
350
+ 'button',
351
+ 'web_app',
352
+ 'photo',
353
+ 'tasks',
354
+ 'reaction_type',
355
+ 'restricted_channels',
356
+ 'target_business_connection_ids',
357
+ 'accepted_gift_types',
358
+ 'link_preview_options',
359
+ ];
360
+ for (const field of jsonFields) {
361
+ if (obj.hasOwnProperty(field) && typeof obj[field] !== 'string') {
362
+ obj[field] = stringify(obj[field]);
363
+ }
364
+ }
365
+ }
366
+
277
367
  /**
278
368
  * Make request against the API
279
369
  * @param {String} _path API endpoint
@@ -294,10 +384,12 @@ class TelegramBot extends EventEmitter {
294
384
  this._fixReplyMarkup(options.form);
295
385
  this._fixEntitiesField(options.form);
296
386
  this._fixReplyParameters(options.form);
387
+ this._fixJsonFields(options.form);
297
388
  }
298
389
  if (options.qs) {
299
390
  this._fixReplyMarkup(options.qs);
300
391
  this._fixReplyParameters(options.qs);
392
+ this._fixJsonFields(options.qs);
301
393
  }
302
394
 
303
395
  options.method = 'POST';
@@ -824,6 +916,34 @@ class TelegramBot extends EventEmitter {
824
916
  debug('Process Update removed_chat_boost %j', removedChatBoost);
825
917
  this.emit('removed_chat_boost', removedChatBoost);
826
918
  }
919
+
920
+ // Update-level types not tied to message content
921
+ const purchasedPaidMedia = update.purchased_paid_media;
922
+ const subscription = update.subscription;
923
+ const managedBot = update.managed_bot;
924
+ const guestMessage = update.guest_message;
925
+ const stoppedMessageGeneration = update.stopped_message_generation;
926
+ const reaction = update.reaction;
927
+
928
+ if (purchasedPaidMedia) {
929
+ debug('Process Update purchased_paid_media %j', purchasedPaidMedia);
930
+ this.emit('purchased_paid_media', purchasedPaidMedia);
931
+ } else if (subscription) {
932
+ debug('Process Update subscription %j', subscription);
933
+ this.emit('subscription', subscription);
934
+ } else if (managedBot) {
935
+ debug('Process Update managed_bot %j', managedBot);
936
+ this.emit('managed_bot', managedBot);
937
+ } else if (guestMessage) {
938
+ debug('Process Update guest_message %j', guestMessage);
939
+ this.emit('guest_message', guestMessage);
940
+ } else if (stoppedMessageGeneration) {
941
+ debug('Process Update stopped_message_generation %j', stoppedMessageGeneration);
942
+ this.emit('stopped_message_generation', stoppedMessageGeneration);
943
+ } else if (reaction) {
944
+ debug('Process Update reaction %j', reaction);
945
+ this.emit('reaction', reaction);
946
+ }
827
947
  }
828
948
 
829
949
  /** Start Telegram Bot API methods */
@@ -3227,7 +3347,14 @@ class TelegramBot extends EventEmitter {
3227
3347
  * @see https://core.telegram.org/bots/api#sendgift
3228
3348
  */
3229
3349
  sendGift(userId, giftId, form = {}) {
3230
- form.user_id = userId;
3350
+ if (typeof userId === 'number' || typeof userId === 'string') {
3351
+ // Check if it looks like a chat ID (negative numbers or @channel)
3352
+ if (String(userId).charAt(0) === '-' || String(userId).charAt(0) === '@') {
3353
+ form.chat_id = userId;
3354
+ } else {
3355
+ form.user_id = userId;
3356
+ }
3357
+ }
3231
3358
  form.gift_id = giftId;
3232
3359
  return this._request('sendGift', { form });
3233
3360
  }
@@ -3618,6 +3745,8 @@ class TelegramBot extends EventEmitter {
3618
3745
  * @see https://core.telegram.org/bots/api#sendchecklist
3619
3746
  */
3620
3747
  sendChecklist(businessConnectionId, title, tasks, form = {}) {
3748
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3749
+ if (!title) return Promise.reject(new Error('title is required'));
3621
3750
  form.business_connection_id = businessConnectionId;
3622
3751
  form.title = title;
3623
3752
  form.tasks = stringify(tasks);
@@ -3634,6 +3763,8 @@ class TelegramBot extends EventEmitter {
3634
3763
  * @see https://core.telegram.org/bots/api#editmessagechecklist
3635
3764
  */
3636
3765
  editMessageChecklist(businessConnectionId, messageId, form = {}) {
3766
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3767
+ if (!messageId) return Promise.reject(new Error('messageId is required'));
3637
3768
  form.business_connection_id = businessConnectionId;
3638
3769
  form.message_id = messageId;
3639
3770
  if (form.tasks) {
@@ -3667,6 +3798,8 @@ class TelegramBot extends EventEmitter {
3667
3798
  * @see https://core.telegram.org/bots/api#approvesuggestedpost
3668
3799
  */
3669
3800
  approveSuggestedPost(businessConnectionId, messageId, form = {}) {
3801
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3802
+ if (!messageId) return Promise.reject(new Error('messageId is required'));
3670
3803
  form.business_connection_id = businessConnectionId;
3671
3804
  form.message_id = messageId;
3672
3805
  return this._request('approveSuggestedPost', { form });
@@ -3682,6 +3815,8 @@ class TelegramBot extends EventEmitter {
3682
3815
  * @see https://core.telegram.org/bots/api#declinesuggestedpost
3683
3816
  */
3684
3817
  declineSuggestedPost(businessConnectionId, messageId, form = {}) {
3818
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3819
+ if (!messageId) return Promise.reject(new Error('messageId is required'));
3685
3820
  form.business_connection_id = businessConnectionId;
3686
3821
  form.message_id = messageId;
3687
3822
  return this._request('declineSuggestedPost', { form });
@@ -3701,6 +3836,8 @@ class TelegramBot extends EventEmitter {
3701
3836
  * @see https://core.telegram.org/bots/api#sendmessagedraft
3702
3837
  */
3703
3838
  sendMessageDraft(chatId, text, form = {}) {
3839
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
3840
+ if (!text) return Promise.reject(new Error('text is required'));
3704
3841
  form.chat_id = chatId;
3705
3842
  form.text = text;
3706
3843
  if (form.entities) {
@@ -3749,6 +3886,8 @@ class TelegramBot extends EventEmitter {
3749
3886
  * @see https://core.telegram.org/bots/api#repoststory
3750
3887
  */
3751
3888
  repostStory(businessConnectionId, storyId, targetBusinessConnectionIds, form = {}) {
3889
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3890
+ if (!storyId) return Promise.reject(new Error('storyId is required'));
3752
3891
  form.business_connection_id = businessConnectionId;
3753
3892
  form.story_id = storyId;
3754
3893
  form.target_business_connection_ids = stringify(targetBusinessConnectionIds);
@@ -3872,6 +4011,8 @@ class TelegramBot extends EventEmitter {
3872
4011
  * @see https://core.telegram.org/bots/api#answerguestquery
3873
4012
  */
3874
4013
  answerGuestQuery(guestQueryId, text, form = {}) {
4014
+ if (!guestQueryId) return Promise.reject(new Error('guestQueryId is required'));
4015
+ if (!text) return Promise.reject(new Error('text is required'));
3875
4016
  form.guest_query_id = guestQueryId;
3876
4017
  form.text = text;
3877
4018
  return this._request('answerGuestQuery', { form });
@@ -3922,6 +4063,7 @@ class TelegramBot extends EventEmitter {
3922
4063
  * @see https://core.telegram.org/bots/api#sendlivephoto
3923
4064
  */
3924
4065
  sendLivePhoto(chatId, photo, video, form = {}, fileOptions = {}) {
4066
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
3925
4067
  const opts = {
3926
4068
  qs: form,
3927
4069
  };
@@ -3990,6 +4132,8 @@ class TelegramBot extends EventEmitter {
3990
4132
  * @see https://core.telegram.org/bots/api#sendrichmessage
3991
4133
  */
3992
4134
  sendRichMessage(chatId, content, form = {}) {
4135
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4136
+ if (!content) return Promise.reject(new Error('content is required'));
3993
4137
  form.chat_id = chatId;
3994
4138
  form.content = stringify(content);
3995
4139
  return this._request('sendRichMessage', { form });
@@ -4005,6 +4149,8 @@ class TelegramBot extends EventEmitter {
4005
4149
  * @see https://core.telegram.org/bots/api#sendrichmessagedraft
4006
4150
  */
4007
4151
  sendRichMessageDraft(chatId, content, form = {}) {
4152
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4153
+ if (!content) return Promise.reject(new Error('content is required'));
4008
4154
  form.chat_id = chatId;
4009
4155
  form.content = stringify(content);
4010
4156
  return this._request('sendRichMessageDraft', { form });
@@ -4020,6 +4166,8 @@ class TelegramBot extends EventEmitter {
4020
4166
  * @see https://core.telegram.org/bots/api#answerchatjoinrequestquery
4021
4167
  */
4022
4168
  answerChatJoinRequestQuery(chatJoinRequestId, queryId, form = {}) {
4169
+ if (!chatJoinRequestId) return Promise.reject(new Error('chatJoinRequestId is required'));
4170
+ if (!queryId) return Promise.reject(new Error('queryId is required'));
4023
4171
  form.chat_join_request_id = chatJoinRequestId;
4024
4172
  form.query_id = queryId;
4025
4173
  return this._request('answerChatJoinRequestQuery', { form });
@@ -4035,6 +4183,8 @@ class TelegramBot extends EventEmitter {
4035
4183
  * @see https://core.telegram.org/bots/api#sendchatjoinrequestwebapp
4036
4184
  */
4037
4185
  sendChatJoinRequestWebApp(chatJoinRequestId, webApp, form = {}) {
4186
+ if (!chatJoinRequestId) return Promise.reject(new Error('chatJoinRequestId is required'));
4187
+ if (!webApp) return Promise.reject(new Error('webApp is required'));
4038
4188
  form.chat_join_request_id = chatJoinRequestId;
4039
4189
  form.web_app = stringify(webApp);
4040
4190
  return this._request('sendChatJoinRequestWebApp', { form });
@@ -4055,6 +4205,8 @@ class TelegramBot extends EventEmitter {
4055
4205
  * @see https://core.telegram.org/bots/api#editephemeralmessagetext
4056
4206
  */
4057
4207
  editEphemeralMessageText(chatId, ephemeralMessageId, text, form = {}) {
4208
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4209
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4058
4210
  form.chat_id = chatId;
4059
4211
  form.ephemeral_message_id = ephemeralMessageId;
4060
4212
  form.text = text;
@@ -4073,6 +4225,8 @@ class TelegramBot extends EventEmitter {
4073
4225
  * @see https://core.telegram.org/bots/api#editephemeralmessagemedia
4074
4226
  */
4075
4227
  editEphemeralMessageMedia(chatId, ephemeralMessageId, media, form = {}, fileOptions = {}) {
4228
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4229
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4076
4230
  form.chat_id = chatId;
4077
4231
  form.ephemeral_message_id = ephemeralMessageId;
4078
4232
  form.media = stringify(media);
@@ -4089,6 +4243,8 @@ class TelegramBot extends EventEmitter {
4089
4243
  * @see https://core.telegram.org/bots/api#editephemeralmessagecaption
4090
4244
  */
4091
4245
  editEphemeralMessageCaption(chatId, ephemeralMessageId, form = {}) {
4246
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4247
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4092
4248
  form.chat_id = chatId;
4093
4249
  form.ephemeral_message_id = ephemeralMessageId;
4094
4250
  this._fixEntitiesField(form);
@@ -4105,6 +4261,8 @@ class TelegramBot extends EventEmitter {
4105
4261
  * @see https://core.telegram.org/bots/api#editephemeralmessagereplymarkup
4106
4262
  */
4107
4263
  editEphemeralMessageReplyMarkup(chatId, ephemeralMessageId, form = {}) {
4264
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4265
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4108
4266
  form.chat_id = chatId;
4109
4267
  form.ephemeral_message_id = ephemeralMessageId;
4110
4268
  return this._request('editEphemeralMessageReplyMarkup', { form });
@@ -4120,6 +4278,8 @@ class TelegramBot extends EventEmitter {
4120
4278
  * @see https://core.telegram.org/bots/api#deleteephemeralmessage
4121
4279
  */
4122
4280
  deleteEphemeralMessage(chatId, ephemeralMessageId, form = {}) {
4281
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4282
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4123
4283
  form.chat_id = chatId;
4124
4284
  form.ephemeral_message_id = ephemeralMessageId;
4125
4285
  return this._request('deleteEphemeralMessage', { form });