@codebam/cf-workers-telegram-bot 5.10.0 → 5.13.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.
|
@@ -7,6 +7,7 @@ export default class TelegramBot extends TelegramApi {
|
|
|
7
7
|
get_set: KVNamespace;
|
|
8
8
|
ai: any;
|
|
9
9
|
db: D1Database;
|
|
10
|
+
bot_name: string;
|
|
10
11
|
constructor(config: Config);
|
|
11
12
|
sean: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
12
13
|
clear: (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
|
+
bot_name;
|
|
12
13
|
constructor(config) {
|
|
13
14
|
super(config.commands, config.webhook, config.handler);
|
|
14
15
|
this.url = config.url;
|
|
@@ -16,6 +17,7 @@ export default class TelegramBot extends TelegramApi {
|
|
|
16
17
|
this.get_set = config.kv?.get_set;
|
|
17
18
|
this.ai = config.ai;
|
|
18
19
|
this.db = config.db;
|
|
20
|
+
this.bot_name = config.bot_name;
|
|
19
21
|
}
|
|
20
22
|
// bot command: /sean
|
|
21
23
|
sean = async (update, args) => {
|
|
@@ -80,41 +82,66 @@ export default class TelegramBot extends TelegramApi {
|
|
|
80
82
|
};
|
|
81
83
|
// bot command: /question
|
|
82
84
|
question = async (update, args) => {
|
|
85
|
+
if (this.ai === undefined) {
|
|
86
|
+
return new Response("ok");
|
|
87
|
+
}
|
|
83
88
|
const ai = new Ai(this.ai);
|
|
84
89
|
let prompt;
|
|
85
90
|
if (args[0][0] === "/") {
|
|
86
91
|
prompt = args.slice(1).join(" ");
|
|
87
92
|
}
|
|
88
93
|
else {
|
|
89
|
-
prompt =
|
|
94
|
+
prompt = args.join(" ");
|
|
90
95
|
}
|
|
91
96
|
if (prompt === "") {
|
|
92
|
-
prompt = "
|
|
97
|
+
prompt = "";
|
|
98
|
+
}
|
|
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();
|
|
105
|
+
}
|
|
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
|
+
}));
|
|
93
113
|
}
|
|
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
114
|
const { response } = await ai.run("@cf/meta/llama-2-7b-chat-int8", {
|
|
103
115
|
messages: [
|
|
116
|
+
{
|
|
117
|
+
role: "system",
|
|
118
|
+
content: `your name is ${this.bot_name}`,
|
|
119
|
+
},
|
|
104
120
|
{
|
|
105
121
|
role: "system",
|
|
106
122
|
content: `you are talking to ${update.message?.from.first_name}`,
|
|
107
123
|
},
|
|
108
|
-
|
|
124
|
+
{
|
|
125
|
+
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
|
+
})(),
|
|
109
134
|
{ role: "user", content: prompt },
|
|
110
135
|
],
|
|
111
136
|
});
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
137
|
+
if (this.db) {
|
|
138
|
+
const { success } = await this.db
|
|
139
|
+
.prepare("INSERT INTO Messages (id, userId, content) VALUES (?, ?, ?)")
|
|
140
|
+
.bind(crypto.randomUUID(), update.message?.from.id, "[INST] " + prompt + " [/INST]" + "\n" + response)
|
|
141
|
+
.run();
|
|
142
|
+
if (!success) {
|
|
143
|
+
console.log("failed to insert data into d1");
|
|
144
|
+
}
|
|
118
145
|
}
|
|
119
146
|
if (response === "") {
|
|
120
147
|
this.clear(update);
|
|
@@ -2,7 +2,7 @@ import { TelegramCommands, Handler, TelegramWebhook, TelegramBot, } from "../../
|
|
|
2
2
|
export default {
|
|
3
3
|
fetch: async (request, env) => new Handler([
|
|
4
4
|
{
|
|
5
|
-
bot_name: "
|
|
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: {
|
|
@@ -47,6 +47,7 @@ export default {
|
|
|
47
47
|
"/start": TelegramCommands.commandList,
|
|
48
48
|
},
|
|
49
49
|
ai: env.AI,
|
|
50
|
+
db: env.DB,
|
|
50
51
|
},
|
|
51
52
|
{
|
|
52
53
|
bot_name: "@ddggbot",
|
|
@@ -63,15 +64,17 @@ export default {
|
|
|
63
64
|
"/start": TelegramCommands.commandList,
|
|
64
65
|
},
|
|
65
66
|
ai: env.AI,
|
|
67
|
+
db: env.DB,
|
|
66
68
|
},
|
|
67
69
|
{
|
|
68
70
|
bot_name: "@SeanB_robot",
|
|
69
71
|
api: TelegramBot,
|
|
70
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)),
|
|
71
73
|
commands: {
|
|
72
|
-
default: TelegramCommands.
|
|
74
|
+
default: TelegramCommands.question,
|
|
73
75
|
},
|
|
74
76
|
ai: env.AI,
|
|
77
|
+
db: env.DB,
|
|
75
78
|
},
|
|
76
79
|
]).handle(request),
|
|
77
80
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codebam/cf-workers-telegram-bot",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.13.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.
|
|
33
|
-
"@typescript-eslint/eslint-plugin": "^6.7.
|
|
34
|
-
"@typescript-eslint/parser": "^6.7.
|
|
32
|
+
"@cloudflare/workers-types": "^4.20231010.0",
|
|
33
|
+
"@typescript-eslint/eslint-plugin": "^6.7.5",
|
|
34
|
+
"@typescript-eslint/parser": "^6.7.5",
|
|
35
35
|
"eslint": "^8.51.0",
|
|
36
36
|
"eslint-config-prettier": "^9.0.0",
|
|
37
|
-
"lerna": "^7.3.
|
|
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.
|
|
42
|
+
"@cloudflare/ai": "^1.0.16"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "f0e63bfc828c86d0559c74d2c305fc0f7371996f"
|
|
45
45
|
}
|