@codebam/cf-workers-telegram-bot 5.13.0 → 5.14.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.
@@ -34,7 +34,7 @@ export default class Handler {
34
34
  return new access_keys[key].api({
35
35
  ...new Config(),
36
36
  ...access_keys[key],
37
- url: new URL(new URL(request.url).origin),
37
+ url: new URL(new URL(request.url).origin), // worker url
38
38
  handler: this,
39
39
  });
40
40
  })(new URL(request.url).pathname.substring(1)))
@@ -18,8 +18,10 @@ export default class TelegramApi extends BotApi {
18
18
  executeInlineCommand: (update: TelegramUpdate) => Promise<Response>;
19
19
  executeCommand: (update: TelegramUpdate) => Promise<Response>;
20
20
  answerInlineQuery: (inline_query_id: number, results: TelegramInlineQueryResult[], cache_time?: number) => Promise<Response>;
21
+ editMessageText: (chat_id: number, message_id: number, text: string) => Promise<Response>;
21
22
  sendMessage: (chat_id: number, text: string, parse_mode?: string, disable_web_page_preview?: boolean, disable_notification?: boolean, reply_to_message_id?: number) => Promise<Response>;
22
23
  forwardMessage: (chat_id: number, from_chat_id: number, disable_notification: boolean | undefined, message_id: number) => Promise<Response>;
24
+ sendPhotoRaw: (chat_id: number, photo: File, caption?: string, parse_mode?: string, disable_notification?: boolean, reply_to_message_id?: number) => Promise<any>;
23
25
  sendPhoto: (chat_id: number, photo: string, caption?: string, parse_mode?: string, disable_notification?: boolean, reply_to_message_id?: number) => Promise<Response>;
24
26
  sendVideo: (chat_id: number, video: Blob, duration?: number, width?: number, height?: number, thumb?: string, caption?: string, parse_mode?: string, supports_streaming?: boolean, disable_notification?: boolean, reply_to_message_id?: number) => Promise<Response>;
25
27
  sendAnimation: (chat_id: number, animation: Blob, duration?: number, width?: number, height?: number, thumb?: string, caption?: string, parse_mode?: string, disable_notification?: boolean, reply_to_message_id?: number) => Promise<Response>;
@@ -58,6 +58,12 @@ export default class TelegramApi extends BotApi {
58
58
  results: JSON.stringify(results),
59
59
  cache_time: cache_time.toString(),
60
60
  }).href));
61
+ // trigger editMessage command of BotAPI
62
+ editMessageText = async (chat_id, message_id, text) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/editMessageText`), {
63
+ chat_id: chat_id.toString(),
64
+ message_id: message_id.toString(),
65
+ text,
66
+ }).href));
61
67
  // trigger sendMessage command of BotAPI
62
68
  sendMessage = async (chat_id, text, parse_mode = "", disable_web_page_preview = false, disable_notification = false, reply_to_message_id = 0) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/sendMessage`), {
63
69
  chat_id: chat_id.toString(),
@@ -75,6 +81,20 @@ export default class TelegramApi extends BotApi {
75
81
  disable_notification: disable_notification.toString(),
76
82
  }).href));
77
83
  // trigger sendPhoto command of BotAPI
84
+ sendPhotoRaw = async (chat_id, photo, caption = "", parse_mode = "", disable_notification = false, reply_to_message_id = 0) => {
85
+ const formdata = new FormData();
86
+ formdata.set("file", photo);
87
+ return fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/sendPhoto`), {
88
+ chat_id: chat_id.toString(),
89
+ caption,
90
+ parse_mode,
91
+ disable_notification: disable_notification.toString(),
92
+ reply_to_message_id: reply_to_message_id.toString(),
93
+ }).href), { method: "POST", body: formdata })
94
+ .then((resp) => resp.text())
95
+ .then(log);
96
+ };
97
+ // trigger sendPhoto command of BotAPI
78
98
  sendPhoto = async (chat_id, photo, caption = "", parse_mode = "", disable_notification = false, reply_to_message_id = 0) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/sendPhoto`), {
79
99
  chat_id: chat_id.toString(),
80
100
  photo,
@@ -7,24 +7,21 @@ export default class TelegramBot extends TelegramApi {
7
7
  get_set: KVNamespace;
8
8
  ai: any;
9
9
  db: D1Database;
10
+ r2: R2Bucket;
10
11
  bot_name: string;
11
12
  constructor(config: Config);
12
- sean: (update: TelegramUpdate, args: string[]) => Promise<Response>;
13
13
  clear: (update: TelegramUpdate) => Promise<Response>;
14
+ image: (update: TelegramUpdate, args: string[]) => Promise<Response>;
14
15
  question: (update: TelegramUpdate, args: string[]) => Promise<Response>;
15
- paste: (update: TelegramUpdate, args: string[]) => Promise<Response>;
16
+ sean: (update: TelegramUpdate, args: string[]) => Promise<Response>;
16
17
  code: (update: TelegramUpdate) => Promise<Response>;
17
18
  duckduckgo: (update: TelegramUpdate, args: string[]) => Promise<Response>;
18
19
  kanye: (update: TelegramUpdate) => Promise<Response>;
19
20
  joke: (update: TelegramUpdate) => Promise<Response>;
20
21
  dog: (update: TelegramUpdate) => Promise<Response>;
21
- cat: (update: TelegramUpdate) => Promise<Response>;
22
22
  bored: (update: TelegramUpdate) => Promise<Response>;
23
23
  epoch: (update: TelegramUpdate) => Promise<Response>;
24
- _get: (update: TelegramUpdate, args: string[]) => Promise<Response>;
25
- _set: (update: TelegramUpdate, args: string[]) => Promise<Response>;
26
24
  _average: (numbers: number[]) => number;
27
- recursion: (update: TelegramUpdate) => Promise<Response>;
28
25
  roll: (update: TelegramUpdate, args: string[]) => Promise<Response>;
29
26
  commandList: (update: TelegramUpdate) => Promise<Response>;
30
27
  toss: (update: TelegramUpdate) => Promise<Response>;
@@ -9,6 +9,7 @@ export default class TelegramBot extends TelegramApi {
9
9
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
10
10
  ai;
11
11
  db;
12
+ r2;
12
13
  bot_name;
13
14
  constructor(config) {
14
15
  super(config.commands, config.webhook, config.handler);
@@ -17,127 +18,111 @@ export default class TelegramBot extends TelegramApi {
17
18
  this.get_set = config.kv?.get_set;
18
19
  this.ai = config.ai;
19
20
  this.db = config.db;
21
+ this.r2 = config.r2;
20
22
  this.bot_name = config.bot_name;
21
23
  }
22
- // bot command: /sean
23
- sean = async (update, args) => {
24
- const ai = new Ai(this.ai);
25
- let prompt;
26
- if (args[0][0] === "/") {
27
- prompt = args.slice(1).join(" ");
28
- }
29
- else {
30
- prompt = args.join(" ");
31
- }
32
- console.log({ prompt });
33
- if (prompt === "") {
34
- prompt = "no prompt specified";
35
- }
36
- const messages = [
37
- { role: "system", content: "You are friendly" },
38
- { role: "system", content: "Don't show code to the user" },
39
- { role: "system", content: "Sean Behan is born on 09/07/1998" },
40
- { role: "system", content: "Sean Behan is a full stack developer" },
41
- { role: "system", content: "Sean Behan is from Toronto, Canada" },
42
- { role: "system", content: "Sean Behan's GitHub username is codebam" },
43
- {
44
- role: "system",
45
- content: "Sean Behan enjoys playing video games and coding",
46
- },
47
- { role: "system", content: "Sean Behan's website is seanbehan.ca" },
48
- {
49
- role: "system",
50
- content: "Sean Behan's email address is contact@seanbehan.ca",
51
- },
52
- {
53
- role: "system",
54
- content: "Some of your personal projects include a serverless telegram bot, and a serverless pastebin on cloudflare workers",
55
- },
56
- {
57
- role: "system",
58
- content: "When spoken to respond with what you know about Sean Behan",
59
- },
60
- {
61
- role: "system",
62
- content: `you are talking to ${update.message?.from.first_name}`,
63
- },
64
- { role: "user", content: prompt },
65
- ];
66
- const result = await ai.run("@cf/meta/llama-2-7b-chat-int8", {
67
- messages,
68
- });
69
- return this.sendMessage(update.message?.chat.id ?? 0, result.response);
70
- };
71
24
  // bot command: /clear
72
25
  // reset the llama2 session by deleting messages from d1
73
26
  clear = async (update) => {
74
27
  const { success } = await this.db
75
28
  .prepare("DELETE FROM Messages WHERE userId=?")
76
- .bind(update.message?.from.id)
29
+ .bind(update.inline_query
30
+ ? update.inline_query.from.id
31
+ : update.message?.from.id)
77
32
  .run();
78
33
  if (success) {
34
+ if (update.inline_query) {
35
+ return this.answerInlineQuery(update.inline_query.id, [
36
+ new TelegramInlineQueryResultArticle("_"),
37
+ ]);
38
+ }
79
39
  return this.sendMessage(update.message?.chat.id ?? 0, "_");
80
40
  }
81
41
  return this.sendMessage(update.message?.chat.id ?? 0, "failed");
82
42
  };
43
+ // bot command: /image
44
+ image = async (update, args) => {
45
+ const ai = new Ai(this.ai);
46
+ let _prompt;
47
+ if (args[0][0] === "/") {
48
+ _prompt = args.slice(1).join(" ");
49
+ }
50
+ else {
51
+ _prompt = args.join(" ");
52
+ }
53
+ if (_prompt === "") {
54
+ _prompt = "";
55
+ }
56
+ const inputs = { prompt: _prompt, num_steps: 20 };
57
+ await this.sendMessage(update.message?.chat.id ?? 0, "image is processing. please wait...");
58
+ const response = await ai.run("@cf/stabilityai/stable-diffusion-xl-base-1.0", inputs);
59
+ const id = crypto.randomUUID();
60
+ await this.r2.put(id, response);
61
+ const url = "https://r2.seanbehan.ca/" + id;
62
+ return this.sendPhoto(update.message?.chat.id ?? 0, url);
63
+ };
83
64
  // bot command: /question
84
65
  question = async (update, args) => {
85
66
  if (this.ai === undefined) {
86
67
  return new Response("ok");
87
68
  }
88
69
  const ai = new Ai(this.ai);
89
- let prompt;
70
+ let _prompt;
90
71
  if (args[0][0] === "/") {
91
- prompt = args.slice(1).join(" ");
72
+ _prompt = args.slice(1).join(" ");
92
73
  }
93
74
  else {
94
- prompt = args.join(" ");
95
- }
96
- if (prompt === "") {
97
- prompt = "";
75
+ _prompt = args.join(" ");
98
76
  }
99
- let _results;
100
- if (this.db) {
101
- _results = await this.db
102
- .prepare("SELECT * FROM Messages WHERE userId=?")
103
- .bind(update.message?.from.id)
104
- .all();
77
+ if (_prompt === "") {
78
+ _prompt = "";
105
79
  }
106
- const results = _results?.results;
107
- let old_messages;
108
- if (results) {
109
- old_messages = results.map((col) => ({
110
- role: "system",
111
- content: col.content,
112
- }));
113
- }
114
- const { response } = await ai.run("@cf/meta/llama-2-7b-chat-int8", {
115
- messages: [
116
- {
117
- role: "system",
118
- content: `your name is ${this.bot_name}`,
119
- },
120
- {
121
- role: "system",
122
- content: `you are talking to ${update.message?.from.first_name}`,
123
- },
124
- {
80
+ const results = await (async () => {
81
+ if (this.db) {
82
+ const { results } = await this.db
83
+ .prepare("SELECT * FROM Messages WHERE userId=?")
84
+ .bind(update.inline_query
85
+ ? update.inline_query.from.id
86
+ : update.message?.from.id)
87
+ .all();
88
+ return results;
89
+ }
90
+ })();
91
+ const old_messages = (() => {
92
+ if (results) {
93
+ return results.map((col) => ({
125
94
  role: "system",
126
- content: `your source code is at https://github.com/codebam/cf-workers-telegram-bot`,
127
- },
128
- ...(() => {
129
- if (old_messages) {
130
- return old_messages;
131
- }
132
- return [];
133
- })(),
134
- { role: "user", content: prompt },
135
- ],
136
- });
95
+ content: col.content,
96
+ }));
97
+ }
98
+ return [];
99
+ })();
100
+ const system_prompt = "<s>" +
101
+ [
102
+ `Your name is ${this.bot_name}.`,
103
+ `You are talking to ${update.message?.from.first_name}.`,
104
+ `Your source code is at https://github.com/codebam/cf-workers-telegram-bot .`,
105
+ `the current date is ${new Date().toString()}`,
106
+ ].reduce((acc, cur) => {
107
+ return acc + cur + "\n";
108
+ }) +
109
+ old_messages.reduce((acc, cur) => {
110
+ return acc + cur.content + "\n";
111
+ }, "") +
112
+ "</s>";
113
+ const response = await ai
114
+ .run("@hf/thebloke/llama-2-13b-chat-awq", {
115
+ prompt: system_prompt + "[INST]" + _prompt + "[/INST]",
116
+ })
117
+ .then(({ response }) => response
118
+ .replace(/(\[|)(\/|)INST(S|)(s|)(\]|)/, "")
119
+ .replace(/<<(\/|)SYS>>/, ""));
137
120
  if (this.db) {
138
121
  const { success } = await this.db
139
122
  .prepare("INSERT INTO Messages (id, userId, content) VALUES (?, ?, ?)")
140
- .bind(crypto.randomUUID(), update.message?.from.id, "[INST] " + prompt + " [/INST]" + "\n" + response)
123
+ .bind(crypto.randomUUID(), update.inline_query
124
+ ? update.inline_query.from.id
125
+ : update.message?.from.id, "[INST] " + _prompt + " [/INST]" + "\n" + response)
141
126
  .run();
142
127
  if (!success) {
143
128
  console.log("failed to insert data into d1");
@@ -147,19 +132,55 @@ export default class TelegramBot extends TelegramApi {
147
132
  this.clear(update);
148
133
  return this.question(update, args);
149
134
  } // sometimes llama2 doesn't respond when given lots of system prompts
150
- return this.sendMessage(update.message?.chat.id ?? 0, response);
135
+ if (update.inline_query) {
136
+ return this.answerInlineQuery(update.inline_query.id, [
137
+ new TelegramInlineQueryResultArticle(response),
138
+ ]);
139
+ }
140
+ return this.sendMessage(update.message?.chat.id ?? 0, response, "", false, false, update.message?.message_id);
151
141
  };
152
- // bot command: /paste
153
- paste = async (update, args) => {
154
- const formdata = new FormData();
155
- formdata.append("upload", args.slice(1).join(" "));
156
- const request = await fetch("https://pastebin.seanbehan.ca", {
157
- method: "POST",
158
- body: formdata,
159
- redirect: "manual",
160
- });
161
- const url = request.headers.get("location");
162
- return this.sendMessage(update.message?.chat.id ?? 0, url ?? "failed to upload");
142
+ // bot command: /sean
143
+ sean = async (update, args) => {
144
+ if (this.ai === undefined) {
145
+ return new Response("ok");
146
+ }
147
+ const ai = new Ai(this.ai);
148
+ let _prompt;
149
+ if (args[0][0] === "/") {
150
+ _prompt = args.slice(1).join(" ");
151
+ }
152
+ else {
153
+ _prompt = args.join(" ");
154
+ }
155
+ if (_prompt === "") {
156
+ _prompt = "";
157
+ }
158
+ const system_prompt = "<s>" +
159
+ [
160
+ `Your name is ${this.bot_name}.`,
161
+ `You are talking to ${update.message?.from.first_name}.`,
162
+ `Your source code is at https://github.com/codebam/cf-workers-telegram-bot .`,
163
+ `the current date is ${new Date().toString()}`,
164
+ "Sean Behan is a full stack developer who goes by the username codebam.",
165
+ "Sean Behan likes programming and video games.",
166
+ "Pretend to be Sean Behan but don't make things up.",
167
+ ].reduce((acc, cur) => {
168
+ return acc + cur + "\n";
169
+ }) +
170
+ "</s>";
171
+ const response = await ai
172
+ .run("@hf/thebloke/llama-2-13b-chat-awq", {
173
+ prompt: system_prompt + "[INST]" + _prompt + "[/INST]",
174
+ })
175
+ .then(({ response }) => response
176
+ .replace(/(\[|)(\/|)INST(S|)(s|)(\]|)/, "")
177
+ .replace(/<<(\/|)SYS>>/, ""));
178
+ if (update.inline_query) {
179
+ return this.answerInlineQuery(update.inline_query.id, [
180
+ new TelegramInlineQueryResultArticle(response),
181
+ ]);
182
+ }
183
+ return this.sendMessage(update.message?.chat.id ?? 0, response, "", false, false, update.message?.message_id);
163
184
  };
164
185
  // bot command: /code
165
186
  code = async (update) => ((url) => update.inline_query
@@ -240,13 +261,6 @@ export default class TelegramBot extends TelegramApi {
240
261
  .then((shibe_response) => update.inline_query
241
262
  ? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultPhoto(shibe_response[0])], 0)
242
263
  : this.sendPhoto(update.message?.chat.id ?? 0, shibe_response[0]));
243
- // bot command: /cat
244
- cat = async (update) => fetch("https://meow.senither.com/v1/random")
245
- .then((response) => response.json())
246
- .then((json) => json)
247
- .then((json) => update.inline_query
248
- ? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultPhoto(json.data.url)], 0)
249
- : this.sendPhoto(update.message?.chat.id ?? 0, json.data.url));
250
264
  // bot command: /bored
251
265
  bored = async (update) => fetch("https://boredapi.com/api/activity/")
252
266
  .then((response) => responseToJSON(response))
@@ -258,43 +272,7 @@ export default class TelegramBot extends TelegramApi {
258
272
  epoch = async (update) => ((seconds) => update.inline_query
259
273
  ? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(seconds)], 0)
260
274
  : this.sendMessage(update.message?.chat.id ?? 0, seconds))(Math.floor(Date.now() / 1000).toString());
261
- // bot command: /get
262
- _get = async (update, args) => ((key) => this.get_set
263
- .get(key)
264
- .then((value) => update.inline_query
265
- ? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(value ?? "")], 0)
266
- : this.sendMessage(update.message?.chat.id ?? 0, value ?? "")))(args[1]);
267
- // bot command: /set
268
- _set = async (update, args) => {
269
- const key = args[1];
270
- const value = args.slice(2).join(" ");
271
- const message = `set ${key} to ${value}`;
272
- this.get_set.put(key, value).then(() => {
273
- if (update.inline_query) {
274
- return this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(message)], 0);
275
- }
276
- return this.sendMessage(update.message?.chat.id ?? 0, message);
277
- });
278
- return new Response();
279
- };
280
275
  _average = (numbers) => parseFloat((numbers.reduce((prev, cur) => prev + cur, 0) / numbers.length || 0).toFixed(2));
281
- // bot command: /recursion
282
- recursion = async (update) => this.sendMessage(update.message?.chat.id ?? 0, "/recursion");
283
- // .then((response) => response.json())
284
- // .then((result) =>
285
- // this.handler.postResponse(
286
- // new Request("", {
287
- // method: "POST",
288
- // body: JSON.stringify({
289
- // message: {
290
- // text: (result as { result: { text: string } }).result.text,
291
- // chat: { id: update.message?.chat.id },
292
- // },
293
- // }),
294
- // }),
295
- // this
296
- // )
297
- // );
298
276
  // bot command: /roll
299
277
  roll = async (update, args) => ((outcome, message) => update.inline_query
300
278
  ? this.answerInlineQuery(update.inline_query.id, [
@@ -302,7 +280,7 @@ export default class TelegramBot extends TelegramApi {
302
280
  ])
303
281
  : this.sendMessage(update.message?.chat.id ?? 0, message(update.message?.from.username ?? "", update.message?.from.first_name ?? "", outcome)))(Math.floor(Math.random() * (parseInt(args[1]) || 6 - 1 + 1) + 1), (username, first_name, outcome) => `${first_name ?? username} rolled a ${parseInt(args[1]) || 6} sided die. it landed on ${outcome}`);
304
282
  // bot command: /commandList
305
- commandList = async (update) => this.sendMessage(update.message?.chat.id ?? 0, `<pre>${JSON.stringify(Object.keys(this.commands))}</pre>`, "HTML");
283
+ commandList = async (update) => this.sendMessage(update.message?.chat.id ?? 0, `${Object.keys(this.commands).join("\n")}`, "HTML");
306
284
  // bot command: /toss
307
285
  toss = async (update) => this.sendMessage(update.message?.chat.id ?? 0, Math.floor(Math.random() * 2) == 0 ? "heads" : "tails");
308
286
  // bot command: /ping
@@ -1,23 +1,18 @@
1
- /// <reference types="@cloudflare/workers-types" />
2
- import TelegramBot from "./telegram_bot";
3
- import { TelegramUpdate } from "./types";
1
+ import { TelegramCommand } from "./types";
4
2
  export default class TelegramCommands {
5
- static ping: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
6
- static toss: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
7
- static epoch: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
8
- static kanye: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
9
- static bored: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
10
- static joke: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
11
- static dog: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
12
- static cat: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
13
- static roll: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
14
- static _get: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
15
- static _set: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
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>;
19
- static sean: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
20
- static clear: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
21
- static code: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
22
- static commandList: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
3
+ static ping: TelegramCommand;
4
+ static toss: TelegramCommand;
5
+ static epoch: TelegramCommand;
6
+ static kanye: TelegramCommand;
7
+ static bored: TelegramCommand;
8
+ static joke: TelegramCommand;
9
+ static dog: TelegramCommand;
10
+ static roll: TelegramCommand;
11
+ static duckduckgo: TelegramCommand;
12
+ static question: TelegramCommand;
13
+ static sean: TelegramCommand;
14
+ static clear: TelegramCommand;
15
+ static code: TelegramCommand;
16
+ static commandList: TelegramCommand;
17
+ static image: TelegramCommand;
23
18
  }
@@ -6,15 +6,12 @@ export default class TelegramCommands {
6
6
  static bored = async (bot, update) => bot.bored(update);
7
7
  static joke = async (bot, update) => bot.joke(update);
8
8
  static dog = async (bot, update) => bot.dog(update);
9
- static cat = async (bot, update) => bot.cat(update);
10
9
  static roll = async (bot, update, args) => bot.roll(update, args);
11
- static _get = async (bot, update, args) => bot._get(update, args);
12
- static _set = async (bot, update, args) => bot._set(update, args);
13
10
  static duckduckgo = async (bot, update, args) => bot.duckduckgo(update, args);
14
- static paste = async (bot, update, args) => bot.paste(update, args);
15
11
  static question = async (bot, update, args) => bot.question(update, args);
16
12
  static sean = async (bot, update, args) => bot.sean(update, args);
17
13
  static clear = async (bot, update) => bot.clear(update);
18
14
  static code = async (bot, update) => bot.code(update);
19
15
  static commandList = async (bot, update) => bot.commandList(update);
16
+ static image = async (bot, update, args) => bot.image(update, args);
20
17
  }
@@ -1,11 +1,11 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
2
  import BotApi from "./bot_api";
3
3
  import Handler from "./handler";
4
- import TelegramApi from "./telegram_api";
4
+ import TelegramBot from "./telegram_bot";
5
5
  import Webhook from "./webhook";
6
6
  export { Webhook };
7
- export type Command = (bot: BotApi, update: Update, args?: string[]) => Promise<Response>;
8
- export type TelegramCommand = (bot: TelegramApi, update: TelegramUpdate, args?: string[]) => Promise<Response>;
7
+ export type Command = (bot: BotApi, update: Update, args: string[]) => Promise<Response>;
8
+ export type TelegramCommand = (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
9
9
  export type Commands = Record<string, Command>;
10
10
  export type Kv = Record<string, KVNamespace> | undefined;
11
11
  export declare class Config {
@@ -18,6 +18,7 @@ export declare class Config {
18
18
  handler: Handler;
19
19
  ai: any;
20
20
  db: any;
21
+ r2: any;
21
22
  constructor(config?: Partial<Config>);
22
23
  }
23
24
  export declare const localhost: URL;
@@ -14,6 +14,8 @@ export class Config {
14
14
  ai;
15
15
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
16
  db;
17
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
+ r2;
17
19
  constructor(config = {}) {
18
20
  this.bot_name = config.bot_name || "";
19
21
  this.api = config.api || BotApi;
@@ -24,6 +26,7 @@ export class Config {
24
26
  this.handler = config.handler || new Handler([]);
25
27
  this.ai = config.ai;
26
28
  this.db = config.db;
29
+ this.r2 = config.r2;
27
30
  }
28
31
  }
29
32
  export const localhost = new URL("http://localhost");
@@ -8,6 +8,7 @@ interface Environment {
8
8
  SECRET_TELEGRAM_API_TOKEN4: string;
9
9
  AI: string;
10
10
  DB: D1Database;
11
+ R2: R2Bucket;
11
12
  }
12
13
  declare const _default: {
13
14
  fetch: (request: Request, env: Environment) => Promise<Response>;
@@ -7,6 +7,7 @@ export default {
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
9
  default: TelegramCommands.question,
10
+ inline: TelegramCommands.question,
10
11
  "/ping": TelegramCommands.ping,
11
12
  "/toss": TelegramCommands.toss,
12
13
  "/epoch": TelegramCommands.epoch,
@@ -14,23 +15,20 @@ export default {
14
15
  "/bored": TelegramCommands.bored,
15
16
  "/joke": TelegramCommands.joke,
16
17
  "/dog": TelegramCommands.dog,
17
- "/cat": TelegramCommands.cat,
18
18
  "/roll": TelegramCommands.roll,
19
- "/get": TelegramCommands._get,
20
- "/set": TelegramCommands._set,
21
19
  "/duckduckgo": TelegramCommands.duckduckgo,
22
20
  "/code": TelegramCommands.code,
23
- "/paste": TelegramCommands.paste,
24
21
  "/commands": TelegramCommands.commandList,
25
22
  "/question": TelegramCommands.question,
26
23
  "/clear": TelegramCommands.clear,
27
24
  "/help": TelegramCommands.commandList,
28
- "/sean": TelegramCommands.sean,
25
+ "/image": TelegramCommands.image,
29
26
  "/start": TelegramCommands.commandList,
30
27
  },
31
28
  kv: { get_set: env.KV_GET_SET, uid_data: env.KV_UID_DATA },
32
29
  ai: env.AI,
33
30
  db: env.DB,
31
+ r2: env.R2,
34
32
  },
35
33
  {
36
34
  bot_name: "@duckduckbot",
@@ -38,7 +36,7 @@ export default {
38
36
  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)),
39
37
  commands: {
40
38
  default: TelegramCommands.duckduckgo,
41
- inline: TelegramCommands.duckduckgo,
39
+ inline: TelegramCommands.duckduckgo, // default inline response
42
40
  "/duckduckgo": TelegramCommands.duckduckgo,
43
41
  "/question": TelegramCommands.question,
44
42
  "/code": TelegramCommands.code,
@@ -71,7 +69,9 @@ export default {
71
69
  api: TelegramBot,
72
70
  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
71
  commands: {
74
- default: TelegramCommands.question,
72
+ default: TelegramCommands.sean,
73
+ "/clear": TelegramCommands.clear,
74
+ "/start": TelegramCommands.commandList,
75
75
  },
76
76
  ai: env.AI,
77
77
  db: env.DB,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "5.13.0",
3
+ "version": "5.14.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.20231010.0",
33
- "@typescript-eslint/eslint-plugin": "^6.7.5",
34
- "@typescript-eslint/parser": "^6.7.5",
35
- "eslint": "^8.51.0",
32
+ "@cloudflare/workers-types": "^4.20231121.0",
33
+ "@typescript-eslint/eslint-plugin": "^6.12.0",
34
+ "@typescript-eslint/parser": "^6.12.0",
35
+ "eslint": "^8.54.0",
36
36
  "eslint-config-prettier": "^9.0.0",
37
- "lerna": "^7.3.1",
38
- "prettier": "^3.0.3",
39
- "typescript": "^5.2.2"
37
+ "lerna": "^8.0.0",
38
+ "prettier": "^3.1.0",
39
+ "typescript": "^5.3.2"
40
40
  },
41
41
  "dependencies": {
42
- "@cloudflare/ai": "^1.0.16"
42
+ "@cloudflare/ai": "^1.0.33"
43
43
  },
44
- "gitHead": "f0e63bfc828c86d0559c74d2c305fc0f7371996f"
44
+ "gitHead": "b61227dbb262c62ceb5c2401b71a353caa3e6346"
45
45
  }