@codebam/cf-workers-telegram-bot 11.13.0 → 12.0.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.
Files changed (59) hide show
  1. package/dist/main.d.ts +1 -21
  2. package/dist/main.js +1 -7
  3. package/dist/telegram_api.d.ts +24 -13
  4. package/dist/telegram_api.js +52 -25
  5. package/dist/telegram_bot.d.ts +2 -3
  6. package/dist/telegram_bot.js +5 -3
  7. package/dist/telegram_execution_context.d.ts +16 -11
  8. package/dist/telegram_execution_context.js +229 -201
  9. package/dist/types/PartialTelegramUpdate.d.ts +6 -6
  10. package/dist/types/TelegramCommand.d.ts +1 -1
  11. package/dist/types/TelegramGuestMessage.d.ts +1 -1
  12. package/dist/types.d.ts +1 -13
  13. package/dist/types.js +1 -5
  14. package/dist/utils.js +4 -3
  15. package/package.json +3 -3
  16. package/dist/types/ChatPermissions.d.ts +0 -17
  17. package/dist/types/ChatPermissions.js +0 -1
  18. package/dist/types/TelegramBusinessConnection.d.ts +0 -10
  19. package/dist/types/TelegramBusinessConnection.js +0 -1
  20. package/dist/types/TelegramBusinessMessage.d.ts +0 -47
  21. package/dist/types/TelegramBusinessMessage.js +0 -1
  22. package/dist/types/TelegramCallbackQuery.d.ts +0 -15
  23. package/dist/types/TelegramCallbackQuery.js +0 -1
  24. package/dist/types/TelegramChat.d.ts +0 -21
  25. package/dist/types/TelegramChat.js +0 -1
  26. package/dist/types/TelegramDocument.d.ts +0 -10
  27. package/dist/types/TelegramDocument.js +0 -1
  28. package/dist/types/TelegramFrom.d.ts +0 -8
  29. package/dist/types/TelegramFrom.js +0 -1
  30. package/dist/types/TelegramInlineQuery.d.ts +0 -9
  31. package/dist/types/TelegramInlineQuery.js +0 -1
  32. package/dist/types/TelegramInlineQueryResult.d.ts +0 -6
  33. package/dist/types/TelegramInlineQueryResult.js +0 -8
  34. package/dist/types/TelegramInlineQueryResultArticle.d.ts +0 -13
  35. package/dist/types/TelegramInlineQueryResultArticle.js +0 -15
  36. package/dist/types/TelegramInlineQueryResultPhoto.d.ts +0 -21
  37. package/dist/types/TelegramInlineQueryResultPhoto.js +0 -23
  38. package/dist/types/TelegramInlineQueryResultVideo.d.ts +0 -21
  39. package/dist/types/TelegramInlineQueryResultVideo.js +0 -23
  40. package/dist/types/TelegramInlineQueryResultVoice.d.ts +0 -13
  41. package/dist/types/TelegramInlineQueryResultVoice.js +0 -14
  42. package/dist/types/TelegramInputMessageContent.d.ts +0 -5
  43. package/dist/types/TelegramInputMessageContent.js +0 -1
  44. package/dist/types/TelegramMessage.d.ts +0 -53
  45. package/dist/types/TelegramMessage.js +0 -1
  46. package/dist/types/TelegramMessageEntity.d.ts +0 -10
  47. package/dist/types/TelegramMessageEntity.js +0 -1
  48. package/dist/types/TelegramPhotoSize.d.ts +0 -8
  49. package/dist/types/TelegramPhotoSize.js +0 -1
  50. package/dist/types/TelegramPreCheckoutQuery.d.ts +0 -23
  51. package/dist/types/TelegramPreCheckoutQuery.js +0 -1
  52. package/dist/types/TelegramSuccessfulPayment.d.ts +0 -22
  53. package/dist/types/TelegramSuccessfulPayment.js +0 -1
  54. package/dist/types/TelegramUpdate.d.ts +0 -22
  55. package/dist/types/TelegramUpdate.js +0 -40
  56. package/dist/types/TelegramUser.d.ts +0 -13
  57. package/dist/types/TelegramUser.js +0 -1
  58. package/dist/types/TelegramVoice.d.ts +0 -7
  59. package/dist/types/TelegramVoice.js +0 -1
package/dist/main.d.ts CHANGED
@@ -1,32 +1,12 @@
1
+ import { Chat as TelegramChat, User as TelegramUser, User as TelegramFrom, MessageEntity as TelegramMessageEntity, PhotoSize as TelegramPhotoSize, Message as TelegramMessage, Voice as TelegramVoice, InputMessageContent as TelegramInputMessageContent, InlineQuery as TelegramInlineQuery, Update as TelegramUpdate, InlineQueryResult as TelegramInlineQueryResult, InlineQueryResultPhoto as TelegramInlineQueryResultPhoto, InlineQueryResultArticle as TelegramInlineQueryResultArticle, InlineQueryResultVideo as TelegramInlineQueryResultVideo, InlineQueryResultVoice as TelegramInlineQueryResultVoice, ChatPermissions as ChatPermissions, Message as TelegramBusinessMessage, CallbackQuery as TelegramCallbackQuery, PreCheckoutQuery as TelegramPreCheckoutQuery, Document as TelegramDocument, SuccessfulPayment as TelegramSuccessfulPayment } from '@grammyjs/types';
1
2
  import TelegramBot from './telegram_bot.js';
2
3
  import TelegramExecutionContext from './telegram_execution_context.js';
3
4
  import Webhook from './webhook.js';
4
5
  import TelegramApi, { TelegramApiBaseParams, SendMessageParams, SendMessageDraftParams, SendPhotoParams, SendVideoParams, SendVoiceParams, SendChatActionParams, AnswerCallbackParams, AnswerInlineParams, AnswerGuestParams, SendInvoiceParams, AnswerPreCheckoutParams, TelegramApiParams } from './telegram_api.js';
5
6
  import TelegramCommand from './types/TelegramCommand.js';
6
- import TelegramFrom from './types/TelegramFrom.js';
7
- import TelegramChat from './types/TelegramChat.js';
8
- import TelegramUser from './types/TelegramUser.js';
9
- import TelegramMessageEntity from './types/TelegramMessageEntity.js';
10
- import TelegramPhotoSize from './types/TelegramPhotoSize.js';
11
- import TelegramMessage from './types/TelegramMessage.js';
12
- import { TelegramVoice } from './types/TelegramVoice.js';
13
7
  import TelegramGuestMessage from './types/TelegramGuestMessage.js';
14
- import TelegramInputMessageContent from './types/TelegramInputMessageContent.js';
15
- import TelegramInlineQuery from './types/TelegramInlineQuery.js';
16
- import TelegramUpdate from './types/TelegramUpdate.js';
17
8
  import PartialTelegramUpdate from './types/PartialTelegramUpdate.js';
18
9
  import TelegramInlineQueryType from './types/TelegramInlineQueryType.js';
19
- import TelegramInlineQueryResult from './types/TelegramInlineQueryResult.js';
20
- import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPhoto.js';
21
- import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultArticle.js';
22
- import TelegramInlineQueryResultVideo from './types/TelegramInlineQueryResultVideo.js';
23
- import TelegramInlineQueryResultVoice from './types/TelegramInlineQueryResultVoice.js';
24
- import ChatPermissions from './types/ChatPermissions.js';
25
- import TelegramBusinessMessage from './types/TelegramBusinessMessage.js';
26
- import TelegramCallbackQuery from './types/TelegramCallbackQuery.js';
27
- import TelegramPreCheckoutQuery from './types/TelegramPreCheckoutQuery.js';
28
- import TelegramDocument from './types/TelegramDocument.js';
29
- import TelegramSuccessfulPayment from './types/TelegramSuccessfulPayment.js';
30
10
  import { markdownToHtml, fetchTool } from './utils.js';
31
11
  export default TelegramBot;
32
12
  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, TelegramInlineQueryResultVoice, ChatPermissions, TelegramBusinessMessage, TelegramCallbackQuery, TelegramPreCheckoutQuery, TelegramDocument, TelegramSuccessfulPayment, markdownToHtml, fetchTool, };
package/dist/main.js CHANGED
@@ -2,12 +2,6 @@ import TelegramBot from './telegram_bot.js';
2
2
  import TelegramExecutionContext from './telegram_execution_context.js';
3
3
  import Webhook from './webhook.js';
4
4
  import TelegramApi from './telegram_api.js';
5
- import TelegramUpdate from './types/TelegramUpdate.js';
6
- import TelegramInlineQueryResult from './types/TelegramInlineQueryResult.js';
7
- import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPhoto.js';
8
- import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultArticle.js';
9
- import TelegramInlineQueryResultVideo from './types/TelegramInlineQueryResultVideo.js';
10
- import TelegramInlineQueryResultVoice from './types/TelegramInlineQueryResultVoice.js';
11
5
  import { markdownToHtml, fetchTool } from './utils.js';
12
6
  export default TelegramBot;
13
- export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, TelegramUpdate, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, TelegramInlineQueryResultVideo, TelegramInlineQueryResultVoice, markdownToHtml, fetchTool, };
7
+ export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, markdownToHtml, fetchTool, };
@@ -1,6 +1,7 @@
1
- import TelegramInlineQueryResult from './types/TelegramInlineQueryResult.js';
1
+ import { InlineQueryResult as TelegramInlineQueryResult } from '@grammyjs/types';
2
2
  /** Interface for common Telegram API parameters */
3
3
  export interface TelegramApiBaseParams {
4
+ [key: string]: unknown;
4
5
  chat_id: number | string;
5
6
  message_thread_id?: number;
6
7
  business_connection_id?: string | number;
@@ -95,7 +96,19 @@ export interface SendVoiceParams extends TelegramApiBaseParams {
95
96
  }
96
97
  /** Type for all possible API parameters */
97
98
  export type TelegramApiParams = SendMessageParams | SendPhotoParams | SendVideoParams | SendVoiceParams | SendChatActionParams | AnswerCallbackParams | AnswerInlineParams | AnswerGuestParams | SendInvoiceParams | AnswerPreCheckoutParams | Record<string, unknown>;
98
- /** Class representing the Telegram API and all its methods */
99
+ /** Interface for edit message text parameters */
100
+ export interface EditMessageTextParams extends Partial<TelegramApiBaseParams> {
101
+ [key: string]: unknown;
102
+ chat_id?: number | string;
103
+ message_id?: number;
104
+ inline_message_id?: string;
105
+ text: string;
106
+ parse_mode?: string;
107
+ disable_web_page_preview?: boolean;
108
+ reply_markup?: object;
109
+ business_connection_id?: string | number;
110
+ }
111
+ /** Class representing the telegram API and all its methods */
99
112
  export default class TelegramApi {
100
113
  /**
101
114
  * Get the API URL for a given bot API and slug
@@ -104,7 +117,7 @@ export default class TelegramApi {
104
117
  * @param data - data to append to the request
105
118
  * @returns Request object with the full URL and parameters
106
119
  */
107
- getApiUrl(botApi: string, slug: string, data: TelegramApiParams): Request;
120
+ getApiUrl(botApi: string, slug: string, data: TelegramApiParams, method?: 'GET' | 'POST'): Request;
108
121
  /**
109
122
  * Fetch a URL and log the response
110
123
  * @param url - the URL to fetch
@@ -195,16 +208,7 @@ export default class TelegramApi {
195
208
  * @param data - data to append to the request
196
209
  * @returns Promise with the API response
197
210
  */
198
- editMessageText(botApi: string, data: {
199
- chat_id?: number | string;
200
- message_id?: number;
201
- inline_message_id?: string;
202
- text: string;
203
- parse_mode?: string;
204
- disable_web_page_preview?: boolean;
205
- reply_markup?: object;
206
- business_connection_id?: string | number;
207
- }): Promise<Response>;
211
+ editMessageText(botApi: string, data: EditMessageTextParams): Promise<Response>;
208
212
  sendMessageDraft(botApi: string, data: SendMessageDraftParams): Promise<Response>;
209
213
  /**
210
214
  * Send an invoice to a user
@@ -220,6 +224,13 @@ export default class TelegramApi {
220
224
  * @returns Promise with the API response
221
225
  */
222
226
  answerPreCheckoutQuery(botApi: string, data: AnswerPreCheckoutParams): Promise<Response>;
227
+ /**
228
+ * Get information about a business connection
229
+ * @param botApi - full URL to the telegram API without slug
230
+ * @param business_connection_id - unique identifier of the business connection
231
+ * @returns Promise with the API response
232
+ */
233
+ getBusinessConnection(botApi: string, business_connection_id: string): Promise<Response>;
223
234
  /**
224
235
  * Get basic information about the bot
225
236
  * @param botApi - full URL to the telegram API without slug
@@ -1,4 +1,4 @@
1
- /** Class representing the Telegram API and all its methods */
1
+ /** Class representing the telegram API and all its methods */
2
2
  export default class TelegramApi {
3
3
  /**
4
4
  * Get the API URL for a given bot API and slug
@@ -7,15 +7,24 @@ export default class TelegramApi {
7
7
  * @param data - data to append to the request
8
8
  * @returns Request object with the full URL and parameters
9
9
  */
10
- getApiUrl(botApi, slug, data) {
11
- const request = new URL(botApi + (slug.startsWith('/') || botApi.endsWith('/') ? '' : '/') + slug);
12
- const params = new URLSearchParams();
13
- for (const [key, value] of Object.entries(data)) {
14
- if (value !== undefined) {
15
- params.append(key, typeof value === 'object' && value !== null ? JSON.stringify(value) : String(value));
10
+ getApiUrl(botApi, slug, data, method = 'GET') {
11
+ const baseUrl = botApi + (slug.startsWith('/') || botApi.endsWith('/') ? '' : '/') + slug;
12
+ if (method === 'GET') {
13
+ const url = new URL(baseUrl);
14
+ for (const [key, value] of Object.entries(data)) {
15
+ if (value !== undefined) {
16
+ url.searchParams.append(key, typeof value === 'object' && value !== null ? JSON.stringify(value) : String(value));
17
+ }
16
18
  }
19
+ return new Request(url.toString());
17
20
  }
18
- return new Request(`${request.toString()}?${params.toString()}`);
21
+ return new Request(baseUrl, {
22
+ method: 'POST',
23
+ headers: {
24
+ 'Content-Type': 'application/json',
25
+ },
26
+ body: JSON.stringify(data),
27
+ });
19
28
  }
20
29
  /**
21
30
  * Fetch a URL and log the response
@@ -31,12 +40,20 @@ export default class TelegramApi {
31
40
  let errorDescription = '';
32
41
  try {
33
42
  const json = (await cloned.json());
34
- errorDescription = json.description ? `: ${json.description}` : '';
43
+ errorDescription = json.description || '';
35
44
  }
36
45
  catch {
37
46
  // ignore
38
47
  }
39
- throw new Error(`Telegram API error: ${String(response.status)} ${response.statusText}${errorDescription}`);
48
+ if (errorDescription.includes('BUSINESS_CONNECTION_INVALID') || errorDescription.includes('BUSINESS_PEER_INVALID')) {
49
+ console.warn(`Telegram API business error: ${errorDescription}`);
50
+ throw new Error('BUSINESS_CONNECTION_INVALID');
51
+ }
52
+ if (errorDescription.includes('PEER_ID_INVALID')) {
53
+ console.warn(`Telegram API peer error: ${errorDescription}`);
54
+ throw new Error('PEER_ID_INVALID');
55
+ }
56
+ throw new Error(`Telegram API error: ${String(response.status)} ${response.statusText}${errorDescription ? ': ' + errorDescription : ''}`);
40
57
  }
41
58
  const cloned = response.clone();
42
59
  try {
@@ -59,7 +76,7 @@ export default class TelegramApi {
59
76
  * @returns Promise with the API response
60
77
  */
61
78
  async sendChatAction(botApi, data) {
62
- const url = this.getApiUrl(botApi, 'sendChatAction', data);
79
+ const url = this.getApiUrl(botApi, 'sendChatAction', data, 'POST');
63
80
  return await this.fetchAndLog(url, 'sendChatAction', data);
64
81
  }
65
82
  /**
@@ -73,7 +90,7 @@ export default class TelegramApi {
73
90
  if (!data.file_id || data.file_id === '') {
74
91
  throw new Error('No file_id provided');
75
92
  }
76
- const url = this.getApiUrl(botApi, 'getFile', data);
93
+ const url = this.getApiUrl(botApi, 'getFile', data, 'POST');
77
94
  const response = await this.fetchAndLog(url, 'getFile', data);
78
95
  const json = await response.json();
79
96
  if (!json.ok || !json.result?.file_path) {
@@ -92,7 +109,7 @@ export default class TelegramApi {
92
109
  * @returns Promise with the API response
93
110
  */
94
111
  async sendMessage(botApi, data) {
95
- const url = this.getApiUrl(botApi, 'sendMessage', data);
112
+ const url = this.getApiUrl(botApi, 'sendMessage', data, 'POST');
96
113
  return await this.fetchAndLog(url, 'sendMessage', data);
97
114
  }
98
115
  /**
@@ -102,7 +119,7 @@ export default class TelegramApi {
102
119
  * @returns Promise with the API response
103
120
  */
104
121
  async sendVideo(botApi, data) {
105
- const url = this.getApiUrl(botApi, 'sendVideo', data);
122
+ const url = this.getApiUrl(botApi, 'sendVideo', data, 'POST');
106
123
  return await this.fetchAndLog(url, 'sendVideo', data);
107
124
  }
108
125
  /**
@@ -112,7 +129,7 @@ export default class TelegramApi {
112
129
  * @returns Promise with the API response
113
130
  */
114
131
  async sendPhoto(botApi, data) {
115
- const url = this.getApiUrl(botApi, 'sendPhoto', data);
132
+ const url = this.getApiUrl(botApi, 'sendPhoto', data, 'POST');
116
133
  return await this.fetchAndLog(url, 'sendPhoto', data);
117
134
  }
118
135
  /**
@@ -122,7 +139,7 @@ export default class TelegramApi {
122
139
  * @returns Promise with the API response
123
140
  */
124
141
  async sendVoice(botApi, data) {
125
- const url = this.getApiUrl(botApi, 'sendVoice', data);
142
+ const url = this.getApiUrl(botApi, 'sendVoice', data, 'POST');
126
143
  return await this.fetchAndLog(url, 'sendVoice', data);
127
144
  }
128
145
  /**
@@ -139,7 +156,7 @@ export default class TelegramApi {
139
156
  is_personal: data.is_personal,
140
157
  next_offset: data.next_offset,
141
158
  };
142
- const url = this.getApiUrl(botApi, 'answerInlineQuery', params);
159
+ const url = this.getApiUrl(botApi, 'answerInlineQuery', params, 'POST');
143
160
  return await this.fetchAndLog(url, 'answerInlineQuery', params);
144
161
  }
145
162
  /**
@@ -149,7 +166,7 @@ export default class TelegramApi {
149
166
  * @returns Promise with the API response
150
167
  */
151
168
  async answerCallback(botApi, data) {
152
- const url = this.getApiUrl(botApi, 'answerCallbackQuery', data);
169
+ const url = this.getApiUrl(botApi, 'answerCallbackQuery', data, 'POST');
153
170
  return await this.fetchAndLog(url, 'answerCallbackQuery', data);
154
171
  }
155
172
  /**
@@ -159,7 +176,7 @@ export default class TelegramApi {
159
176
  * @returns Promise with the API response
160
177
  */
161
178
  async answerGuestQuery(botApi, data) {
162
- const url = this.getApiUrl(botApi, 'answerGuestQuery', data);
179
+ const url = this.getApiUrl(botApi, 'answerGuestQuery', data, 'POST');
163
180
  return await this.fetchAndLog(url, 'answerGuestQuery', data);
164
181
  }
165
182
  /**
@@ -169,7 +186,7 @@ export default class TelegramApi {
169
186
  * @returns Promise with the API response
170
187
  */
171
188
  async deleteMessage(botApi, data) {
172
- const url = this.getApiUrl(botApi, 'deleteMessage', data);
189
+ const url = this.getApiUrl(botApi, 'deleteMessage', data, 'POST');
173
190
  return await this.fetchAndLog(url, 'deleteMessage', data);
174
191
  }
175
192
  /**
@@ -179,11 +196,11 @@ export default class TelegramApi {
179
196
  * @returns Promise with the API response
180
197
  */
181
198
  async editMessageText(botApi, data) {
182
- const url = this.getApiUrl(botApi, 'editMessageText', data);
199
+ const url = this.getApiUrl(botApi, 'editMessageText', data, 'POST');
183
200
  return await this.fetchAndLog(url, 'editMessageText', data);
184
201
  }
185
202
  async sendMessageDraft(botApi, data) {
186
- const url = this.getApiUrl(botApi, 'sendMessageDraft', data);
203
+ const url = this.getApiUrl(botApi, 'sendMessageDraft', data, 'POST');
187
204
  return await this.fetchAndLog(url, 'sendMessageDraft', data);
188
205
  }
189
206
  /**
@@ -193,7 +210,7 @@ export default class TelegramApi {
193
210
  * @returns Promise with the API response
194
211
  */
195
212
  async sendInvoice(botApi, data) {
196
- const url = this.getApiUrl(botApi, 'sendInvoice', data);
213
+ const url = this.getApiUrl(botApi, 'sendInvoice', data, 'POST');
197
214
  return await this.fetchAndLog(url, 'sendInvoice', data);
198
215
  }
199
216
  /**
@@ -203,16 +220,26 @@ export default class TelegramApi {
203
220
  * @returns Promise with the API response
204
221
  */
205
222
  async answerPreCheckoutQuery(botApi, data) {
206
- const url = this.getApiUrl(botApi, 'answerPreCheckoutQuery', data);
223
+ const url = this.getApiUrl(botApi, 'answerPreCheckoutQuery', data, 'POST');
207
224
  return await this.fetchAndLog(url, 'answerPreCheckoutQuery', data);
208
225
  }
226
+ /**
227
+ * Get information about a business connection
228
+ * @param botApi - full URL to the telegram API without slug
229
+ * @param business_connection_id - unique identifier of the business connection
230
+ * @returns Promise with the API response
231
+ */
232
+ async getBusinessConnection(botApi, business_connection_id) {
233
+ const url = this.getApiUrl(botApi, 'getBusinessConnection', { business_connection_id }, 'POST');
234
+ return await this.fetchAndLog(url, 'getBusinessConnection', { business_connection_id });
235
+ }
209
236
  /**
210
237
  * Get basic information about the bot
211
238
  * @param botApi - full URL to the telegram API without slug
212
239
  * @returns Promise with the API response
213
240
  */
214
241
  async getMe(botApi) {
215
- const url = this.getApiUrl(botApi, 'getMe', {});
242
+ const url = this.getApiUrl(botApi, 'getMe', {}, 'GET');
216
243
  return await this.fetchAndLog(url, 'getMe', {});
217
244
  }
218
245
  }
@@ -1,4 +1,4 @@
1
- import TelegramUpdate from './types/TelegramUpdate.js';
1
+ import { Update as TelegramUpdate } from '@grammyjs/types';
2
2
  import TelegramExecutionContext from './telegram_execution_context.js';
3
3
  import Webhook from './webhook.js';
4
4
  /** Class representing a telegram bot. */
@@ -10,8 +10,7 @@ export default class TelegramBot {
10
10
  /** The telegram webhook object */
11
11
  webhook: Webhook;
12
12
  /** The telegram update object */
13
- update: TelegramUpdate;
14
- /** The telegram commands record map */
13
+ update: TelegramUpdate; /** The telegram commands record map */
15
14
  commands: Record<string, (ctx: TelegramExecutionContext) => Promise<Response | void>>;
16
15
  /** Middleware functions to run before handlers */
17
16
  middleware: ((ctx: TelegramExecutionContext) => Promise<Response | void>)[];
@@ -1,4 +1,3 @@
1
- import TelegramUpdate from './types/TelegramUpdate.js';
2
1
  import TelegramExecutionContext from './telegram_execution_context.js';
3
2
  import Webhook from './webhook.js';
4
3
  /** Class representing a telegram bot. */
@@ -10,8 +9,7 @@ export default class TelegramBot {
10
9
  /** The telegram webhook object */
11
10
  webhook = new Webhook('', new Request('http://127.0.0.1'));
12
11
  /** The telegram update object */
13
- update = new TelegramUpdate({});
14
- /** The telegram commands record map */
12
+ update = { update_id: 0 }; /** The telegram commands record map */
15
13
  commands = {};
16
14
  /** Middleware functions to run before handlers */
17
15
  middleware = [];
@@ -162,6 +160,10 @@ export default class TelegramBot {
162
160
  console.log(this.update);
163
161
  const ctx = new TelegramExecutionContext(this, this.update);
164
162
  this.currentContext = ctx;
163
+ if (!(await ctx.shouldProcess())) {
164
+ console.log('Skipping update processing based on context validation');
165
+ return new Response('ok');
166
+ }
165
167
  // Run middleware
166
168
  for (const middleware of this.middleware) {
167
169
  const result = await middleware(ctx);
@@ -1,9 +1,12 @@
1
+ import { Update as TelegramUpdate, InlineQueryResult as TelegramInlineQueryResult } from '@grammyjs/types';
1
2
  import TelegramApi from './telegram_api.js';
2
3
  import TelegramBot from './telegram_bot.js';
3
- import TelegramUpdate from './types/TelegramUpdate.js';
4
- import TelegramInlineQueryResult from './types/TelegramInlineQueryResult.js';
5
4
  /** Class representing the context of execution */
6
5
  export default class TelegramExecutionContext {
6
+ /** Cache for business connection owners */
7
+ private static businessOwners;
8
+ /** Cache for dead business connections */
9
+ private static poisonedConnections;
7
10
  /** an instance of the telegram bot */
8
11
  bot: TelegramBot;
9
12
  /** an instance of the telegram update */
@@ -44,6 +47,11 @@ export default class TelegramExecutionContext {
44
47
  * Determine the type of update received
45
48
  * @returns The update type as a string
46
49
  */
50
+ /**
51
+ * Determine if the current update should be processed.
52
+ * For business messages, this checks if the connection is valid and has reply permissions.
53
+ */
54
+ shouldProcess(): Promise<boolean>;
47
55
  private determineUpdateType;
48
56
  /**
49
57
  * Get the chat ID from the current update
@@ -66,6 +74,10 @@ export default class TelegramExecutionContext {
66
74
  * @param options - any additional options to pass to sendVideo
67
75
  * @returns Promise with the API response
68
76
  */
77
+ /**
78
+ * Helper to handle business connection fallbacks
79
+ */
80
+ private withBusinessFallback;
69
81
  replyVideo(video: string, options?: Record<string, number | string | boolean>): Promise<Response | null>;
70
82
  /**
71
83
  * Get File from telegram file_id
@@ -149,14 +161,7 @@ export default class TelegramExecutionContext {
149
161
  * @param options - any additional options to pass to sendMessage/editMessageText
150
162
  * @returns Promise with the API response
151
163
  */
152
- streamReply(message: string, draft_id: number, parse_mode?: string, options?: Record<string, number | string | boolean | object>): Promise<Response>;
153
- /**
154
- * Reply to the last message with text
155
- * @param message - text to reply with
156
- * @param parse_mode - one of HTML, MarkdownV2, Markdown, or an empty string for ascii
157
- * @param options - any additional options to pass to sendMessage
158
- * @returns Promise with the API response
159
- */
164
+ streamReply(message: string, draft_id: number, parse_mode?: string, options?: Record<string, number | string | boolean | object>, finish?: boolean): Promise<Response | null>;
160
165
  reply(message: string, parse_mode?: string, reply?: boolean, options?: Record<string, number | string | boolean>): Promise<Response | null>;
161
166
  /**
162
167
  * Send an invoice for Telegram Stars
@@ -166,7 +171,7 @@ export default class TelegramExecutionContext {
166
171
  * @param amount - amount of stars
167
172
  * @returns Promise with the API response
168
173
  */
169
- sendStarsInvoice(title: string, description: string, payload: string, amount: number): Promise<Response>;
174
+ sendStarsInvoice(title: string, description: string, payload: string, amount: number): Promise<Response | null>;
170
175
  /**
171
176
  * Answer a pre-checkout query
172
177
  * @param ok - whether the payment can proceed