@codebam/cf-workers-telegram-bot 7.7.0 → 7.8.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/README.md CHANGED
@@ -1,16 +1,10 @@
1
1
  <h3 align="center">
2
2
  <img src="https://raw.githubusercontent.com/codebam/cf-workers-telegram-bot/master/assets/logo.png" width="100" />
3
3
  <br/>
4
- CF workers telegram bot
4
+ CF Workers Telegram Bot
5
5
  <br/>
6
6
  </h3>
7
7
 
8
- <h6 align="center">
9
- <a href="https://github.com/codebam/cf-workers-telegram-bot/wiki">Wiki</a>
10
- ·
11
- <a href="https://codebam.github.io/cf-workers-telegram-bot-docs/">Docs</a>
12
- </h6>
13
-
14
8
  <p align="center">
15
9
  <a href="https://github.com/codebam/cf-workers-telegram-bot/stargazers"> <img src="https://img.shields.io/github/stars/codebam/cf-workers-telegram-bot?style=for-the-badge&logo=starship&color=111111&logoColor=ffffff&labelColor=000000" alt="GitHub stars"/></a>
16
10
  <a href="https://github.com/codebam/cf-workers-telegram-bot/issues">
@@ -19,7 +13,7 @@ CF workers telegram bot
19
13
  <a href="https://www.npmjs.com/package/@codebam/cf-workers-telegram-bot"> <img src="https://img.shields.io/npm/v/@codebam/cf-workers-telegram-bot?style=for-the-badge&logo=npm&color=111111&logoColor=ffffff&labelColor=000000" alt="npm version" /></a>
20
14
  </p>
21
15
 
22
- ![screenshot of cf-workers-telegram-bot](/screenshot.png)
16
+ ![screenshot of cf-workers-telegram-bot](https://raw.githubusercontent.com/codebam/cf-workers-telegram-bot/master/assets/screenshot.png)
23
17
 
24
18
  ```sh
25
19
  npm i @codebam/cf-workers-telegram-bot
package/dist/api.d.ts CHANGED
@@ -1,14 +1,29 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
- import { SerializableData, TelegramInlineQueryResultArticle } from "./types";
2
+ import { SerializableData, TelegramInlineQueryResultArticle, TelegramInlineQueryResultPhoto } from './types';
3
+ import TelegramInlineQueryResultVideo from './types/TelegramInlineQueryResultVideo';
3
4
  export default class API {
4
- static getApiUrl(botApi: string, slug: string, data: Record<string, SerializableData>): string;
5
+ static getApiUrl(botApi: string, slug: string, data: Record<string, SerializableData>): Request<unknown, CfProperties<unknown>>;
6
+ static getFile(botApi: string, data: {
7
+ file_id: string;
8
+ }, token: string): Promise<ArrayBuffer>;
5
9
  static sendMessage(botApi: string, data: {
6
10
  reply_to_message_id: number | string;
7
11
  chat_id: number | string;
8
12
  text: string;
9
13
  }): Promise<Response>;
14
+ static sendVideo(botApi: string, data: {
15
+ reply_to_message_id: number | string;
16
+ chat_id: number | string;
17
+ video: string;
18
+ }): Promise<Response>;
19
+ static sendPhoto(botApi: string, data: {
20
+ reply_to_message_id: number | string;
21
+ chat_id: number | string;
22
+ photo: string;
23
+ caption: string;
24
+ }): Promise<Response>;
10
25
  static answerInline(botApi: string, data: {
11
26
  inline_query_id: number | string;
12
- results: TelegramInlineQueryResultArticle[];
27
+ results: TelegramInlineQueryResultArticle[] | TelegramInlineQueryResultPhoto[] | TelegramInlineQueryResultVideo[];
13
28
  }): Promise<Response>;
14
29
  }
package/dist/api.js CHANGED
@@ -5,16 +5,32 @@ export default class API {
5
5
  for (const i in data) {
6
6
  params.append(i, data[i].toString());
7
7
  }
8
- return `${request}?${params}`;
8
+ return new Request(`${request}?${params}`);
9
+ }
10
+ static async getFile(botApi, data, token) {
11
+ const url = this.getApiUrl(botApi, 'getFile', data);
12
+ const response = await fetch(url);
13
+ const json = (await response.json());
14
+ const file_path = json.result.file_path;
15
+ const file_response = await fetch(`https://api.telegram.org/file/bot${token}/${file_path}`);
16
+ return await file_response.arrayBuffer();
9
17
  }
10
18
  static async sendMessage(botApi, data) {
11
19
  const url = this.getApiUrl(botApi, 'sendMessage', data);
12
20
  return await fetch(url);
13
21
  }
22
+ static async sendVideo(botApi, data) {
23
+ const url = this.getApiUrl(botApi, 'sendVideo', data);
24
+ return await fetch(url);
25
+ }
26
+ static async sendPhoto(botApi, data) {
27
+ const url = this.getApiUrl(botApi, 'sendPhoto', data);
28
+ return await fetch(url);
29
+ }
14
30
  static async answerInline(botApi, data) {
15
31
  const url = this.getApiUrl(botApi, 'answerInlineQuery', {
16
32
  inline_query_id: data.inline_query_id,
17
- results: JSON.stringify(data.results)
33
+ results: JSON.stringify(data.results),
18
34
  });
19
35
  return await fetch(url);
20
36
  }
package/dist/ctx.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
- import TelegramBot from "./telegram_bot";
3
- import { SerializableData, TelegramUpdate } from "./types";
4
- export default class ExecutionContext {
2
+ import TelegramBot from './telegram_bot';
3
+ import { SerializableData, TelegramUpdate } from './types';
4
+ export default class TelegramExecutionContext {
5
5
  bot: TelegramBot;
6
6
  update: TelegramUpdate;
7
7
  update_type: string;
@@ -12,5 +12,8 @@ export default class ExecutionContext {
12
12
  setData(key: string, value: SerializableData): this;
13
13
  deleteData(key: string): this;
14
14
  getData(key: string): SerializableData;
15
- reply(message: string): Promise<void>;
15
+ replyVideo(video: string): Promise<Response | undefined>;
16
+ getFile(file_id: string): Promise<ArrayBuffer>;
17
+ replyPhoto(photo: string, caption?: string): Promise<Response | undefined>;
18
+ reply(message: string): Promise<Response | undefined>;
16
19
  }
package/dist/ctx.js CHANGED
@@ -1,6 +1,7 @@
1
- import API from "./api";
2
- import { TelegramInlineQueryResultArticle } from "./types";
3
- export default class ExecutionContext {
1
+ import API from './api';
2
+ import { TelegramInlineQueryResultArticle, TelegramInlineQueryResultPhoto } from './types';
3
+ import TelegramInlineQueryResultVideo from './types/TelegramInlineQueryResultVideo';
4
+ export default class TelegramExecutionContext {
4
5
  bot;
5
6
  update;
6
7
  update_type = '';
@@ -8,7 +9,10 @@ export default class ExecutionContext {
8
9
  constructor(bot, update) {
9
10
  this.bot = bot;
10
11
  this.update = update;
11
- if (this.update.message?.text) {
12
+ if (this.update.message?.photo) {
13
+ this.update_type = 'photo';
14
+ }
15
+ else if (this.update.message?.text) {
12
16
  this.update_type = 'message';
13
17
  }
14
18
  else if (this.update.inline_query?.query) {
@@ -16,7 +20,7 @@ export default class ExecutionContext {
16
20
  }
17
21
  }
18
22
  getText() {
19
- return this.update.message?.text || this.update.inline_query?.query || "";
23
+ return this.update.message?.text || this.update.inline_query?.query || '';
20
24
  }
21
25
  next() {
22
26
  return new Response('ok');
@@ -32,23 +36,70 @@ export default class ExecutionContext {
32
36
  getData(key) {
33
37
  return this.data[key];
34
38
  }
35
- async reply(message) {
39
+ async replyVideo(video) {
36
40
  switch (this.update_type) {
37
- case 'message': {
38
- await API.sendMessage(this.bot.api.toString(), {
41
+ case 'message':
42
+ return await API.sendVideo(this.bot.api.toString(), {
39
43
  chat_id: this.update.message?.chat.id.toString() ?? '',
40
44
  reply_to_message_id: this.update.message?.message_id.toString() ?? '',
41
- text: message
45
+ video,
46
+ });
47
+ case 'inline':
48
+ return await API.answerInline(this.bot.api.toString(), {
49
+ inline_query_id: this.update.inline_query?.id.toString() ?? '',
50
+ results: [new TelegramInlineQueryResultVideo(video)],
42
51
  });
52
+ default:
43
53
  break;
44
- }
45
- case 'inline': {
46
- await API.answerInline(this.bot.api.toString(), {
54
+ }
55
+ }
56
+ async getFile(file_id) {
57
+ return await API.getFile(this.bot.api.toString(), { file_id }, this.bot.token);
58
+ }
59
+ async replyPhoto(photo, caption = '') {
60
+ switch (this.update_type) {
61
+ case 'photo':
62
+ return await API.sendPhoto(this.bot.api.toString(), {
63
+ chat_id: this.update.message?.chat.id.toString() ?? '',
64
+ reply_to_message_id: this.update.message?.message_id.toString() ?? '',
65
+ photo,
66
+ caption,
67
+ });
68
+ case 'message':
69
+ return await API.sendPhoto(this.bot.api.toString(), {
70
+ chat_id: this.update.message?.chat.id.toString() ?? '',
71
+ reply_to_message_id: this.update.message?.message_id.toString() ?? '',
72
+ photo,
73
+ caption,
74
+ });
75
+ case 'inline':
76
+ return await API.answerInline(this.bot.api.toString(), {
47
77
  inline_query_id: this.update.inline_query?.id.toString() ?? '',
48
- results: [new TelegramInlineQueryResultArticle(message)]
78
+ results: [new TelegramInlineQueryResultPhoto(photo)],
49
79
  });
80
+ default:
50
81
  break;
51
- }
82
+ }
83
+ }
84
+ async reply(message) {
85
+ switch (this.update_type) {
86
+ case 'message':
87
+ return await API.sendMessage(this.bot.api.toString(), {
88
+ chat_id: this.update.message?.chat.id.toString() ?? '',
89
+ reply_to_message_id: this.update.message?.message_id.toString() ?? '',
90
+ text: message,
91
+ });
92
+ case 'photo':
93
+ return await API.sendMessage(this.bot.api.toString(), {
94
+ chat_id: this.update.message?.chat.id.toString() ?? '',
95
+ reply_to_message_id: this.update.message?.message_id.toString() ?? '',
96
+ text: message,
97
+ });
98
+ case 'inline':
99
+ return await API.answerInline(this.bot.api.toString(), {
100
+ inline_query_id: this.update.inline_query?.id.toString() ?? '',
101
+ results: [new TelegramInlineQueryResultArticle(message)],
102
+ });
52
103
  default:
53
104
  break;
54
105
  }
@@ -1,6 +1,6 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
2
  import { TelegramUpdate } from './types';
3
- import ExecutionContext from './ctx';
3
+ import TelegramExecutionContext from './ctx';
4
4
  import Webhook from './webhook';
5
5
  export default class TelegramBot {
6
6
  token: string;
@@ -8,9 +8,9 @@ export default class TelegramBot {
8
8
  api: URL;
9
9
  update: TelegramUpdate;
10
10
  update_type: string;
11
- commands: Record<string, (ctx: ExecutionContext) => Promise<Response>>;
12
- currentContext: ExecutionContext;
11
+ commands: Record<string, (ctx: TelegramExecutionContext) => Promise<Response>>;
12
+ currentContext: TelegramExecutionContext;
13
13
  constructor(token: string);
14
- on(event: string, callback: (ctx: ExecutionContext) => Promise<Response>): this;
14
+ on(event: string, callback: (ctx: TelegramExecutionContext) => Promise<Response>): this;
15
15
  handle(request: Request): Promise<Response>;
16
16
  }
@@ -1,5 +1,5 @@
1
1
  import { TelegramUpdate } from './types';
2
- import ExecutionContext from './ctx';
2
+ import TelegramExecutionContext from './ctx';
3
3
  import Webhook from './webhook';
4
4
  export default class TelegramBot {
5
5
  token;
@@ -18,8 +18,6 @@ export default class TelegramBot {
18
18
  }
19
19
  on(event, callback) {
20
20
  if (event !== 'on') {
21
- // eslint-disable-next-line
22
- // @ts-ignore TS7053
23
21
  this.commands[event] = callback;
24
22
  }
25
23
  return this;
@@ -43,25 +41,26 @@ export default class TelegramBot {
43
41
  console.log(this.update);
44
42
  let command = 'default';
45
43
  let args = [];
46
- const ctx = new ExecutionContext(this, this.update);
44
+ const ctx = new TelegramExecutionContext(this, this.update);
47
45
  this.currentContext = ctx;
48
46
  switch (ctx.update_type) {
49
47
  case 'message': {
50
- // @ts-expect-error already checked above
51
- args = this.update.message.text.split(' ');
48
+ args = this.update.message?.text?.split(' ') ?? [];
52
49
  break;
53
50
  }
54
51
  case 'inline': {
55
- // @ts-expect-error already checked above
56
- args = this.update.inline_query.query.split(' ');
52
+ args = this.update.inline_query?.query.split(' ') ?? [];
53
+ break;
54
+ }
55
+ case 'photo': {
56
+ command = ':photo';
57
57
  break;
58
58
  }
59
59
  default:
60
60
  break;
61
61
  }
62
62
  if (args.at(0)?.startsWith('/')) {
63
- // @ts-expect-error already checked above
64
- command = args.at(0).slice(1);
63
+ command = args.at(0)?.slice(1) ?? 'default';
65
64
  }
66
65
  this.commands['any']?.(ctx);
67
66
  if (!this.commands[command]) {
@@ -1,4 +1,5 @@
1
1
  import TelegramInlineQueryResult from './TelegramInlineQueryResult';
2
+ import TelegramInputMessageContent from './TelegramInputMessageContent';
2
3
  export default class TelegramInlineQueryResultPhoto extends TelegramInlineQueryResult {
3
4
  photo_url: string;
4
5
  thumb_url: string;
@@ -9,5 +10,6 @@ export default class TelegramInlineQueryResultPhoto extends TelegramInlineQueryR
9
10
  caption?: string;
10
11
  parse_mode?: string;
11
12
  caption_entities?: string;
13
+ input_message_content?: TelegramInputMessageContent;
12
14
  constructor(photo: string);
13
15
  }
@@ -10,7 +10,7 @@ export default class TelegramInlineQueryResultPhoto extends TelegramInlineQueryR
10
10
  parse_mode;
11
11
  caption_entities;
12
12
  // reply_markup?: TelegramInlineKeyboardMarkup;
13
- // input_message_content?: TelegramInputMessageContent;
13
+ input_message_content;
14
14
  constructor(photo) {
15
15
  super('photo');
16
16
  this.photo_url = photo;
@@ -0,0 +1,15 @@
1
+ import TelegramInlineQueryResult from './TelegramInlineQueryResult';
2
+ import TelegramInputMessageContent from './TelegramInputMessageContent';
3
+ export default class TelegramInlineQueryResultVideo extends TelegramInlineQueryResult {
4
+ video_url: string;
5
+ thumb_url: string;
6
+ photo_width?: number;
7
+ photo_height?: number;
8
+ title?: string;
9
+ description?: string;
10
+ caption?: string;
11
+ parse_mode?: string;
12
+ caption_entities?: string;
13
+ input_message_content?: TelegramInputMessageContent;
14
+ constructor(video: string);
15
+ }
@@ -0,0 +1,19 @@
1
+ import TelegramInlineQueryResult from './TelegramInlineQueryResult';
2
+ export default class TelegramInlineQueryResultVideo extends TelegramInlineQueryResult {
3
+ video_url;
4
+ thumb_url;
5
+ photo_width;
6
+ photo_height;
7
+ title;
8
+ description;
9
+ caption;
10
+ parse_mode;
11
+ caption_entities;
12
+ // reply_markup?: TelegramInlineKeyboardMarkup;
13
+ input_message_content;
14
+ constructor(video) {
15
+ super('video');
16
+ this.video_url = video;
17
+ this.thumb_url = video;
18
+ }
19
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "7.7.0",
3
+ "version": "7.8.0",
4
4
  "description": "serverless telegram bot on cf workers",
5
5
  "main": "./dist/main/src/main.js",
6
6
  "module": "./dist/main/src/main.js",
@@ -43,5 +43,5 @@
43
43
  "typescript-eslint": "^7.8.0",
44
44
  "vitest": "^1.6.0"
45
45
  },
46
- "gitHead": "11d5f60c49614aaeaa1e980790be6c2c3ef2057a"
46
+ "gitHead": "f70461a7e08ddedf437e1ee761c0c40c6184f3f2"
47
47
  }