@codebam/cf-workers-telegram-bot 11.3.3 → 11.5.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/dist/main.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import TelegramBot from './telegram_bot.js';
2
2
  import TelegramExecutionContext from './telegram_execution_context.js';
3
3
  import Webhook from './webhook.js';
4
- import TelegramApi, { TelegramApiBaseParams, SendMessageParams, SendMessageDraftParams, SendPhotoParams, SendVideoParams, SendChatActionParams, AnswerCallbackParams, AnswerInlineParams, AnswerGuestParams, SendInvoiceParams, AnswerPreCheckoutParams, TelegramApiParams } from './telegram_api.js';
4
+ import TelegramApi, { TelegramApiBaseParams, SendMessageParams, SendMessageDraftParams, SendPhotoParams, SendVideoParams, SendVoiceParams, SendChatActionParams, AnswerCallbackParams, AnswerInlineParams, AnswerGuestParams, SendInvoiceParams, AnswerPreCheckoutParams, TelegramApiParams } from './telegram_api.js';
5
5
  import TelegramCommand from './types/TelegramCommand.js';
6
6
  import TelegramFrom from './types/TelegramFrom.js';
7
7
  import TelegramChat from './types/TelegramChat.js';
@@ -9,6 +9,7 @@ import TelegramUser from './types/TelegramUser.js';
9
9
  import TelegramMessageEntity from './types/TelegramMessageEntity.js';
10
10
  import TelegramPhotoSize from './types/TelegramPhotoSize.js';
11
11
  import TelegramMessage from './types/TelegramMessage.js';
12
+ import { TelegramVoice } from './types/TelegramVoice.js';
12
13
  import TelegramGuestMessage from './types/TelegramGuestMessage.js';
13
14
  import TelegramInputMessageContent from './types/TelegramInputMessageContent.js';
14
15
  import TelegramInlineQuery from './types/TelegramInlineQuery.js';
@@ -26,4 +27,4 @@ import TelegramPreCheckoutQuery from './types/TelegramPreCheckoutQuery.js';
26
27
  import TelegramDocument from './types/TelegramDocument.js';
27
28
  import TelegramSuccessfulPayment from './types/TelegramSuccessfulPayment.js';
28
29
  export default TelegramBot;
29
- export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, TelegramApiBaseParams, SendMessageParams, SendMessageDraftParams, SendPhotoParams, SendVideoParams, SendChatActionParams, AnswerCallbackParams, AnswerInlineParams, AnswerGuestParams, SendInvoiceParams, AnswerPreCheckoutParams, TelegramApiParams, TelegramCommand, TelegramFrom, TelegramChat, TelegramUser, TelegramMessageEntity, TelegramPhotoSize, TelegramMessage, TelegramGuestMessage, TelegramInputMessageContent, TelegramInlineQuery, TelegramUpdate, PartialTelegramUpdate, TelegramInlineQueryType, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, TelegramInlineQueryResultVideo, ChatPermissions, TelegramBusinessMessage, TelegramCallbackQuery, TelegramPreCheckoutQuery, TelegramDocument, TelegramSuccessfulPayment, };
30
+ export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, TelegramApiBaseParams, SendMessageParams, SendMessageDraftParams, SendPhotoParams, SendVideoParams, SendVoiceParams, SendChatActionParams, AnswerCallbackParams, AnswerInlineParams, AnswerGuestParams, SendInvoiceParams, AnswerPreCheckoutParams, TelegramApiParams, TelegramCommand, TelegramFrom, TelegramChat, TelegramUser, TelegramMessageEntity, TelegramPhotoSize, TelegramMessage, TelegramVoice, TelegramGuestMessage, TelegramInputMessageContent, TelegramInlineQuery, TelegramUpdate, PartialTelegramUpdate, TelegramInlineQueryType, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, TelegramInlineQueryResultVideo, ChatPermissions, TelegramBusinessMessage, TelegramCallbackQuery, TelegramPreCheckoutQuery, TelegramDocument, TelegramSuccessfulPayment, };
@@ -84,8 +84,19 @@ export interface AnswerPreCheckoutParams {
84
84
  ok: boolean;
85
85
  error_message?: string;
86
86
  }
87
+ /** Interface for voice parameters */
88
+ export interface SendVoiceParams extends TelegramApiBaseParams {
89
+ voice: string;
90
+ caption?: string;
91
+ parse_mode?: string;
92
+ duration?: number;
93
+ reply_to_message_id?: number | string;
94
+ disable_notification?: boolean;
95
+ protect_content?: boolean;
96
+ reply_markup?: object;
97
+ }
87
98
  /** Type for all possible API parameters */
88
- export type TelegramApiParams = SendMessageParams | SendPhotoParams | SendVideoParams | SendChatActionParams | AnswerCallbackParams | AnswerInlineParams | AnswerGuestParams | SendInvoiceParams | AnswerPreCheckoutParams | Record<string, unknown>;
99
+ export type TelegramApiParams = SendMessageParams | SendPhotoParams | SendVideoParams | SendVoiceParams | SendChatActionParams | AnswerCallbackParams | AnswerInlineParams | AnswerGuestParams | SendInvoiceParams | AnswerPreCheckoutParams | Record<string, unknown>;
89
100
  /** Class representing the Telegram API and all its methods */
90
101
  export default class TelegramApi {
91
102
  /**
@@ -142,6 +153,13 @@ export default class TelegramApi {
142
153
  * @returns Promise with the API response
143
154
  */
144
155
  sendPhoto(botApi: string, data: SendPhotoParams): Promise<Response>;
156
+ /**
157
+ * Send a voice message to a given botApi
158
+ * @param botApi - full URL to the telegram API without slug
159
+ * @param data - data to append to the request
160
+ * @returns Promise with the API response
161
+ */
162
+ sendVoice(botApi: string, data: SendVoiceParams): Promise<Response>;
145
163
  /**
146
164
  * Send an inline response to a given botApi
147
165
  * @param botApi - full URL to the telegram API without slug
@@ -106,6 +106,16 @@ export default class TelegramApi {
106
106
  const url = this.getApiUrl(botApi, 'sendPhoto', data);
107
107
  return await this.fetchAndLog(url, 'sendPhoto', data);
108
108
  }
109
+ /**
110
+ * Send a voice message to a given botApi
111
+ * @param botApi - full URL to the telegram API without slug
112
+ * @param data - data to append to the request
113
+ * @returns Promise with the API response
114
+ */
115
+ async sendVoice(botApi, data) {
116
+ const url = this.getApiUrl(botApi, 'sendVoice', data);
117
+ return await this.fetchAndLog(url, 'sendVoice', data);
118
+ }
109
119
  /**
110
120
  * Send an inline response to a given botApi
111
121
  * @param botApi - full URL to the telegram API without slug
@@ -105,6 +105,8 @@ export default class TelegramBot {
105
105
  switch (ctx.update_type) {
106
106
  case 'photo':
107
107
  return ':photo' in this.commands ? ':photo' : this.defaultCommand;
108
+ case 'voice':
109
+ return ':voice' in this.commands ? ':voice' : this.defaultCommand;
108
110
  case 'document':
109
111
  return ':document' in this.commands ? ':document' : this.defaultCommand;
110
112
  case 'callback':
@@ -80,6 +80,14 @@ export default class TelegramExecutionContext {
80
80
  * @returns Promise with the API response
81
81
  */
82
82
  replyPhoto(photo: string, caption?: string, options?: Record<string, number | string | boolean>): Promise<Response | null>;
83
+ /**
84
+ * Reply to the last message with a voice message
85
+ * @param voice - url or file_id to voice
86
+ * @param caption - voice caption
87
+ * @param options - any additional options to pass to sendVoice
88
+ * @returns Promise with the API response
89
+ */
90
+ replyVoice(voice: string, caption?: string, options?: Record<string, number | string | boolean>): Promise<Response | null>;
83
91
  /**
84
92
  * Send typing in a chat
85
93
  * @returns Promise with the API response
@@ -70,6 +70,9 @@ export default class TelegramExecutionContext {
70
70
  if (this.update.message?.photo) {
71
71
  return 'photo';
72
72
  }
73
+ else if (this.update.message?.voice) {
74
+ return 'voice';
75
+ }
73
76
  else if (this.update.message?.text) {
74
77
  return 'message';
75
78
  }
@@ -143,6 +146,7 @@ export default class TelegramExecutionContext {
143
146
  */
144
147
  async replyVideo(video, options = {}) {
145
148
  switch (this.update_type) {
149
+ case 'voice':
146
150
  case 'message':
147
151
  case 'guest_message':
148
152
  return await this.api.sendVideo(this.bot.api.toString(), {
@@ -179,6 +183,7 @@ export default class TelegramExecutionContext {
179
183
  */
180
184
  async replyPhoto(photo, caption = '', options = {}) {
181
185
  switch (this.update_type) {
186
+ case 'voice':
182
187
  case 'photo':
183
188
  case 'message':
184
189
  case 'guest_message':
@@ -199,12 +204,37 @@ export default class TelegramExecutionContext {
199
204
  return null;
200
205
  }
201
206
  }
207
+ /**
208
+ * Reply to the last message with a voice message
209
+ * @param voice - url or file_id to voice
210
+ * @param caption - voice caption
211
+ * @param options - any additional options to pass to sendVoice
212
+ * @returns Promise with the API response
213
+ */
214
+ async replyVoice(voice, caption = '', options = {}) {
215
+ switch (this.update_type) {
216
+ case 'voice':
217
+ case 'message':
218
+ case 'guest_message':
219
+ return await this.api.sendVoice(this.bot.api.toString(), {
220
+ ...options,
221
+ chat_id: this.getChatId(),
222
+ message_thread_id: this.getThreadId(),
223
+ reply_to_message_id: this.getMessageId(),
224
+ voice,
225
+ caption,
226
+ });
227
+ default:
228
+ return null;
229
+ }
230
+ }
202
231
  /**
203
232
  * Send typing in a chat
204
233
  * @returns Promise with the API response
205
234
  */
206
235
  async sendTyping() {
207
236
  switch (this.update_type) {
237
+ case 'voice':
208
238
  case 'message':
209
239
  case 'photo':
210
240
  case 'document':
@@ -278,6 +308,7 @@ export default class TelegramExecutionContext {
278
308
  */
279
309
  async reply(message, parse_mode = '', reply = true, options = {}) {
280
310
  switch (this.update_type) {
311
+ case 'voice':
281
312
  case 'message':
282
313
  case 'photo':
283
314
  case 'document':
@@ -5,6 +5,7 @@ import TelegramMessageEntity from './TelegramMessageEntity.js';
5
5
  import TelegramPhotoSize from './TelegramPhotoSize.js';
6
6
  import TelegramUser from './TelegramUser.js';
7
7
  import TelegramSuccessfulPayment from './TelegramSuccessfulPayment.js';
8
+ import { TelegramVoice } from './TelegramVoice.js';
8
9
  interface TelegramMessage {
9
10
  message_id: number;
10
11
  message_thread_id?: number;
@@ -29,6 +30,7 @@ interface TelegramMessage {
29
30
  entities?: TelegramMessageEntity[];
30
31
  document?: TelegramDocument;
31
32
  photo?: TelegramPhotoSize[];
33
+ voice?: TelegramVoice;
32
34
  caption?: string;
33
35
  caption_entities?: TelegramMessageEntity[];
34
36
  new_chat_members?: TelegramUser[];
@@ -0,0 +1,7 @@
1
+ export interface TelegramVoice {
2
+ file_id: string;
3
+ file_unique_id: string;
4
+ duration: number;
5
+ mime_type?: string;
6
+ file_size?: number;
7
+ }
@@ -0,0 +1 @@
1
+ export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "11.3.3",
3
+ "version": "11.5.0",
4
4
  "description": "serverless telegram bot on cf workers",
5
5
  "main": "./dist/main.js",
6
6
  "module": "./dist/main.js",