@codebam/cf-workers-telegram-bot 5.15.0 → 5.17.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.
|
@@ -9,6 +9,7 @@ export default class TelegramBot extends TelegramApi {
|
|
|
9
9
|
db: D1Database;
|
|
10
10
|
r2: R2Bucket;
|
|
11
11
|
bot_name: string;
|
|
12
|
+
chat_model: string;
|
|
12
13
|
constructor(config: Config);
|
|
13
14
|
translate: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
14
15
|
clear: (update: TelegramUpdate) => Promise<Response>;
|
|
@@ -1,7 +1,6 @@
|
|
|
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";
|
|
5
4
|
export default class TelegramBot extends TelegramApi {
|
|
6
5
|
url;
|
|
7
6
|
kv;
|
|
@@ -11,6 +10,7 @@ export default class TelegramBot extends TelegramApi {
|
|
|
11
10
|
db;
|
|
12
11
|
r2;
|
|
13
12
|
bot_name;
|
|
13
|
+
chat_model;
|
|
14
14
|
constructor(config) {
|
|
15
15
|
super(config.commands, config.webhook, config.handler);
|
|
16
16
|
this.url = config.url;
|
|
@@ -20,13 +20,13 @@ export default class TelegramBot extends TelegramApi {
|
|
|
20
20
|
this.db = config.db;
|
|
21
21
|
this.r2 = config.r2;
|
|
22
22
|
this.bot_name = config.bot_name;
|
|
23
|
+
this.chat_model = config.chat_model;
|
|
23
24
|
}
|
|
24
25
|
// bot command: /translate
|
|
25
26
|
translate = async (update, args) => {
|
|
26
27
|
if (this.ai === undefined) {
|
|
27
28
|
return new Response("ok");
|
|
28
29
|
}
|
|
29
|
-
const ai = new Ai(this.ai);
|
|
30
30
|
let _prompt;
|
|
31
31
|
if (args[0][0] === "/") {
|
|
32
32
|
_prompt = args.slice(1).join(" ");
|
|
@@ -39,7 +39,7 @@ export default class TelegramBot extends TelegramApi {
|
|
|
39
39
|
}
|
|
40
40
|
const langs = ["french", "arabic", "german", "spanish", "russian"];
|
|
41
41
|
const inline_articles = await Promise.all(langs.map(async (lang) => {
|
|
42
|
-
const response = await ai.run("@cf/meta/m2m100-1.2b", {
|
|
42
|
+
const response = await this.ai.run("@cf/meta/m2m100-1.2b", {
|
|
43
43
|
text: _prompt,
|
|
44
44
|
source_lang: lang,
|
|
45
45
|
target_lang: "english",
|
|
@@ -69,7 +69,6 @@ export default class TelegramBot extends TelegramApi {
|
|
|
69
69
|
};
|
|
70
70
|
// bot command: /image
|
|
71
71
|
image = async (update, args) => {
|
|
72
|
-
const ai = new Ai(this.ai);
|
|
73
72
|
let _prompt;
|
|
74
73
|
if (args[0][0] === "/") {
|
|
75
74
|
_prompt = args.slice(1).join(" ");
|
|
@@ -82,7 +81,7 @@ export default class TelegramBot extends TelegramApi {
|
|
|
82
81
|
}
|
|
83
82
|
const inputs = { prompt: _prompt, num_steps: 20 };
|
|
84
83
|
await this.sendMessage(update.message?.chat.id ?? 0, "image is processing. please wait...");
|
|
85
|
-
const response = await ai.run("@cf/stabilityai/stable-diffusion-xl-base-1.0", inputs);
|
|
84
|
+
const response = await this.ai.run("@cf/stabilityai/stable-diffusion-xl-base-1.0", inputs);
|
|
86
85
|
const id = crypto.randomUUID();
|
|
87
86
|
await this.r2.put(id, response);
|
|
88
87
|
const url = "https://r2.seanbehan.ca/" + id;
|
|
@@ -93,7 +92,6 @@ export default class TelegramBot extends TelegramApi {
|
|
|
93
92
|
if (this.ai === undefined) {
|
|
94
93
|
return new Response("ok");
|
|
95
94
|
}
|
|
96
|
-
const ai = new Ai(this.ai);
|
|
97
95
|
let _prompt;
|
|
98
96
|
if (args[0][0] === "/") {
|
|
99
97
|
_prompt = args.slice(1).join(" ");
|
|
@@ -139,8 +137,8 @@ export default class TelegramBot extends TelegramApi {
|
|
|
139
137
|
"</s>";
|
|
140
138
|
const p = system_prompt + "[INST]" + _prompt + "[/INST]";
|
|
141
139
|
const prompt = p.slice(p.length - 4096, p.length);
|
|
142
|
-
const response = await ai
|
|
143
|
-
.run(
|
|
140
|
+
const response = await this.ai
|
|
141
|
+
.run(this.chat_model, {
|
|
144
142
|
prompt,
|
|
145
143
|
max_tokens: 596,
|
|
146
144
|
})
|
|
@@ -174,7 +172,6 @@ export default class TelegramBot extends TelegramApi {
|
|
|
174
172
|
if (this.ai === undefined) {
|
|
175
173
|
return new Response("ok");
|
|
176
174
|
}
|
|
177
|
-
const ai = new Ai(this.ai);
|
|
178
175
|
let _prompt;
|
|
179
176
|
if (args[0][0] === "/") {
|
|
180
177
|
_prompt = args.slice(1).join(" ");
|
|
@@ -223,8 +220,8 @@ export default class TelegramBot extends TelegramApi {
|
|
|
223
220
|
"</s>";
|
|
224
221
|
const p = system_prompt + "[INST]" + _prompt + "[/INST]";
|
|
225
222
|
const prompt = p.slice(p.length - 4096, p.length);
|
|
226
|
-
const response = await ai
|
|
227
|
-
.run(
|
|
223
|
+
const response = await this.ai
|
|
224
|
+
.run(this.chat_model, {
|
|
228
225
|
prompt,
|
|
229
226
|
max_tokens: 596,
|
|
230
227
|
})
|
package/dist/main/src/types.d.ts
CHANGED
package/dist/main/src/types.js
CHANGED
|
@@ -16,6 +16,7 @@ export class Config {
|
|
|
16
16
|
db;
|
|
17
17
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
18
|
r2;
|
|
19
|
+
chat_model;
|
|
19
20
|
constructor(config = {}) {
|
|
20
21
|
this.bot_name = config.bot_name || "";
|
|
21
22
|
this.api = config.api || BotApi;
|
|
@@ -27,6 +28,7 @@ export class Config {
|
|
|
27
28
|
this.ai = config.ai;
|
|
28
29
|
this.db = config.db;
|
|
29
30
|
this.r2 = config.r2;
|
|
31
|
+
this.chat_model = config.chat_model;
|
|
30
32
|
}
|
|
31
33
|
}
|
|
32
34
|
export const localhost = new URL("http://localhost");
|
|
@@ -7,9 +7,10 @@ interface Environment {
|
|
|
7
7
|
SECRET_TELEGRAM_API_TOKEN3: string;
|
|
8
8
|
SECRET_TELEGRAM_API_TOKEN4: string;
|
|
9
9
|
SECRET_TELEGRAM_API_TOKEN5: string;
|
|
10
|
-
AI:
|
|
10
|
+
AI: Ai;
|
|
11
11
|
DB: D1Database;
|
|
12
12
|
R2: R2Bucket;
|
|
13
|
+
CHAT_MODEL: string;
|
|
13
14
|
}
|
|
14
15
|
declare const _default: {
|
|
15
16
|
fetch: (request: Request, env: Environment) => Promise<Response>;
|
package/package.json
CHANGED
|
@@ -1,45 +1,48 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
2
|
+
"name": "@codebam/cf-workers-telegram-bot",
|
|
3
|
+
"version": "5.17.0",
|
|
4
|
+
"description": "serverless telegram bot on cf workers",
|
|
5
|
+
"main": "./dist/main/src/main.js",
|
|
6
|
+
"module": "./dist/main/src/main.js",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"LICENSE",
|
|
10
|
+
"LICENSE_MIT",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"keywords": [
|
|
14
|
+
"cloudflare",
|
|
15
|
+
"telegram",
|
|
16
|
+
"worker",
|
|
17
|
+
"webhook"
|
|
18
|
+
],
|
|
19
|
+
"type": "module",
|
|
20
|
+
"private": false,
|
|
21
|
+
"scripts": {
|
|
22
|
+
"build": "tsc --project tsconfig.json",
|
|
23
|
+
"lint": "eslint src"
|
|
24
|
+
},
|
|
25
|
+
"author": "codebam",
|
|
26
|
+
"license": "Apache-2.0",
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": "https://github.com/codebam/cf-workers-telegram-bot.git"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@cloudflare/workers-types": "^4.20240502.0",
|
|
33
|
+
"@eslint/js": "^9.1.1",
|
|
34
|
+
"@typescript-eslint/eslint-plugin": "^7.8.0",
|
|
35
|
+
"@typescript-eslint/parser": "^7.8.0",
|
|
36
|
+
"eslint": "^9.1.1",
|
|
37
|
+
"eslint-config-prettier": "^9.1.0",
|
|
38
|
+
"globals": "^15.1.0",
|
|
39
|
+
"lerna": "^8.1.2",
|
|
40
|
+
"prettier": "^3.2.5",
|
|
41
|
+
"typescript": "^5.4.5",
|
|
42
|
+
"typescript-eslint": "^7.8.0"
|
|
43
|
+
},
|
|
44
|
+
"dependencies": {
|
|
45
|
+
"@cloudflare/ai": "1.0.53"
|
|
46
|
+
},
|
|
47
|
+
"gitHead": "efc0514c91ebd14c2d564b6917c79073ac07a56f"
|
|
45
48
|
}
|