@codebam/cf-workers-telegram-bot 5.2.0 → 5.7.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.
@@ -1,10 +1,10 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
2
  import Handler from "./handler";
3
- import { Commands, Config, Update, Webhook } from "./types";
3
+ import { Commands, Config, Update } from "./types";
4
4
  export default class BotApi {
5
5
  commands: Commands;
6
6
  webhook: Config["webhook"];
7
7
  handler: Handler;
8
8
  update: (update: Update) => Promise<Response>;
9
- constructor(commands: Commands, webhook: Webhook, handler: Handler);
9
+ constructor(config: Partial<Config>);
10
10
  }
@@ -1,11 +1,14 @@
1
+ import Handler from "./handler";
2
+ import { Webhook, localhost } from "./types";
1
3
  export default class BotApi {
2
4
  commands;
3
5
  webhook;
4
6
  handler;
5
7
  update;
6
- constructor(commands, webhook, handler) {
7
- this.commands = commands;
8
- this.webhook = webhook;
9
- this.handler = handler;
8
+ constructor(config) {
9
+ this.commands = config.commands || {};
10
+ this.webhook =
11
+ config.webhook || new Webhook(new URL(localhost), "", new URL(localhost));
12
+ this.handler = config.handler || new Handler([]);
10
13
  }
11
14
  }
@@ -1,12 +1,12 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
2
  import BotApi from "./bot_api";
3
- import { PartialConfig } from "./types";
3
+ import { Config } from "./types";
4
4
  export default class Handler {
5
- configs: PartialConfig[];
6
- constructor(configs: PartialConfig[]);
5
+ configs: Partial<Config>[];
6
+ constructor(configs: Partial<Config>[]);
7
7
  getResponse: (_request?: Request, _bot?: BotApi) => Promise<Response>;
8
8
  postResponse: (_request?: Request, _bot?: BotApi) => Promise<Response>;
9
9
  responses: Record<string, (_request?: Request, _bot?: BotApi) => Promise<Response>>;
10
- getAccessKeys: (configs: PartialConfig[]) => Promise<Record<string, any> | Record<string, never>>;
10
+ getAccessKeys: (configs: Partial<Config>[]) => Promise<Record<string, Config> | Record<string, never>>;
11
11
  handle: (request: Request) => Promise<Response>;
12
12
  }
@@ -24,24 +24,19 @@ export default class Handler {
24
24
  POST: this.postResponse,
25
25
  default: () => new Promise(() => new Response()),
26
26
  };
27
- getAccessKeys = async (configs
28
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
29
- ) => Promise.all(configs.map((bot_config) => sha256(bot_config.webhook?.token ?? "").then((hash) => [
27
+ getAccessKeys = async (configs) => Promise.all(configs.map((bot_config) => sha256(bot_config.webhook?.token ?? "").then((hash) => [
30
28
  hash,
31
29
  bot_config,
32
30
  ]))).then((result) => Object.fromEntries(result));
33
31
  // handles the request
34
32
  handle = async (request) => this.getAccessKeys(this.configs).then((access_keys) => Object.keys(this.responses).includes(request.method)
35
33
  ? this.responses[request.method](request, ((key) => {
36
- if (access_keys[key]) {
37
- return new access_keys[key].api({
38
- ...new Config(),
39
- url: new URL(new URL(request.url).origin),
40
- handler: this,
41
- ...access_keys[key],
42
- });
43
- }
44
- return this.responses.default();
34
+ return new access_keys[key].api({
35
+ ...new Config(),
36
+ ...access_keys[key],
37
+ url: new URL(new URL(request.url).origin),
38
+ handler: this,
39
+ });
45
40
  })(new URL(request.url).pathname.substring(1)))
46
41
  : this.responses.default());
47
42
  }
@@ -2,7 +2,7 @@ import BotApi from "./bot_api";
2
2
  import { addSearchParams, log } from "./libs";
3
3
  export default class TelegramApi extends BotApi {
4
4
  constructor(commands, webhook, handler) {
5
- super(commands, webhook, handler);
5
+ super({ commands, webhook, handler });
6
6
  }
7
7
  inlineQueryUpdate = async (update) => this.executeInlineCommand(update);
8
8
  messageUpdate = async (update) => typeof update.message?.text === "string"
@@ -10,23 +10,14 @@ export type Commands = Record<string, Command>;
10
10
  export type Kv = Record<string, KVNamespace> | undefined;
11
11
  export declare class Config {
12
12
  bot_name: string;
13
- api: object;
13
+ api: typeof BotApi;
14
14
  webhook: Webhook;
15
15
  commands: Record<string, Command>;
16
- kv: Kv | undefined;
16
+ kv: Kv;
17
17
  url: URL;
18
- handler: Handler | undefined;
19
- constructor(config?: PartialConfig);
18
+ handler: Handler;
19
+ constructor(config?: Partial<Config>);
20
20
  }
21
- export type PartialConfig = {
22
- bot_name?: string;
23
- api?: object;
24
- webhook?: Webhook;
25
- commands?: Record<string, Command>;
26
- kv?: Kv | undefined;
27
- url?: URL;
28
- handler?: Handler;
29
- };
30
21
  export declare const localhost: URL;
31
22
  export declare class WebhookCommands {
32
23
  [key: string]: () => Promise<Response>;
@@ -1,3 +1,5 @@
1
+ import BotApi from "./bot_api";
2
+ import Handler from "./handler";
1
3
  import Webhook from "./webhook";
2
4
  export { Webhook };
3
5
  export class Config {
@@ -10,12 +12,12 @@ export class Config {
10
12
  handler;
11
13
  constructor(config = {}) {
12
14
  this.bot_name = config.bot_name || "";
13
- this.api = config.api || {};
15
+ this.api = config.api || BotApi;
14
16
  this.webhook = config.webhook || new Webhook(localhost, "", localhost);
15
17
  this.commands = config.commands || {};
16
18
  this.kv = config.kv;
17
19
  this.url = config.url || new URL(localhost);
18
- this.handler = config.handler;
20
+ this.handler = config.handler || new Handler([]);
19
21
  }
20
22
  }
21
23
  export const localhost = new URL("http://localhost");
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "5.2.0",
3
+ "version": "5.7.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",
@@ -38,5 +38,5 @@
38
38
  "prettier": "^3.0.1",
39
39
  "typescript": "^5.1.6"
40
40
  },
41
- "gitHead": "18c40115f1a31c1863f41ccbf8723f155947d739"
41
+ "gitHead": "636be817961219f3407aa2d61d9345e388d9a21d"
42
42
  }