@codebam/cf-workers-telegram-bot 5.17.0 → 6.2.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/README.md +18 -6
- package/dist/main/src/bot_api.d.ts +3 -3
- package/dist/main/src/commands/bored.d.ts +5 -0
- package/dist/main/src/commands/bored.js +8 -0
- package/dist/main/src/commands/clear.d.ts +5 -0
- package/dist/main/src/commands/clear.js +14 -0
- package/dist/main/src/commands/code.d.ts +5 -0
- package/dist/main/src/commands/code.js +4 -0
- package/dist/main/src/commands/commandlist.d.ts +5 -0
- package/dist/main/src/commands/commandlist.js +1 -0
- package/dist/main/src/commands/dog.d.ts +5 -0
- package/dist/main/src/commands/dog.js +7 -0
- package/dist/main/src/commands/duckduckgo.d.ts +5 -0
- package/dist/main/src/commands/duckduckgo.js +42 -0
- package/dist/main/src/commands/epoch.d.ts +5 -0
- package/dist/main/src/commands/epoch.js +4 -0
- package/dist/main/src/commands/getchatinfo.d.ts +5 -0
- package/dist/main/src/commands/getchatinfo.js +2 -0
- package/dist/main/src/commands/image.d.ts +5 -0
- package/dist/main/src/commands/image.js +19 -0
- package/dist/main/src/commands/joke.d.ts +5 -0
- package/dist/main/src/commands/joke.js +8 -0
- package/dist/main/src/commands/kanye.d.ts +5 -0
- package/dist/main/src/commands/kanye.js +8 -0
- package/dist/main/src/commands/ping.d.ts +5 -0
- package/dist/main/src/commands/ping.js +1 -0
- package/dist/main/src/commands/question.d.ts +5 -0
- package/dist/main/src/commands/question.js +74 -0
- package/dist/main/src/commands/roll.d.ts +5 -0
- package/dist/main/src/commands/roll.js +6 -0
- package/dist/main/src/commands/sean.d.ts +5 -0
- package/dist/main/src/commands/sean.js +76 -0
- package/dist/main/src/commands/toss.d.ts +5 -0
- package/dist/main/src/commands/toss.js +1 -0
- package/dist/main/src/commands/translate.d.ts +5 -0
- package/dist/main/src/commands/translate.js +26 -0
- package/dist/main/src/handler.d.ts +2 -2
- package/dist/main/src/handler.js +5 -7
- package/dist/main/src/libs.js +6 -11
- package/dist/main/src/main.d.ts +11 -11
- package/dist/main/src/main.js +11 -11
- package/dist/main/src/telegram_api.d.ts +3 -3
- package/dist/main/src/telegram_api.js +20 -27
- package/dist/main/src/telegram_bot.d.ts +20 -21
- package/dist/main/src/telegram_bot.js +52 -334
- package/dist/main/src/telegram_commands.d.ts +1 -1
- package/dist/main/src/telegram_commands.js +16 -16
- package/dist/main/src/telegram_webhook.d.ts +2 -2
- package/dist/main/src/telegram_webhook.js +5 -6
- package/dist/main/src/types/Balance.d.ts +6 -0
- package/dist/main/src/types/Balance.js +1 -0
- package/dist/main/src/types/Bored.d.ts +10 -0
- package/dist/main/src/types/Bored.js +1 -0
- package/dist/main/src/types/Command.d.ts +5 -0
- package/dist/main/src/types/Command.js +1 -0
- package/dist/main/src/types/Commands.d.ts +3 -0
- package/dist/main/src/types/Commands.js +1 -0
- package/dist/main/src/types/Config.d.ts +20 -0
- package/dist/main/src/types/Config.js +33 -0
- package/dist/main/src/types/DDGQueryResponse.d.ts +12 -0
- package/dist/main/src/types/DDGQueryResponse.js +1 -0
- package/dist/main/src/types/Joke.d.ts +20 -0
- package/dist/main/src/types/Joke.js +1 -0
- package/dist/main/src/types/Kv.d.ts +3 -0
- package/dist/main/src/types/Kv.js +1 -0
- package/dist/main/src/types/PartialTelegramUpdate.d.ts +11 -0
- package/dist/main/src/types/PartialTelegramUpdate.js +1 -0
- package/dist/main/src/types/TelegramChat.d.ts +21 -0
- package/dist/main/src/types/TelegramChat.js +1 -0
- package/dist/main/src/types/TelegramCommand.d.ts +5 -0
- package/dist/main/src/types/TelegramCommand.js +1 -0
- package/dist/main/src/types/TelegramFrom.d.ts +8 -0
- package/dist/main/src/types/TelegramFrom.js +1 -0
- package/dist/main/src/types/TelegramInlineQuery.d.ts +9 -0
- package/dist/main/src/types/TelegramInlineQuery.js +1 -0
- package/dist/main/src/types/TelegramInlineQueryResult.d.ts +6 -0
- package/dist/main/src/types/TelegramInlineQueryResult.js +8 -0
- package/dist/main/src/types/TelegramInlineQueryResultArticle.d.ts +8 -0
- package/dist/main/src/types/TelegramInlineQueryResultArticle.js +15 -0
- package/dist/main/src/types/TelegramInlineQueryResultPhoto.d.ts +13 -0
- package/dist/main/src/types/TelegramInlineQueryResultPhoto.js +19 -0
- package/dist/main/src/types/TelegramInlineQueryType.d.ts +2 -0
- package/dist/main/src/types/TelegramInlineQueryType.js +1 -0
- package/dist/main/src/types/TelegramInputMessageContent.d.ts +5 -0
- package/dist/main/src/types/TelegramInputMessageContent.js +1 -0
- package/dist/main/src/types/TelegramMessage.d.ts +42 -0
- package/dist/main/src/types/TelegramMessage.js +1 -0
- package/dist/main/src/types/TelegramMessageEntity.d.ts +10 -0
- package/dist/main/src/types/TelegramMessageEntity.js +1 -0
- package/dist/main/src/types/TelegramPhotoSize.d.ts +8 -0
- package/dist/main/src/types/TelegramPhotoSize.js +1 -0
- package/dist/main/src/types/TelegramUpdate.d.ts +13 -0
- package/dist/main/src/types/TelegramUpdate.js +36 -0
- package/dist/main/src/types/TelegramUser.d.ts +12 -0
- package/dist/main/src/types/TelegramUser.js +1 -0
- package/dist/main/src/types/Update.d.ts +3 -0
- package/dist/main/src/types/Update.js +2 -0
- package/dist/main/src/types/WebhookCommands.d.ts +4 -0
- package/dist/main/src/types/WebhookCommands.js +2 -0
- package/dist/main/src/types/localhost.d.ts +3 -0
- package/dist/main/src/types/localhost.js +1 -0
- package/dist/main/src/types.d.ts +28 -215
- package/dist/main/src/types.js +10 -113
- package/dist/main/src/webhook.d.ts +1 -1
- package/dist/main/src/webhook.js +3 -4
- package/dist/worker/src/worker.js +37 -37
- package/package.json +4 -7
package/dist/main/src/main.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import BotApi from
|
|
2
|
-
import Handler from
|
|
3
|
-
import * as Libs from
|
|
4
|
-
import TelegramApi from
|
|
5
|
-
import TelegramBot from
|
|
6
|
-
import TelegramCommands from
|
|
7
|
-
import TelegramWebhook from
|
|
8
|
-
import * as Types from
|
|
9
|
-
import Webhook from
|
|
10
|
-
import worker from
|
|
11
|
-
export { BotApi, Handler, Libs, TelegramApi, TelegramBot, TelegramCommands, TelegramWebhook, Types, Webhook, worker
|
|
1
|
+
import BotApi from './bot_api';
|
|
2
|
+
import Handler from './handler';
|
|
3
|
+
import * as Libs from './libs';
|
|
4
|
+
import TelegramApi from './telegram_api';
|
|
5
|
+
import TelegramBot from './telegram_bot';
|
|
6
|
+
import TelegramCommands from './telegram_commands';
|
|
7
|
+
import TelegramWebhook from './telegram_webhook';
|
|
8
|
+
import * as Types from './types';
|
|
9
|
+
import Webhook from './webhook';
|
|
10
|
+
import worker from '../../worker/src/worker';
|
|
11
|
+
export { BotApi, Handler, Libs, TelegramApi, TelegramBot, TelegramCommands, TelegramWebhook, Types, Webhook, worker };
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import BotApi from
|
|
3
|
-
import { Commands, TelegramInlineQueryResult, TelegramUpdate, Webhook, Update } from
|
|
4
|
-
import Handler from
|
|
2
|
+
import BotApi from './bot_api';
|
|
3
|
+
import { Commands, TelegramInlineQueryResult, TelegramUpdate, Webhook, Update } from './types';
|
|
4
|
+
import Handler from './handler';
|
|
5
5
|
export default class TelegramApi extends BotApi {
|
|
6
6
|
constructor(commands: Commands, webhook: Webhook, handler: Handler);
|
|
7
7
|
inlineQueryUpdate: (update: TelegramUpdate) => Promise<Response>;
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
import BotApi from
|
|
2
|
-
import { addSearchParams, log } from
|
|
1
|
+
import BotApi from './bot_api';
|
|
2
|
+
import { addSearchParams, log } from './libs';
|
|
3
3
|
export default class TelegramApi extends BotApi {
|
|
4
4
|
constructor(commands, webhook, handler) {
|
|
5
5
|
super({ commands, webhook, handler });
|
|
6
6
|
}
|
|
7
7
|
inlineQueryUpdate = async (update) => this.executeInlineCommand(update);
|
|
8
|
-
messageUpdate = async (update) => typeof update.message?.text ===
|
|
9
|
-
? this.executeCommand(update).then(async () => this.greetUsers(update))
|
|
10
|
-
: this.updates.default;
|
|
8
|
+
messageUpdate = async (update) => typeof update.message?.text === 'string' ? this.executeCommand(update).then(async () => this.greetUsers(update)) : this.updates.default;
|
|
11
9
|
updates = {
|
|
12
10
|
inline_query: this.inlineQueryUpdate,
|
|
13
11
|
message: this.messageUpdate,
|
|
@@ -17,7 +15,7 @@ export default class TelegramApi extends BotApi {
|
|
|
17
15
|
console.log({ update });
|
|
18
16
|
if (update) {
|
|
19
17
|
if (update.inline_query) {
|
|
20
|
-
if (update.inline_query.query !==
|
|
18
|
+
if (update.inline_query.query !== '') {
|
|
21
19
|
return this.updates.inline_query(update);
|
|
22
20
|
}
|
|
23
21
|
}
|
|
@@ -33,33 +31,28 @@ export default class TelegramApi extends BotApi {
|
|
|
33
31
|
greetUsers = async (update) => update.message?.new_chat_members
|
|
34
32
|
? this.sendMessage(update.message.chat.id, `Welcome to ${update.message.chat.title}, ${update.message.from.username}`)
|
|
35
33
|
: this.updates.default;
|
|
36
|
-
getCommand = (args) => args[0]?.split(
|
|
34
|
+
getCommand = (args) => args[0]?.split('@')[0];
|
|
37
35
|
// run command passed from executeCommand
|
|
38
36
|
_executeCommand = async (update, text, args = []) => log({ execute: { text, args } })
|
|
39
37
|
? ((text_args) => ((command) => this.commands[command]
|
|
40
38
|
? this.commands[command]?.(this, update, [...text_args, ...args])
|
|
41
39
|
: log({
|
|
42
40
|
error: `command '${command}' does not exist, using default`,
|
|
43
|
-
}) &&
|
|
44
|
-
this.commands["default"]?.(this, update, [
|
|
45
|
-
...text_args,
|
|
46
|
-
...args,
|
|
47
|
-
]))(
|
|
41
|
+
}) && this.commands['default']?.(this, update, [...text_args, ...args]))(
|
|
48
42
|
// run the command
|
|
49
43
|
this.getCommand(text_args)))(
|
|
50
44
|
// get the command to run
|
|
51
45
|
text
|
|
52
46
|
.trimStart()
|
|
53
|
-
.replace(/^([^\s]*\s)\s*/gm,
|
|
54
|
-
.split(
|
|
47
|
+
.replace(/^([^\s]*\s)\s*/gm, '$1')
|
|
48
|
+
.split(' '))
|
|
55
49
|
: this.updates.default;
|
|
56
50
|
// execute the inline custom bot commands from bot configurations
|
|
57
|
-
executeInlineCommand = async (update) => this._executeCommand(update, update.inline_query?.query ??
|
|
58
|
-
? this._executeCommand(update,
|
|
51
|
+
executeInlineCommand = async (update) => this._executeCommand(update, update.inline_query?.query ?? '').then(async (command_response) => command_response
|
|
52
|
+
? this._executeCommand(update, 'inline', update.inline_query?.query.trimStart().split(' ')).then((_command_response) => _command_response)
|
|
59
53
|
: this.updates.default);
|
|
60
54
|
// execute the custom bot commands from bot configurations
|
|
61
|
-
executeCommand = async (update) => this._executeCommand(update, update.message?.text ??
|
|
62
|
-
this.updates.default;
|
|
55
|
+
executeCommand = async (update) => this._executeCommand(update, update.message?.text ?? '') ?? this.updates.default;
|
|
63
56
|
// trigger answerInlineQuery command of BotAPI
|
|
64
57
|
answerInlineQuery = async (inline_query_id, results, cache_time = 0) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/answerInlineQuery`), {
|
|
65
58
|
inline_query_id: inline_query_id.toString(),
|
|
@@ -73,7 +66,7 @@ export default class TelegramApi extends BotApi {
|
|
|
73
66
|
text,
|
|
74
67
|
}).href));
|
|
75
68
|
// trigger sendMessage command of BotAPI
|
|
76
|
-
sendMessage = async (chat_id, text, parse_mode =
|
|
69
|
+
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`), {
|
|
77
70
|
chat_id: chat_id.toString(),
|
|
78
71
|
text,
|
|
79
72
|
parse_mode: parse_mode,
|
|
@@ -89,21 +82,21 @@ export default class TelegramApi extends BotApi {
|
|
|
89
82
|
disable_notification: disable_notification.toString(),
|
|
90
83
|
}).href));
|
|
91
84
|
// trigger sendPhoto command of BotAPI
|
|
92
|
-
sendPhotoRaw = async (chat_id, photo, caption =
|
|
85
|
+
sendPhotoRaw = async (chat_id, photo, caption = '', parse_mode = '', disable_notification = false, reply_to_message_id = 0) => {
|
|
93
86
|
const formdata = new FormData();
|
|
94
|
-
formdata.set(
|
|
87
|
+
formdata.set('file', photo);
|
|
95
88
|
return fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/sendPhoto`), {
|
|
96
89
|
chat_id: chat_id.toString(),
|
|
97
90
|
caption,
|
|
98
91
|
parse_mode,
|
|
99
92
|
disable_notification: disable_notification.toString(),
|
|
100
93
|
reply_to_message_id: reply_to_message_id.toString(),
|
|
101
|
-
}).href), { method:
|
|
94
|
+
}).href), { method: 'POST', body: formdata })
|
|
102
95
|
.then((resp) => resp.text())
|
|
103
96
|
.then(log);
|
|
104
97
|
};
|
|
105
98
|
// trigger sendPhoto command of BotAPI
|
|
106
|
-
sendPhoto = async (chat_id, photo, caption =
|
|
99
|
+
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`), {
|
|
107
100
|
chat_id: chat_id.toString(),
|
|
108
101
|
photo,
|
|
109
102
|
caption,
|
|
@@ -112,7 +105,7 @@ export default class TelegramApi extends BotApi {
|
|
|
112
105
|
reply_to_message_id: reply_to_message_id.toString(),
|
|
113
106
|
}).href));
|
|
114
107
|
// trigger sendVideo command of BotAPI
|
|
115
|
-
sendVideo = async (chat_id, video, duration = 0, width = 0, height = 0, thumb =
|
|
108
|
+
sendVideo = async (chat_id, video, duration = 0, width = 0, height = 0, thumb = '', caption = '', parse_mode = '', supports_streaming = false, disable_notification = false, reply_to_message_id = 0) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/sendVideo`), {
|
|
116
109
|
chat_id: chat_id.toString(),
|
|
117
110
|
video: JSON.stringify(video),
|
|
118
111
|
duration: duration.toString(),
|
|
@@ -126,7 +119,7 @@ export default class TelegramApi extends BotApi {
|
|
|
126
119
|
reply_to_message_id: reply_to_message_id.toString(),
|
|
127
120
|
}).href));
|
|
128
121
|
// trigger sendAnimation command of BotAPI
|
|
129
|
-
sendAnimation = async (chat_id, animation, duration = 0, width = 0, height = 0, thumb =
|
|
122
|
+
sendAnimation = async (chat_id, animation, duration = 0, width = 0, height = 0, thumb = '', caption = '', parse_mode = '', disable_notification = false, reply_to_message_id = 0) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/sendAnimation`), {
|
|
130
123
|
chat_id: chat_id.toString(),
|
|
131
124
|
animation: JSON.stringify(animation),
|
|
132
125
|
duration: duration.toString(),
|
|
@@ -148,7 +141,7 @@ export default class TelegramApi extends BotApi {
|
|
|
148
141
|
reply_to_message_id: reply_to_message_id.toString(),
|
|
149
142
|
}).href));
|
|
150
143
|
// trigger senPoll command of BotAPI
|
|
151
|
-
sendPoll = async (chat_id, question, options, is_anonymous = false, type =
|
|
144
|
+
sendPoll = async (chat_id, question, options, is_anonymous = false, type = '', allows_multiple_answers = false, correct_option_id = 0, explanation = '', explanation_parse_mode = '', open_period = 0, close_date = 0, is_closed = false, disable_notification = false, reply_to_message_id = 0) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/sendPoll`), {
|
|
152
145
|
chat_id: chat_id.toString(),
|
|
153
146
|
question,
|
|
154
147
|
options: options.toString(),
|
|
@@ -165,7 +158,7 @@ export default class TelegramApi extends BotApi {
|
|
|
165
158
|
reply_to_message_id: reply_to_message_id.toString(),
|
|
166
159
|
}).href));
|
|
167
160
|
// trigger senDice command of BotAPI
|
|
168
|
-
sendDice = async (chat_id, emoji =
|
|
161
|
+
sendDice = async (chat_id, emoji = '', disable_notification = false, reply_to_message_id = 0) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/sendDice`), {
|
|
169
162
|
chat_id: chat_id.toString(),
|
|
170
163
|
emoji,
|
|
171
164
|
disable_notification: disable_notification.toString(),
|
|
@@ -1,32 +1,31 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import TelegramApi from
|
|
3
|
-
import {
|
|
2
|
+
import TelegramApi from './telegram_api';
|
|
3
|
+
import { Config, Kv, TelegramUpdate } from './types';
|
|
4
4
|
export default class TelegramBot extends TelegramApi {
|
|
5
|
+
translate: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
6
|
+
clear: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
7
|
+
image: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
8
|
+
question: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
9
|
+
sean: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
10
|
+
code: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
11
|
+
duckduckgo: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
12
|
+
kanye: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
13
|
+
joke: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
14
|
+
dog: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
15
|
+
bored: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
16
|
+
epoch: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
17
|
+
roll: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
18
|
+
commandList: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
19
|
+
toss: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
20
|
+
ping: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
21
|
+
getChatInfo: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
5
22
|
url: URL;
|
|
6
23
|
kv: Kv;
|
|
7
24
|
get_set: KVNamespace;
|
|
8
|
-
ai:
|
|
25
|
+
ai: Ai;
|
|
9
26
|
db: D1Database;
|
|
10
27
|
r2: R2Bucket;
|
|
11
28
|
bot_name: string;
|
|
12
29
|
chat_model: string;
|
|
13
30
|
constructor(config: Config);
|
|
14
|
-
translate: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
15
|
-
clear: (update: TelegramUpdate) => Promise<Response>;
|
|
16
|
-
image: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
17
|
-
question: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
18
|
-
sean: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
19
|
-
code: (update: TelegramUpdate) => Promise<Response>;
|
|
20
|
-
duckduckgo: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
21
|
-
kanye: (update: TelegramUpdate) => Promise<Response>;
|
|
22
|
-
joke: (update: TelegramUpdate) => Promise<Response>;
|
|
23
|
-
dog: (update: TelegramUpdate) => Promise<Response>;
|
|
24
|
-
bored: (update: TelegramUpdate) => Promise<Response>;
|
|
25
|
-
epoch: (update: TelegramUpdate) => Promise<Response>;
|
|
26
|
-
_average: (numbers: number[]) => number;
|
|
27
|
-
roll: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
28
|
-
commandList: (update: TelegramUpdate) => Promise<Response>;
|
|
29
|
-
toss: (update: TelegramUpdate) => Promise<Response>;
|
|
30
|
-
ping: (update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
31
|
-
getChatInfo: (update: TelegramUpdate) => Promise<Response>;
|
|
32
31
|
}
|
|
@@ -1,11 +1,42 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
1
|
+
import TelegramApi from './telegram_api';
|
|
2
|
+
import translate from './commands/translate';
|
|
3
|
+
import clear from './commands/clear';
|
|
4
|
+
import image from './commands/image';
|
|
5
|
+
import question from './commands/question';
|
|
6
|
+
import sean from './commands/sean';
|
|
7
|
+
import code from './commands/code';
|
|
8
|
+
import duckduckgo from './commands/duckduckgo';
|
|
9
|
+
import kanye from './commands/kanye';
|
|
10
|
+
import joke from './commands/joke';
|
|
11
|
+
import dog from './commands/dog';
|
|
12
|
+
import bored from './commands/bored';
|
|
13
|
+
import epoch from './commands/epoch';
|
|
14
|
+
import roll from './commands/roll';
|
|
15
|
+
import commandlist from './commands/commandlist';
|
|
16
|
+
import toss from './commands/toss';
|
|
17
|
+
import ping from './commands/ping';
|
|
18
|
+
import getchatinfo from './commands/getchatinfo';
|
|
4
19
|
export default class TelegramBot extends TelegramApi {
|
|
20
|
+
translate;
|
|
21
|
+
clear;
|
|
22
|
+
image;
|
|
23
|
+
question;
|
|
24
|
+
sean;
|
|
25
|
+
code;
|
|
26
|
+
duckduckgo;
|
|
27
|
+
kanye;
|
|
28
|
+
joke;
|
|
29
|
+
dog;
|
|
30
|
+
bored;
|
|
31
|
+
epoch;
|
|
32
|
+
roll;
|
|
33
|
+
commandList;
|
|
34
|
+
toss;
|
|
35
|
+
ping;
|
|
36
|
+
getChatInfo;
|
|
5
37
|
url;
|
|
6
38
|
kv;
|
|
7
39
|
get_set;
|
|
8
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
40
|
ai;
|
|
10
41
|
db;
|
|
11
42
|
r2;
|
|
@@ -13,6 +44,23 @@ export default class TelegramBot extends TelegramApi {
|
|
|
13
44
|
chat_model;
|
|
14
45
|
constructor(config) {
|
|
15
46
|
super(config.commands, config.webhook, config.handler);
|
|
47
|
+
this.translate = translate;
|
|
48
|
+
this.clear = clear;
|
|
49
|
+
this.image = image;
|
|
50
|
+
this.question = question;
|
|
51
|
+
this.sean = sean;
|
|
52
|
+
this.code = code;
|
|
53
|
+
this.duckduckgo = duckduckgo;
|
|
54
|
+
this.kanye = kanye;
|
|
55
|
+
this.joke = joke;
|
|
56
|
+
this.dog = dog;
|
|
57
|
+
this.bored = bored;
|
|
58
|
+
this.epoch = epoch;
|
|
59
|
+
this.roll = roll;
|
|
60
|
+
this.commandList = commandlist;
|
|
61
|
+
this.toss = toss;
|
|
62
|
+
this.ping = ping;
|
|
63
|
+
this.getChatInfo = getchatinfo;
|
|
16
64
|
this.url = config.url;
|
|
17
65
|
this.kv = config.kv;
|
|
18
66
|
this.get_set = config.kv?.get_set;
|
|
@@ -22,334 +70,4 @@ export default class TelegramBot extends TelegramApi {
|
|
|
22
70
|
this.bot_name = config.bot_name;
|
|
23
71
|
this.chat_model = config.chat_model;
|
|
24
72
|
}
|
|
25
|
-
// bot command: /translate
|
|
26
|
-
translate = async (update, args) => {
|
|
27
|
-
if (this.ai === undefined) {
|
|
28
|
-
return new Response("ok");
|
|
29
|
-
}
|
|
30
|
-
let _prompt;
|
|
31
|
-
if (args[0][0] === "/") {
|
|
32
|
-
_prompt = args.slice(1).join(" ");
|
|
33
|
-
}
|
|
34
|
-
else {
|
|
35
|
-
_prompt = args.join(" ");
|
|
36
|
-
}
|
|
37
|
-
if (_prompt === "") {
|
|
38
|
-
_prompt = "";
|
|
39
|
-
}
|
|
40
|
-
const langs = ["french", "arabic", "german", "spanish", "russian"];
|
|
41
|
-
const inline_articles = await Promise.all(langs.map(async (lang) => {
|
|
42
|
-
const response = await this.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);
|
|
50
|
-
};
|
|
51
|
-
// bot command: /clear
|
|
52
|
-
// reset the llama2 session by deleting messages from d1
|
|
53
|
-
clear = async (update) => {
|
|
54
|
-
const { success } = await this.db
|
|
55
|
-
.prepare("DELETE FROM Messages WHERE userId=?")
|
|
56
|
-
.bind(update.inline_query
|
|
57
|
-
? update.inline_query.from.id
|
|
58
|
-
: update.message?.from.id)
|
|
59
|
-
.run();
|
|
60
|
-
if (success) {
|
|
61
|
-
if (update.inline_query) {
|
|
62
|
-
return this.answerInlineQuery(update.inline_query.id, [
|
|
63
|
-
new TelegramInlineQueryResultArticle("_"),
|
|
64
|
-
]);
|
|
65
|
-
}
|
|
66
|
-
return this.sendMessage(update.message?.chat.id ?? 0, "_");
|
|
67
|
-
}
|
|
68
|
-
return this.sendMessage(update.message?.chat.id ?? 0, "failed");
|
|
69
|
-
};
|
|
70
|
-
// bot command: /image
|
|
71
|
-
image = async (update, args) => {
|
|
72
|
-
let _prompt;
|
|
73
|
-
if (args[0][0] === "/") {
|
|
74
|
-
_prompt = args.slice(1).join(" ");
|
|
75
|
-
}
|
|
76
|
-
else {
|
|
77
|
-
_prompt = args.join(" ");
|
|
78
|
-
}
|
|
79
|
-
if (_prompt === "") {
|
|
80
|
-
_prompt = "";
|
|
81
|
-
}
|
|
82
|
-
const inputs = { prompt: _prompt, num_steps: 20 };
|
|
83
|
-
await this.sendMessage(update.message?.chat.id ?? 0, "image is processing. please wait...");
|
|
84
|
-
const response = await this.ai.run("@cf/stabilityai/stable-diffusion-xl-base-1.0", inputs);
|
|
85
|
-
const id = crypto.randomUUID();
|
|
86
|
-
await this.r2.put(id, response);
|
|
87
|
-
const url = "https://r2.seanbehan.ca/" + id;
|
|
88
|
-
return this.sendPhoto(update.message?.chat.id ?? 0, url);
|
|
89
|
-
};
|
|
90
|
-
// bot command: /question
|
|
91
|
-
question = async (update, args) => {
|
|
92
|
-
if (this.ai === undefined) {
|
|
93
|
-
return new Response("ok");
|
|
94
|
-
}
|
|
95
|
-
let _prompt;
|
|
96
|
-
if (args[0][0] === "/") {
|
|
97
|
-
_prompt = args.slice(1).join(" ");
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
_prompt = args.join(" ");
|
|
101
|
-
}
|
|
102
|
-
if (_prompt === "") {
|
|
103
|
-
_prompt = "";
|
|
104
|
-
}
|
|
105
|
-
const results = await (async () => {
|
|
106
|
-
if (this.db) {
|
|
107
|
-
const { results } = await this.db
|
|
108
|
-
.prepare("SELECT * FROM Messages WHERE userId=?")
|
|
109
|
-
.bind(update.inline_query
|
|
110
|
-
? update.inline_query.from.id
|
|
111
|
-
: update.message?.from.id)
|
|
112
|
-
.all();
|
|
113
|
-
return results;
|
|
114
|
-
}
|
|
115
|
-
})();
|
|
116
|
-
const old_messages = (() => {
|
|
117
|
-
if (results) {
|
|
118
|
-
return results.map((col) => ({
|
|
119
|
-
role: "system",
|
|
120
|
-
content: col.content,
|
|
121
|
-
}));
|
|
122
|
-
}
|
|
123
|
-
return [];
|
|
124
|
-
})();
|
|
125
|
-
const system_prompt = "<s>" +
|
|
126
|
-
[
|
|
127
|
-
`Your name is ${this.bot_name}.`,
|
|
128
|
-
`You are talking to ${update.message?.from.first_name}.`,
|
|
129
|
-
`Your source code is at https://github.com/codebam/cf-workers-telegram-bot .`,
|
|
130
|
-
`the current date is ${new Date().toString()}`,
|
|
131
|
-
].reduce((acc, cur) => {
|
|
132
|
-
return acc + cur + "\n";
|
|
133
|
-
}) +
|
|
134
|
-
old_messages.reduce((acc, cur) => {
|
|
135
|
-
return acc + cur.content + "\n";
|
|
136
|
-
}, "") +
|
|
137
|
-
"</s>";
|
|
138
|
-
const p = system_prompt + "[INST]" + _prompt + "[/INST]";
|
|
139
|
-
const prompt = p.slice(p.length - 4096, p.length);
|
|
140
|
-
const response = await this.ai
|
|
141
|
-
.run(this.chat_model, {
|
|
142
|
-
prompt,
|
|
143
|
-
max_tokens: 596,
|
|
144
|
-
})
|
|
145
|
-
.then(({ response }) => response
|
|
146
|
-
.replace(/(\[|)(\/|)INST(S|)(s|)(\]|)/, "")
|
|
147
|
-
.replace(/<<(\/|)SYS>>/, ""));
|
|
148
|
-
if (this.db) {
|
|
149
|
-
const { success } = await this.db
|
|
150
|
-
.prepare("INSERT INTO Messages (id, userId, content) VALUES (?, ?, ?)")
|
|
151
|
-
.bind(crypto.randomUUID(), update.inline_query
|
|
152
|
-
? update.inline_query.from.id
|
|
153
|
-
: update.message?.from.id, "[INST] " + _prompt + " [/INST]" + "\n" + response)
|
|
154
|
-
.run();
|
|
155
|
-
if (!success) {
|
|
156
|
-
console.log("failed to insert data into d1");
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
if (response === "") {
|
|
160
|
-
this.clear(update);
|
|
161
|
-
return this.question(update, args);
|
|
162
|
-
} // sometimes llama2 doesn't respond when given lots of system prompts
|
|
163
|
-
if (update.inline_query) {
|
|
164
|
-
return this.answerInlineQuery(update.inline_query.id, [
|
|
165
|
-
new TelegramInlineQueryResultArticle(response),
|
|
166
|
-
]);
|
|
167
|
-
}
|
|
168
|
-
return this.sendMessage(update.message?.chat.id ?? 0, response, "", false, false, update.message?.message_id);
|
|
169
|
-
};
|
|
170
|
-
// bot command: /sean
|
|
171
|
-
sean = async (update, args) => {
|
|
172
|
-
if (this.ai === undefined) {
|
|
173
|
-
return new Response("ok");
|
|
174
|
-
}
|
|
175
|
-
let _prompt;
|
|
176
|
-
if (args[0][0] === "/") {
|
|
177
|
-
_prompt = args.slice(1).join(" ");
|
|
178
|
-
}
|
|
179
|
-
else {
|
|
180
|
-
_prompt = args.join(" ");
|
|
181
|
-
}
|
|
182
|
-
if (_prompt === "") {
|
|
183
|
-
_prompt = "";
|
|
184
|
-
}
|
|
185
|
-
const results = await (async () => {
|
|
186
|
-
if (this.db) {
|
|
187
|
-
const { results } = await this.db
|
|
188
|
-
.prepare("SELECT * FROM Messages WHERE userId=?")
|
|
189
|
-
.bind(update.inline_query
|
|
190
|
-
? update.inline_query.from.id
|
|
191
|
-
: update.message?.from.id)
|
|
192
|
-
.all();
|
|
193
|
-
return results;
|
|
194
|
-
}
|
|
195
|
-
})();
|
|
196
|
-
const old_messages = (() => {
|
|
197
|
-
if (results) {
|
|
198
|
-
return results.map((col) => ({
|
|
199
|
-
role: "system",
|
|
200
|
-
content: col.content,
|
|
201
|
-
}));
|
|
202
|
-
}
|
|
203
|
-
return [];
|
|
204
|
-
})();
|
|
205
|
-
const system_prompt = "<s>" +
|
|
206
|
-
[
|
|
207
|
-
`Your name is ${this.bot_name}.`,
|
|
208
|
-
`You are talking to ${update.message?.from.first_name}.`,
|
|
209
|
-
`Your source code is at https://github.com/codebam/cf-workers-telegram-bot .`,
|
|
210
|
-
`the current date is ${new Date().toString()}`,
|
|
211
|
-
"Sean Behan is a full stack developer who goes by the username codebam.",
|
|
212
|
-
"Sean Behan likes programming and video games.",
|
|
213
|
-
"Pretend to be Sean Behan but don't make things up.",
|
|
214
|
-
].reduce((acc, cur) => {
|
|
215
|
-
return acc + cur + "\n";
|
|
216
|
-
}) +
|
|
217
|
-
old_messages.reduce((acc, cur) => {
|
|
218
|
-
return acc + cur.content + "\n";
|
|
219
|
-
}, "") +
|
|
220
|
-
"</s>";
|
|
221
|
-
const p = system_prompt + "[INST]" + _prompt + "[/INST]";
|
|
222
|
-
const prompt = p.slice(p.length - 4096, p.length);
|
|
223
|
-
const response = await this.ai
|
|
224
|
-
.run(this.chat_model, {
|
|
225
|
-
prompt,
|
|
226
|
-
max_tokens: 596,
|
|
227
|
-
})
|
|
228
|
-
.then(({ response }) => response
|
|
229
|
-
.replace(/(\[|)(\/|)INST(S|)(s|)(\]|)/, "")
|
|
230
|
-
.replace(/<<(\/|)SYS>>/, "")
|
|
231
|
-
.replace(/[OUT]/, ""));
|
|
232
|
-
if (this.db) {
|
|
233
|
-
const { success } = await this.db
|
|
234
|
-
.prepare("INSERT INTO Messages (id, userId, content) VALUES (?, ?, ?)")
|
|
235
|
-
.bind(crypto.randomUUID(), update.inline_query
|
|
236
|
-
? update.inline_query.from.id
|
|
237
|
-
: update.message?.from.id, "[INST] " + _prompt + " [/INST]" + "\n" + response)
|
|
238
|
-
.run();
|
|
239
|
-
if (!success) {
|
|
240
|
-
console.log("failed to insert data into d1");
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
if (update.inline_query) {
|
|
244
|
-
return this.answerInlineQuery(update.inline_query.id, [
|
|
245
|
-
new TelegramInlineQueryResultArticle(response),
|
|
246
|
-
]);
|
|
247
|
-
}
|
|
248
|
-
return this.sendMessage(update.message?.chat.id ?? 0, response, "", false, false, update.message?.message_id);
|
|
249
|
-
};
|
|
250
|
-
// bot command: /code
|
|
251
|
-
code = async (update) => ((url) => update.inline_query
|
|
252
|
-
? this.answerInlineQuery(update.inline_query.id, [
|
|
253
|
-
new TelegramInlineQueryResultArticle(url),
|
|
254
|
-
])
|
|
255
|
-
: this.sendMessage(update.message?.chat.id ?? 0, url))("https://github.com/codebam/cf-workers-telegram-bot");
|
|
256
|
-
// bot command: /duckduckgo
|
|
257
|
-
duckduckgo = async (update, args) => ((query) => ((duckduckgo_url) => update.inline_query && query === ""
|
|
258
|
-
? this.answerInlineQuery(update.inline_query.id, [
|
|
259
|
-
new TelegramInlineQueryResultArticle("https://duckduckgo.com"),
|
|
260
|
-
])
|
|
261
|
-
: update.inline_query
|
|
262
|
-
? fetch(addSearchParams(new URL("https://api.duckduckgo.com"), {
|
|
263
|
-
q: query,
|
|
264
|
-
format: "json",
|
|
265
|
-
t: "telegram_bot",
|
|
266
|
-
no_redirect: "1",
|
|
267
|
-
}).href).then((response) => response
|
|
268
|
-
.json()
|
|
269
|
-
.then((results) => results)
|
|
270
|
-
.then((ddg_response) => ((instant_answer_url, thumb_url, default_thumb_url = "https://duckduckgo.com/assets/icons/meta/DDG-icon_256x256.png") => this.answerInlineQuery(update.inline_query?.id ?? 0, instant_answer_url !== ""
|
|
271
|
-
? [
|
|
272
|
-
new TelegramInlineQueryResultArticle(`${instant_answer_url}\n\n<a href="${addSearchParams(new URL(duckduckgo_url), {
|
|
273
|
-
q: args
|
|
274
|
-
.slice(2)
|
|
275
|
-
.join(" ")
|
|
276
|
-
.replace(/^!\w* /, ""),
|
|
277
|
-
}).href}">Results From DuckDuckGo</a>`, instant_answer_url, "HTML", thumb_url),
|
|
278
|
-
new TelegramInlineQueryResultArticle(duckduckgo_url, duckduckgo_url, "", default_thumb_url),
|
|
279
|
-
]
|
|
280
|
-
: [
|
|
281
|
-
new TelegramInlineQueryResultArticle(duckduckgo_url, duckduckgo_url, "", default_thumb_url),
|
|
282
|
-
], 3600 // 1 hour
|
|
283
|
-
))(ddg_response.Redirect ?? ddg_response.AbstractURL, ddg_response.Redirect === ""
|
|
284
|
-
? `https://duckduckgo.com${ddg_response.Image !== "" && ddg_response.Image
|
|
285
|
-
? ddg_response.Image
|
|
286
|
-
: ddg_response.RelatedTopics.length !== 0 &&
|
|
287
|
-
ddg_response.RelatedTopics[0].Icon.URL !== ""
|
|
288
|
-
? ddg_response.RelatedTopics[0].Icon.URL
|
|
289
|
-
: "/i/f96d4798.png"}`
|
|
290
|
-
: "")))
|
|
291
|
-
: this.sendMessage(update.message?.chat.id ?? 0, duckduckgo_url))(query === ""
|
|
292
|
-
? "https://duckduckgo.com"
|
|
293
|
-
: (() => {
|
|
294
|
-
if (query[0][0] !== "/") {
|
|
295
|
-
return addSearchParams(new URL("https://duckduckgo.com"), {
|
|
296
|
-
q: query,
|
|
297
|
-
}).href;
|
|
298
|
-
}
|
|
299
|
-
return addSearchParams(new URL("https://duckduckgo.com"), {
|
|
300
|
-
q: query.split(" ").slice(1).join(" "),
|
|
301
|
-
}).href;
|
|
302
|
-
})()))(args.join(" "));
|
|
303
|
-
// bot command: /kanye
|
|
304
|
-
kanye = async (update) => fetch("https://api.kanye.rest")
|
|
305
|
-
.then((response) => responseToJSON(response))
|
|
306
|
-
.then((json) => ((message) => update.inline_query
|
|
307
|
-
? this.answerInlineQuery(update.inline_query.id, [
|
|
308
|
-
new TelegramInlineQueryResultArticle(message),
|
|
309
|
-
])
|
|
310
|
-
: this.sendMessage(update.message?.chat.id ?? 0, message))(`Kanye says... ${json.quote}`))
|
|
311
|
-
.catch(() => new Response("Failed to parse JSON"));
|
|
312
|
-
// bot command: /joke
|
|
313
|
-
joke = async (update) => fetch("https://v2.jokeapi.dev/joke/Any?safe-mode")
|
|
314
|
-
.then((response) => responseToJSON(response))
|
|
315
|
-
.then((joke) => joke)
|
|
316
|
-
.then((joke_response) => ((message) => update.inline_query
|
|
317
|
-
? this.answerInlineQuery(update.inline_query.id, [
|
|
318
|
-
new TelegramInlineQueryResultArticle(message, joke_response.joke ?? joke_response.setup, "HTML"),
|
|
319
|
-
], 0)
|
|
320
|
-
: this.sendMessage(update.message?.chat.id ?? 0, message, "HTML"))(joke_response.joke ??
|
|
321
|
-
`${joke_response.setup}\n\n<tg-spoiler>${joke_response.delivery}</tg-spoiler>`));
|
|
322
|
-
// bot command: /dog
|
|
323
|
-
dog = async (update) => fetch("https://shibe.online/api/shibes")
|
|
324
|
-
.then((response) => response.json())
|
|
325
|
-
.then((json) => json)
|
|
326
|
-
.then((shibe_response) => update.inline_query
|
|
327
|
-
? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultPhoto(shibe_response[0])], 0)
|
|
328
|
-
: this.sendPhoto(update.message?.chat.id ?? 0, shibe_response[0]));
|
|
329
|
-
// bot command: /bored
|
|
330
|
-
bored = async (update) => fetch("https://boredapi.com/api/activity/")
|
|
331
|
-
.then((response) => responseToJSON(response))
|
|
332
|
-
.then((json) => json)
|
|
333
|
-
.then((bored_response) => update.inline_query
|
|
334
|
-
? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(bored_response.activity)], 0)
|
|
335
|
-
: this.sendMessage(update.message?.chat.id ?? 0, bored_response.activity));
|
|
336
|
-
// bot command: /epoch
|
|
337
|
-
epoch = async (update) => ((seconds) => update.inline_query
|
|
338
|
-
? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(seconds)], 0)
|
|
339
|
-
: this.sendMessage(update.message?.chat.id ?? 0, seconds))(Math.floor(Date.now() / 1000).toString());
|
|
340
|
-
_average = (numbers) => parseFloat((numbers.reduce((prev, cur) => prev + cur, 0) / numbers.length || 0).toFixed(2));
|
|
341
|
-
// bot command: /roll
|
|
342
|
-
roll = async (update, args) => ((outcome, message) => update.inline_query
|
|
343
|
-
? this.answerInlineQuery(update.inline_query.id, [
|
|
344
|
-
new TelegramInlineQueryResultArticle(message(update.inline_query.from.username, update.inline_query.from.first_name, outcome)),
|
|
345
|
-
])
|
|
346
|
-
: 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}`);
|
|
347
|
-
// bot command: /commandList
|
|
348
|
-
commandList = async (update) => this.sendMessage(update.message?.chat.id ?? 0, `${Object.keys(this.commands).join("\n")}`, "HTML");
|
|
349
|
-
// bot command: /toss
|
|
350
|
-
toss = async (update) => this.sendMessage(update.message?.chat.id ?? 0, Math.floor(Math.random() * 2) == 0 ? "heads" : "tails");
|
|
351
|
-
// bot command: /ping
|
|
352
|
-
ping = async (update, args) => this.sendMessage(update.message?.chat.id ?? 0, args.length === 1 ? "pong" : args.slice(1).join(" "));
|
|
353
|
-
// bot command: /chatInfo
|
|
354
|
-
getChatInfo = async (update) => this.sendMessage(update.message?.chat.id ?? 0, preTagString(prettyJSON(update.message?.chat ?? 0)), "HTML");
|
|
355
73
|
}
|