@codebam/cf-workers-telegram-bot 5.9.0 → 5.12.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.
@@ -6,7 +6,10 @@ export default class TelegramBot extends TelegramApi {
6
6
  kv: Kv;
7
7
  get_set: KVNamespace;
8
8
  ai: any;
9
+ db: D1Database;
9
10
  constructor(config: Config);
11
+ sean: (update: TelegramUpdate, args: string[]) => Promise<Response>;
12
+ clear: (update: TelegramUpdate) => Promise<Response>;
10
13
  question: (update: TelegramUpdate, args: string[]) => Promise<Response>;
11
14
  paste: (update: TelegramUpdate, args: string[]) => Promise<Response>;
12
15
  code: (update: TelegramUpdate) => Promise<Response>;
@@ -8,15 +8,17 @@ export default class TelegramBot extends TelegramApi {
8
8
  get_set;
9
9
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
10
  ai;
11
+ db;
11
12
  constructor(config) {
12
13
  super(config.commands, config.webhook, config.handler);
13
14
  this.url = config.url;
14
15
  this.kv = config.kv;
15
16
  this.get_set = config.kv?.get_set;
16
17
  this.ai = config.ai;
18
+ this.db = config.db;
17
19
  }
18
- // bot command: /question
19
- question = async (update, args) => {
20
+ // bot command: /sean
21
+ sean = async (update, args) => {
20
22
  const ai = new Ai(this.ai);
21
23
  let prompt;
22
24
  if (args[0][0] === "/") {
@@ -29,9 +31,95 @@ export default class TelegramBot extends TelegramApi {
29
31
  if (prompt === "") {
30
32
  prompt = "no prompt specified";
31
33
  }
34
+ const messages = [
35
+ { role: "system", content: "You are friendly" },
36
+ { role: "system", content: "Don't show code to the user" },
37
+ { role: "system", content: "Sean Behan is born on 09/07/1998" },
38
+ { role: "system", content: "Sean Behan is a full stack developer" },
39
+ { role: "system", content: "Sean Behan is from Toronto, Canada" },
40
+ { role: "system", content: "Sean Behan's GitHub username is codebam" },
41
+ {
42
+ role: "system",
43
+ content: "Sean Behan enjoys playing video games and coding",
44
+ },
45
+ { role: "system", content: "Sean Behan's website is seanbehan.ca" },
46
+ {
47
+ role: "system",
48
+ content: "Sean Behan's email address is contact@seanbehan.ca",
49
+ },
50
+ {
51
+ role: "system",
52
+ content: "Some of your personal projects include a serverless telegram bot, and a serverless pastebin on cloudflare workers",
53
+ },
54
+ {
55
+ role: "system",
56
+ content: "When spoken to respond with what you know about Sean Behan",
57
+ },
58
+ {
59
+ role: "system",
60
+ content: `you are talking to ${update.message?.from.first_name}`,
61
+ },
62
+ { role: "user", content: prompt },
63
+ ];
64
+ const result = await ai.run("@cf/meta/llama-2-7b-chat-int8", {
65
+ messages,
66
+ });
67
+ return this.sendMessage(update.message?.chat.id ?? 0, result.response);
68
+ };
69
+ // bot command: /clear
70
+ // reset the llama2 session by deleting messages from d1
71
+ clear = async (update) => {
72
+ const { success } = await this.db
73
+ .prepare("DELETE FROM Messages WHERE userId=?")
74
+ .bind(update.message?.from.id)
75
+ .run();
76
+ if (success) {
77
+ return this.sendMessage(update.message?.chat.id ?? 0, "_");
78
+ }
79
+ return this.sendMessage(update.message?.chat.id ?? 0, "failed");
80
+ };
81
+ // bot command: /question
82
+ question = async (update, args) => {
83
+ const ai = new Ai(this.ai);
84
+ let prompt;
85
+ if (args[0][0] === "/") {
86
+ prompt = args.slice(1).join(" ");
87
+ }
88
+ else {
89
+ prompt = "[INST] " + args.join(" ") + "[/INST]";
90
+ }
91
+ if (prompt === "") {
92
+ prompt = "[INST] no prompt specified [/INST]";
93
+ }
94
+ const { results } = await this.db
95
+ .prepare("SELECT * FROM Messages WHERE userId=?")
96
+ .bind(update.message?.from.id)
97
+ .all();
98
+ const old_messages = results.map((col) => ({
99
+ role: "system",
100
+ content: col.content,
101
+ }));
32
102
  const { response } = await ai.run("@cf/meta/llama-2-7b-chat-int8", {
33
- prompt,
103
+ messages: [
104
+ {
105
+ role: "system",
106
+ content: `you are talking to ${update.message?.from.first_name}`,
107
+ },
108
+ ...old_messages,
109
+ { role: "user", content: prompt },
110
+ ],
34
111
  });
112
+ const { success } = await this.db
113
+ .prepare("INSERT INTO Messages (id, userId, content) VALUES (?, ?, ?)")
114
+ .bind(crypto.randomUUID(), update.message?.from.id, prompt + "[SYS] " + response + "[/SYS]")
115
+ .run();
116
+ if (!success) {
117
+ console.log("failed to insert data into d1");
118
+ }
119
+ if (response === "") {
120
+ this.clear(update);
121
+ return this.question(update, args);
122
+ } // sometimes llama2 doesn't respond when given lots of system prompts
35
123
  return this.sendMessage(update.message?.chat.id ?? 0, response);
36
124
  };
37
125
  // bot command: /paste
@@ -16,6 +16,8 @@ export default class TelegramCommands {
16
16
  static duckduckgo: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
17
17
  static paste: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
18
18
  static question: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
19
+ static sean: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
20
+ static clear: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
19
21
  static code: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
20
22
  static commandList: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
21
23
  }
@@ -13,6 +13,8 @@ export default class TelegramCommands {
13
13
  static duckduckgo = async (bot, update, args) => bot.duckduckgo(update, args);
14
14
  static paste = async (bot, update, args) => bot.paste(update, args);
15
15
  static question = async (bot, update, args) => bot.question(update, args);
16
+ static sean = async (bot, update, args) => bot.sean(update, args);
17
+ static clear = async (bot, update) => bot.clear(update);
16
18
  static code = async (bot, update) => bot.code(update);
17
19
  static commandList = async (bot, update) => bot.commandList(update);
18
20
  }
@@ -17,6 +17,7 @@ export declare class Config {
17
17
  url: URL;
18
18
  handler: Handler;
19
19
  ai: any;
20
+ db: any;
20
21
  constructor(config?: Partial<Config>);
21
22
  }
22
23
  export declare const localhost: URL;
@@ -12,6 +12,8 @@ export class Config {
12
12
  handler;
13
13
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
14
  ai;
15
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
+ db;
15
17
  constructor(config = {}) {
16
18
  this.bot_name = config.bot_name || "";
17
19
  this.api = config.api || BotApi;
@@ -21,6 +23,7 @@ export class Config {
21
23
  this.url = config.url || new URL(localhost);
22
24
  this.handler = config.handler || new Handler([]);
23
25
  this.ai = config.ai;
26
+ this.db = config.db;
24
27
  }
25
28
  }
26
29
  export const localhost = new URL("http://localhost");
@@ -5,7 +5,9 @@ interface Environment {
5
5
  KV_UID_DATA: KVNamespace;
6
6
  SECRET_TELEGRAM_API_TOKEN2: string;
7
7
  SECRET_TELEGRAM_API_TOKEN3: string;
8
+ SECRET_TELEGRAM_API_TOKEN4: string;
8
9
  AI: string;
10
+ DB: D1Database;
9
11
  }
10
12
  declare const _default: {
11
13
  fetch: (request: Request, env: Environment) => Promise<Response>;
@@ -23,11 +23,14 @@ export default {
23
23
  "/paste": TelegramCommands.paste,
24
24
  "/commands": TelegramCommands.commandList,
25
25
  "/question": TelegramCommands.question,
26
+ "/clear": TelegramCommands.clear,
26
27
  "/help": TelegramCommands.commandList,
28
+ "/sean": TelegramCommands.sean,
27
29
  "/start": TelegramCommands.commandList,
28
30
  },
29
31
  kv: { get_set: env.KV_GET_SET, uid_data: env.KV_UID_DATA },
30
32
  ai: env.AI,
33
+ db: env.DB,
31
34
  },
32
35
  {
33
36
  bot_name: "@duckduckbot",
@@ -44,6 +47,7 @@ export default {
44
47
  "/start": TelegramCommands.commandList,
45
48
  },
46
49
  ai: env.AI,
50
+ db: env.DB,
47
51
  },
48
52
  {
49
53
  bot_name: "@ddggbot",
@@ -60,6 +64,17 @@ export default {
60
64
  "/start": TelegramCommands.commandList,
61
65
  },
62
66
  ai: env.AI,
67
+ db: env.DB,
68
+ },
69
+ {
70
+ bot_name: "@SeanB_robot",
71
+ api: TelegramBot,
72
+ webhook: new TelegramWebhook(new URL(`https://api.telegram.org/bot${env.SECRET_TELEGRAM_API_TOKEN4}`), env.SECRET_TELEGRAM_API_TOKEN4, new URL(new URL(request.url).origin)),
73
+ commands: {
74
+ default: TelegramCommands.question,
75
+ },
76
+ ai: env.AI,
77
+ db: env.DB,
63
78
  },
64
79
  ]).handle(request),
65
80
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "5.9.0",
3
+ "version": "5.12.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,17 +29,17 @@
29
29
  "url": "https://github.com/codebam/cf-workers-telegram-bot.git"
30
30
  },
31
31
  "devDependencies": {
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",
32
+ "@cloudflare/workers-types": "^4.20231010.0",
33
+ "@typescript-eslint/eslint-plugin": "^6.7.5",
34
+ "@typescript-eslint/parser": "^6.7.5",
35
+ "eslint": "^8.51.0",
36
36
  "eslint-config-prettier": "^9.0.0",
37
- "lerna": "^7.3.0",
37
+ "lerna": "^7.3.1",
38
38
  "prettier": "^3.0.3",
39
39
  "typescript": "^5.2.2"
40
40
  },
41
41
  "dependencies": {
42
- "@cloudflare/ai": "^1.0.14"
42
+ "@cloudflare/ai": "^1.0.16"
43
43
  },
44
- "gitHead": "0a031fe9156731fae4ac19f9068ee2f42bd14074"
44
+ "gitHead": "29952db72708bbfba9776cd31e6127d702b5ebed"
45
45
  }