@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/lib/telegram.js CHANGED
@@ -23,7 +23,33 @@ const fs = require('fs');
23
23
  const pump = require('pump');
24
24
  const deprecate = require('./utils').deprecate;
25
25
 
26
- const _messageTypes = ['text', 'animation', 'audio', 'channel_chat_created', 'contact', 'delete_chat_photo', 'dice', 'document', 'game', 'group_chat_created', 'invoice', 'left_chat_member', 'location', 'migrate_from_chat_id', 'migrate_to_chat_id', 'new_chat_members', 'new_chat_photo', 'new_chat_title', 'passport_data', 'photo', 'pinned_message', 'poll', 'sticker', 'successful_payment', 'supergroup_chat_created', 'video', 'video_note', 'voice', 'video_chat_started', 'video_chat_ended', 'video_chat_participants_invited', 'video_chat_scheduled', 'message_auto_delete_timer_changed', 'chat_invite_link', 'chat_member_updated', 'web_app_data', 'message_reaction'];
26
+ const _messageTypes = ['text', 'animation', 'audio', 'channel_chat_created', 'contact', 'delete_chat_photo', 'dice', 'document', 'game', 'group_chat_created', 'invoice', 'left_chat_member', 'location', 'migrate_from_chat_id', 'migrate_to_chat_id', 'new_chat_members', 'new_chat_photo', 'new_chat_title', 'passport_data', 'photo', 'pinned_message', 'poll', 'sticker', 'successful_payment', 'supergroup_chat_created', 'video', 'video_note', 'voice', 'video_chat_started', 'video_chat_ended', 'video_chat_participants_invited', 'video_chat_scheduled', 'message_auto_delete_timer_changed', 'chat_invite_link', 'chat_member_updated', 'web_app_data', 'message_reaction',
27
+ // Bot API 7.7
28
+ 'refunded_payment',
29
+ // Bot API 9.0
30
+ 'gift', 'unique_gift', 'paid_message_price_changed', 'paid_star_count',
31
+ // Bot API 9.1
32
+ 'checklist', 'checklist_tasks_done', 'checklist_tasks_added', 'direct_message_price_changed',
33
+ // Bot API 9.2
34
+ 'direct_messages_topic', 'suggested_post_info', 'suggested_post_approved', 'suggested_post_approval_failed', 'suggested_post_declined', 'suggested_post_paid', 'suggested_post_refunded',
35
+ // Bot API 9.3
36
+ 'gift_upgrade_sent',
37
+ // Bot API 9.4
38
+ 'chat_owner_left', 'chat_owner_changed',
39
+ // Bot API 9.5
40
+ 'sender_tag',
41
+ // Bot API 9.6
42
+ 'managed_bot_created', 'poll_option_added', 'poll_option_deleted',
43
+ // Bot API 10.0
44
+ 'guest_bot_caller_user', 'guest_bot_caller_chat', 'guest_query_id', 'live_photo',
45
+ // Bot API 10.1
46
+ 'rich_message',
47
+ // Bot API 10.2
48
+ 'ephemeral_message_id', 'receiver_user', 'community_chat_added', 'community_chat_removed',
49
+ // Bot API 10.3
50
+ 'stopped_message_generation', 'community_chat_joined',
51
+ // Bot API 8.3+
52
+ 'giveaway_created', 'giveaway', 'giveaway_winners', 'giveaway_completed', 'chat_boost_added', 'story', 'chat_background_set', 'forum_topic_created', 'forum_topic_closed', 'forum_topic_reopened', 'forum_topic_edited', 'general_forum_topic_hidden', 'general_forum_topic_unhidden', 'write_access_allowed', 'boost'];
27
53
 
28
54
  const _deprecatedMessageTypes = ['new_chat_participant', 'left_chat_participant'];
29
55
 
@@ -232,6 +258,21 @@ class TelegramBot extends EventEmitter {
232
258
  }
233
259
  }
234
260
 
261
+ /**
262
+ * Fix JSON-serialized object fields by making them JSON strings if they are still objects.
263
+ * Covers new Bot API 7.4-10.3 fields that accept JSON-serialized objects.
264
+ * @param {Object} obj Object; either 'form' or 'qs'
265
+ * @private
266
+ */
267
+ _fixJsonFields(obj) {
268
+ const jsonFields = ['rich_message', 'content', 'result', 'button', 'web_app', 'photo', 'tasks', 'reaction_type', 'restricted_channels', 'target_business_connection_ids', 'accepted_gift_types', 'link_preview_options'];
269
+ for (const field of jsonFields) {
270
+ if (obj.hasOwnProperty(field) && typeof obj[field] !== 'string') {
271
+ obj[field] = stringify(obj[field]);
272
+ }
273
+ }
274
+ }
275
+
235
276
  /**
236
277
  * Make request against the API
237
278
  * @param {String} _path API endpoint
@@ -252,10 +293,12 @@ class TelegramBot extends EventEmitter {
252
293
  this._fixReplyMarkup(options.form);
253
294
  this._fixEntitiesField(options.form);
254
295
  this._fixReplyParameters(options.form);
296
+ this._fixJsonFields(options.form);
255
297
  }
256
298
  if (options.qs) {
257
299
  this._fixReplyMarkup(options.qs);
258
300
  this._fixReplyParameters(options.qs);
301
+ this._fixJsonFields(options.qs);
259
302
  }
260
303
 
261
304
  options.method = 'POST';
@@ -775,6 +818,34 @@ class TelegramBot extends EventEmitter {
775
818
  debug('Process Update removed_chat_boost %j', removedChatBoost);
776
819
  this.emit('removed_chat_boost', removedChatBoost);
777
820
  }
821
+
822
+ // Update-level types not tied to message content
823
+ const purchasedPaidMedia = update.purchased_paid_media;
824
+ const subscription = update.subscription;
825
+ const managedBot = update.managed_bot;
826
+ const guestMessage = update.guest_message;
827
+ const stoppedMessageGeneration = update.stopped_message_generation;
828
+ const reaction = update.reaction;
829
+
830
+ if (purchasedPaidMedia) {
831
+ debug('Process Update purchased_paid_media %j', purchasedPaidMedia);
832
+ this.emit('purchased_paid_media', purchasedPaidMedia);
833
+ } else if (subscription) {
834
+ debug('Process Update subscription %j', subscription);
835
+ this.emit('subscription', subscription);
836
+ } else if (managedBot) {
837
+ debug('Process Update managed_bot %j', managedBot);
838
+ this.emit('managed_bot', managedBot);
839
+ } else if (guestMessage) {
840
+ debug('Process Update guest_message %j', guestMessage);
841
+ this.emit('guest_message', guestMessage);
842
+ } else if (stoppedMessageGeneration) {
843
+ debug('Process Update stopped_message_generation %j', stoppedMessageGeneration);
844
+ this.emit('stopped_message_generation', stoppedMessageGeneration);
845
+ } else if (reaction) {
846
+ debug('Process Update reaction %j', reaction);
847
+ this.emit('reaction', reaction);
848
+ }
778
849
  }
779
850
 
780
851
  /** Start Telegram Bot API methods */
@@ -3165,7 +3236,14 @@ class TelegramBot extends EventEmitter {
3165
3236
  * @see https://core.telegram.org/bots/api#sendgift
3166
3237
  */
3167
3238
  sendGift(userId, giftId, form = {}) {
3168
- form.user_id = userId;
3239
+ if (typeof userId === 'number' || typeof userId === 'string') {
3240
+ // Check if it looks like a chat ID (negative numbers or @channel)
3241
+ if (String(userId).charAt(0) === '-' || String(userId).charAt(0) === '@') {
3242
+ form.chat_id = userId;
3243
+ } else {
3244
+ form.user_id = userId;
3245
+ }
3246
+ }
3169
3247
  form.gift_id = giftId;
3170
3248
  return this._request('sendGift', { form });
3171
3249
  }
@@ -3556,6 +3634,8 @@ class TelegramBot extends EventEmitter {
3556
3634
  * @see https://core.telegram.org/bots/api#sendchecklist
3557
3635
  */
3558
3636
  sendChecklist(businessConnectionId, title, tasks, form = {}) {
3637
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3638
+ if (!title) return Promise.reject(new Error('title is required'));
3559
3639
  form.business_connection_id = businessConnectionId;
3560
3640
  form.title = title;
3561
3641
  form.tasks = stringify(tasks);
@@ -3572,6 +3652,8 @@ class TelegramBot extends EventEmitter {
3572
3652
  * @see https://core.telegram.org/bots/api#editmessagechecklist
3573
3653
  */
3574
3654
  editMessageChecklist(businessConnectionId, messageId, form = {}) {
3655
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3656
+ if (!messageId) return Promise.reject(new Error('messageId is required'));
3575
3657
  form.business_connection_id = businessConnectionId;
3576
3658
  form.message_id = messageId;
3577
3659
  if (form.tasks) {
@@ -3605,6 +3687,8 @@ class TelegramBot extends EventEmitter {
3605
3687
  * @see https://core.telegram.org/bots/api#approvesuggestedpost
3606
3688
  */
3607
3689
  approveSuggestedPost(businessConnectionId, messageId, form = {}) {
3690
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3691
+ if (!messageId) return Promise.reject(new Error('messageId is required'));
3608
3692
  form.business_connection_id = businessConnectionId;
3609
3693
  form.message_id = messageId;
3610
3694
  return this._request('approveSuggestedPost', { form });
@@ -3620,6 +3704,8 @@ class TelegramBot extends EventEmitter {
3620
3704
  * @see https://core.telegram.org/bots/api#declinesuggestedpost
3621
3705
  */
3622
3706
  declineSuggestedPost(businessConnectionId, messageId, form = {}) {
3707
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3708
+ if (!messageId) return Promise.reject(new Error('messageId is required'));
3623
3709
  form.business_connection_id = businessConnectionId;
3624
3710
  form.message_id = messageId;
3625
3711
  return this._request('declineSuggestedPost', { form });
@@ -3639,6 +3725,8 @@ class TelegramBot extends EventEmitter {
3639
3725
  * @see https://core.telegram.org/bots/api#sendmessagedraft
3640
3726
  */
3641
3727
  sendMessageDraft(chatId, text, form = {}) {
3728
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
3729
+ if (!text) return Promise.reject(new Error('text is required'));
3642
3730
  form.chat_id = chatId;
3643
3731
  form.text = text;
3644
3732
  if (form.entities) {
@@ -3687,6 +3775,8 @@ class TelegramBot extends EventEmitter {
3687
3775
  * @see https://core.telegram.org/bots/api#repoststory
3688
3776
  */
3689
3777
  repostStory(businessConnectionId, storyId, targetBusinessConnectionIds, form = {}) {
3778
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3779
+ if (!storyId) return Promise.reject(new Error('storyId is required'));
3690
3780
  form.business_connection_id = businessConnectionId;
3691
3781
  form.story_id = storyId;
3692
3782
  form.target_business_connection_ids = stringify(targetBusinessConnectionIds);
@@ -3810,6 +3900,8 @@ class TelegramBot extends EventEmitter {
3810
3900
  * @see https://core.telegram.org/bots/api#answerguestquery
3811
3901
  */
3812
3902
  answerGuestQuery(guestQueryId, text, form = {}) {
3903
+ if (!guestQueryId) return Promise.reject(new Error('guestQueryId is required'));
3904
+ if (!text) return Promise.reject(new Error('text is required'));
3813
3905
  form.guest_query_id = guestQueryId;
3814
3906
  form.text = text;
3815
3907
  return this._request('answerGuestQuery', { form });
@@ -3860,6 +3952,7 @@ class TelegramBot extends EventEmitter {
3860
3952
  * @see https://core.telegram.org/bots/api#sendlivephoto
3861
3953
  */
3862
3954
  sendLivePhoto(chatId, photo, video, form = {}, fileOptions = {}) {
3955
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
3863
3956
  const opts = {
3864
3957
  qs: form
3865
3958
  };
@@ -3928,6 +4021,8 @@ class TelegramBot extends EventEmitter {
3928
4021
  * @see https://core.telegram.org/bots/api#sendrichmessage
3929
4022
  */
3930
4023
  sendRichMessage(chatId, content, form = {}) {
4024
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4025
+ if (!content) return Promise.reject(new Error('content is required'));
3931
4026
  form.chat_id = chatId;
3932
4027
  form.content = stringify(content);
3933
4028
  return this._request('sendRichMessage', { form });
@@ -3943,6 +4038,8 @@ class TelegramBot extends EventEmitter {
3943
4038
  * @see https://core.telegram.org/bots/api#sendrichmessagedraft
3944
4039
  */
3945
4040
  sendRichMessageDraft(chatId, content, form = {}) {
4041
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4042
+ if (!content) return Promise.reject(new Error('content is required'));
3946
4043
  form.chat_id = chatId;
3947
4044
  form.content = stringify(content);
3948
4045
  return this._request('sendRichMessageDraft', { form });
@@ -3958,6 +4055,8 @@ class TelegramBot extends EventEmitter {
3958
4055
  * @see https://core.telegram.org/bots/api#answerchatjoinrequestquery
3959
4056
  */
3960
4057
  answerChatJoinRequestQuery(chatJoinRequestId, queryId, form = {}) {
4058
+ if (!chatJoinRequestId) return Promise.reject(new Error('chatJoinRequestId is required'));
4059
+ if (!queryId) return Promise.reject(new Error('queryId is required'));
3961
4060
  form.chat_join_request_id = chatJoinRequestId;
3962
4061
  form.query_id = queryId;
3963
4062
  return this._request('answerChatJoinRequestQuery', { form });
@@ -3973,6 +4072,8 @@ class TelegramBot extends EventEmitter {
3973
4072
  * @see https://core.telegram.org/bots/api#sendchatjoinrequestwebapp
3974
4073
  */
3975
4074
  sendChatJoinRequestWebApp(chatJoinRequestId, webApp, form = {}) {
4075
+ if (!chatJoinRequestId) return Promise.reject(new Error('chatJoinRequestId is required'));
4076
+ if (!webApp) return Promise.reject(new Error('webApp is required'));
3976
4077
  form.chat_join_request_id = chatJoinRequestId;
3977
4078
  form.web_app = stringify(webApp);
3978
4079
  return this._request('sendChatJoinRequestWebApp', { form });
@@ -3993,6 +4094,8 @@ class TelegramBot extends EventEmitter {
3993
4094
  * @see https://core.telegram.org/bots/api#editephemeralmessagetext
3994
4095
  */
3995
4096
  editEphemeralMessageText(chatId, ephemeralMessageId, text, form = {}) {
4097
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4098
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
3996
4099
  form.chat_id = chatId;
3997
4100
  form.ephemeral_message_id = ephemeralMessageId;
3998
4101
  form.text = text;
@@ -4011,6 +4114,8 @@ class TelegramBot extends EventEmitter {
4011
4114
  * @see https://core.telegram.org/bots/api#editephemeralmessagemedia
4012
4115
  */
4013
4116
  editEphemeralMessageMedia(chatId, ephemeralMessageId, media, form = {}, fileOptions = {}) {
4117
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4118
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4014
4119
  form.chat_id = chatId;
4015
4120
  form.ephemeral_message_id = ephemeralMessageId;
4016
4121
  form.media = stringify(media);
@@ -4027,6 +4132,8 @@ class TelegramBot extends EventEmitter {
4027
4132
  * @see https://core.telegram.org/bots/api#editephemeralmessagecaption
4028
4133
  */
4029
4134
  editEphemeralMessageCaption(chatId, ephemeralMessageId, form = {}) {
4135
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4136
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4030
4137
  form.chat_id = chatId;
4031
4138
  form.ephemeral_message_id = ephemeralMessageId;
4032
4139
  this._fixEntitiesField(form);
@@ -4043,6 +4150,8 @@ class TelegramBot extends EventEmitter {
4043
4150
  * @see https://core.telegram.org/bots/api#editephemeralmessagereplymarkup
4044
4151
  */
4045
4152
  editEphemeralMessageReplyMarkup(chatId, ephemeralMessageId, form = {}) {
4153
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4154
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4046
4155
  form.chat_id = chatId;
4047
4156
  form.ephemeral_message_id = ephemeralMessageId;
4048
4157
  return this._request('editEphemeralMessageReplyMarkup', { form });
@@ -4058,6 +4167,8 @@ class TelegramBot extends EventEmitter {
4058
4167
  * @see https://core.telegram.org/bots/api#deleteephemeralmessage
4059
4168
  */
4060
4169
  deleteEphemeralMessage(chatId, ephemeralMessageId, form = {}) {
4170
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4171
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4061
4172
  form.chat_id = chatId;
4062
4173
  form.ephemeral_message_id = ephemeralMessageId;
4063
4174
  return this._request('deleteEphemeralMessage', { form });
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@zero-bot.net/tg-bot-api",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "Telegram Bot API",
5
5
  "main": "./index.js",
6
+ "types": "./lib/telegram.d.ts",
6
7
  "directories": {
7
8
  "example": "examples",
8
9
  "test": "test"