Skip to content

Commit ad2f3d9

Browse files
committed
v11.7.0: fix guest mode
1 parent cf6c414 commit ad2f3d9

3 files changed

Lines changed: 79 additions & 13 deletions

File tree

src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import TelegramInlineQueryResult from './types/TelegramInlineQueryResult.js';
3434
import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPhoto.js';
3535
import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultArticle.js';
3636
import TelegramInlineQueryResultVideo from './types/TelegramInlineQueryResultVideo.js';
37+
import TelegramInlineQueryResultVoice from './types/TelegramInlineQueryResultVoice.js';
3738
import ChatPermissions from './types/ChatPermissions.js';
3839
import TelegramBusinessMessage from './types/TelegramBusinessMessage.js';
3940
import TelegramCallbackQuery from './types/TelegramCallbackQuery.js';
@@ -78,6 +79,7 @@ export {
7879
TelegramInlineQueryResultPhoto,
7980
TelegramInlineQueryResultArticle,
8081
TelegramInlineQueryResultVideo,
82+
TelegramInlineQueryResultVoice,
8183
ChatPermissions,
8284
TelegramBusinessMessage,
8385
TelegramCallbackQuery,

src/telegram_execution_context.ts

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultA
44
import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPhoto.js';
55
import TelegramUpdate from './types/TelegramUpdate.js';
66
import TelegramInlineQueryResultVideo from './types/TelegramInlineQueryResultVideo.js';
7+
import TelegramInlineQueryResultVoice from './types/TelegramInlineQueryResultVoice.js';
8+
import TelegramInlineQueryResult from './types/TelegramInlineQueryResult.js';
79

810
/** Class representing the context of execution */
911
export default class TelegramExecutionContext {
@@ -149,19 +151,20 @@ export default class TelegramExecutionContext {
149151
switch (this.update_type) {
150152
case 'voice':
151153
case 'message':
152-
case 'guest_message':
153154
return await this.api.sendVideo(this.bot.api.toString(), {
154155
...options,
155156
chat_id: this.getChatId(),
156157
message_thread_id: this.getThreadId(),
157158
reply_to_message_id: this.getMessageId(),
158159
video,
159160
});
161+
case 'guest_message':
162+
return await this.answerGuestQueryVideo(video);
160163
case 'inline':
161164
return await this.api.answerInline(this.bot.api.toString(), {
162165
...options,
163166
inline_query_id: this.update.inline_query?.id.toString() ?? '',
164-
results: [new TelegramInlineQueryResultVideo(video)],
167+
results: [new TelegramInlineQueryResultVideo({ video })],
165168
});
166169

167170
default:
@@ -190,7 +193,6 @@ export default class TelegramExecutionContext {
190193
case 'voice':
191194
case 'photo':
192195
case 'message':
193-
case 'guest_message':
194196
return await this.api.sendPhoto(this.bot.api.toString(), {
195197
...options,
196198
chat_id: this.getChatId(),
@@ -199,10 +201,12 @@ export default class TelegramExecutionContext {
199201
photo,
200202
caption,
201203
});
204+
case 'guest_message':
205+
return await this.answerGuestQueryPhoto(photo, caption);
202206
case 'inline':
203207
return await this.api.answerInline(this.bot.api.toString(), {
204208
inline_query_id: this.update.inline_query?.id.toString() ?? '',
205-
results: [new TelegramInlineQueryResultPhoto(photo)],
209+
results: [new TelegramInlineQueryResultPhoto({ photo })],
206210
});
207211

208212
default:
@@ -221,7 +225,6 @@ export default class TelegramExecutionContext {
221225
switch (this.update_type) {
222226
case 'voice':
223227
case 'message':
224-
case 'guest_message':
225228
return await this.api.sendVoice(this.bot.api.toString(), {
226229
...options,
227230
chat_id: this.getChatId(),
@@ -230,6 +233,8 @@ export default class TelegramExecutionContext {
230233
voice,
231234
caption,
232235
});
236+
case 'guest_message':
237+
return await this.answerGuestQueryVoice(voice, caption);
233238
default:
234239
return null;
235240
}
@@ -281,17 +286,59 @@ export default class TelegramExecutionContext {
281286

282287
/**
283288
* Answer a guest query
284-
* @param message - text to reply with
285-
* @param parse_mode - one of HTML, MarkdownV2, Markdown, or an empty string for ascii
289+
* @param result - the result to reply with
286290
* @returns Promise with the API response
287291
*/
288-
async answerGuestQuery(message: string, parse_mode = '') {
292+
async answerGuestQuery(result: TelegramInlineQueryResult) {
289293
return await this.api.answerGuestQuery(this.bot.api.toString(), {
290294
guest_query_id: this.update.guest_message?.guest_query_id ?? '',
291-
result: new TelegramInlineQueryResultArticle({ content: message, title: 'Response', parse_mode }),
295+
result,
292296
});
293297
}
294298

299+
/**
300+
* Answer a guest query with text
301+
* @param message - text to reply with
302+
* @param parse_mode - one of HTML, MarkdownV2, Markdown, or an empty string for ascii
303+
* @returns Promise with the API response
304+
*/
305+
async answerGuestQueryText(message: string, parse_mode = '') {
306+
return await this.answerGuestQuery(new TelegramInlineQueryResultArticle({ content: message, title: 'Response', parse_mode }));
307+
}
308+
309+
/**
310+
* Answer a guest query with a photo
311+
* @param photo - url or file_id to photo
312+
* @param caption - photo caption
313+
* @param parse_mode - one of HTML, MarkdownV2, Markdown, or an empty string for ascii
314+
* @returns Promise with the API response
315+
*/
316+
async answerGuestQueryPhoto(photo: string, caption = '', parse_mode = '') {
317+
return await this.answerGuestQuery(new TelegramInlineQueryResultPhoto({ photo, caption, parse_mode }));
318+
}
319+
320+
/**
321+
* Answer a guest query with a video
322+
* @param video - url or file_id to video
323+
* @param caption - video caption
324+
* @param parse_mode - one of HTML, MarkdownV2, Markdown, or an empty string for ascii
325+
* @returns Promise with the API response
326+
*/
327+
async answerGuestQueryVideo(video: string, caption = '', parse_mode = '') {
328+
return await this.answerGuestQuery(new TelegramInlineQueryResultVideo({ video, caption, parse_mode }));
329+
}
330+
331+
/**
332+
* Answer a guest query with a voice message
333+
* @param voice - url or file_id to voice
334+
* @param caption - voice caption
335+
* @param parse_mode - one of HTML, MarkdownV2, Markdown, or an empty string for ascii
336+
* @returns Promise with the API response
337+
*/
338+
async answerGuestQueryVoice(voice: string, caption = '', parse_mode = '') {
339+
return await this.answerGuestQuery(new TelegramInlineQueryResultVoice({ voice, caption, parse_mode }));
340+
}
341+
295342

296343
/** Map of draft IDs to message IDs for streaming */
297344
private drafts = new Map<number, number>();
@@ -321,6 +368,10 @@ export default class TelegramExecutionContext {
321368
});
322369
}
323370

371+
if (this.update_type === 'guest_message') {
372+
return await this.answerGuestQueryText(message, parse_mode);
373+
}
374+
324375
const response = await this.api.sendMessage(this.bot.api.toString(), {
325376
...options,
326377
chat_id: this.getChatId(),
@@ -357,10 +408,6 @@ export default class TelegramExecutionContext {
357408
case 'message':
358409
case 'photo':
359410
case 'document':
360-
case 'guest_message':
361-
if (this.update_type === 'guest_message') {
362-
return await this.answerGuestQuery(message, parse_mode);
363-
}
364411
if (reply) {
365412
return await this.api.sendMessage(this.bot.api.toString(), {
366413
...options,
@@ -378,6 +425,8 @@ export default class TelegramExecutionContext {
378425
text: message,
379426
parse_mode,
380427
});
428+
case 'guest_message':
429+
return await this.answerGuestQueryText(message, parse_mode);
381430
case 'business_message':
382431
return await this.api.sendMessage(this.bot.api.toString(), {
383432
chat_id: this.getChatId(),
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import TelegramInlineQueryResult from './TelegramInlineQueryResult.js';
2+
3+
export default class TelegramInlineQueryResultVoice extends TelegramInlineQueryResult {
4+
voice_url: string;
5+
title: string;
6+
caption?: string;
7+
parse_mode?: string;
8+
constructor(data: { voice: string; title?: string; caption?: string; parse_mode?: string }) {
9+
super('voice');
10+
this.voice_url = data.voice;
11+
this.title = data.title ?? 'Voice';
12+
this.caption = data.caption;
13+
this.parse_mode = data.parse_mode;
14+
}
15+
}

0 commit comments

Comments
 (0)