@codebam/cf-workers-telegram-bot 5.8.0 → 5.10.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.
- package/dist/main/src/telegram_api.js +6 -2
- package/dist/main/src/telegram_bot.d.ts +6 -0
- package/dist/main/src/telegram_bot.js +133 -3
- package/dist/main/src/telegram_commands.d.ts +4 -0
- package/dist/main/src/telegram_commands.js +4 -0
- package/dist/main/src/types.d.ts +2 -0
- package/dist/main/src/types.js +6 -0
- package/dist/worker/src/worker.d.ts +3 -0
- package/dist/worker/src/worker.js +22 -0
- package/package.json +11 -8
|
@@ -31,8 +31,12 @@ export default class TelegramApi extends BotApi {
|
|
|
31
31
|
? ((text_args) => ((command) => this.commands[command]
|
|
32
32
|
? this.commands[command]?.(this, update, [...text_args, ...args])
|
|
33
33
|
: log({
|
|
34
|
-
error: `command '${command}' does not exist`,
|
|
35
|
-
}) &&
|
|
34
|
+
error: `command '${command}' does not exist, using default`,
|
|
35
|
+
}) &&
|
|
36
|
+
this.commands["default"]?.(this, update, [
|
|
37
|
+
...text_args,
|
|
38
|
+
...args,
|
|
39
|
+
]))(
|
|
36
40
|
// run the command
|
|
37
41
|
this.getCommand(text_args)))(
|
|
38
42
|
// get the command to run
|
|
@@ -5,7 +5,13 @@ export default class TelegramBot extends TelegramApi {
|
|
|
5
5
|
url: URL;
|
|
6
6
|
kv: Kv;
|
|
7
7
|
get_set: KVNamespace;
|
|
8
|
+
ai: any;
|
|
9
|
+
db: D1Database;
|
|
8
10
|
constructor(config: Config);
|
|
11
|
+
sean: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
12
|
+
clear: (update: TelegramUpdate) => Promise<Response>;
|
|
13
|
+
question: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
14
|
+
paste: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
9
15
|
code: (update: TelegramUpdate) => Promise<Response>;
|
|
10
16
|
duckduckgo: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
11
17
|
kanye: (update: TelegramUpdate) => Promise<Response>;
|
|
@@ -1,16 +1,139 @@
|
|
|
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";
|
|
4
5
|
export default class TelegramBot extends TelegramApi {
|
|
5
6
|
url;
|
|
6
7
|
kv;
|
|
7
8
|
get_set;
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
10
|
+
ai;
|
|
11
|
+
db;
|
|
8
12
|
constructor(config) {
|
|
9
13
|
super(config.commands, config.webhook, config.handler);
|
|
10
14
|
this.url = config.url;
|
|
11
15
|
this.kv = config.kv;
|
|
12
16
|
this.get_set = config.kv?.get_set;
|
|
17
|
+
this.ai = config.ai;
|
|
18
|
+
this.db = config.db;
|
|
13
19
|
}
|
|
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
|
+
// 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
|
+
}));
|
|
102
|
+
const { response } = await ai.run("@cf/meta/llama-2-7b-chat-int8", {
|
|
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
|
+
],
|
|
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
|
|
123
|
+
return this.sendMessage(update.message?.chat.id ?? 0, response);
|
|
124
|
+
};
|
|
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");
|
|
136
|
+
};
|
|
14
137
|
// bot command: /code
|
|
15
138
|
code = async (update) => ((url) => update.inline_query
|
|
16
139
|
? this.answerInlineQuery(update.inline_query.id, [
|
|
@@ -54,9 +177,16 @@ export default class TelegramBot extends TelegramApi {
|
|
|
54
177
|
: "")))
|
|
55
178
|
: this.sendMessage(update.message?.chat.id ?? 0, duckduckgo_url))(query === ""
|
|
56
179
|
? "https://duckduckgo.com"
|
|
57
|
-
:
|
|
58
|
-
|
|
59
|
-
|
|
180
|
+
: (() => {
|
|
181
|
+
if (query[0][0] !== "/") {
|
|
182
|
+
return addSearchParams(new URL("https://duckduckgo.com"), {
|
|
183
|
+
q: query,
|
|
184
|
+
}).href;
|
|
185
|
+
}
|
|
186
|
+
return addSearchParams(new URL("https://duckduckgo.com"), {
|
|
187
|
+
q: query.split(" ").slice(1).join(" "),
|
|
188
|
+
}).href;
|
|
189
|
+
})()))(args.join(" "));
|
|
60
190
|
// bot command: /kanye
|
|
61
191
|
kanye = async (update) => fetch("https://api.kanye.rest")
|
|
62
192
|
.then((response) => responseToJSON(response))
|
|
@@ -14,6 +14,10 @@ export default class TelegramCommands {
|
|
|
14
14
|
static _get: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
15
15
|
static _set: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
16
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>;
|
|
17
21
|
static code: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
18
22
|
static commandList: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
19
23
|
}
|
|
@@ -11,6 +11,10 @@ export default class TelegramCommands {
|
|
|
11
11
|
static _get = async (bot, update, args) => bot._get(update, args);
|
|
12
12
|
static _set = async (bot, update, args) => bot._set(update, args);
|
|
13
13
|
static duckduckgo = async (bot, update, args) => bot.duckduckgo(update, args);
|
|
14
|
+
static paste = async (bot, update, args) => bot.paste(update, args);
|
|
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);
|
|
14
18
|
static code = async (bot, update) => bot.code(update);
|
|
15
19
|
static commandList = async (bot, update) => bot.commandList(update);
|
|
16
20
|
}
|
package/dist/main/src/types.d.ts
CHANGED
package/dist/main/src/types.js
CHANGED
|
@@ -10,6 +10,10 @@ export class Config {
|
|
|
10
10
|
kv;
|
|
11
11
|
url;
|
|
12
12
|
handler;
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
|
+
ai;
|
|
15
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
|
+
db;
|
|
13
17
|
constructor(config = {}) {
|
|
14
18
|
this.bot_name = config.bot_name || "";
|
|
15
19
|
this.api = config.api || BotApi;
|
|
@@ -18,6 +22,8 @@ export class Config {
|
|
|
18
22
|
this.kv = config.kv;
|
|
19
23
|
this.url = config.url || new URL(localhost);
|
|
20
24
|
this.handler = config.handler || new Handler([]);
|
|
25
|
+
this.ai = config.ai;
|
|
26
|
+
this.db = config.db;
|
|
21
27
|
}
|
|
22
28
|
}
|
|
23
29
|
export const localhost = new URL("http://localhost");
|
|
@@ -5,6 +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;
|
|
9
|
+
AI: string;
|
|
10
|
+
DB: D1Database;
|
|
8
11
|
}
|
|
9
12
|
declare const _default: {
|
|
10
13
|
fetch: (request: Request, env: Environment) => Promise<Response>;
|
|
@@ -6,6 +6,7 @@ export default {
|
|
|
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
|
+
default: TelegramCommands.question,
|
|
9
10
|
"/ping": TelegramCommands.ping,
|
|
10
11
|
"/toss": TelegramCommands.toss,
|
|
11
12
|
"/epoch": TelegramCommands.epoch,
|
|
@@ -19,37 +20,58 @@ export default {
|
|
|
19
20
|
"/set": TelegramCommands._set,
|
|
20
21
|
"/duckduckgo": TelegramCommands.duckduckgo,
|
|
21
22
|
"/code": TelegramCommands.code,
|
|
23
|
+
"/paste": TelegramCommands.paste,
|
|
22
24
|
"/commands": TelegramCommands.commandList,
|
|
25
|
+
"/question": TelegramCommands.question,
|
|
26
|
+
"/clear": TelegramCommands.clear,
|
|
23
27
|
"/help": TelegramCommands.commandList,
|
|
28
|
+
"/sean": TelegramCommands.sean,
|
|
24
29
|
"/start": TelegramCommands.commandList,
|
|
25
30
|
},
|
|
26
31
|
kv: { get_set: env.KV_GET_SET, uid_data: env.KV_UID_DATA },
|
|
32
|
+
ai: env.AI,
|
|
33
|
+
db: env.DB,
|
|
27
34
|
},
|
|
28
35
|
{
|
|
29
36
|
bot_name: "@duckduckbot",
|
|
30
37
|
api: TelegramBot,
|
|
31
38
|
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)),
|
|
32
39
|
commands: {
|
|
40
|
+
default: TelegramCommands.duckduckgo,
|
|
33
41
|
inline: TelegramCommands.duckduckgo,
|
|
34
42
|
"/duckduckgo": TelegramCommands.duckduckgo,
|
|
43
|
+
"/question": TelegramCommands.question,
|
|
35
44
|
"/code": TelegramCommands.code,
|
|
36
45
|
"/commands": TelegramCommands.commandList,
|
|
37
46
|
"/help": TelegramCommands.commandList,
|
|
38
47
|
"/start": TelegramCommands.commandList,
|
|
39
48
|
},
|
|
49
|
+
ai: env.AI,
|
|
40
50
|
},
|
|
41
51
|
{
|
|
42
52
|
bot_name: "@ddggbot",
|
|
43
53
|
api: TelegramBot,
|
|
44
54
|
webhook: new TelegramWebhook(new URL(`https://api.telegram.org/bot${env.SECRET_TELEGRAM_API_TOKEN3}`), env.SECRET_TELEGRAM_API_TOKEN3, new URL(new URL(request.url).origin)),
|
|
45
55
|
commands: {
|
|
56
|
+
default: TelegramCommands.duckduckgo,
|
|
46
57
|
inline: TelegramCommands.duckduckgo,
|
|
47
58
|
"/duckduckgo": TelegramCommands.duckduckgo,
|
|
59
|
+
"/question": TelegramCommands.question,
|
|
48
60
|
"/code": TelegramCommands.code,
|
|
49
61
|
"/commands": TelegramCommands.commandList,
|
|
50
62
|
"/help": TelegramCommands.commandList,
|
|
51
63
|
"/start": TelegramCommands.commandList,
|
|
52
64
|
},
|
|
65
|
+
ai: env.AI,
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
bot_name: "@SeanB_robot",
|
|
69
|
+
api: TelegramBot,
|
|
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)),
|
|
71
|
+
commands: {
|
|
72
|
+
default: TelegramCommands.sean,
|
|
73
|
+
},
|
|
74
|
+
ai: env.AI,
|
|
53
75
|
},
|
|
54
76
|
]).handle(request),
|
|
55
77
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codebam/cf-workers-telegram-bot",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.10.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,14 +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.
|
|
35
|
-
"eslint": "^8.
|
|
32
|
+
"@cloudflare/workers-types": "^4.20231002.0",
|
|
33
|
+
"@typescript-eslint/eslint-plugin": "^6.7.4",
|
|
34
|
+
"@typescript-eslint/parser": "^6.7.4",
|
|
35
|
+
"eslint": "^8.51.0",
|
|
36
36
|
"eslint-config-prettier": "^9.0.0",
|
|
37
|
-
"lerna": "^7.
|
|
38
|
-
"prettier": "^3.0.
|
|
37
|
+
"lerna": "^7.3.0",
|
|
38
|
+
"prettier": "^3.0.3",
|
|
39
39
|
"typescript": "^5.2.2"
|
|
40
40
|
},
|
|
41
|
-
"
|
|
41
|
+
"dependencies": {
|
|
42
|
+
"@cloudflare/ai": "^1.0.15"
|
|
43
|
+
},
|
|
44
|
+
"gitHead": "27402e886cc18f7d94647fc97765ff0b62c94ce6"
|
|
42
45
|
}
|