@codebam/cf-workers-telegram-bot 5.8.0 → 5.9.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.
@@ -31,8 +31,12 @@ export default class TelegramApi extends BotApi {
31
31
  ? ((text_args) => ((command) => this.commands[command]
32
32
  ? this.commands[command]?.(this, update, [...text_args, ...args])
33
33
  : log({
34
- error: `command '${command}' does not exist`,
35
- }) && this.updates.default)(
34
+ error: `command '${command}' does not exist, using default`,
35
+ }) &&
36
+ this.commands["default"]?.(this, update, [
37
+ ...text_args,
38
+ ...args,
39
+ ]))(
36
40
  // run the command
37
41
  this.getCommand(text_args)))(
38
42
  // get the command to run
@@ -5,7 +5,10 @@ export default class TelegramBot extends TelegramApi {
5
5
  url: URL;
6
6
  kv: Kv;
7
7
  get_set: KVNamespace;
8
+ ai: any;
8
9
  constructor(config: Config);
10
+ question: (update: TelegramUpdate, args: string[]) => Promise<Response>;
11
+ paste: (update: TelegramUpdate, args: string[]) => Promise<Response>;
9
12
  code: (update: TelegramUpdate) => Promise<Response>;
10
13
  duckduckgo: (update: TelegramUpdate, args: string[]) => Promise<Response>;
11
14
  kanye: (update: TelegramUpdate) => Promise<Response>;
@@ -1,16 +1,51 @@
1
1
  import { preTagString, prettyJSON, addSearchParams, responseToJSON, } from "./libs";
2
2
  import TelegramApi from "./telegram_api";
3
3
  import { TelegramInlineQueryResultArticle, TelegramInlineQueryResultPhoto, } from "./types";
4
+ import { Ai } from "@cloudflare/ai";
4
5
  export default class TelegramBot extends TelegramApi {
5
6
  url;
6
7
  kv;
7
8
  get_set;
9
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
+ ai;
8
11
  constructor(config) {
9
12
  super(config.commands, config.webhook, config.handler);
10
13
  this.url = config.url;
11
14
  this.kv = config.kv;
12
15
  this.get_set = config.kv?.get_set;
16
+ this.ai = config.ai;
13
17
  }
18
+ // bot command: /question
19
+ question = async (update, args) => {
20
+ const ai = new Ai(this.ai);
21
+ let prompt;
22
+ if (args[0][0] === "/") {
23
+ prompt = args.slice(1).join(" ");
24
+ }
25
+ else {
26
+ prompt = args.join(" ");
27
+ }
28
+ console.log({ prompt });
29
+ if (prompt === "") {
30
+ prompt = "no prompt specified";
31
+ }
32
+ const { response } = await ai.run("@cf/meta/llama-2-7b-chat-int8", {
33
+ prompt,
34
+ });
35
+ return this.sendMessage(update.message?.chat.id ?? 0, response);
36
+ };
37
+ // bot command: /paste
38
+ paste = async (update, args) => {
39
+ const formdata = new FormData();
40
+ formdata.append("upload", args.slice(1).join(" "));
41
+ const request = await fetch("https://pastebin.seanbehan.ca", {
42
+ method: "POST",
43
+ body: formdata,
44
+ redirect: "manual",
45
+ });
46
+ const url = request.headers.get("location");
47
+ return this.sendMessage(update.message?.chat.id ?? 0, url ?? "failed to upload");
48
+ };
14
49
  // bot command: /code
15
50
  code = async (update) => ((url) => update.inline_query
16
51
  ? this.answerInlineQuery(update.inline_query.id, [
@@ -54,9 +89,16 @@ export default class TelegramBot extends TelegramApi {
54
89
  : "")))
55
90
  : this.sendMessage(update.message?.chat.id ?? 0, duckduckgo_url))(query === ""
56
91
  ? "https://duckduckgo.com"
57
- : addSearchParams(new URL("https://duckduckgo.com"), {
58
- q: query,
59
- }).href))(args.slice(1).join(" "));
92
+ : (() => {
93
+ if (query[0][0] !== "/") {
94
+ return addSearchParams(new URL("https://duckduckgo.com"), {
95
+ q: query,
96
+ }).href;
97
+ }
98
+ return addSearchParams(new URL("https://duckduckgo.com"), {
99
+ q: query.split(" ").slice(1).join(" "),
100
+ }).href;
101
+ })()))(args.join(" "));
60
102
  // bot command: /kanye
61
103
  kanye = async (update) => fetch("https://api.kanye.rest")
62
104
  .then((response) => responseToJSON(response))
@@ -14,6 +14,8 @@ export default class TelegramCommands {
14
14
  static _get: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
15
15
  static _set: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
16
16
  static duckduckgo: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
17
+ static paste: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
18
+ static question: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
17
19
  static code: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
18
20
  static commandList: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
19
21
  }
@@ -11,6 +11,8 @@ export default class TelegramCommands {
11
11
  static _get = async (bot, update, args) => bot._get(update, args);
12
12
  static _set = async (bot, update, args) => bot._set(update, args);
13
13
  static duckduckgo = async (bot, update, args) => bot.duckduckgo(update, args);
14
+ static paste = async (bot, update, args) => bot.paste(update, args);
15
+ static question = async (bot, update, args) => bot.question(update, args);
14
16
  static code = async (bot, update) => bot.code(update);
15
17
  static commandList = async (bot, update) => bot.commandList(update);
16
18
  }
@@ -16,6 +16,7 @@ export declare class Config {
16
16
  kv: Kv;
17
17
  url: URL;
18
18
  handler: Handler;
19
+ ai: any;
19
20
  constructor(config?: Partial<Config>);
20
21
  }
21
22
  export declare const localhost: URL;
@@ -10,6 +10,8 @@ export class Config {
10
10
  kv;
11
11
  url;
12
12
  handler;
13
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
+ ai;
13
15
  constructor(config = {}) {
14
16
  this.bot_name = config.bot_name || "";
15
17
  this.api = config.api || BotApi;
@@ -18,6 +20,7 @@ export class Config {
18
20
  this.kv = config.kv;
19
21
  this.url = config.url || new URL(localhost);
20
22
  this.handler = config.handler || new Handler([]);
23
+ this.ai = config.ai;
21
24
  }
22
25
  }
23
26
  export const localhost = new URL("http://localhost");
@@ -5,6 +5,7 @@ interface Environment {
5
5
  KV_UID_DATA: KVNamespace;
6
6
  SECRET_TELEGRAM_API_TOKEN2: string;
7
7
  SECRET_TELEGRAM_API_TOKEN3: string;
8
+ AI: string;
8
9
  }
9
10
  declare const _default: {
10
11
  fetch: (request: Request, env: Environment) => Promise<Response>;
@@ -6,6 +6,7 @@ export default {
6
6
  api: TelegramBot,
7
7
  webhook: new TelegramWebhook(new URL(`https://api.telegram.org/bot${env.SECRET_TELEGRAM_API_TOKEN}`), env.SECRET_TELEGRAM_API_TOKEN, new URL(new URL(request.url).origin)),
8
8
  commands: {
9
+ default: TelegramCommands.question,
9
10
  "/ping": TelegramCommands.ping,
10
11
  "/toss": TelegramCommands.toss,
11
12
  "/epoch": TelegramCommands.epoch,
@@ -19,37 +20,46 @@ export default {
19
20
  "/set": TelegramCommands._set,
20
21
  "/duckduckgo": TelegramCommands.duckduckgo,
21
22
  "/code": TelegramCommands.code,
23
+ "/paste": TelegramCommands.paste,
22
24
  "/commands": TelegramCommands.commandList,
25
+ "/question": TelegramCommands.question,
23
26
  "/help": TelegramCommands.commandList,
24
27
  "/start": TelegramCommands.commandList,
25
28
  },
26
29
  kv: { get_set: env.KV_GET_SET, uid_data: env.KV_UID_DATA },
30
+ ai: env.AI,
27
31
  },
28
32
  {
29
33
  bot_name: "@duckduckbot",
30
34
  api: TelegramBot,
31
35
  webhook: new TelegramWebhook(new URL(`https://api.telegram.org/bot${env.SECRET_TELEGRAM_API_TOKEN2}`), env.SECRET_TELEGRAM_API_TOKEN2, new URL(new URL(request.url).origin)),
32
36
  commands: {
37
+ default: TelegramCommands.duckduckgo,
33
38
  inline: TelegramCommands.duckduckgo,
34
39
  "/duckduckgo": TelegramCommands.duckduckgo,
40
+ "/question": TelegramCommands.question,
35
41
  "/code": TelegramCommands.code,
36
42
  "/commands": TelegramCommands.commandList,
37
43
  "/help": TelegramCommands.commandList,
38
44
  "/start": TelegramCommands.commandList,
39
45
  },
46
+ ai: env.AI,
40
47
  },
41
48
  {
42
49
  bot_name: "@ddggbot",
43
50
  api: TelegramBot,
44
51
  webhook: new TelegramWebhook(new URL(`https://api.telegram.org/bot${env.SECRET_TELEGRAM_API_TOKEN3}`), env.SECRET_TELEGRAM_API_TOKEN3, new URL(new URL(request.url).origin)),
45
52
  commands: {
53
+ default: TelegramCommands.duckduckgo,
46
54
  inline: TelegramCommands.duckduckgo,
47
55
  "/duckduckgo": TelegramCommands.duckduckgo,
56
+ "/question": TelegramCommands.question,
48
57
  "/code": TelegramCommands.code,
49
58
  "/commands": TelegramCommands.commandList,
50
59
  "/help": TelegramCommands.commandList,
51
60
  "/start": TelegramCommands.commandList,
52
61
  },
62
+ ai: env.AI,
53
63
  },
54
64
  ]).handle(request),
55
65
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "5.8.0",
3
+ "version": "5.9.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",
@@ -29,14 +29,17 @@
29
29
  "url": "https://github.com/codebam/cf-workers-telegram-bot.git"
30
30
  },
31
31
  "devDependencies": {
32
- "@cloudflare/workers-types": "^4.20230914.0",
33
- "@typescript-eslint/eslint-plugin": "^6.7.2",
34
- "@typescript-eslint/parser": "^6.7.2",
35
- "eslint": "^8.49.0",
32
+ "@cloudflare/workers-types": "^4.20230922.0",
33
+ "@typescript-eslint/eslint-plugin": "^6.7.3",
34
+ "@typescript-eslint/parser": "^6.7.3",
35
+ "eslint": "^8.50.0",
36
36
  "eslint-config-prettier": "^9.0.0",
37
- "lerna": "^7.1.5",
38
- "prettier": "^3.0.2",
37
+ "lerna": "^7.3.0",
38
+ "prettier": "^3.0.3",
39
39
  "typescript": "^5.2.2"
40
40
  },
41
- "gitHead": "4101ac66eb6a0f94d2fb939863286b334f79cbbc"
41
+ "dependencies": {
42
+ "@cloudflare/ai": "^1.0.14"
43
+ },
44
+ "gitHead": "0a031fe9156731fae4ac19f9068ee2f42bd14074"
42
45
  }