@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/lib/telegram.js CHANGED
@@ -23,7 +23,31 @@ 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'];
27
51
 
28
52
  const _deprecatedMessageTypes = ['new_chat_participant', 'left_chat_participant'];
29
53
 
@@ -232,6 +256,21 @@ class TelegramBot extends EventEmitter {
232
256
  }
233
257
  }
234
258
 
259
+ /**
260
+ * Fix JSON-serialized object fields by making them JSON strings if they are still objects.
261
+ * Covers new Bot API 7.4-10.3 fields that accept JSON-serialized objects.
262
+ * @param {Object} obj Object; either 'form' or 'qs'
263
+ * @private
264
+ */
265
+ _fixJsonFields(obj) {
266
+ 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'];
267
+ for (const field of jsonFields) {
268
+ if (obj.hasOwnProperty(field) && typeof obj[field] !== 'string') {
269
+ obj[field] = stringify(obj[field]);
270
+ }
271
+ }
272
+ }
273
+
235
274
  /**
236
275
  * Make request against the API
237
276
  * @param {String} _path API endpoint
@@ -252,10 +291,12 @@ class TelegramBot extends EventEmitter {
252
291
  this._fixReplyMarkup(options.form);
253
292
  this._fixEntitiesField(options.form);
254
293
  this._fixReplyParameters(options.form);
294
+ this._fixJsonFields(options.form);
255
295
  }
256
296
  if (options.qs) {
257
297
  this._fixReplyMarkup(options.qs);
258
298
  this._fixReplyParameters(options.qs);
299
+ this._fixJsonFields(options.qs);
259
300
  }
260
301
 
261
302
  options.method = 'POST';
@@ -3556,6 +3597,8 @@ class TelegramBot extends EventEmitter {
3556
3597
  * @see https://core.telegram.org/bots/api#sendchecklist
3557
3598
  */
3558
3599
  sendChecklist(businessConnectionId, title, tasks, form = {}) {
3600
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3601
+ if (!title) return Promise.reject(new Error('title is required'));
3559
3602
  form.business_connection_id = businessConnectionId;
3560
3603
  form.title = title;
3561
3604
  form.tasks = stringify(tasks);
@@ -3572,6 +3615,8 @@ class TelegramBot extends EventEmitter {
3572
3615
  * @see https://core.telegram.org/bots/api#editmessagechecklist
3573
3616
  */
3574
3617
  editMessageChecklist(businessConnectionId, messageId, form = {}) {
3618
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3619
+ if (!messageId) return Promise.reject(new Error('messageId is required'));
3575
3620
  form.business_connection_id = businessConnectionId;
3576
3621
  form.message_id = messageId;
3577
3622
  if (form.tasks) {
@@ -3605,6 +3650,8 @@ class TelegramBot extends EventEmitter {
3605
3650
  * @see https://core.telegram.org/bots/api#approvesuggestedpost
3606
3651
  */
3607
3652
  approveSuggestedPost(businessConnectionId, messageId, form = {}) {
3653
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3654
+ if (!messageId) return Promise.reject(new Error('messageId is required'));
3608
3655
  form.business_connection_id = businessConnectionId;
3609
3656
  form.message_id = messageId;
3610
3657
  return this._request('approveSuggestedPost', { form });
@@ -3620,6 +3667,8 @@ class TelegramBot extends EventEmitter {
3620
3667
  * @see https://core.telegram.org/bots/api#declinesuggestedpost
3621
3668
  */
3622
3669
  declineSuggestedPost(businessConnectionId, messageId, form = {}) {
3670
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3671
+ if (!messageId) return Promise.reject(new Error('messageId is required'));
3623
3672
  form.business_connection_id = businessConnectionId;
3624
3673
  form.message_id = messageId;
3625
3674
  return this._request('declineSuggestedPost', { form });
@@ -3639,6 +3688,8 @@ class TelegramBot extends EventEmitter {
3639
3688
  * @see https://core.telegram.org/bots/api#sendmessagedraft
3640
3689
  */
3641
3690
  sendMessageDraft(chatId, text, form = {}) {
3691
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
3692
+ if (!text) return Promise.reject(new Error('text is required'));
3642
3693
  form.chat_id = chatId;
3643
3694
  form.text = text;
3644
3695
  if (form.entities) {
@@ -3687,6 +3738,8 @@ class TelegramBot extends EventEmitter {
3687
3738
  * @see https://core.telegram.org/bots/api#repoststory
3688
3739
  */
3689
3740
  repostStory(businessConnectionId, storyId, targetBusinessConnectionIds, form = {}) {
3741
+ if (!businessConnectionId) return Promise.reject(new Error('businessConnectionId is required'));
3742
+ if (!storyId) return Promise.reject(new Error('storyId is required'));
3690
3743
  form.business_connection_id = businessConnectionId;
3691
3744
  form.story_id = storyId;
3692
3745
  form.target_business_connection_ids = stringify(targetBusinessConnectionIds);
@@ -3810,6 +3863,8 @@ class TelegramBot extends EventEmitter {
3810
3863
  * @see https://core.telegram.org/bots/api#answerguestquery
3811
3864
  */
3812
3865
  answerGuestQuery(guestQueryId, text, form = {}) {
3866
+ if (!guestQueryId) return Promise.reject(new Error('guestQueryId is required'));
3867
+ if (!text) return Promise.reject(new Error('text is required'));
3813
3868
  form.guest_query_id = guestQueryId;
3814
3869
  form.text = text;
3815
3870
  return this._request('answerGuestQuery', { form });
@@ -3860,6 +3915,7 @@ class TelegramBot extends EventEmitter {
3860
3915
  * @see https://core.telegram.org/bots/api#sendlivephoto
3861
3916
  */
3862
3917
  sendLivePhoto(chatId, photo, video, form = {}, fileOptions = {}) {
3918
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
3863
3919
  const opts = {
3864
3920
  qs: form
3865
3921
  };
@@ -3928,6 +3984,8 @@ class TelegramBot extends EventEmitter {
3928
3984
  * @see https://core.telegram.org/bots/api#sendrichmessage
3929
3985
  */
3930
3986
  sendRichMessage(chatId, content, form = {}) {
3987
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
3988
+ if (!content) return Promise.reject(new Error('content is required'));
3931
3989
  form.chat_id = chatId;
3932
3990
  form.content = stringify(content);
3933
3991
  return this._request('sendRichMessage', { form });
@@ -3943,6 +4001,8 @@ class TelegramBot extends EventEmitter {
3943
4001
  * @see https://core.telegram.org/bots/api#sendrichmessagedraft
3944
4002
  */
3945
4003
  sendRichMessageDraft(chatId, content, form = {}) {
4004
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4005
+ if (!content) return Promise.reject(new Error('content is required'));
3946
4006
  form.chat_id = chatId;
3947
4007
  form.content = stringify(content);
3948
4008
  return this._request('sendRichMessageDraft', { form });
@@ -3958,6 +4018,8 @@ class TelegramBot extends EventEmitter {
3958
4018
  * @see https://core.telegram.org/bots/api#answerchatjoinrequestquery
3959
4019
  */
3960
4020
  answerChatJoinRequestQuery(chatJoinRequestId, queryId, form = {}) {
4021
+ if (!chatJoinRequestId) return Promise.reject(new Error('chatJoinRequestId is required'));
4022
+ if (!queryId) return Promise.reject(new Error('queryId is required'));
3961
4023
  form.chat_join_request_id = chatJoinRequestId;
3962
4024
  form.query_id = queryId;
3963
4025
  return this._request('answerChatJoinRequestQuery', { form });
@@ -3973,6 +4035,8 @@ class TelegramBot extends EventEmitter {
3973
4035
  * @see https://core.telegram.org/bots/api#sendchatjoinrequestwebapp
3974
4036
  */
3975
4037
  sendChatJoinRequestWebApp(chatJoinRequestId, webApp, form = {}) {
4038
+ if (!chatJoinRequestId) return Promise.reject(new Error('chatJoinRequestId is required'));
4039
+ if (!webApp) return Promise.reject(new Error('webApp is required'));
3976
4040
  form.chat_join_request_id = chatJoinRequestId;
3977
4041
  form.web_app = stringify(webApp);
3978
4042
  return this._request('sendChatJoinRequestWebApp', { form });
@@ -3993,6 +4057,8 @@ class TelegramBot extends EventEmitter {
3993
4057
  * @see https://core.telegram.org/bots/api#editephemeralmessagetext
3994
4058
  */
3995
4059
  editEphemeralMessageText(chatId, ephemeralMessageId, text, form = {}) {
4060
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4061
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
3996
4062
  form.chat_id = chatId;
3997
4063
  form.ephemeral_message_id = ephemeralMessageId;
3998
4064
  form.text = text;
@@ -4011,6 +4077,8 @@ class TelegramBot extends EventEmitter {
4011
4077
  * @see https://core.telegram.org/bots/api#editephemeralmessagemedia
4012
4078
  */
4013
4079
  editEphemeralMessageMedia(chatId, ephemeralMessageId, media, form = {}, fileOptions = {}) {
4080
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4081
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4014
4082
  form.chat_id = chatId;
4015
4083
  form.ephemeral_message_id = ephemeralMessageId;
4016
4084
  form.media = stringify(media);
@@ -4027,6 +4095,8 @@ class TelegramBot extends EventEmitter {
4027
4095
  * @see https://core.telegram.org/bots/api#editephemeralmessagecaption
4028
4096
  */
4029
4097
  editEphemeralMessageCaption(chatId, ephemeralMessageId, form = {}) {
4098
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4099
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4030
4100
  form.chat_id = chatId;
4031
4101
  form.ephemeral_message_id = ephemeralMessageId;
4032
4102
  this._fixEntitiesField(form);
@@ -4043,6 +4113,8 @@ class TelegramBot extends EventEmitter {
4043
4113
  * @see https://core.telegram.org/bots/api#editephemeralmessagereplymarkup
4044
4114
  */
4045
4115
  editEphemeralMessageReplyMarkup(chatId, ephemeralMessageId, form = {}) {
4116
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4117
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4046
4118
  form.chat_id = chatId;
4047
4119
  form.ephemeral_message_id = ephemeralMessageId;
4048
4120
  return this._request('editEphemeralMessageReplyMarkup', { form });
@@ -4058,6 +4130,8 @@ class TelegramBot extends EventEmitter {
4058
4130
  * @see https://core.telegram.org/bots/api#deleteephemeralmessage
4059
4131
  */
4060
4132
  deleteEphemeralMessage(chatId, ephemeralMessageId, form = {}) {
4133
+ if (!chatId) return Promise.reject(new Error('chatId is required'));
4134
+ if (!ephemeralMessageId) return Promise.reject(new Error('ephemeralMessageId is required'));
4061
4135
  form.chat_id = chatId;
4062
4136
  form.ephemeral_message_id = ephemeralMessageId;
4063
4137
  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.2.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"