11import { 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' ;
312import 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 */
1222export 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