Skip to content

Commit 779bf40

Browse files
committed
support guest mode
1 parent 216d89c commit 779bf40

14 files changed

Lines changed: 326 additions & 157 deletions

package-lock.json

Lines changed: 208 additions & 98 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"version": "",
2525
"dependencies": {
26-
"@eslint/eslintrc": "^3.3.3"
26+
"@eslint/eslintrc": "^3.3.3",
27+
"wrangler": "^4.90.0"
2728
}
2829
}

packages/main/src/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import TelegramUser from './types/TelegramUser.js';
99
import TelegramMessageEntity from './types/TelegramMessageEntity.js';
1010
import TelegramPhotoSize from './types/TelegramPhotoSize.js';
1111
import TelegramMessage from './types/TelegramMessage.js';
12+
import TelegramGuestMessage from './types/TelegramGuestMessage.js';
1213
import TelegramInputMessageContent from './types/TelegramInputMessageContent.js';
1314
import TelegramInlineQuery from './types/TelegramInlineQuery.js';
1415
import TelegramUpdate from './types/TelegramUpdate.js';
@@ -32,6 +33,7 @@ export {
3233
TelegramMessageEntity,
3334
TelegramPhotoSize,
3435
TelegramMessage,
36+
TelegramGuestMessage,
3537
TelegramInputMessageContent,
3638
TelegramInlineQuery,
3739
TelegramUpdate,

packages/main/src/telegram_api.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ interface AnswerInlineParams {
6969
next_offset?: string;
7070
}
7171

72+
/** Interface for guest query parameters */
73+
interface AnswerGuestParams {
74+
guest_query_id: string;
75+
result: TelegramInlineQueryResultArticle | TelegramInlineQueryResultPhoto | TelegramInlineQueryResultVideo;
76+
}
77+
7278
/** Type for all possible API parameters */
7379
type TelegramApiParams =
7480
| SendMessageParams
@@ -77,6 +83,7 @@ type TelegramApiParams =
7783
| SendChatActionParams
7884
| AnswerCallbackParams
7985
| AnswerInlineParams
86+
| AnswerGuestParams
8087
| Record<string, unknown>;
8188

8289
/** Class representing the Telegram API and all its methods */
@@ -206,6 +213,17 @@ export default class TelegramApi {
206213
return await fetch(url);
207214
}
208215

216+
/**
217+
* Send a guest response to a given botApi
218+
* @param botApi - full URL to the telegram API without slug
219+
* @param data - data to append to the request
220+
* @returns Promise with the API response
221+
*/
222+
async answerGuestQuery(botApi: string, data: AnswerGuestParams): Promise<Response> {
223+
const url = this.getApiUrl(botApi, 'answerGuestQuery', data);
224+
return await fetch(url);
225+
}
226+
209227
/**
210228
* Delete a message
211229
* @param botApi - full URL to the telegram API without slug

packages/main/src/telegram_bot.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ export default class TelegramBot {
7474
return ':callback' in this.commands ? ':callback' : this.defaultCommand;
7575
case 'inline':
7676
return ':inline' in this.commands ? ':inline' : this.defaultCommand;
77+
case 'guest_message':
78+
return ':guest_message' in this.commands ? ':guest_message' : this.defaultCommand;
7779
}
7880

7981
// Then check if it's a command starting with /
@@ -94,7 +96,8 @@ export default class TelegramBot {
9496
switch (ctx.update_type) {
9597
case 'message':
9698
case 'business_message':
97-
return this.update.message?.text?.split(' ') ?? [];
99+
case 'guest_message':
100+
return (this.update.message?.text ?? this.update.guest_message?.text)?.split(' ') ?? [];
98101
case 'inline':
99102
return this.update.inline_query?.query.split(' ') ?? [];
100103
default:

packages/main/src/telegram_execution_context.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ export default class TelegramExecutionContext {
4545
return 'callback';
4646
} else if (this.update.business_message) {
4747
return 'business_message';
48+
} else if (this.update.guest_message) {
49+
return 'guest_message';
4850
}
4951
return '';
5052
}
@@ -58,6 +60,8 @@ export default class TelegramExecutionContext {
5860
return this.update.message.chat.id.toString();
5961
} else if (this.update.business_message?.chat.id) {
6062
return this.update.business_message.chat.id.toString();
63+
} else if (this.update.guest_message?.chat.id) {
64+
return this.update.guest_message.chat.id.toString();
6165
}
6266
return '';
6367
}
@@ -67,7 +71,12 @@ export default class TelegramExecutionContext {
6771
* @returns The message ID as a string or empty string if not available
6872
*/
6973
private getMessageId(): string {
70-
return this.update.message?.message_id.toString() ?? '';
74+
if (this.update.message?.message_id) {
75+
return this.update.message.message_id.toString();
76+
} else if (this.update.guest_message?.message_id) {
77+
return this.update.guest_message.message_id.toString();
78+
}
79+
return '';
7180
}
7281

7382
/**
@@ -79,6 +88,7 @@ export default class TelegramExecutionContext {
7988
async replyVideo(video: string, options: Record<string, number | string | boolean> = {}) {
8089
switch (this.update_type) {
8190
case 'message':
91+
case 'guest_message':
8292
return await this.api.sendVideo(this.bot.api.toString(), {
8393
...options,
8494
chat_id: this.getChatId(),
@@ -117,6 +127,7 @@ export default class TelegramExecutionContext {
117127
switch (this.update_type) {
118128
case 'photo':
119129
case 'message':
130+
case 'guest_message':
120131
return await this.api.sendPhoto(this.bot.api.toString(), {
121132
...options,
122133
chat_id: this.getChatId(),
@@ -144,6 +155,7 @@ export default class TelegramExecutionContext {
144155
case 'message':
145156
case 'photo':
146157
case 'document':
158+
case 'guest_message':
147159
return await this.api.sendChatAction(this.bot.api.toString(), {
148160
chat_id: this.getChatId(),
149161
action: 'typing',
@@ -176,6 +188,19 @@ export default class TelegramExecutionContext {
176188
return null;
177189
}
178190

191+
/**
192+
* Answer a guest query
193+
* @param message - text to reply with
194+
* @param parse_mode - one of HTML, MarkdownV2, Markdown, or an empty string for ascii
195+
* @returns Promise with the API response
196+
*/
197+
async answerGuestQuery(message: string, parse_mode = '') {
198+
return await this.api.answerGuestQuery(this.bot.api.toString(), {
199+
guest_query_id: this.update.guest_message?.guest_query_id ?? '',
200+
result: new TelegramInlineQueryResultArticle({ content: message, title: 'Response', parse_mode }),
201+
});
202+
}
203+
179204

180205
/**
181206
* Reply to the last message with a stream of text
@@ -206,6 +231,10 @@ export default class TelegramExecutionContext {
206231
case 'message':
207232
case 'photo':
208233
case 'document':
234+
case 'guest_message':
235+
if (this.update_type === 'guest_message') {
236+
return await this.answerGuestQuery(message, parse_mode);
237+
}
209238
if (reply) {
210239
return await this.api.sendMessage(this.bot.api.toString(), {
211240
...options,

packages/main/src/types/PartialTelegramUpdate.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import TelegramBusinessMessage from './TelegramBusinessMessage.js';
22
import TelegramInlineQuery from './TelegramInlineQuery.js';
33
import TelegramMessage from './TelegramMessage.js';
4+
import TelegramGuestMessage from './TelegramGuestMessage.js';
45

56
interface PartialTelegramUpdate {
67
update_id?: number;
@@ -10,5 +11,6 @@ interface PartialTelegramUpdate {
1011
edited_channel_post?: TelegramMessage;
1112
inline_query?: TelegramInlineQuery;
1213
business_message?: TelegramBusinessMessage;
14+
guest_message?: TelegramGuestMessage;
1315
}
1416
export default PartialTelegramUpdate;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import TelegramMessage from "./TelegramMessage.js";
2+
3+
export default interface TelegramGuestMessage extends TelegramMessage {
4+
guest_query_id: string;
5+
}

packages/main/src/types/TelegramMessage.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,8 @@ interface TelegramMessage {
5757
// invoice?: TelegramInvoice;
5858
// successful_payment?: TelegramSuccessfulPayment;
5959
connected_website?: string;
60+
guest_bot_caller_user?: TelegramUser;
61+
guest_bot_caller_chat?: TelegramChat;
62+
guest_query_id?: string;
6063
}
6164
export default TelegramMessage;

packages/main/src/types/TelegramUpdate.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import TelegramMessage from './TelegramMessage.js';
33
import PartialTelegramUpdate from './PartialTelegramUpdate.js';
44
import TelegramCallbackQuery from './TelegramCallbackQuery.js';
55
import TelegramBusinessMessage from './TelegramBusinessMessage.js';
6+
import TelegramGuestMessage from './TelegramGuestMessage.js';
67

78
export default class TelegramUpdate {
89
update_id: number;
@@ -14,6 +15,7 @@ export default class TelegramUpdate {
1415
// chosen_inline_result?: TelegramChosenInlineResult;
1516
callback_query?: TelegramCallbackQuery;
1617
business_message?: TelegramBusinessMessage;
18+
guest_message?: TelegramGuestMessage;
1719
// shipping_query?: TelegramShippingQuery;
1820
// pre_checkout_query?: TelegramPreCheckoutQuery;
1921
// poll?: TelegramPoll;
@@ -29,6 +31,7 @@ export default class TelegramUpdate {
2931
this.edited_channel_post = update.edited_channel_post;
3032
this.inline_query = update.inline_query;
3133
this.business_message = update.business_message;
34+
this.guest_message = update.guest_message;
3235
// chosen_inline_result = update.chosen_inline_result;
3336
// callback_query = update.callback_query;
3437
// shipping_query = update.shipping_query;

0 commit comments

Comments
 (0)