@codebam/cf-workers-telegram-bot 9.1.4 → 9.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/main.d.ts +2 -1
- package/dist/telegram_api.d.ts +58 -1
- package/dist/telegram_api.js +81 -25
- package/dist/telegram_bot.js +8 -1
- package/dist/telegram_execution_context.d.ts +32 -1
- package/dist/telegram_execution_context.js +96 -3
- package/dist/types/PartialTelegramUpdate.d.ts +6 -0
- package/dist/types/TelegramGuestMessage.d.ts +4 -0
- package/dist/types/TelegramGuestMessage.js +1 -0
- package/dist/types/TelegramMessage.d.ts +5 -0
- package/dist/types/TelegramPreCheckoutQuery.d.ts +23 -0
- package/dist/types/TelegramPreCheckoutQuery.js +1 -0
- package/dist/types/TelegramSuccessfulPayment.d.ts +22 -0
- package/dist/types/TelegramSuccessfulPayment.js +1 -0
- package/dist/types/TelegramUpdate.d.ts +4 -0
- package/dist/types/TelegramUpdate.js +5 -3
- package/dist/types/TelegramUser.d.ts +1 -0
- package/dist/webhook.js +35 -4
- package/package.json +15 -12
package/dist/main.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ import TelegramUser from './types/TelegramUser.js';
|
|
|
9
9
|
import TelegramMessageEntity from './types/TelegramMessageEntity.js';
|
|
10
10
|
import TelegramPhotoSize from './types/TelegramPhotoSize.js';
|
|
11
11
|
import TelegramMessage from './types/TelegramMessage.js';
|
|
12
|
+
import TelegramGuestMessage from './types/TelegramGuestMessage.js';
|
|
12
13
|
import TelegramInputMessageContent from './types/TelegramInputMessageContent.js';
|
|
13
14
|
import TelegramInlineQuery from './types/TelegramInlineQuery.js';
|
|
14
15
|
import TelegramUpdate from './types/TelegramUpdate.js';
|
|
@@ -19,4 +20,4 @@ import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPho
|
|
|
19
20
|
import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultArticle.js';
|
|
20
21
|
import ChatPermissions from './types/ChatPermissions.js';
|
|
21
22
|
export default TelegramBot;
|
|
22
|
-
export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, TelegramCommand, TelegramFrom, TelegramChat, TelegramUser, TelegramMessageEntity, TelegramPhotoSize, TelegramMessage, TelegramInputMessageContent, TelegramInlineQuery, TelegramUpdate, PartialTelegramUpdate, TelegramInlineQueryType, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, ChatPermissions, };
|
|
23
|
+
export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, TelegramCommand, TelegramFrom, TelegramChat, TelegramUser, TelegramMessageEntity, TelegramPhotoSize, TelegramMessage, TelegramGuestMessage, TelegramInputMessageContent, TelegramInlineQuery, TelegramUpdate, PartialTelegramUpdate, TelegramInlineQueryType, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, ChatPermissions, };
|
package/dist/telegram_api.d.ts
CHANGED
|
@@ -16,6 +16,10 @@ interface SendMessageParams extends TelegramApiBaseParams {
|
|
|
16
16
|
protect_content?: boolean;
|
|
17
17
|
reply_markup?: object;
|
|
18
18
|
}
|
|
19
|
+
/** Interface for message draft parameters */
|
|
20
|
+
interface SendMessageDraftParams extends SendMessageParams {
|
|
21
|
+
draft_id: number;
|
|
22
|
+
}
|
|
19
23
|
/** Interface for photo parameters */
|
|
20
24
|
interface SendPhotoParams extends TelegramApiBaseParams {
|
|
21
25
|
photo: string;
|
|
@@ -56,8 +60,31 @@ interface AnswerInlineParams {
|
|
|
56
60
|
is_personal?: boolean;
|
|
57
61
|
next_offset?: string;
|
|
58
62
|
}
|
|
63
|
+
/** Interface for guest query parameters */
|
|
64
|
+
interface AnswerGuestParams {
|
|
65
|
+
guest_query_id: string;
|
|
66
|
+
result: TelegramInlineQueryResultArticle | TelegramInlineQueryResultPhoto | TelegramInlineQueryResultVideo;
|
|
67
|
+
}
|
|
68
|
+
/** Interface for invoice parameters */
|
|
69
|
+
interface SendInvoiceParams extends TelegramApiBaseParams {
|
|
70
|
+
title: string;
|
|
71
|
+
description: string;
|
|
72
|
+
payload: string;
|
|
73
|
+
provider_token: string;
|
|
74
|
+
currency: string;
|
|
75
|
+
prices: {
|
|
76
|
+
label: string;
|
|
77
|
+
amount: number;
|
|
78
|
+
}[];
|
|
79
|
+
}
|
|
80
|
+
/** Interface for pre-checkout query parameters */
|
|
81
|
+
interface AnswerPreCheckoutParams {
|
|
82
|
+
pre_checkout_query_id: string;
|
|
83
|
+
ok: boolean;
|
|
84
|
+
error_message?: string;
|
|
85
|
+
}
|
|
59
86
|
/** Type for all possible API parameters */
|
|
60
|
-
type TelegramApiParams = SendMessageParams | SendPhotoParams | SendVideoParams | SendChatActionParams | AnswerCallbackParams | AnswerInlineParams | Record<string, unknown>;
|
|
87
|
+
type TelegramApiParams = SendMessageParams | SendPhotoParams | SendVideoParams | SendChatActionParams | AnswerCallbackParams | AnswerInlineParams | AnswerGuestParams | SendInvoiceParams | AnswerPreCheckoutParams | Record<string, unknown>;
|
|
61
88
|
/** Class representing the Telegram API and all its methods */
|
|
62
89
|
export default class TelegramApi {
|
|
63
90
|
/**
|
|
@@ -68,6 +95,14 @@ export default class TelegramApi {
|
|
|
68
95
|
* @returns Request object with the full URL and parameters
|
|
69
96
|
*/
|
|
70
97
|
getApiUrl(botApi: string, slug: string, data: TelegramApiParams): Request;
|
|
98
|
+
/**
|
|
99
|
+
* Fetch a URL and log the response
|
|
100
|
+
* @param url - the URL to fetch
|
|
101
|
+
* @param slug - the API method name
|
|
102
|
+
* @param data - the data sent to the API
|
|
103
|
+
* @returns Promise with the API response
|
|
104
|
+
*/
|
|
105
|
+
private fetchAndLog;
|
|
71
106
|
/**
|
|
72
107
|
* Send a chat action to indicate the bot is doing something
|
|
73
108
|
* @param botApi - full URL to the telegram API without slug
|
|
@@ -120,6 +155,13 @@ export default class TelegramApi {
|
|
|
120
155
|
* @returns Promise with the API response
|
|
121
156
|
*/
|
|
122
157
|
answerCallback(botApi: string, data: AnswerCallbackParams): Promise<Response>;
|
|
158
|
+
/**
|
|
159
|
+
* Send a guest response to a given botApi
|
|
160
|
+
* @param botApi - full URL to the telegram API without slug
|
|
161
|
+
* @param data - data to append to the request
|
|
162
|
+
* @returns Promise with the API response
|
|
163
|
+
*/
|
|
164
|
+
answerGuestQuery(botApi: string, data: AnswerGuestParams): Promise<Response>;
|
|
123
165
|
/**
|
|
124
166
|
* Delete a message
|
|
125
167
|
* @param botApi - full URL to the telegram API without slug
|
|
@@ -145,5 +187,20 @@ export default class TelegramApi {
|
|
|
145
187
|
disable_web_page_preview?: boolean;
|
|
146
188
|
reply_markup?: object;
|
|
147
189
|
}): Promise<Response>;
|
|
190
|
+
sendMessageDraft(botApi: string, data: SendMessageDraftParams): Promise<Response>;
|
|
191
|
+
/**
|
|
192
|
+
* Send an invoice to a user
|
|
193
|
+
* @param botApi - full URL to the telegram API without slug
|
|
194
|
+
* @param data - invoice parameters
|
|
195
|
+
* @returns Promise with the API response
|
|
196
|
+
*/
|
|
197
|
+
sendInvoice(botApi: string, data: SendInvoiceParams): Promise<Response>;
|
|
198
|
+
/**
|
|
199
|
+
* Answer a pre-checkout query
|
|
200
|
+
* @param botApi - full URL to the telegram API without slug
|
|
201
|
+
* @param data - pre-checkout parameters
|
|
202
|
+
* @returns Promise with the API response
|
|
203
|
+
*/
|
|
204
|
+
answerPreCheckoutQuery(botApi: string, data: AnswerPreCheckoutParams): Promise<Response>;
|
|
148
205
|
}
|
|
149
206
|
export {};
|
package/dist/telegram_api.js
CHANGED
|
@@ -17,6 +17,32 @@ export default class TelegramApi {
|
|
|
17
17
|
}
|
|
18
18
|
return new Request(`${request.toString()}?${params.toString()}`);
|
|
19
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Fetch a URL and log the response
|
|
22
|
+
* @param url - the URL to fetch
|
|
23
|
+
* @param slug - the API method name
|
|
24
|
+
* @param data - the data sent to the API
|
|
25
|
+
* @returns Promise with the API response
|
|
26
|
+
*/
|
|
27
|
+
async fetchAndLog(url, slug, data) {
|
|
28
|
+
const response = await fetch(url);
|
|
29
|
+
if (response.status !== 200) {
|
|
30
|
+
throw new Error(`Telegram API error: ${String(response.status)} ${response.statusText}`);
|
|
31
|
+
}
|
|
32
|
+
const cloned = response.clone();
|
|
33
|
+
try {
|
|
34
|
+
const json = await cloned.json();
|
|
35
|
+
console.log({
|
|
36
|
+
method: slug,
|
|
37
|
+
params: data,
|
|
38
|
+
response: json,
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
catch (e) {
|
|
42
|
+
console.error(`Error logging response for ${slug}: ${e instanceof Error ? e.message : String(e)}`);
|
|
43
|
+
}
|
|
44
|
+
return response;
|
|
45
|
+
}
|
|
20
46
|
/**
|
|
21
47
|
* Send a chat action to indicate the bot is doing something
|
|
22
48
|
* @param botApi - full URL to the telegram API without slug
|
|
@@ -25,7 +51,7 @@ export default class TelegramApi {
|
|
|
25
51
|
*/
|
|
26
52
|
async sendChatAction(botApi, data) {
|
|
27
53
|
const url = this.getApiUrl(botApi, 'sendChatAction', data);
|
|
28
|
-
return await
|
|
54
|
+
return await this.fetchAndLog(url, 'sendChatAction', data);
|
|
29
55
|
}
|
|
30
56
|
/**
|
|
31
57
|
* Get a file with a given file_id
|
|
@@ -36,24 +62,19 @@ export default class TelegramApi {
|
|
|
36
62
|
*/
|
|
37
63
|
async getFile(botApi, data, token) {
|
|
38
64
|
if (!data.file_id || data.file_id === '') {
|
|
39
|
-
|
|
65
|
+
throw new Error('No file_id provided');
|
|
40
66
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
const json = await response.json();
|
|
48
|
-
if (!json.ok || !json.result?.file_path) {
|
|
49
|
-
return new Response(json.description ?? 'Failed to get file path', { status: 400 });
|
|
50
|
-
}
|
|
51
|
-
return await fetch(`https://api.telegram.org/file/bot${token}/${json.result.file_path}`);
|
|
67
|
+
const url = this.getApiUrl(botApi, 'getFile', data);
|
|
68
|
+
const response = await this.fetchAndLog(url, 'getFile', data);
|
|
69
|
+
const json = await response.json();
|
|
70
|
+
if (!json.ok || !json.result?.file_path) {
|
|
71
|
+
throw new Error(json.description ?? 'Failed to get file path');
|
|
52
72
|
}
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
73
|
+
const fileResponse = await fetch(`https://api.telegram.org/file/bot${token}/${json.result.file_path}`);
|
|
74
|
+
if (fileResponse.status !== 200) {
|
|
75
|
+
throw new Error(`Telegram File API error: ${String(fileResponse.status)} ${fileResponse.statusText}`);
|
|
56
76
|
}
|
|
77
|
+
return fileResponse;
|
|
57
78
|
}
|
|
58
79
|
/**
|
|
59
80
|
* Send a message to a given botApi
|
|
@@ -63,7 +84,7 @@ export default class TelegramApi {
|
|
|
63
84
|
*/
|
|
64
85
|
async sendMessage(botApi, data) {
|
|
65
86
|
const url = this.getApiUrl(botApi, 'sendMessage', data);
|
|
66
|
-
return await
|
|
87
|
+
return await this.fetchAndLog(url, 'sendMessage', data);
|
|
67
88
|
}
|
|
68
89
|
/**
|
|
69
90
|
* Send a video message to a given botApi
|
|
@@ -73,7 +94,7 @@ export default class TelegramApi {
|
|
|
73
94
|
*/
|
|
74
95
|
async sendVideo(botApi, data) {
|
|
75
96
|
const url = this.getApiUrl(botApi, 'sendVideo', data);
|
|
76
|
-
return await
|
|
97
|
+
return await this.fetchAndLog(url, 'sendVideo', data);
|
|
77
98
|
}
|
|
78
99
|
/**
|
|
79
100
|
* Send a photo message to a given botApi
|
|
@@ -83,7 +104,7 @@ export default class TelegramApi {
|
|
|
83
104
|
*/
|
|
84
105
|
async sendPhoto(botApi, data) {
|
|
85
106
|
const url = this.getApiUrl(botApi, 'sendPhoto', data);
|
|
86
|
-
return await
|
|
107
|
+
return await this.fetchAndLog(url, 'sendPhoto', data);
|
|
87
108
|
}
|
|
88
109
|
/**
|
|
89
110
|
* Send an inline response to a given botApi
|
|
@@ -92,14 +113,15 @@ export default class TelegramApi {
|
|
|
92
113
|
* @returns Promise with the API response
|
|
93
114
|
*/
|
|
94
115
|
async answerInline(botApi, data) {
|
|
95
|
-
const
|
|
116
|
+
const params = {
|
|
96
117
|
inline_query_id: data.inline_query_id,
|
|
97
118
|
results: data.results,
|
|
98
119
|
cache_time: data.cache_time,
|
|
99
120
|
is_personal: data.is_personal,
|
|
100
121
|
next_offset: data.next_offset,
|
|
101
|
-
}
|
|
102
|
-
|
|
122
|
+
};
|
|
123
|
+
const url = this.getApiUrl(botApi, 'answerInlineQuery', params);
|
|
124
|
+
return await this.fetchAndLog(url, 'answerInlineQuery', params);
|
|
103
125
|
}
|
|
104
126
|
/**
|
|
105
127
|
* Send a callback response to a given botApi
|
|
@@ -109,7 +131,17 @@ export default class TelegramApi {
|
|
|
109
131
|
*/
|
|
110
132
|
async answerCallback(botApi, data) {
|
|
111
133
|
const url = this.getApiUrl(botApi, 'answerCallbackQuery', data);
|
|
112
|
-
return await
|
|
134
|
+
return await this.fetchAndLog(url, 'answerCallbackQuery', data);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Send a guest response to a given botApi
|
|
138
|
+
* @param botApi - full URL to the telegram API without slug
|
|
139
|
+
* @param data - data to append to the request
|
|
140
|
+
* @returns Promise with the API response
|
|
141
|
+
*/
|
|
142
|
+
async answerGuestQuery(botApi, data) {
|
|
143
|
+
const url = this.getApiUrl(botApi, 'answerGuestQuery', data);
|
|
144
|
+
return await this.fetchAndLog(url, 'answerGuestQuery', data);
|
|
113
145
|
}
|
|
114
146
|
/**
|
|
115
147
|
* Delete a message
|
|
@@ -119,7 +151,7 @@ export default class TelegramApi {
|
|
|
119
151
|
*/
|
|
120
152
|
async deleteMessage(botApi, data) {
|
|
121
153
|
const url = this.getApiUrl(botApi, 'deleteMessage', data);
|
|
122
|
-
return await
|
|
154
|
+
return await this.fetchAndLog(url, 'deleteMessage', data);
|
|
123
155
|
}
|
|
124
156
|
/**
|
|
125
157
|
* Edit a message text
|
|
@@ -129,6 +161,30 @@ export default class TelegramApi {
|
|
|
129
161
|
*/
|
|
130
162
|
async editMessageText(botApi, data) {
|
|
131
163
|
const url = this.getApiUrl(botApi, 'editMessageText', data);
|
|
132
|
-
return await
|
|
164
|
+
return await this.fetchAndLog(url, 'editMessageText', data);
|
|
165
|
+
}
|
|
166
|
+
async sendMessageDraft(botApi, data) {
|
|
167
|
+
const url = this.getApiUrl(botApi, 'sendMessageDraft', data);
|
|
168
|
+
return await this.fetchAndLog(url, 'sendMessageDraft', data);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Send an invoice to a user
|
|
172
|
+
* @param botApi - full URL to the telegram API without slug
|
|
173
|
+
* @param data - invoice parameters
|
|
174
|
+
* @returns Promise with the API response
|
|
175
|
+
*/
|
|
176
|
+
async sendInvoice(botApi, data) {
|
|
177
|
+
const url = this.getApiUrl(botApi, 'sendInvoice', data);
|
|
178
|
+
return await this.fetchAndLog(url, 'sendInvoice', data);
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Answer a pre-checkout query
|
|
182
|
+
* @param botApi - full URL to the telegram API without slug
|
|
183
|
+
* @param data - pre-checkout parameters
|
|
184
|
+
* @returns Promise with the API response
|
|
185
|
+
*/
|
|
186
|
+
async answerPreCheckoutQuery(botApi, data) {
|
|
187
|
+
const url = this.getApiUrl(botApi, 'answerPreCheckoutQuery', data);
|
|
188
|
+
return await this.fetchAndLog(url, 'answerPreCheckoutQuery', data);
|
|
133
189
|
}
|
|
134
190
|
}
|
package/dist/telegram_bot.js
CHANGED
|
@@ -67,6 +67,12 @@ export default class TelegramBot {
|
|
|
67
67
|
return ':callback' in this.commands ? ':callback' : this.defaultCommand;
|
|
68
68
|
case 'inline':
|
|
69
69
|
return ':inline' in this.commands ? ':inline' : this.defaultCommand;
|
|
70
|
+
case 'guest_message':
|
|
71
|
+
return ':guest_message' in this.commands ? ':guest_message' : this.defaultCommand;
|
|
72
|
+
case 'pre_checkout_query':
|
|
73
|
+
return ':pre_checkout_query' in this.commands ? ':pre_checkout_query' : this.defaultCommand;
|
|
74
|
+
case 'successful_payment':
|
|
75
|
+
return ':successful_payment' in this.commands ? ':successful_payment' : this.defaultCommand;
|
|
70
76
|
}
|
|
71
77
|
// Then check if it's a command starting with /
|
|
72
78
|
if (args[0].startsWith('/')) {
|
|
@@ -84,7 +90,8 @@ export default class TelegramBot {
|
|
|
84
90
|
switch (ctx.update_type) {
|
|
85
91
|
case 'message':
|
|
86
92
|
case 'business_message':
|
|
87
|
-
|
|
93
|
+
case 'guest_message':
|
|
94
|
+
return (this.update.message?.text ?? this.update.guest_message?.text)?.split(' ') ?? [];
|
|
88
95
|
case 'inline':
|
|
89
96
|
return this.update.inline_query?.query.split(' ') ?? [];
|
|
90
97
|
default:
|
|
@@ -66,6 +66,21 @@ export default class TelegramExecutionContext {
|
|
|
66
66
|
* @returns Promise with the API response
|
|
67
67
|
*/
|
|
68
68
|
replyInline(title: string, message: string, parse_mode?: string): Promise<Response | null>;
|
|
69
|
+
/**
|
|
70
|
+
* Answer a guest query
|
|
71
|
+
* @param message - text to reply with
|
|
72
|
+
* @param parse_mode - one of HTML, MarkdownV2, Markdown, or an empty string for ascii
|
|
73
|
+
* @returns Promise with the API response
|
|
74
|
+
*/
|
|
75
|
+
answerGuestQuery(message: string, parse_mode?: string): Promise<Response>;
|
|
76
|
+
/**
|
|
77
|
+
* Reply to the last message with a stream of text
|
|
78
|
+
* @param message - text to reply with
|
|
79
|
+
* @param parse_mode - one of HTML, MarkdownV2, Markdown, or an empty string for ascii
|
|
80
|
+
* @param options - any additional options to pass to sendMessage
|
|
81
|
+
* @returns Promise with the API response
|
|
82
|
+
*/
|
|
83
|
+
streamReply(message: string, draft_id: number, parse_mode?: string, options?: Record<string, number | string | boolean>): Promise<Response>;
|
|
69
84
|
/**
|
|
70
85
|
* Reply to the last message with text
|
|
71
86
|
* @param message - text to reply with
|
|
@@ -73,5 +88,21 @@ export default class TelegramExecutionContext {
|
|
|
73
88
|
* @param options - any additional options to pass to sendMessage
|
|
74
89
|
* @returns Promise with the API response
|
|
75
90
|
*/
|
|
76
|
-
reply(message: string, parse_mode?: string, options?: Record<string, number | string | boolean>): Promise<Response | null>;
|
|
91
|
+
reply(message: string, parse_mode?: string, reply?: boolean, options?: Record<string, number | string | boolean>): Promise<Response | null>;
|
|
92
|
+
/**
|
|
93
|
+
* Send an invoice for Telegram Stars
|
|
94
|
+
* @param title - product name
|
|
95
|
+
* @param description - product description
|
|
96
|
+
* @param payload - bot-defined invoice payload
|
|
97
|
+
* @param amount - amount of stars
|
|
98
|
+
* @returns Promise with the API response
|
|
99
|
+
*/
|
|
100
|
+
sendStarsInvoice(title: string, description: string, payload: string, amount: number): Promise<Response>;
|
|
101
|
+
/**
|
|
102
|
+
* Answer a pre-checkout query
|
|
103
|
+
* @param ok - whether the payment can proceed
|
|
104
|
+
* @param error_message - error message if not ok
|
|
105
|
+
* @returns Promise with the API response
|
|
106
|
+
*/
|
|
107
|
+
answerPreCheckoutQuery(ok: boolean, error_message?: string): Promise<Response>;
|
|
77
108
|
}
|
|
@@ -45,6 +45,15 @@ export default class TelegramExecutionContext {
|
|
|
45
45
|
else if (this.update.business_message) {
|
|
46
46
|
return 'business_message';
|
|
47
47
|
}
|
|
48
|
+
else if (this.update.guest_message) {
|
|
49
|
+
return 'guest_message';
|
|
50
|
+
}
|
|
51
|
+
else if (this.update.pre_checkout_query) {
|
|
52
|
+
return 'pre_checkout_query';
|
|
53
|
+
}
|
|
54
|
+
else if (this.update.message?.successful_payment) {
|
|
55
|
+
return 'successful_payment';
|
|
56
|
+
}
|
|
48
57
|
return '';
|
|
49
58
|
}
|
|
50
59
|
/**
|
|
@@ -58,6 +67,9 @@ export default class TelegramExecutionContext {
|
|
|
58
67
|
else if (this.update.business_message?.chat.id) {
|
|
59
68
|
return this.update.business_message.chat.id.toString();
|
|
60
69
|
}
|
|
70
|
+
else if (this.update.guest_message?.chat.id) {
|
|
71
|
+
return this.update.guest_message.chat.id.toString();
|
|
72
|
+
}
|
|
61
73
|
return '';
|
|
62
74
|
}
|
|
63
75
|
/**
|
|
@@ -65,7 +77,13 @@ export default class TelegramExecutionContext {
|
|
|
65
77
|
* @returns The message ID as a string or empty string if not available
|
|
66
78
|
*/
|
|
67
79
|
getMessageId() {
|
|
68
|
-
|
|
80
|
+
if (this.update.message?.message_id) {
|
|
81
|
+
return this.update.message.message_id.toString();
|
|
82
|
+
}
|
|
83
|
+
else if (this.update.guest_message?.message_id) {
|
|
84
|
+
return this.update.guest_message.message_id.toString();
|
|
85
|
+
}
|
|
86
|
+
return '';
|
|
69
87
|
}
|
|
70
88
|
/**
|
|
71
89
|
* Reply to the last message with a video
|
|
@@ -76,6 +94,7 @@ export default class TelegramExecutionContext {
|
|
|
76
94
|
async replyVideo(video, options = {}) {
|
|
77
95
|
switch (this.update_type) {
|
|
78
96
|
case 'message':
|
|
97
|
+
case 'guest_message':
|
|
79
98
|
return await this.api.sendVideo(this.bot.api.toString(), {
|
|
80
99
|
...options,
|
|
81
100
|
chat_id: this.getChatId(),
|
|
@@ -111,6 +130,7 @@ export default class TelegramExecutionContext {
|
|
|
111
130
|
switch (this.update_type) {
|
|
112
131
|
case 'photo':
|
|
113
132
|
case 'message':
|
|
133
|
+
case 'guest_message':
|
|
114
134
|
return await this.api.sendPhoto(this.bot.api.toString(), {
|
|
115
135
|
...options,
|
|
116
136
|
chat_id: this.getChatId(),
|
|
@@ -136,6 +156,7 @@ export default class TelegramExecutionContext {
|
|
|
136
156
|
case 'message':
|
|
137
157
|
case 'photo':
|
|
138
158
|
case 'document':
|
|
159
|
+
case 'guest_message':
|
|
139
160
|
return await this.api.sendChatAction(this.bot.api.toString(), {
|
|
140
161
|
chat_id: this.getChatId(),
|
|
141
162
|
action: 'typing',
|
|
@@ -166,6 +187,34 @@ export default class TelegramExecutionContext {
|
|
|
166
187
|
}
|
|
167
188
|
return null;
|
|
168
189
|
}
|
|
190
|
+
/**
|
|
191
|
+
* Answer a guest query
|
|
192
|
+
* @param message - text to reply with
|
|
193
|
+
* @param parse_mode - one of HTML, MarkdownV2, Markdown, or an empty string for ascii
|
|
194
|
+
* @returns Promise with the API response
|
|
195
|
+
*/
|
|
196
|
+
async answerGuestQuery(message, parse_mode = '') {
|
|
197
|
+
return await this.api.answerGuestQuery(this.bot.api.toString(), {
|
|
198
|
+
guest_query_id: this.update.guest_message?.guest_query_id ?? '',
|
|
199
|
+
result: new TelegramInlineQueryResultArticle({ content: message, title: 'Response', parse_mode }),
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Reply to the last message with a stream of text
|
|
204
|
+
* @param message - text to reply with
|
|
205
|
+
* @param parse_mode - one of HTML, MarkdownV2, Markdown, or an empty string for ascii
|
|
206
|
+
* @param options - any additional options to pass to sendMessage
|
|
207
|
+
* @returns Promise with the API response
|
|
208
|
+
*/
|
|
209
|
+
async streamReply(message, draft_id, parse_mode = '', options = {}) {
|
|
210
|
+
return await this.api.sendMessageDraft(this.bot.api.toString(), {
|
|
211
|
+
...options,
|
|
212
|
+
chat_id: this.getChatId(),
|
|
213
|
+
text: message,
|
|
214
|
+
parse_mode,
|
|
215
|
+
draft_id,
|
|
216
|
+
});
|
|
217
|
+
}
|
|
169
218
|
/**
|
|
170
219
|
* Reply to the last message with text
|
|
171
220
|
* @param message - text to reply with
|
|
@@ -173,15 +222,27 @@ export default class TelegramExecutionContext {
|
|
|
173
222
|
* @param options - any additional options to pass to sendMessage
|
|
174
223
|
* @returns Promise with the API response
|
|
175
224
|
*/
|
|
176
|
-
async reply(message, parse_mode = '', options = {}) {
|
|
225
|
+
async reply(message, parse_mode = '', reply = true, options = {}) {
|
|
177
226
|
switch (this.update_type) {
|
|
178
227
|
case 'message':
|
|
179
228
|
case 'photo':
|
|
180
229
|
case 'document':
|
|
230
|
+
case 'guest_message':
|
|
231
|
+
if (this.update_type === 'guest_message') {
|
|
232
|
+
return await this.answerGuestQuery(message, parse_mode);
|
|
233
|
+
}
|
|
234
|
+
if (reply) {
|
|
235
|
+
return await this.api.sendMessage(this.bot.api.toString(), {
|
|
236
|
+
...options,
|
|
237
|
+
chat_id: this.getChatId(),
|
|
238
|
+
reply_to_message_id: this.getMessageId(),
|
|
239
|
+
text: message,
|
|
240
|
+
parse_mode,
|
|
241
|
+
});
|
|
242
|
+
}
|
|
181
243
|
return await this.api.sendMessage(this.bot.api.toString(), {
|
|
182
244
|
...options,
|
|
183
245
|
chat_id: this.getChatId(),
|
|
184
|
-
reply_to_message_id: this.getMessageId(),
|
|
185
246
|
text: message,
|
|
186
247
|
parse_mode,
|
|
187
248
|
});
|
|
@@ -208,4 +269,36 @@ export default class TelegramExecutionContext {
|
|
|
208
269
|
return null;
|
|
209
270
|
}
|
|
210
271
|
}
|
|
272
|
+
/**
|
|
273
|
+
* Send an invoice for Telegram Stars
|
|
274
|
+
* @param title - product name
|
|
275
|
+
* @param description - product description
|
|
276
|
+
* @param payload - bot-defined invoice payload
|
|
277
|
+
* @param amount - amount of stars
|
|
278
|
+
* @returns Promise with the API response
|
|
279
|
+
*/
|
|
280
|
+
async sendStarsInvoice(title, description, payload, amount) {
|
|
281
|
+
return await this.api.sendInvoice(this.bot.api.toString(), {
|
|
282
|
+
chat_id: this.getChatId(),
|
|
283
|
+
title,
|
|
284
|
+
description,
|
|
285
|
+
payload,
|
|
286
|
+
provider_token: '',
|
|
287
|
+
currency: 'XTR',
|
|
288
|
+
prices: [{ label: title, amount }],
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Answer a pre-checkout query
|
|
293
|
+
* @param ok - whether the payment can proceed
|
|
294
|
+
* @param error_message - error message if not ok
|
|
295
|
+
* @returns Promise with the API response
|
|
296
|
+
*/
|
|
297
|
+
async answerPreCheckoutQuery(ok, error_message) {
|
|
298
|
+
return await this.api.answerPreCheckoutQuery(this.bot.api.toString(), {
|
|
299
|
+
pre_checkout_query_id: this.update.pre_checkout_query?.id ?? '',
|
|
300
|
+
ok,
|
|
301
|
+
error_message,
|
|
302
|
+
});
|
|
303
|
+
}
|
|
211
304
|
}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import TelegramBusinessMessage from './TelegramBusinessMessage.js';
|
|
2
2
|
import TelegramInlineQuery from './TelegramInlineQuery.js';
|
|
3
3
|
import TelegramMessage from './TelegramMessage.js';
|
|
4
|
+
import TelegramGuestMessage from './TelegramGuestMessage.js';
|
|
5
|
+
import TelegramPreCheckoutQuery from './TelegramPreCheckoutQuery.js';
|
|
6
|
+
import TelegramCallbackQuery from './TelegramCallbackQuery.js';
|
|
4
7
|
interface PartialTelegramUpdate {
|
|
5
8
|
update_id?: number;
|
|
6
9
|
message?: TelegramMessage;
|
|
@@ -9,5 +12,8 @@ interface PartialTelegramUpdate {
|
|
|
9
12
|
edited_channel_post?: TelegramMessage;
|
|
10
13
|
inline_query?: TelegramInlineQuery;
|
|
11
14
|
business_message?: TelegramBusinessMessage;
|
|
15
|
+
guest_message?: TelegramGuestMessage;
|
|
16
|
+
pre_checkout_query?: TelegramPreCheckoutQuery;
|
|
17
|
+
callback_query?: TelegramCallbackQuery;
|
|
12
18
|
}
|
|
13
19
|
export default PartialTelegramUpdate;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -4,6 +4,7 @@ import TelegramFrom from './TelegramFrom.js';
|
|
|
4
4
|
import TelegramMessageEntity from './TelegramMessageEntity.js';
|
|
5
5
|
import TelegramPhotoSize from './TelegramPhotoSize.js';
|
|
6
6
|
import TelegramUser from './TelegramUser.js';
|
|
7
|
+
import TelegramSuccessfulPayment from './TelegramSuccessfulPayment.js';
|
|
7
8
|
interface TelegramMessage {
|
|
8
9
|
message_id: number;
|
|
9
10
|
from: TelegramFrom;
|
|
@@ -40,6 +41,10 @@ interface TelegramMessage {
|
|
|
40
41
|
migrate_to_chat_id?: number;
|
|
41
42
|
migrate_from_chat_id?: number;
|
|
42
43
|
pinned_message?: TelegramMessage;
|
|
44
|
+
successful_payment?: TelegramSuccessfulPayment;
|
|
43
45
|
connected_website?: string;
|
|
46
|
+
guest_bot_caller_user?: TelegramUser;
|
|
47
|
+
guest_bot_caller_chat?: TelegramChat;
|
|
48
|
+
guest_query_id?: string;
|
|
44
49
|
}
|
|
45
50
|
export default TelegramMessage;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import TelegramUser from './TelegramUser.js';
|
|
2
|
+
interface TelegramPreCheckoutQuery {
|
|
3
|
+
id: string;
|
|
4
|
+
from: TelegramUser;
|
|
5
|
+
currency: string;
|
|
6
|
+
total_amount: number;
|
|
7
|
+
invoice_payload: string;
|
|
8
|
+
shipping_option_id?: string;
|
|
9
|
+
order_info?: {
|
|
10
|
+
name?: string;
|
|
11
|
+
phone_number?: string;
|
|
12
|
+
email?: string;
|
|
13
|
+
shipping_address?: {
|
|
14
|
+
country_code: string;
|
|
15
|
+
state: string;
|
|
16
|
+
city: string;
|
|
17
|
+
street_line1: string;
|
|
18
|
+
street_line2: string;
|
|
19
|
+
post_code: string;
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export default TelegramPreCheckoutQuery;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
interface TelegramSuccessfulPayment {
|
|
2
|
+
currency: string;
|
|
3
|
+
total_amount: number;
|
|
4
|
+
invoice_payload: string;
|
|
5
|
+
shipping_option_id?: string;
|
|
6
|
+
order_info?: {
|
|
7
|
+
name?: string;
|
|
8
|
+
phone_number?: string;
|
|
9
|
+
email?: string;
|
|
10
|
+
shipping_address?: {
|
|
11
|
+
country_code: string;
|
|
12
|
+
state: string;
|
|
13
|
+
city: string;
|
|
14
|
+
street_line1: string;
|
|
15
|
+
street_line2: string;
|
|
16
|
+
post_code: string;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
telegram_payment_charge_id: string;
|
|
20
|
+
provider_payment_charge_id: string;
|
|
21
|
+
}
|
|
22
|
+
export default TelegramSuccessfulPayment;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -3,6 +3,8 @@ import TelegramMessage from './TelegramMessage.js';
|
|
|
3
3
|
import PartialTelegramUpdate from './PartialTelegramUpdate.js';
|
|
4
4
|
import TelegramCallbackQuery from './TelegramCallbackQuery.js';
|
|
5
5
|
import TelegramBusinessMessage from './TelegramBusinessMessage.js';
|
|
6
|
+
import TelegramGuestMessage from './TelegramGuestMessage.js';
|
|
7
|
+
import TelegramPreCheckoutQuery from './TelegramPreCheckoutQuery.js';
|
|
6
8
|
export default class TelegramUpdate {
|
|
7
9
|
update_id: number;
|
|
8
10
|
message?: TelegramMessage;
|
|
@@ -12,5 +14,7 @@ export default class TelegramUpdate {
|
|
|
12
14
|
inline_query?: TelegramInlineQuery;
|
|
13
15
|
callback_query?: TelegramCallbackQuery;
|
|
14
16
|
business_message?: TelegramBusinessMessage;
|
|
17
|
+
guest_message?: TelegramGuestMessage;
|
|
18
|
+
pre_checkout_query?: TelegramPreCheckoutQuery;
|
|
15
19
|
constructor(update: PartialTelegramUpdate);
|
|
16
20
|
}
|
|
@@ -8,8 +8,9 @@ export default class TelegramUpdate {
|
|
|
8
8
|
// chosen_inline_result?: TelegramChosenInlineResult;
|
|
9
9
|
callback_query;
|
|
10
10
|
business_message;
|
|
11
|
+
guest_message;
|
|
11
12
|
// shipping_query?: TelegramShippingQuery;
|
|
12
|
-
|
|
13
|
+
pre_checkout_query;
|
|
13
14
|
// poll?: TelegramPoll;
|
|
14
15
|
// poll_answer?: TelegramPollAnswer;
|
|
15
16
|
// my_chat_member?: TelegramChatMemberUpdated;
|
|
@@ -23,10 +24,11 @@ export default class TelegramUpdate {
|
|
|
23
24
|
this.edited_channel_post = update.edited_channel_post;
|
|
24
25
|
this.inline_query = update.inline_query;
|
|
25
26
|
this.business_message = update.business_message;
|
|
27
|
+
this.guest_message = update.guest_message;
|
|
26
28
|
// chosen_inline_result = update.chosen_inline_result;
|
|
27
|
-
|
|
29
|
+
this.callback_query = update.callback_query;
|
|
28
30
|
// shipping_query = update.shipping_query;
|
|
29
|
-
|
|
31
|
+
this.pre_checkout_query = update.pre_checkout_query;
|
|
30
32
|
// poll = update.poll;
|
|
31
33
|
// poll_answer = update.poll_answer;
|
|
32
34
|
// my_chat_member = update.my_chat_member;
|
package/dist/webhook.js
CHANGED
|
@@ -29,12 +29,28 @@ export default class Webhook {
|
|
|
29
29
|
// Configure webhook parameters
|
|
30
30
|
const params = new URLSearchParams({
|
|
31
31
|
url: this.webhook.toString(),
|
|
32
|
-
max_connections: '
|
|
33
|
-
allowed_updates: JSON.stringify(['message', 'inline_query', 'business_message', 'business_connection']),
|
|
32
|
+
max_connections: '40',
|
|
33
|
+
allowed_updates: JSON.stringify(['message', 'inline_query', 'guest_message', 'business_message', 'business_connection']),
|
|
34
34
|
drop_pending_updates: 'true',
|
|
35
35
|
});
|
|
36
36
|
try {
|
|
37
|
-
|
|
37
|
+
const response = await fetch(`${url.toString()}?${params.toString()}`);
|
|
38
|
+
if (response.status !== 200) {
|
|
39
|
+
throw new Error(`Telegram API error (setWebhook): ${String(response.status)} ${response.statusText}`);
|
|
40
|
+
}
|
|
41
|
+
const cloned = response.clone();
|
|
42
|
+
try {
|
|
43
|
+
const json = await cloned.json();
|
|
44
|
+
console.log({
|
|
45
|
+
method: 'setWebhook',
|
|
46
|
+
params: Object.fromEntries(params),
|
|
47
|
+
response: json,
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
catch (e) {
|
|
51
|
+
console.error(`Error logging response for setWebhook: ${e instanceof Error ? e.message : String(e)}`);
|
|
52
|
+
}
|
|
53
|
+
return response;
|
|
38
54
|
}
|
|
39
55
|
catch (error) {
|
|
40
56
|
console.error('Failed to set webhook:', error);
|
|
@@ -51,7 +67,22 @@ export default class Webhook {
|
|
|
51
67
|
const baseUrl = this.api.toString();
|
|
52
68
|
const url = new URL(`${baseUrl}${baseUrl.endsWith('/') ? '' : '/'}deleteWebhook`);
|
|
53
69
|
try {
|
|
54
|
-
|
|
70
|
+
const response = await fetch(url.toString());
|
|
71
|
+
if (response.status !== 200) {
|
|
72
|
+
throw new Error(`Telegram API error (deleteWebhook): ${String(response.status)} ${response.statusText}`);
|
|
73
|
+
}
|
|
74
|
+
const cloned = response.clone();
|
|
75
|
+
try {
|
|
76
|
+
const json = await cloned.json();
|
|
77
|
+
console.log({
|
|
78
|
+
method: 'deleteWebhook',
|
|
79
|
+
response: json,
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
catch (e) {
|
|
83
|
+
console.error(`Error logging response for deleteWebhook: ${e instanceof Error ? e.message : String(e)}`);
|
|
84
|
+
}
|
|
85
|
+
return response;
|
|
55
86
|
}
|
|
56
87
|
catch (error) {
|
|
57
88
|
console.error('Failed to delete webhook:', error);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codebam/cf-workers-telegram-bot",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.4.0",
|
|
4
4
|
"description": "serverless telegram bot on cf workers",
|
|
5
5
|
"main": "./dist/main.js",
|
|
6
6
|
"module": "./dist/main.js",
|
|
@@ -19,6 +19,9 @@
|
|
|
19
19
|
],
|
|
20
20
|
"type": "module",
|
|
21
21
|
"private": false,
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
},
|
|
22
25
|
"scripts": {
|
|
23
26
|
"build": "tsc --project tsconfig.json",
|
|
24
27
|
"lint": "eslint src",
|
|
@@ -31,19 +34,19 @@
|
|
|
31
34
|
"url": "https://github.com/codebam/cf-workers-telegram-bot.git"
|
|
32
35
|
},
|
|
33
36
|
"devDependencies": {
|
|
34
|
-
"@cloudflare/workers-types": "^4.
|
|
37
|
+
"@cloudflare/workers-types": "^4.20260510.1",
|
|
35
38
|
"@eslint/js": "^10.0.1",
|
|
36
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
37
|
-
"@typescript-eslint/parser": "^8.
|
|
38
|
-
"eslint": "^10.
|
|
39
|
+
"@typescript-eslint/eslint-plugin": "^8.59.2",
|
|
40
|
+
"@typescript-eslint/parser": "^8.59.2",
|
|
41
|
+
"eslint": "^10.3.0",
|
|
39
42
|
"eslint-config-prettier": "^10.1.8",
|
|
40
|
-
"globals": "^17.
|
|
41
|
-
"prettier": "^3.8.
|
|
42
|
-
"typescript": "^
|
|
43
|
-
"typescript-eslint": "^8.
|
|
44
|
-
"vitest": "^4.
|
|
43
|
+
"globals": "^17.6.0",
|
|
44
|
+
"prettier": "^3.8.3",
|
|
45
|
+
"typescript": "^6.0.3",
|
|
46
|
+
"typescript-eslint": "^8.59.2",
|
|
47
|
+
"vitest": "^4.1.5"
|
|
45
48
|
},
|
|
46
49
|
"dependencies": {
|
|
47
|
-
"typedoc": "0.28.
|
|
50
|
+
"typedoc": "0.28.19"
|
|
48
51
|
}
|
|
49
|
-
}
|
|
52
|
+
}
|