Skip to content

Commit 53bc684

Browse files
committed
update
1 parent d529655 commit 53bc684

2 files changed

Lines changed: 49 additions & 41 deletions

File tree

src/telegram_api.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { InlineQueryResult as TelegramInlineQueryResult } from '@grammyjs/types'
33

44
/** Interface for common Telegram API parameters */
55
export interface TelegramApiBaseParams {
6+
[key: string]: unknown;
67
chat_id: number | string;
78
message_thread_id?: number;
89
business_connection_id?: string | number;
@@ -118,7 +119,20 @@ export type TelegramApiParams =
118119
| AnswerPreCheckoutParams
119120
| Record<string, unknown>;
120121

121-
/** Class representing the Telegram API and all its methods */
122+
/** Interface for edit message text parameters */
123+
export interface EditMessageTextParams extends Partial<TelegramApiBaseParams> {
124+
[key: string]: unknown;
125+
chat_id?: number | string;
126+
message_id?: number;
127+
inline_message_id?: string;
128+
text: string;
129+
parse_mode?: string;
130+
disable_web_page_preview?: boolean;
131+
reply_markup?: object;
132+
business_connection_id?: string | number;
133+
}
134+
135+
/** Class representing the telegram API and all its methods */
122136
export default class TelegramApi {
123137
/**
124138
* Get the API URL for a given bot API and slug
@@ -327,16 +341,7 @@ export default class TelegramApi {
327341
*/
328342
async editMessageText(
329343
botApi: string,
330-
data: {
331-
chat_id?: number | string;
332-
message_id?: number;
333-
inline_message_id?: string;
334-
text: string;
335-
parse_mode?: string;
336-
disable_web_page_preview?: boolean;
337-
reply_markup?: object;
338-
business_connection_id?: string | number;
339-
},
344+
data: EditMessageTextParams,
340345
): Promise<Response> {
341346
const url = this.getApiUrl(botApi, 'editMessageText', data);
342347
return await this.fetchAndLog(url, 'editMessageText', data);

src/telegram_execution_context.ts

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
import { Update as TelegramUpdate, InlineQueryResult as TelegramInlineQueryResult, ParseMode } from '@grammyjs/types';
2-
import TelegramApi from './telegram_api.js';
2+
import TelegramApi, {
3+
TelegramApiBaseParams,
4+
SendMessageParams,
5+
SendPhotoParams,
6+
SendVideoParams,
7+
SendVoiceParams,
8+
SendChatActionParams,
9+
SendInvoiceParams,
10+
EditMessageTextParams
11+
} from './telegram_api.js';
312
import TelegramBot from './telegram_bot.js';
413

514

@@ -8,6 +17,7 @@ import TelegramBot from './telegram_bot.js';
817

918

1019

20+
1121
/** Class representing the context of execution */
1222
export default class TelegramExecutionContext {
1323
/** Cache for business connection owners */
@@ -213,10 +223,10 @@ export default class TelegramExecutionContext {
213223
/**
214224
* Helper to handle business connection fallbacks
215225
*/
216-
private async withBusinessFallback<T>(
217-
params: any,
218-
apiMethod: (botApi: string, data: any) => Promise<T>
219-
): Promise<T | null> {
226+
private async withBusinessFallback<T extends Partial<TelegramApiBaseParams>>(
227+
params: T,
228+
apiMethod: (botApi: string, data: T) => Promise<Response>
229+
): Promise<Response | null> {
220230
const connectionId = params.business_connection_id?.toString();
221231

222232
if (connectionId) {
@@ -249,7 +259,7 @@ export default class TelegramExecutionContext {
249259
}
250260
}
251261

252-
if (ownerId !== undefined && params.chat_id.toString() === ownerId.toString()) {
262+
if (ownerId !== undefined && params.chat_id?.toString() === ownerId.toString()) {
253263
return null;
254264
}
255265
}
@@ -273,7 +283,7 @@ export default class TelegramExecutionContext {
273283
}
274284

275285
async replyVideo(video: string, options: Record<string, number | string | boolean> = {}) {
276-
const params: any = {
286+
const params: SendVideoParams = {
277287
...options,
278288
chat_id: this.getChatId(),
279289
message_thread_id: this.getThreadId(),
@@ -282,7 +292,7 @@ export default class TelegramExecutionContext {
282292
};
283293

284294
if (this.update_type === 'business_message') {
285-
params.business_connection_id = this.update.business_message?.business_connection_id;
295+
params['business_connection_id'] = this.update.business_message?.business_connection_id;
286296
return await this.withBusinessFallback(params, (api, data) => this.api.sendVideo(api, data));
287297
}
288298

@@ -318,7 +328,7 @@ export default class TelegramExecutionContext {
318328
* @returns Promise with the API response
319329
*/
320330
async replyPhoto(photo: string, caption = '', options: Record<string, number | string | boolean> = {}) {
321-
const params: any = {
331+
const params: SendPhotoParams = {
322332
...options,
323333
chat_id: this.getChatId(),
324334
message_thread_id: this.getThreadId(),
@@ -328,7 +338,7 @@ export default class TelegramExecutionContext {
328338
};
329339

330340
if (this.update_type === 'business_message') {
331-
params.business_connection_id = this.update.business_message?.business_connection_id;
341+
params['business_connection_id'] = this.update.business_message?.business_connection_id;
332342
return await this.withBusinessFallback(params, (api, data) => this.api.sendPhoto(api, data));
333343
}
334344

@@ -354,7 +364,7 @@ export default class TelegramExecutionContext {
354364
* @returns Promise with the API response
355365
*/
356366
async replyVoice(voice: string, caption = '', options: Record<string, number | string | boolean> = {}) {
357-
const params: any = {
367+
const params: SendVoiceParams = {
358368
...options,
359369
chat_id: this.getChatId(),
360370
message_thread_id: this.getThreadId(),
@@ -364,7 +374,7 @@ export default class TelegramExecutionContext {
364374
};
365375

366376
if (this.update_type === 'business_message') {
367-
params.business_connection_id = this.update.business_message?.business_connection_id;
377+
params['business_connection_id'] = this.update.business_message?.business_connection_id;
368378
return await this.withBusinessFallback(params, (api, data) => this.api.sendVoice(api, data));
369379
}
370380

@@ -380,14 +390,14 @@ export default class TelegramExecutionContext {
380390
* @returns Promise with the API response
381391
*/
382392
async sendTyping() {
383-
const params: any = {
393+
const params: SendChatActionParams = {
384394
chat_id: this.getChatId(),
385395
message_thread_id: this.getThreadId(),
386396
action: 'typing',
387397
};
388398

389399
if (this.update_type === 'business_message') {
390-
params.business_connection_id = this.update.business_message?.business_connection_id;
400+
params['business_connection_id'] = this.update.business_message?.business_connection_id;
391401
return await this.withBusinessFallback(params, (api, data) => this.api.sendChatAction(api, data));
392402
}
393403

@@ -487,7 +497,7 @@ export default class TelegramExecutionContext {
487497
const message_id = this.drafts.get(draft_id);
488498

489499
if (message_id) {
490-
const params: any = {
500+
const params: EditMessageTextParams = {
491501
chat_id: this.getChatId(),
492502
message_id,
493503
text: message,
@@ -508,8 +518,8 @@ export default class TelegramExecutionContext {
508518
return await this.answerGuestQueryText(message, parse_mode);
509519
}
510520

511-
const params: any = {
512-
...options,
521+
const params: SendMessageParams = {
522+
...options as unknown as SendMessageParams, // Cast options to any here because options is a broad record
513523
chat_id: this.getChatId(),
514524
message_thread_id: this.getThreadId(),
515525
text: message,
@@ -537,13 +547,6 @@ export default class TelegramExecutionContext {
537547
return response;
538548
}
539549

540-
/**
541-
* Reply to the last message with text
542-
* @param message - text to reply with
543-
* @param parse_mode - one of HTML, MarkdownV2, Markdown, or an empty string for ascii
544-
* @param options - any additional options to pass to sendMessage
545-
* @returns Promise with the API response
546-
*/
547550
async reply(message: string, parse_mode = '', reply = true, options: Record<string, number | string | boolean> = {}) {
548551
if (this.update_type === 'guest_message') {
549552
return await this.answerGuestQueryText(message, parse_mode);
@@ -553,7 +556,7 @@ export default class TelegramExecutionContext {
553556
return await this.replyInline('Response', message, parse_mode);
554557
}
555558

556-
const params: any = {
559+
const params: SendMessageParams = {
557560
...options,
558561
chat_id: this.getChatId(),
559562
message_thread_id: this.getThreadId(),
@@ -566,13 +569,13 @@ export default class TelegramExecutionContext {
566569
}
567570

568571
if (this.update_type === 'business_message') {
569-
params.business_connection_id = this.update.business_message?.business_connection_id;
572+
params['business_connection_id'] = this.update.business_message?.business_connection_id;
570573
return await this.withBusinessFallback(params, (api, data) => this.api.sendMessage(api, data));
571574
}
572575

573576
if (this.update_type === 'callback') {
574577
if (this.update.callback_query?.message?.chat.id) {
575-
params.chat_id = this.update.callback_query.message.chat.id.toString();
578+
params['chat_id'] = this.update.callback_query.message.chat.id.toString();
576579
}
577580
}
578581

@@ -588,8 +591,8 @@ export default class TelegramExecutionContext {
588591
* @returns Promise with the API response
589592
*/
590593
async sendStarsInvoice(title: string, description: string, payload: string, amount: number) {
591-
const params: any = {
592-
chat_id: this.getChatId(),
594+
const params: SendInvoiceParams = {
595+
chat_id: this.getChatId(),
593596
message_thread_id: this.getThreadId(),
594597
title,
595598
description,
@@ -600,7 +603,7 @@ export default class TelegramExecutionContext {
600603
};
601604

602605
if (this.update_type === 'business_message') {
603-
params.business_connection_id = this.update.business_message?.business_connection_id;
606+
params['business_connection_id'] = this.update.business_message?.business_connection_id;
604607
return await this.withBusinessFallback(params, (api, data) => this.api.sendInvoice(api, data));
605608
}
606609

0 commit comments

Comments
 (0)