Skip to content

Commit 2f55e1f

Browse files
committed
update
1 parent 2ce9352 commit 2f55e1f

2 files changed

Lines changed: 37 additions & 25 deletions

File tree

src/telegram_api.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -141,17 +141,26 @@ export default class TelegramApi {
141141
* @param data - data to append to the request
142142
* @returns Request object with the full URL and parameters
143143
*/
144-
getApiUrl(botApi: string, slug: string, data: TelegramApiParams): Request {
145-
const request = new URL(botApi + (slug.startsWith('/') || botApi.endsWith('/') ? '' : '/') + slug);
146-
const params = new URLSearchParams();
147-
148-
for (const [key, value] of Object.entries(data)) {
149-
if (value !== undefined) {
150-
params.append(key, typeof value === 'object' && value !== null ? JSON.stringify(value) : String(value));
144+
getApiUrl(botApi: string, slug: string, data: TelegramApiParams, method: 'GET' | 'POST' = 'GET'): Request {
145+
const baseUrl = botApi + (slug.startsWith('/') || botApi.endsWith('/') ? '' : '/') + slug;
146+
147+
if (method === 'GET') {
148+
const url = new URL(baseUrl);
149+
for (const [key, value] of Object.entries(data)) {
150+
if (value !== undefined) {
151+
url.searchParams.append(key, typeof value === 'object' && value !== null ? JSON.stringify(value) : String(value));
152+
}
151153
}
154+
return new Request(url.toString());
152155
}
153156

154-
return new Request(`${request.toString()}?${params.toString()}`);
157+
return new Request(baseUrl, {
158+
method: 'POST',
159+
headers: {
160+
'Content-Type': 'application/json',
161+
},
162+
body: JSON.stringify(data),
163+
});
155164
}
156165

157166
/**
@@ -206,7 +215,7 @@ export default class TelegramApi {
206215
* @returns Promise with the API response
207216
*/
208217
async sendChatAction(botApi: string, data: SendChatActionParams): Promise<Response> {
209-
const url = this.getApiUrl(botApi, 'sendChatAction', data);
218+
const url = this.getApiUrl(botApi, '', data, 'POST');
210219
return await this.fetchAndLog(url, 'sendChatAction', data);
211220
}
212221

@@ -222,7 +231,7 @@ export default class TelegramApi {
222231
throw new Error('No file_id provided');
223232
}
224233

225-
const url = this.getApiUrl(botApi, 'getFile', data);
234+
const url = this.getApiUrl(botApi, '', data, 'POST');
226235
const response = await this.fetchAndLog(url, 'getFile', data);
227236

228237
const json: { ok: boolean; result?: { file_path: string }; description?: string } = await response.json();
@@ -245,7 +254,7 @@ export default class TelegramApi {
245254
* @returns Promise with the API response
246255
*/
247256
async sendMessage(botApi: string, data: SendMessageParams): Promise<Response> {
248-
const url = this.getApiUrl(botApi, 'sendMessage', data);
257+
const url = this.getApiUrl(botApi, '', data, 'POST');
249258
return await this.fetchAndLog(url, 'sendMessage', data);
250259
}
251260

@@ -256,7 +265,7 @@ export default class TelegramApi {
256265
* @returns Promise with the API response
257266
*/
258267
async sendVideo(botApi: string, data: SendVideoParams): Promise<Response> {
259-
const url = this.getApiUrl(botApi, 'sendVideo', data);
268+
const url = this.getApiUrl(botApi, '', data, 'POST');
260269
return await this.fetchAndLog(url, 'sendVideo', data);
261270
}
262271

@@ -267,7 +276,7 @@ export default class TelegramApi {
267276
* @returns Promise with the API response
268277
*/
269278
async sendPhoto(botApi: string, data: SendPhotoParams): Promise<Response> {
270-
const url = this.getApiUrl(botApi, 'sendPhoto', data);
279+
const url = this.getApiUrl(botApi, '', data, 'POST');
271280
return await this.fetchAndLog(url, 'sendPhoto', data);
272281
}
273282

@@ -278,7 +287,7 @@ export default class TelegramApi {
278287
* @returns Promise with the API response
279288
*/
280289
async sendVoice(botApi: string, data: SendVoiceParams): Promise<Response> {
281-
const url = this.getApiUrl(botApi, 'sendVoice', data);
290+
const url = this.getApiUrl(botApi, '', data, 'POST');
282291
return await this.fetchAndLog(url, 'sendVoice', data);
283292
}
284293

@@ -307,7 +316,7 @@ export default class TelegramApi {
307316
* @returns Promise with the API response
308317
*/
309318
async answerCallback(botApi: string, data: AnswerCallbackParams): Promise<Response> {
310-
const url = this.getApiUrl(botApi, 'answerCallbackQuery', data);
319+
const url = this.getApiUrl(botApi, '', data, 'POST');
311320
return await this.fetchAndLog(url, 'answerCallbackQuery', data);
312321
}
313322

@@ -318,7 +327,7 @@ export default class TelegramApi {
318327
* @returns Promise with the API response
319328
*/
320329
async answerGuestQuery(botApi: string, data: AnswerGuestParams): Promise<Response> {
321-
const url = this.getApiUrl(botApi, 'answerGuestQuery', data);
330+
const url = this.getApiUrl(botApi, '', data, 'POST');
322331
return await this.fetchAndLog(url, 'answerGuestQuery', data);
323332
}
324333

@@ -329,7 +338,7 @@ export default class TelegramApi {
329338
* @returns Promise with the API response
330339
*/
331340
async deleteMessage(botApi: string, data: { chat_id: number | string; message_id: number }): Promise<Response> {
332-
const url = this.getApiUrl(botApi, 'deleteMessage', data);
341+
const url = this.getApiUrl(botApi, '', data, 'POST');
333342
return await this.fetchAndLog(url, 'deleteMessage', data);
334343
}
335344

@@ -343,15 +352,15 @@ export default class TelegramApi {
343352
botApi: string,
344353
data: EditMessageTextParams,
345354
): Promise<Response> {
346-
const url = this.getApiUrl(botApi, 'editMessageText', data);
355+
const url = this.getApiUrl(botApi, '', data, 'POST');
347356
return await this.fetchAndLog(url, 'editMessageText', data);
348357
}
349358

350359
async sendMessageDraft(
351360
botApi: string,
352361
data: SendMessageDraftParams,
353362
): Promise<Response> {
354-
const url = this.getApiUrl(botApi, 'sendMessageDraft', data);
363+
const url = this.getApiUrl(botApi, '', data, 'POST');
355364
return await this.fetchAndLog(url, 'sendMessageDraft', data);
356365
}
357366

@@ -362,7 +371,7 @@ export default class TelegramApi {
362371
* @returns Promise with the API response
363372
*/
364373
async sendInvoice(botApi: string, data: SendInvoiceParams): Promise<Response> {
365-
const url = this.getApiUrl(botApi, 'sendInvoice', data);
374+
const url = this.getApiUrl(botApi, '', data, 'POST');
366375
return await this.fetchAndLog(url, 'sendInvoice', data);
367376
}
368377

@@ -373,7 +382,7 @@ export default class TelegramApi {
373382
* @returns Promise with the API response
374383
*/
375384
async answerPreCheckoutQuery(botApi: string, data: AnswerPreCheckoutParams): Promise<Response> {
376-
const url = this.getApiUrl(botApi, 'answerPreCheckoutQuery', data);
385+
const url = this.getApiUrl(botApi, '', data, 'POST');
377386
return await this.fetchAndLog(url, 'answerPreCheckoutQuery', data);
378387
}
379388

src/telegram_execution_context.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,12 +496,15 @@ export default class TelegramExecutionContext {
496496
options: Record<string, number | string | boolean | object> = {},
497497
finish = false
498498
) {
499-
if (finish) {
500-
return await this.reply(message, parse_mode, true, options as any);
499+
if (this.update_type === 'guest_message') {
500+
if (finish) {
501+
return await this.answerGuestQueryText(message, parse_mode);
502+
}
503+
return null;
501504
}
502505

503-
if (this.update_type === 'guest_message') {
504-
return await this.answerGuestQueryText(message, parse_mode);
506+
if (finish) {
507+
return await this.reply(message, parse_mode, true, options as any);
505508
}
506509

507510
const params: SendMessageDraftParams = {

0 commit comments

Comments
 (0)