@codebam/cf-workers-telegram-bot 8.0.1 → 8.1.1

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.
@@ -10,6 +10,11 @@ export default class TelegramApi {
10
10
  * @param data - data to append to the request
11
11
  */
12
12
  getApiUrl(botApi: string, slug: string, data: Record<string, number | string | boolean>): Request<unknown, CfProperties<unknown>>;
13
+ sendChatAction(botApi: string, data: {
14
+ business_connection_id?: string;
15
+ chat_id: number | string;
16
+ action: string;
17
+ }): Promise<Response>;
13
18
  /**
14
19
  * Get a file with a given file_id
15
20
  * @param botApi - full URL to the telegram API without slug
@@ -14,6 +14,11 @@ export default class TelegramApi {
14
14
  }
15
15
  return new Request(`${request.toString()}?${params.toString()}`);
16
16
  }
17
+ async sendChatAction(botApi, data) {
18
+ const url = this.getApiUrl(botApi, 'sendChatAction', data);
19
+ const response = await fetch(url);
20
+ return response;
21
+ }
17
22
  /**
18
23
  * Get a file with a given file_id
19
24
  * @param botApi - full URL to the telegram API without slug
@@ -21,6 +26,9 @@ export default class TelegramApi {
21
26
  * @param token - bot token
22
27
  */
23
28
  async getFile(botApi, data, token) {
29
+ if (data.file_id === '') {
30
+ return new Response();
31
+ }
24
32
  const url = this.getApiUrl(botApi, 'getFile', data);
25
33
  const response = await fetch(url);
26
34
  const json = await response.json();
@@ -35,6 +35,17 @@ export default class TelegramExecutionContext {
35
35
  * @param options - any additional options to pass to sendPhoto
36
36
  */
37
37
  replyPhoto(photo: string, caption?: string, options?: Record<string, number | string | boolean>): Promise<Response | undefined>;
38
+ /**
39
+ * Send typing in a chat
40
+ */
41
+ sendTyping(): Promise<Response | undefined>;
42
+ /**
43
+ * Reply to an inline message with a title and content
44
+ * @param title - title to reply with
45
+ * @param message - message contents to reply with
46
+ * @param parse_mode - parse mode to use
47
+ */
48
+ replyInline(title: string, message: string, parse_mode?: string): Promise<Response | undefined>;
38
49
  /**
39
50
  * Reply to the last message with text
40
51
  * @param message - text to reply with
@@ -35,7 +35,7 @@ export default class TelegramExecutionContext {
35
35
  else if (this.update.callback_query?.id) {
36
36
  this.update_type = 'callback';
37
37
  }
38
- else if (this.update.business_message?.text) {
38
+ else if (this.update.business_message) {
39
39
  this.update_type = 'business_message';
40
40
  }
41
41
  }
@@ -103,6 +103,43 @@ export default class TelegramExecutionContext {
103
103
  break;
104
104
  }
105
105
  }
106
+ /**
107
+ * Send typing in a chat
108
+ */
109
+ async sendTyping() {
110
+ switch (this.update_type) {
111
+ case 'message':
112
+ return await this.api.sendChatAction(this.bot.api.toString(), {
113
+ chat_id: this.update.message?.chat.id.toString() ?? '',
114
+ action: 'typing',
115
+ });
116
+ case 'business_message':
117
+ return await this.api.sendChatAction(this.bot.api.toString(), {
118
+ business_connection_id: this.update.business_message?.business_connection_id.toString(),
119
+ chat_id: this.update.business_message?.chat.id.toString() ?? '',
120
+ action: 'typing',
121
+ });
122
+ default:
123
+ break;
124
+ }
125
+ }
126
+ /**
127
+ * Reply to an inline message with a title and content
128
+ * @param title - title to reply with
129
+ * @param message - message contents to reply with
130
+ * @param parse_mode - parse mode to use
131
+ */
132
+ async replyInline(title, message, parse_mode = '') {
133
+ switch (this.update_type) {
134
+ case 'inline':
135
+ return await this.api.answerInline(this.bot.api.toString(), {
136
+ inline_query_id: this.update.inline_query?.id.toString() ?? '',
137
+ results: [new TelegramInlineQueryResultArticle({ content: message, title, parse_mode })],
138
+ });
139
+ default:
140
+ break;
141
+ }
142
+ }
106
143
  /**
107
144
  * Reply to the last message with text
108
145
  * @param message - text to reply with
@@ -137,7 +174,7 @@ export default class TelegramExecutionContext {
137
174
  case 'inline':
138
175
  return await this.api.answerInline(this.bot.api.toString(), {
139
176
  inline_query_id: this.update.inline_query?.id.toString() ?? '',
140
- results: [new TelegramInlineQueryResultArticle(message)],
177
+ results: [new TelegramInlineQueryResultArticle({ title: message, content: message, parse_mode })],
141
178
  });
142
179
  case 'document':
143
180
  return await this.api.sendMessage(this.bot.api.toString(), {
@@ -4,5 +4,10 @@ export default class TelegramInlineQueryResultArticle extends TelegramInlineQuer
4
4
  title: string;
5
5
  input_message_content: TelegramInputMessageContent;
6
6
  thumb_url: string;
7
- constructor(content: string, title?: string, parse_mode?: string, thumb_url?: string);
7
+ constructor(data: {
8
+ content: string;
9
+ title?: string;
10
+ parse_mode?: string;
11
+ thumb_url?: string;
12
+ });
8
13
  }
@@ -3,13 +3,13 @@ export default class TelegramInlineQueryResultArticle extends TelegramInlineQuer
3
3
  title;
4
4
  input_message_content;
5
5
  thumb_url;
6
- constructor(content, title = content, parse_mode = '', thumb_url = '') {
6
+ constructor(data) {
7
7
  super('article');
8
- this.title = title;
8
+ this.title = data.title ?? '';
9
9
  this.input_message_content = {
10
- message_text: content.toString(),
11
- parse_mode,
10
+ message_text: data.content.toString(),
11
+ parse_mode: data.parse_mode ?? '',
12
12
  };
13
- this.thumb_url = thumb_url;
13
+ this.thumb_url = data.thumb_url ?? '';
14
14
  }
15
15
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "8.0.1",
3
+ "version": "8.1.1",
4
4
  "description": "serverless telegram bot on cf workers",
5
5
  "main": "./dist/main.js",
6
6
  "module": "./dist/main.js",
@@ -31,16 +31,16 @@
31
31
  "url": "https://github.com/codebam/cf-workers-telegram-bot.git"
32
32
  },
33
33
  "devDependencies": {
34
- "@cloudflare/workers-types": "^4.20241127.0",
35
- "@eslint/js": "^9.15.0",
36
- "@typescript-eslint/eslint-plugin": "^8.16.0",
37
- "@typescript-eslint/parser": "^8.16.0",
38
- "eslint": "^9.15.0",
34
+ "@cloudflare/workers-types": "^4.20241202.0",
35
+ "@eslint/js": "^9.16.0",
36
+ "@typescript-eslint/eslint-plugin": "^8.17.0",
37
+ "@typescript-eslint/parser": "^8.17.0",
38
+ "eslint": "^9.16.0",
39
39
  "eslint-config-prettier": "^9.1.0",
40
- "globals": "^15.12.0",
41
- "prettier": "^3.4.1",
40
+ "globals": "^15.13.0",
41
+ "prettier": "^3.4.2",
42
42
  "typescript": "^5.7.2",
43
- "typescript-eslint": "^8.16.0",
44
- "vitest": "^2.1.6"
43
+ "typescript-eslint": "^8.17.0",
44
+ "vitest": "^2.1.8"
45
45
  }
46
46
  }