@@ -7,193 +7,215 @@ import TelegramInlineQueryResultVideo from './types/TelegramInlineQueryResultVid
77
88/** Class representing the context of execution */
99export default class TelegramExecutionContext {
10- /** an instance of the telegram bot */
11- bot : TelegramBot ;
12- /** an instance of the telegram update */
13- update : TelegramUpdate ;
14- /** string representing the type of update that was sent */
15- update_type = '' ;
16- /** reference to TelegramApi class */
17- api = new TelegramApi ( ) ;
10+ /** an instance of the telegram bot */
11+ bot : TelegramBot ;
12+ /** an instance of the telegram update */
13+ update : TelegramUpdate ;
14+ /** string representing the type of update that was sent */
15+ update_type = '' ;
16+ /** reference to TelegramApi class */
17+ api = new TelegramApi ( ) ;
1818
19- /**
20- * Create a telegram execution context
21- * @param bot - the telegram bot
22- * @param update - the telegram update
23- */
24- constructor ( bot : TelegramBot , update : TelegramUpdate ) {
25- this . bot = bot ;
26- this . update = update ;
19+ /**
20+ * Create a telegram execution context
21+ * @param bot - the telegram bot
22+ * @param update - the telegram update
23+ */
24+ constructor ( bot : TelegramBot , update : TelegramUpdate ) {
25+ this . bot = bot ;
26+ this . update = update ;
2727
28- if ( this . update . message ?. photo ) {
29- this . update_type = 'photo' ;
30- } else if ( this . update . message ?. text ) {
31- this . update_type = 'message' ;
32- } else if ( this . update . inline_query ?. query ) {
33- this . update_type = 'inline' ;
34- } else if ( this . update . message ?. document ) {
35- this . update_type = 'document' ;
36- } else if ( this . update . callback_query ?. id ) {
37- this . update_type = 'callback' ;
38- } else if ( this . update . business_message ) {
39- this . update_type = 'business_message' ;
40- }
41- }
28+ this . update_type = this . determineUpdateType ( ) ;
29+ }
4230
43- /**
44- * Reply to the last message with a video
45- * @param video - string to a video on the internet or a file_id on telegram
46- * @param options - any additional options to pass to sendVideo
47- */
48- async replyVideo ( video : string , options : Record < string , number | string | boolean > = { } ) {
49- switch ( this . update_type ) {
50- case ' message' :
51- return await this . api . sendVideo ( this . bot . api . toString ( ) , {
52- ... options ,
53- chat_id : this . update . message ?. chat . id . toString ( ) ?? '' ,
54- reply_to_message_id : this . update . message ?. message_id . toString ( ) ?? '' ,
55- video ,
56- } ) ;
57- case 'inline' :
58- return await this . api . answerInline ( this . bot . api . toString ( ) , {
59- ... options ,
60- inline_query_id : this . update . inline_query ?. id . toString ( ) ?? '' ,
61- results : [ new TelegramInlineQueryResultVideo ( video ) ] ,
62- } ) ;
31+ /**
32+ * Determine the type of update received
33+ * @returns The update type as a string
34+ */
35+ private determineUpdateType ( ) : string {
36+ if ( this . update . message ?. photo ) {
37+ return 'photo' ;
38+ } else if ( this . update . message ?. text ) {
39+ return 'message' ;
40+ } else if ( this . update . inline_query ?. query ) {
41+ return 'inline' ;
42+ } else if ( this . update . message ?. document ) {
43+ return 'document' ;
44+ } else if ( this . update . callback_query ?. id ) {
45+ return 'callback' ;
46+ } else if ( this . update . business_message ) {
47+ return 'business_message' ;
48+ }
49+ return '' ;
50+ }
6351
64- default :
65- break ;
66- }
67- }
52+ /**
53+ * Get the chat ID from the current update
54+ * @returns The chat ID as a string or empty string if not available
55+ */
56+ private getChatId ( ) : string {
57+ if ( this . update . message ?. chat . id ) {
58+ return this . update . message . chat . id . toString ( ) ;
59+ } else if ( this . update . business_message ?. chat . id ) {
60+ return this . update . business_message . chat . id . toString ( ) ;
61+ }
62+ return '' ;
63+ }
6864
69- /**
70- * Get File from telegram file_id
71- * @param file_id - telegram file_id
72- */
73- async getFile ( file_id : string ) {
74- return await this . api . getFile ( this . bot . api . toString ( ) , { file_id } , this . bot . token ) ;
75- }
65+ /**
66+ * Get the message ID from the current update
67+ * @returns The message ID as a string or empty string if not available
68+ */
69+ private getMessageId ( ) : string {
70+ return this . update . message ?. message_id . toString ( ) ?? '' ;
71+ }
7672
77- /**
78- * Reply to the last message with a photo
79- * @param photo - url or file_id to photo
80- * @param caption - photo caption
81- * @param options - any additional options to pass to sendPhoto
82- */
83- async replyPhoto ( photo : string , caption = '' , options : Record < string , number | string | boolean > = { } ) {
84- switch ( this . update_type ) {
85- case 'photo' :
86- return await this . api . sendPhoto ( this . bot . api . toString ( ) , {
87- ...options ,
88- chat_id : this . update . message ?. chat . id . toString ( ) ?? '' ,
89- reply_to_message_id : this . update . message ?. message_id . toString ( ) ?? '' ,
90- photo,
91- caption,
92- } ) ;
93- case 'message' :
94- return await this . api . sendPhoto ( this . bot . api . toString ( ) , {
95- ...options ,
96- chat_id : this . update . message ?. chat . id . toString ( ) ?? '' ,
97- reply_to_message_id : this . update . message ?. message_id . toString ( ) ?? '' ,
98- photo,
99- caption,
100- } ) ;
101- case 'inline' :
102- return await this . api . answerInline ( this . bot . api . toString ( ) , {
103- inline_query_id : this . update . inline_query ?. id . toString ( ) ?? '' ,
104- results : [ new TelegramInlineQueryResultPhoto ( photo ) ] ,
105- } ) ;
73+ /**
74+ * Reply to the last message with a video
75+ * @param video - string to a video on the internet or a file_id on telegram
76+ * @param options - any additional options to pass to sendVideo
77+ * @returns Promise with the API response
78+ */
79+ async replyVideo ( video : string , options : Record < string , number | string | boolean > = { } ) {
80+ switch ( this . update_type ) {
81+ case 'message' :
82+ return await this . api . sendVideo ( this . bot . api . toString ( ) , {
83+ ...options ,
84+ chat_id : this . getChatId ( ) ,
85+ reply_to_message_id : this . getMessageId ( ) ,
86+ video,
87+ } ) ;
88+ case 'inline' :
89+ return await this . api . answerInline ( this . bot . api . toString ( ) , {
90+ ...options ,
91+ inline_query_id : this . update . inline_query ?. id . toString ( ) ?? '' ,
92+ results : [ new TelegramInlineQueryResultVideo ( video ) ] ,
93+ } ) ;
10694
107- default :
108- break ;
109- }
110- }
95+ default :
96+ return null ;
97+ }
98+ }
11199
112- /**
113- * Send typing in a chat
114- */
115- async sendTyping ( ) {
116- switch ( this . update_type ) {
117- case 'message' :
118- return await this . api . sendChatAction ( this . bot . api . toString ( ) , {
119- chat_id : this . update . message ?. chat . id . toString ( ) ?? '' ,
120- action : 'typing' ,
121- } ) ;
122- case 'business_message' :
123- return await this . api . sendChatAction ( this . bot . api . toString ( ) , {
124- business_connection_id : this . update . business_message ?. business_connection_id . toString ( ) ,
125- chat_id : this . update . business_message ?. chat . id . toString ( ) ?? '' ,
126- action : 'typing' ,
127- } ) ;
128- default :
129- break ;
130- }
131- }
100+ /**
101+ * Get File from telegram file_id
102+ * @param file_id - telegram file_id
103+ * @returns Promise with the file response
104+ */
105+ async getFile ( file_id : string ) {
106+ return await this . api . getFile ( this . bot . api . toString ( ) , { file_id } , this . bot . token ) ;
107+ }
132108
133- /**
134- * Reply to an inline message with a title and content
135- * @param title - title to reply with
136- * @param message - message contents to reply with
137- * @param parse_mode - parse mode to use
138- */
139- async replyInline ( title : string , message : string , parse_mode = '' ) {
140- switch ( this . update_type ) {
141- case 'inline' :
142- return await this . api . answerInline ( this . bot . api . toString ( ) , {
143- inline_query_id : this . update . inline_query ?. id . toString ( ) ?? '' ,
144- results : [ new TelegramInlineQueryResultArticle ( { content : message , title, parse_mode } ) ] ,
145- } ) ;
146- default :
147- break ;
148- }
149- }
109+ /**
110+ * Reply to the last message with a photo
111+ * @param photo - url or file_id to photo
112+ * @param caption - photo caption
113+ * @param options - any additional options to pass to sendPhoto
114+ * @returns Promise with the API response
115+ */
116+ async replyPhoto ( photo : string , caption = '' , options : Record < string , number | string | boolean > = { } ) {
117+ switch ( this . update_type ) {
118+ case 'photo' :
119+ case 'message' :
120+ return await this . api . sendPhoto ( this . bot . api . toString ( ) , {
121+ ...options ,
122+ chat_id : this . getChatId ( ) ,
123+ reply_to_message_id : this . getMessageId ( ) ,
124+ photo,
125+ caption,
126+ } ) ;
127+ case 'inline' :
128+ return await this . api . answerInline ( this . bot . api . toString ( ) , {
129+ inline_query_id : this . update . inline_query ?. id . toString ( ) ?? '' ,
130+ results : [ new TelegramInlineQueryResultPhoto ( photo ) ] ,
131+ } ) ;
150132
151- /**
152- * Reply to the last message with text
153- * @param message - text to reply with
154- * @param parse_mode - one of HTML, MarkdownV2, Markdown, or an empty string for ascii
155- * @param options - any additional options to pass to sendMessage
156- */
157- async reply ( message : string , parse_mode = '' , options : Record < string , number | string | boolean > = { } ) {
158- switch ( this . update_type ) {
159- case 'message' :
160- return await this . api . sendMessage ( this . bot . api . toString ( ) , {
161- ...options ,
162- chat_id : this . update . message ?. chat . id . toString ( ) ?? '' ,
163- reply_to_message_id : this . update . message ?. message_id . toString ( ) ?? '' ,
164- text : message ,
165- parse_mode,
166- } ) ;
167- case 'business_message' :
168- return await this . api . sendMessage ( this . bot . api . toString ( ) , {
169- chat_id : this . update . business_message ?. chat . id . toString ( ) ?? '' ,
170- text : message ,
171- business_connection_id : this . update . business_message ?. business_connection_id . toString ( ) ,
172- parse_mode,
173- } ) ;
174- case 'photo' :
175- return await this . api . sendMessage ( this . bot . api . toString ( ) , {
176- ...options ,
177- chat_id : this . update . message ?. chat . id . toString ( ) ?? '' ,
178- reply_to_message_id : this . update . message ?. message_id . toString ( ) ?? '' ,
179- text : message ,
180- parse_mode,
181- } ) ;
182- case 'inline' :
183- return await this . api . answerInline ( this . bot . api . toString ( ) , {
184- inline_query_id : this . update . inline_query ?. id . toString ( ) ?? '' ,
185- results : [ new TelegramInlineQueryResultArticle ( { title : message , content : message , parse_mode } ) ] ,
186- } ) ;
187- case 'document' :
188- return await this . api . sendMessage ( this . bot . api . toString ( ) , {
189- ...options ,
190- chat_id : this . update . message ?. chat . id . toString ( ) ?? '' ,
191- reply_to_message_id : this . update . message ?. message_id . toString ( ) ?? '' ,
192- text : message ,
193- parse_mode,
194- } ) ;
195- default :
196- break ;
197- }
198- }
133+ default :
134+ return null ;
135+ }
136+ }
137+
138+ /**
139+ * Send typing in a chat
140+ * @returns Promise with the API response
141+ */
142+ async sendTyping ( ) {
143+ switch ( this . update_type ) {
144+ case 'message' :
145+ case 'photo' :
146+ case 'document' :
147+ return await this . api . sendChatAction ( this . bot . api . toString ( ) , {
148+ chat_id : this . getChatId ( ) ,
149+ action : 'typing' ,
150+ } ) ;
151+ case 'business_message' :
152+ return await this . api . sendChatAction ( this . bot . api . toString ( ) , {
153+ business_connection_id : this . update . business_message ?. business_connection_id . toString ( ) ?? '' ,
154+ chat_id : this . getChatId ( ) ,
155+ action : 'typing' ,
156+ } ) ;
157+ default :
158+ return null ;
159+ }
160+ }
161+
162+ /**
163+ * Reply to an inline message with a title and content
164+ * @param title - title to reply with
165+ * @param message - message contents to reply with
166+ * @param parse_mode - parse mode to use
167+ * @returns Promise with the API response
168+ */
169+ async replyInline ( title : string , message : string , parse_mode = '' ) {
170+ if ( this . update_type === 'inline' ) {
171+ return await this . api . answerInline ( this . bot . api . toString ( ) , {
172+ inline_query_id : this . update . inline_query ?. id . toString ( ) ?? '' ,
173+ results : [ new TelegramInlineQueryResultArticle ( { content : message , title, parse_mode } ) ] ,
174+ } ) ;
175+ }
176+ return null ;
177+ }
178+
179+ /**
180+ * Reply to the last message with text
181+ * @param message - text to reply with
182+ * @param parse_mode - one of HTML, MarkdownV2, Markdown, or an empty string for ascii
183+ * @param options - any additional options to pass to sendMessage
184+ * @returns Promise with the API response
185+ */
186+ async reply ( message : string , parse_mode = '' , options : Record < string , number | string | boolean > = { } ) {
187+ switch ( this . update_type ) {
188+ case 'message' :
189+ case 'photo' :
190+ case 'document' :
191+ return await this . api . sendMessage ( this . bot . api . toString ( ) , {
192+ ...options ,
193+ chat_id : this . getChatId ( ) ,
194+ reply_to_message_id : this . getMessageId ( ) ,
195+ text : message ,
196+ parse_mode,
197+ } ) ;
198+ case 'business_message' :
199+ return await this . api . sendMessage ( this . bot . api . toString ( ) , {
200+ chat_id : this . getChatId ( ) ,
201+ text : message ,
202+ business_connection_id : this . update . business_message ?. business_connection_id . toString ( ) ?? '' ,
203+ parse_mode,
204+ } ) ;
205+ case 'callback' :
206+ if ( this . update . callback_query ?. message . chat . id ) {
207+ return await this . api . sendMessage ( this . bot . api . toString ( ) , {
208+ ...options ,
209+ chat_id : this . update . callback_query . message . chat . id . toString ( ) ,
210+ text : message ,
211+ parse_mode,
212+ } ) ;
213+ }
214+ return null ;
215+ case 'inline' :
216+ return await this . replyInline ( 'Response' , message , parse_mode ) ;
217+ default :
218+ return null ;
219+ }
220+ }
199221}
0 commit comments