@codebam/cf-workers-telegram-bot 5.13.0 → 5.15.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/handler.js +5 -5
- package/dist/main/src/telegram_api.d.ts +2 -0
- package/dist/main/src/telegram_api.js +36 -8
- package/dist/main/src/telegram_bot.d.ts +4 -6
- package/dist/main/src/telegram_bot.js +189 -143
- package/dist/main/src/telegram_commands.d.ts +17 -21
- package/dist/main/src/telegram_commands.js +2 -4
- package/dist/main/src/types.d.ts +4 -3
- package/dist/main/src/types.js +3 -0
- package/dist/worker/src/worker.d.ts +2 -0
- package/dist/worker/src/worker.js +19 -8
- package/package.json +11 -11
package/dist/main/src/handler.js
CHANGED
|
@@ -24,17 +24,17 @@ export default class Handler {
|
|
|
24
24
|
POST: this.postResponse,
|
|
25
25
|
default: () => new Promise(() => new Response()),
|
|
26
26
|
};
|
|
27
|
-
getAccessKeys = async (configs) => Promise.all(configs.map((bot_config) => sha256(bot_config.webhook?.token ?? "").then((hash) =>
|
|
28
|
-
hash
|
|
29
|
-
bot_config
|
|
30
|
-
|
|
27
|
+
getAccessKeys = async (configs) => Promise.all(configs.map((bot_config) => sha256(bot_config.webhook?.token ?? "").then((hash) => {
|
|
28
|
+
console.log(hash);
|
|
29
|
+
return [hash, bot_config];
|
|
30
|
+
}))).then((result) => Object.fromEntries(result));
|
|
31
31
|
// handles the request
|
|
32
32
|
handle = async (request) => this.getAccessKeys(this.configs).then((access_keys) => Object.keys(this.responses).includes(request.method)
|
|
33
33
|
? this.responses[request.method](request, ((key) => {
|
|
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>;
|
|
@@ -13,14 +13,22 @@ export default class TelegramApi extends BotApi {
|
|
|
13
13
|
message: this.messageUpdate,
|
|
14
14
|
default: new Response(),
|
|
15
15
|
};
|
|
16
|
-
update = async (update) =>
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
update = async (update) => {
|
|
17
|
+
console.log({ update });
|
|
18
|
+
if (update) {
|
|
19
|
+
if (update.inline_query) {
|
|
20
|
+
if (update.inline_query.query !== "") {
|
|
21
|
+
return this.updates.inline_query(update);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
if (update.message) {
|
|
26
|
+
return this.updates.message(update);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
return this.updates.default;
|
|
31
|
+
};
|
|
24
32
|
// greet new users who join
|
|
25
33
|
greetUsers = async (update) => update.message?.new_chat_members
|
|
26
34
|
? this.sendMessage(update.message.chat.id, `Welcome to ${update.message.chat.title}, ${update.message.from.username}`)
|
|
@@ -58,6 +66,12 @@ export default class TelegramApi extends BotApi {
|
|
|
58
66
|
results: JSON.stringify(results),
|
|
59
67
|
cache_time: cache_time.toString(),
|
|
60
68
|
}).href));
|
|
69
|
+
// trigger editMessage command of BotAPI
|
|
70
|
+
editMessageText = async (chat_id, message_id, text) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/editMessageText`), {
|
|
71
|
+
chat_id: chat_id.toString(),
|
|
72
|
+
message_id: message_id.toString(),
|
|
73
|
+
text,
|
|
74
|
+
}).href));
|
|
61
75
|
// trigger sendMessage command of BotAPI
|
|
62
76
|
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
77
|
chat_id: chat_id.toString(),
|
|
@@ -75,6 +89,20 @@ export default class TelegramApi extends BotApi {
|
|
|
75
89
|
disable_notification: disable_notification.toString(),
|
|
76
90
|
}).href));
|
|
77
91
|
// trigger sendPhoto command of BotAPI
|
|
92
|
+
sendPhotoRaw = async (chat_id, photo, caption = "", parse_mode = "", disable_notification = false, reply_to_message_id = 0) => {
|
|
93
|
+
const formdata = new FormData();
|
|
94
|
+
formdata.set("file", photo);
|
|
95
|
+
return fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/sendPhoto`), {
|
|
96
|
+
chat_id: chat_id.toString(),
|
|
97
|
+
caption,
|
|
98
|
+
parse_mode,
|
|
99
|
+
disable_notification: disable_notification.toString(),
|
|
100
|
+
reply_to_message_id: reply_to_message_id.toString(),
|
|
101
|
+
}).href), { method: "POST", body: formdata })
|
|
102
|
+
.then((resp) => resp.text())
|
|
103
|
+
.then(log);
|
|
104
|
+
};
|
|
105
|
+
// trigger sendPhoto command of BotAPI
|
|
78
106
|
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
107
|
chat_id: chat_id.toString(),
|
|
80
108
|
photo,
|
|
@@ -7,24 +7,22 @@ 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
|
-
|
|
13
|
+
translate: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
13
14
|
clear: (update: TelegramUpdate) => Promise<Response>;
|
|
15
|
+
image: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
14
16
|
question: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
15
|
-
|
|
17
|
+
sean: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
16
18
|
code: (update: TelegramUpdate) => Promise<Response>;
|
|
17
19
|
duckduckgo: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
18
20
|
kanye: (update: TelegramUpdate) => Promise<Response>;
|
|
19
21
|
joke: (update: TelegramUpdate) => Promise<Response>;
|
|
20
22
|
dog: (update: TelegramUpdate) => Promise<Response>;
|
|
21
|
-
cat: (update: TelegramUpdate) => Promise<Response>;
|
|
22
23
|
bored: (update: TelegramUpdate) => Promise<Response>;
|
|
23
24
|
epoch: (update: TelegramUpdate) => Promise<Response>;
|
|
24
|
-
_get: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
25
|
-
_set: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
26
25
|
_average: (numbers: number[]) => number;
|
|
27
|
-
recursion: (update: TelegramUpdate) => Promise<Response>;
|
|
28
26
|
roll: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
29
27
|
commandList: (update: TelegramUpdate) => Promise<Response>;
|
|
30
28
|
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,141 @@ 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: /
|
|
23
|
-
|
|
24
|
+
// bot command: /translate
|
|
25
|
+
translate = async (update, args) => {
|
|
26
|
+
if (this.ai === undefined) {
|
|
27
|
+
return new Response("ok");
|
|
28
|
+
}
|
|
24
29
|
const ai = new Ai(this.ai);
|
|
25
|
-
let
|
|
30
|
+
let _prompt;
|
|
26
31
|
if (args[0][0] === "/") {
|
|
27
|
-
|
|
32
|
+
_prompt = args.slice(1).join(" ");
|
|
28
33
|
}
|
|
29
34
|
else {
|
|
30
|
-
|
|
35
|
+
_prompt = args.join(" ");
|
|
31
36
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
prompt = "no prompt specified";
|
|
37
|
+
if (_prompt === "") {
|
|
38
|
+
_prompt = "";
|
|
35
39
|
}
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
{
|
|
44
|
-
|
|
45
|
-
|
|
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);
|
|
40
|
+
const langs = ["french", "arabic", "german", "spanish", "russian"];
|
|
41
|
+
const inline_articles = await Promise.all(langs.map(async (lang) => {
|
|
42
|
+
const response = await ai.run("@cf/meta/m2m100-1.2b", {
|
|
43
|
+
text: _prompt,
|
|
44
|
+
source_lang: lang,
|
|
45
|
+
target_lang: "english",
|
|
46
|
+
});
|
|
47
|
+
return new TelegramInlineQueryResultArticle(response.translated_text, `${lang}: ${response.translated_text}`);
|
|
48
|
+
}));
|
|
49
|
+
return this.answerInlineQuery(update.inline_query?.id ?? 0, inline_articles);
|
|
70
50
|
};
|
|
71
51
|
// bot command: /clear
|
|
72
52
|
// reset the llama2 session by deleting messages from d1
|
|
73
53
|
clear = async (update) => {
|
|
74
54
|
const { success } = await this.db
|
|
75
55
|
.prepare("DELETE FROM Messages WHERE userId=?")
|
|
76
|
-
.bind(update.
|
|
56
|
+
.bind(update.inline_query
|
|
57
|
+
? update.inline_query.from.id
|
|
58
|
+
: update.message?.from.id)
|
|
77
59
|
.run();
|
|
78
60
|
if (success) {
|
|
61
|
+
if (update.inline_query) {
|
|
62
|
+
return this.answerInlineQuery(update.inline_query.id, [
|
|
63
|
+
new TelegramInlineQueryResultArticle("_"),
|
|
64
|
+
]);
|
|
65
|
+
}
|
|
79
66
|
return this.sendMessage(update.message?.chat.id ?? 0, "_");
|
|
80
67
|
}
|
|
81
68
|
return this.sendMessage(update.message?.chat.id ?? 0, "failed");
|
|
82
69
|
};
|
|
70
|
+
// bot command: /image
|
|
71
|
+
image = async (update, args) => {
|
|
72
|
+
const ai = new Ai(this.ai);
|
|
73
|
+
let _prompt;
|
|
74
|
+
if (args[0][0] === "/") {
|
|
75
|
+
_prompt = args.slice(1).join(" ");
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
_prompt = args.join(" ");
|
|
79
|
+
}
|
|
80
|
+
if (_prompt === "") {
|
|
81
|
+
_prompt = "";
|
|
82
|
+
}
|
|
83
|
+
const inputs = { prompt: _prompt, num_steps: 20 };
|
|
84
|
+
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);
|
|
86
|
+
const id = crypto.randomUUID();
|
|
87
|
+
await this.r2.put(id, response);
|
|
88
|
+
const url = "https://r2.seanbehan.ca/" + id;
|
|
89
|
+
return this.sendPhoto(update.message?.chat.id ?? 0, url);
|
|
90
|
+
};
|
|
83
91
|
// bot command: /question
|
|
84
92
|
question = async (update, args) => {
|
|
85
93
|
if (this.ai === undefined) {
|
|
86
94
|
return new Response("ok");
|
|
87
95
|
}
|
|
88
96
|
const ai = new Ai(this.ai);
|
|
89
|
-
let
|
|
97
|
+
let _prompt;
|
|
90
98
|
if (args[0][0] === "/") {
|
|
91
|
-
|
|
99
|
+
_prompt = args.slice(1).join(" ");
|
|
92
100
|
}
|
|
93
101
|
else {
|
|
94
|
-
|
|
95
|
-
}
|
|
96
|
-
if (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();
|
|
102
|
+
_prompt = args.join(" ");
|
|
105
103
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
if (results) {
|
|
109
|
-
old_messages = results.map((col) => ({
|
|
110
|
-
role: "system",
|
|
111
|
-
content: col.content,
|
|
112
|
-
}));
|
|
104
|
+
if (_prompt === "") {
|
|
105
|
+
_prompt = "";
|
|
113
106
|
}
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
{
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
107
|
+
const results = await (async () => {
|
|
108
|
+
if (this.db) {
|
|
109
|
+
const { results } = await this.db
|
|
110
|
+
.prepare("SELECT * FROM Messages WHERE userId=?")
|
|
111
|
+
.bind(update.inline_query
|
|
112
|
+
? update.inline_query.from.id
|
|
113
|
+
: update.message?.from.id)
|
|
114
|
+
.all();
|
|
115
|
+
return results;
|
|
116
|
+
}
|
|
117
|
+
})();
|
|
118
|
+
const old_messages = (() => {
|
|
119
|
+
if (results) {
|
|
120
|
+
return results.map((col) => ({
|
|
125
121
|
role: "system",
|
|
126
|
-
content:
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
122
|
+
content: col.content,
|
|
123
|
+
}));
|
|
124
|
+
}
|
|
125
|
+
return [];
|
|
126
|
+
})();
|
|
127
|
+
const system_prompt = "<s>" +
|
|
128
|
+
[
|
|
129
|
+
`Your name is ${this.bot_name}.`,
|
|
130
|
+
`You are talking to ${update.message?.from.first_name}.`,
|
|
131
|
+
`Your source code is at https://github.com/codebam/cf-workers-telegram-bot .`,
|
|
132
|
+
`the current date is ${new Date().toString()}`,
|
|
133
|
+
].reduce((acc, cur) => {
|
|
134
|
+
return acc + cur + "\n";
|
|
135
|
+
}) +
|
|
136
|
+
old_messages.reduce((acc, cur) => {
|
|
137
|
+
return acc + cur.content + "\n";
|
|
138
|
+
}, "") +
|
|
139
|
+
"</s>";
|
|
140
|
+
const p = system_prompt + "[INST]" + _prompt + "[/INST]";
|
|
141
|
+
const prompt = p.slice(p.length - 4096, p.length);
|
|
142
|
+
const response = await ai
|
|
143
|
+
.run("@hf/thebloke/orca-2-13b-awq", {
|
|
144
|
+
prompt,
|
|
145
|
+
max_tokens: 596,
|
|
146
|
+
})
|
|
147
|
+
.then(({ response }) => response
|
|
148
|
+
.replace(/(\[|)(\/|)INST(S|)(s|)(\]|)/, "")
|
|
149
|
+
.replace(/<<(\/|)SYS>>/, ""));
|
|
137
150
|
if (this.db) {
|
|
138
151
|
const { success } = await this.db
|
|
139
152
|
.prepare("INSERT INTO Messages (id, userId, content) VALUES (?, ?, ?)")
|
|
140
|
-
.bind(crypto.randomUUID(), update.
|
|
153
|
+
.bind(crypto.randomUUID(), update.inline_query
|
|
154
|
+
? update.inline_query.from.id
|
|
155
|
+
: update.message?.from.id, "[INST] " + _prompt + " [/INST]" + "\n" + response)
|
|
141
156
|
.run();
|
|
142
157
|
if (!success) {
|
|
143
158
|
console.log("failed to insert data into d1");
|
|
@@ -147,19 +162,93 @@ export default class TelegramBot extends TelegramApi {
|
|
|
147
162
|
this.clear(update);
|
|
148
163
|
return this.question(update, args);
|
|
149
164
|
} // sometimes llama2 doesn't respond when given lots of system prompts
|
|
150
|
-
|
|
165
|
+
if (update.inline_query) {
|
|
166
|
+
return this.answerInlineQuery(update.inline_query.id, [
|
|
167
|
+
new TelegramInlineQueryResultArticle(response),
|
|
168
|
+
]);
|
|
169
|
+
}
|
|
170
|
+
return this.sendMessage(update.message?.chat.id ?? 0, response, "", false, false, update.message?.message_id);
|
|
151
171
|
};
|
|
152
|
-
// bot command: /
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
172
|
+
// bot command: /sean
|
|
173
|
+
sean = async (update, args) => {
|
|
174
|
+
if (this.ai === undefined) {
|
|
175
|
+
return new Response("ok");
|
|
176
|
+
}
|
|
177
|
+
const ai = new Ai(this.ai);
|
|
178
|
+
let _prompt;
|
|
179
|
+
if (args[0][0] === "/") {
|
|
180
|
+
_prompt = args.slice(1).join(" ");
|
|
181
|
+
}
|
|
182
|
+
else {
|
|
183
|
+
_prompt = args.join(" ");
|
|
184
|
+
}
|
|
185
|
+
if (_prompt === "") {
|
|
186
|
+
_prompt = "";
|
|
187
|
+
}
|
|
188
|
+
const results = await (async () => {
|
|
189
|
+
if (this.db) {
|
|
190
|
+
const { results } = await this.db
|
|
191
|
+
.prepare("SELECT * FROM Messages WHERE userId=?")
|
|
192
|
+
.bind(update.inline_query
|
|
193
|
+
? update.inline_query.from.id
|
|
194
|
+
: update.message?.from.id)
|
|
195
|
+
.all();
|
|
196
|
+
return results;
|
|
197
|
+
}
|
|
198
|
+
})();
|
|
199
|
+
const old_messages = (() => {
|
|
200
|
+
if (results) {
|
|
201
|
+
return results.map((col) => ({
|
|
202
|
+
role: "system",
|
|
203
|
+
content: col.content,
|
|
204
|
+
}));
|
|
205
|
+
}
|
|
206
|
+
return [];
|
|
207
|
+
})();
|
|
208
|
+
const system_prompt = "<s>" +
|
|
209
|
+
[
|
|
210
|
+
`Your name is ${this.bot_name}.`,
|
|
211
|
+
`You are talking to ${update.message?.from.first_name}.`,
|
|
212
|
+
`Your source code is at https://github.com/codebam/cf-workers-telegram-bot .`,
|
|
213
|
+
`the current date is ${new Date().toString()}`,
|
|
214
|
+
"Sean Behan is a full stack developer who goes by the username codebam.",
|
|
215
|
+
"Sean Behan likes programming and video games.",
|
|
216
|
+
"Pretend to be Sean Behan but don't make things up.",
|
|
217
|
+
].reduce((acc, cur) => {
|
|
218
|
+
return acc + cur + "\n";
|
|
219
|
+
}) +
|
|
220
|
+
old_messages.reduce((acc, cur) => {
|
|
221
|
+
return acc + cur.content + "\n";
|
|
222
|
+
}, "") +
|
|
223
|
+
"</s>";
|
|
224
|
+
const p = system_prompt + "[INST]" + _prompt + "[/INST]";
|
|
225
|
+
const prompt = p.slice(p.length - 4096, p.length);
|
|
226
|
+
const response = await ai
|
|
227
|
+
.run("@hf/thebloke/orca-2-13b-awq", {
|
|
228
|
+
prompt,
|
|
229
|
+
max_tokens: 596,
|
|
230
|
+
})
|
|
231
|
+
.then(({ response }) => response
|
|
232
|
+
.replace(/(\[|)(\/|)INST(S|)(s|)(\]|)/, "")
|
|
233
|
+
.replace(/<<(\/|)SYS>>/, "")
|
|
234
|
+
.replace(/[OUT]/, ""));
|
|
235
|
+
if (this.db) {
|
|
236
|
+
const { success } = await this.db
|
|
237
|
+
.prepare("INSERT INTO Messages (id, userId, content) VALUES (?, ?, ?)")
|
|
238
|
+
.bind(crypto.randomUUID(), update.inline_query
|
|
239
|
+
? update.inline_query.from.id
|
|
240
|
+
: update.message?.from.id, "[INST] " + _prompt + " [/INST]" + "\n" + response)
|
|
241
|
+
.run();
|
|
242
|
+
if (!success) {
|
|
243
|
+
console.log("failed to insert data into d1");
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (update.inline_query) {
|
|
247
|
+
return this.answerInlineQuery(update.inline_query.id, [
|
|
248
|
+
new TelegramInlineQueryResultArticle(response),
|
|
249
|
+
]);
|
|
250
|
+
}
|
|
251
|
+
return this.sendMessage(update.message?.chat.id ?? 0, response, "", false, false, update.message?.message_id);
|
|
163
252
|
};
|
|
164
253
|
// bot command: /code
|
|
165
254
|
code = async (update) => ((url) => update.inline_query
|
|
@@ -185,7 +274,7 @@ export default class TelegramBot extends TelegramApi {
|
|
|
185
274
|
? [
|
|
186
275
|
new TelegramInlineQueryResultArticle(`${instant_answer_url}\n\n<a href="${addSearchParams(new URL(duckduckgo_url), {
|
|
187
276
|
q: args
|
|
188
|
-
.slice(
|
|
277
|
+
.slice(2)
|
|
189
278
|
.join(" ")
|
|
190
279
|
.replace(/^!\w* /, ""),
|
|
191
280
|
}).href}">Results From DuckDuckGo</a>`, instant_answer_url, "HTML", thumb_url),
|
|
@@ -240,13 +329,6 @@ export default class TelegramBot extends TelegramApi {
|
|
|
240
329
|
.then((shibe_response) => update.inline_query
|
|
241
330
|
? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultPhoto(shibe_response[0])], 0)
|
|
242
331
|
: 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
332
|
// bot command: /bored
|
|
251
333
|
bored = async (update) => fetch("https://boredapi.com/api/activity/")
|
|
252
334
|
.then((response) => responseToJSON(response))
|
|
@@ -258,43 +340,7 @@ export default class TelegramBot extends TelegramApi {
|
|
|
258
340
|
epoch = async (update) => ((seconds) => update.inline_query
|
|
259
341
|
? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(seconds)], 0)
|
|
260
342
|
: 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
343
|
_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
344
|
// bot command: /roll
|
|
299
345
|
roll = async (update, args) => ((outcome, message) => update.inline_query
|
|
300
346
|
? this.answerInlineQuery(update.inline_query.id, [
|
|
@@ -302,7 +348,7 @@ export default class TelegramBot extends TelegramApi {
|
|
|
302
348
|
])
|
|
303
349
|
: 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
350
|
// bot command: /commandList
|
|
305
|
-
commandList = async (update) => this.sendMessage(update.message?.chat.id ?? 0,
|
|
351
|
+
commandList = async (update) => this.sendMessage(update.message?.chat.id ?? 0, `${Object.keys(this.commands).join("\n")}`, "HTML");
|
|
306
352
|
// bot command: /toss
|
|
307
353
|
toss = async (update) => this.sendMessage(update.message?.chat.id ?? 0, Math.floor(Math.random() * 2) == 0 ? "heads" : "tails");
|
|
308
354
|
// bot command: /ping
|
|
@@ -1,23 +1,19 @@
|
|
|
1
|
-
|
|
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:
|
|
6
|
-
static toss:
|
|
7
|
-
static epoch:
|
|
8
|
-
static kanye:
|
|
9
|
-
static bored:
|
|
10
|
-
static joke:
|
|
11
|
-
static dog:
|
|
12
|
-
static
|
|
13
|
-
static
|
|
14
|
-
static
|
|
15
|
-
static
|
|
16
|
-
static
|
|
17
|
-
static
|
|
18
|
-
static
|
|
19
|
-
static
|
|
20
|
-
static
|
|
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;
|
|
18
|
+
static translate: TelegramCommand;
|
|
23
19
|
}
|
|
@@ -6,15 +6,13 @@ 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);
|
|
17
|
+
static translate = async (bot, update, args) => bot.translate(update, args);
|
|
20
18
|
}
|
package/dist/main/src/types.d.ts
CHANGED
|
@@ -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
|
|
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
|
|
8
|
-
export type TelegramCommand = (bot:
|
|
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;
|
package/dist/main/src/types.js
CHANGED
|
@@ -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");
|
|
@@ -6,8 +6,10 @@ interface Environment {
|
|
|
6
6
|
SECRET_TELEGRAM_API_TOKEN2: string;
|
|
7
7
|
SECRET_TELEGRAM_API_TOKEN3: string;
|
|
8
8
|
SECRET_TELEGRAM_API_TOKEN4: string;
|
|
9
|
+
SECRET_TELEGRAM_API_TOKEN5: string;
|
|
9
10
|
AI: string;
|
|
10
11
|
DB: D1Database;
|
|
12
|
+
R2: R2Bucket;
|
|
11
13
|
}
|
|
12
14
|
declare const _default: {
|
|
13
15
|
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
|
-
"/
|
|
29
|
-
"/start": TelegramCommands.
|
|
25
|
+
"/image": TelegramCommands.image,
|
|
26
|
+
"/start": TelegramCommands.question,
|
|
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,10 +69,23 @@ 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.
|
|
72
|
+
default: TelegramCommands.sean,
|
|
73
|
+
"/clear": TelegramCommands.clear,
|
|
74
|
+
"/start": TelegramCommands.commandList,
|
|
75
75
|
},
|
|
76
76
|
ai: env.AI,
|
|
77
77
|
db: env.DB,
|
|
78
78
|
},
|
|
79
|
+
{
|
|
80
|
+
bot_name: "@TranslatePartyBot",
|
|
81
|
+
api: TelegramBot,
|
|
82
|
+
webhook: new TelegramWebhook(new URL(`https://api.telegram.org/bot${env.SECRET_TELEGRAM_API_TOKEN5}`), env.SECRET_TELEGRAM_API_TOKEN5, new URL(new URL(request.url).origin)),
|
|
83
|
+
commands: {
|
|
84
|
+
default: TelegramCommands.translate,
|
|
85
|
+
inline: TelegramCommands.translate,
|
|
86
|
+
"/start": TelegramCommands.commandList,
|
|
87
|
+
},
|
|
88
|
+
ai: env.AI,
|
|
89
|
+
},
|
|
79
90
|
]).handle(request),
|
|
80
91
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codebam/cf-workers-telegram-bot",
|
|
3
|
-
"version": "5.
|
|
3
|
+
"version": "5.15.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": "^
|
|
34
|
-
"@typescript-eslint/parser": "^
|
|
35
|
-
"eslint": "^8.
|
|
36
|
-
"eslint-config-prettier": "^9.
|
|
37
|
-
"lerna": "^
|
|
38
|
-
"prettier": "^3.
|
|
39
|
-
"typescript": "^5.
|
|
32
|
+
"@cloudflare/workers-types": "^4.20240320.1",
|
|
33
|
+
"@typescript-eslint/eslint-plugin": "^7.4.0",
|
|
34
|
+
"@typescript-eslint/parser": "^7.4.0",
|
|
35
|
+
"eslint": "^8.57.0",
|
|
36
|
+
"eslint-config-prettier": "^9.1.0",
|
|
37
|
+
"lerna": "^8.1.2",
|
|
38
|
+
"prettier": "^3.2.5",
|
|
39
|
+
"typescript": "^5.4.3"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@cloudflare/ai": "
|
|
42
|
+
"@cloudflare/ai": "1.0.53"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "f590f9423dda46b7b591d8344c13504f6fa69d6e"
|
|
45
45
|
}
|