@codebam/cf-workers-telegram-bot 5.12.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,23 +7,21 @@ export default class TelegramBot extends TelegramApi {
7
7
  get_set: KVNamespace;
8
8
  ai: any;
9
9
  db: D1Database;
10
+ r2: R2Bucket;
11
+ bot_name: string;
10
12
  constructor(config: Config);
11
- sean: (update: TelegramUpdate, args: string[]) => Promise<Response>;
12
13
  clear: (update: TelegramUpdate) => Promise<Response>;
14
+ image: (update: TelegramUpdate, args: string[]) => Promise<Response>;
13
15
  question: (update: TelegramUpdate, args: string[]) => Promise<Response>;
14
- paste: (update: TelegramUpdate, args: string[]) => Promise<Response>;
16
+ sean: (update: TelegramUpdate, args: string[]) => Promise<Response>;
15
17
  code: (update: TelegramUpdate) => Promise<Response>;
16
18
  duckduckgo: (update: TelegramUpdate, args: string[]) => Promise<Response>;
17
19
  kanye: (update: TelegramUpdate) => Promise<Response>;
18
20
  joke: (update: TelegramUpdate) => Promise<Response>;
19
21
  dog: (update: TelegramUpdate) => Promise<Response>;
20
- cat: (update: TelegramUpdate) => Promise<Response>;
21
22
  bored: (update: TelegramUpdate) => Promise<Response>;
22
23
  epoch: (update: TelegramUpdate) => Promise<Response>;
23
- _get: (update: TelegramUpdate, args: string[]) => Promise<Response>;
24
- _set: (update: TelegramUpdate, args: string[]) => Promise<Response>;
25
24
  _average: (numbers: number[]) => number;
26
- recursion: (update: TelegramUpdate) => Promise<Response>;
27
25
  roll: (update: TelegramUpdate, args: string[]) => Promise<Response>;
28
26
  commandList: (update: TelegramUpdate) => Promise<Response>;
29
27
  toss: (update: TelegramUpdate) => Promise<Response>;
@@ -9,6 +9,8 @@ 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;
13
+ bot_name;
12
14
  constructor(config) {
13
15
  super(config.commands, config.webhook, config.handler);
14
16
  this.url = config.url;
@@ -16,123 +18,169 @@ export default class TelegramBot extends TelegramApi {
16
18
  this.get_set = config.kv?.get_set;
17
19
  this.ai = config.ai;
18
20
  this.db = config.db;
21
+ this.r2 = config.r2;
22
+ this.bot_name = config.bot_name;
19
23
  }
20
- // bot command: /sean
21
- sean = async (update, args) => {
22
- const ai = new Ai(this.ai);
23
- let prompt;
24
- if (args[0][0] === "/") {
25
- prompt = args.slice(1).join(" ");
26
- }
27
- else {
28
- prompt = args.join(" ");
29
- }
30
- console.log({ prompt });
31
- if (prompt === "") {
32
- prompt = "no prompt specified";
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
24
  // bot command: /clear
70
25
  // reset the llama2 session by deleting messages from d1
71
26
  clear = async (update) => {
72
27
  const { success } = await this.db
73
28
  .prepare("DELETE FROM Messages WHERE userId=?")
74
- .bind(update.message?.from.id)
29
+ .bind(update.inline_query
30
+ ? update.inline_query.from.id
31
+ : update.message?.from.id)
75
32
  .run();
76
33
  if (success) {
34
+ if (update.inline_query) {
35
+ return this.answerInlineQuery(update.inline_query.id, [
36
+ new TelegramInlineQueryResultArticle("_"),
37
+ ]);
38
+ }
77
39
  return this.sendMessage(update.message?.chat.id ?? 0, "_");
78
40
  }
79
41
  return this.sendMessage(update.message?.chat.id ?? 0, "failed");
80
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
+ };
81
64
  // bot command: /question
82
65
  question = async (update, args) => {
66
+ if (this.ai === undefined) {
67
+ return new Response("ok");
68
+ }
83
69
  const ai = new Ai(this.ai);
84
- let prompt;
70
+ let _prompt;
85
71
  if (args[0][0] === "/") {
86
- prompt = args.slice(1).join(" ");
72
+ _prompt = args.slice(1).join(" ");
87
73
  }
88
74
  else {
89
- prompt = "[INST] " + args.join(" ") + "[/INST]";
75
+ _prompt = args.join(" ");
90
76
  }
91
- if (prompt === "") {
92
- prompt = "[INST] no prompt specified [/INST]";
77
+ if (_prompt === "") {
78
+ _prompt = "";
93
79
  }
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
- }));
102
- const { response } = await ai.run("@cf/meta/llama-2-7b-chat-int8", {
103
- messages: [
104
- {
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) => ({
105
94
  role: "system",
106
- content: `you are talking to ${update.message?.from.first_name}`,
107
- },
108
- ...old_messages,
109
- { role: "user", content: prompt },
110
- ],
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");
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>>/, ""));
120
+ if (this.db) {
121
+ const { success } = await this.db
122
+ .prepare("INSERT INTO Messages (id, userId, content) VALUES (?, ?, ?)")
123
+ .bind(crypto.randomUUID(), update.inline_query
124
+ ? update.inline_query.from.id
125
+ : update.message?.from.id, "[INST] " + _prompt + " [/INST]" + "\n" + response)
126
+ .run();
127
+ if (!success) {
128
+ console.log("failed to insert data into d1");
129
+ }
118
130
  }
119
131
  if (response === "") {
120
132
  this.clear(update);
121
133
  return this.question(update, args);
122
134
  } // sometimes llama2 doesn't respond when given lots of system prompts
123
- 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);
124
141
  };
125
- // bot command: /paste
126
- paste = async (update, args) => {
127
- const formdata = new FormData();
128
- formdata.append("upload", args.slice(1).join(" "));
129
- const request = await fetch("https://pastebin.seanbehan.ca", {
130
- method: "POST",
131
- body: formdata,
132
- redirect: "manual",
133
- });
134
- const url = request.headers.get("location");
135
- 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);
136
184
  };
137
185
  // bot command: /code
138
186
  code = async (update) => ((url) => update.inline_query
@@ -213,13 +261,6 @@ export default class TelegramBot extends TelegramApi {
213
261
  .then((shibe_response) => update.inline_query
214
262
  ? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultPhoto(shibe_response[0])], 0)
215
263
  : this.sendPhoto(update.message?.chat.id ?? 0, shibe_response[0]));
216
- // bot command: /cat
217
- cat = async (update) => fetch("https://meow.senither.com/v1/random")
218
- .then((response) => response.json())
219
- .then((json) => json)
220
- .then((json) => update.inline_query
221
- ? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultPhoto(json.data.url)], 0)
222
- : this.sendPhoto(update.message?.chat.id ?? 0, json.data.url));
223
264
  // bot command: /bored
224
265
  bored = async (update) => fetch("https://boredapi.com/api/activity/")
225
266
  .then((response) => responseToJSON(response))
@@ -231,43 +272,7 @@ export default class TelegramBot extends TelegramApi {
231
272
  epoch = async (update) => ((seconds) => update.inline_query
232
273
  ? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(seconds)], 0)
233
274
  : this.sendMessage(update.message?.chat.id ?? 0, seconds))(Math.floor(Date.now() / 1000).toString());
234
- // bot command: /get
235
- _get = async (update, args) => ((key) => this.get_set
236
- .get(key)
237
- .then((value) => update.inline_query
238
- ? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(value ?? "")], 0)
239
- : this.sendMessage(update.message?.chat.id ?? 0, value ?? "")))(args[1]);
240
- // bot command: /set
241
- _set = async (update, args) => {
242
- const key = args[1];
243
- const value = args.slice(2).join(" ");
244
- const message = `set ${key} to ${value}`;
245
- this.get_set.put(key, value).then(() => {
246
- if (update.inline_query) {
247
- return this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(message)], 0);
248
- }
249
- return this.sendMessage(update.message?.chat.id ?? 0, message);
250
- });
251
- return new Response();
252
- };
253
275
  _average = (numbers) => parseFloat((numbers.reduce((prev, cur) => prev + cur, 0) / numbers.length || 0).toFixed(2));
254
- // bot command: /recursion
255
- recursion = async (update) => this.sendMessage(update.message?.chat.id ?? 0, "/recursion");
256
- // .then((response) => response.json())
257
- // .then((result) =>
258
- // this.handler.postResponse(
259
- // new Request("", {
260
- // method: "POST",
261
- // body: JSON.stringify({
262
- // message: {
263
- // text: (result as { result: { text: string } }).result.text,
264
- // chat: { id: update.message?.chat.id },
265
- // },
266
- // }),
267
- // }),
268
- // this
269
- // )
270
- // );
271
276
  // bot command: /roll
272
277
  roll = async (update, args) => ((outcome, message) => update.inline_query
273
278
  ? this.answerInlineQuery(update.inline_query.id, [
@@ -275,7 +280,7 @@ export default class TelegramBot extends TelegramApi {
275
280
  ])
276
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}`);
277
282
  // bot command: /commandList
278
- 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");
279
284
  // bot command: /toss
280
285
  toss = async (update) => this.sendMessage(update.message?.chat.id ?? 0, Math.floor(Math.random() * 2) == 0 ? "heads" : "tails");
281
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>;
@@ -2,11 +2,12 @@ import { TelegramCommands, Handler, TelegramWebhook, TelegramBot, } from "../../
2
2
  export default {
3
3
  fetch: async (request, env) => new Handler([
4
4
  {
5
- bot_name: "cf-workers-telegram-bot",
5
+ bot_name: "@TuxRobot",
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
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.12.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": "29952db72708bbfba9776cd31e6127d702b5ebed"
44
+ "gitHead": "b61227dbb262c62ceb5c2401b71a353caa3e6346"
45
45
  }