@codebam/cf-workers-telegram-bot 4.0.0 → 4.1.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 +15 -24
- package/dist/main/src/libs.d.ts +1 -1
- package/dist/main/src/libs.js +1 -1
- package/dist/main/src/telegram_api.js +17 -20
- package/dist/main/src/telegram_bot.js +60 -57
- package/package.json +6 -4
package/dist/main/src/handler.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import BotApi from "./bot_api";
|
|
2
1
|
import { sha256, log } from "./libs";
|
|
3
|
-
import { Config,
|
|
2
|
+
import { Config, localhost, } from "./types";
|
|
4
3
|
export default class Handler {
|
|
5
4
|
configs;
|
|
6
5
|
constructor(configs) {
|
|
@@ -13,18 +12,13 @@ export default class Handler {
|
|
|
13
12
|
}
|
|
14
13
|
return this.responses.default();
|
|
15
14
|
};
|
|
16
|
-
postResponse = async (_request, _bot) =>
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
const request = _request ?? new Request("");
|
|
21
|
-
return (request
|
|
15
|
+
postResponse = async (_request, _bot) => _bot?.webhook.token === ""
|
|
16
|
+
? this.responses.default()
|
|
17
|
+
: _request
|
|
18
|
+
? _request
|
|
22
19
|
.json()
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
return this.responses.default();
|
|
27
|
-
};
|
|
20
|
+
.then((update) => _bot.update(update))
|
|
21
|
+
: this.responses.default();
|
|
28
22
|
responses = {
|
|
29
23
|
GET: this.getResponse,
|
|
30
24
|
POST: this.postResponse,
|
|
@@ -37,15 +31,12 @@ export default class Handler {
|
|
|
37
31
|
bot_config,
|
|
38
32
|
]))).then((result) => Object.fromEntries(result));
|
|
39
33
|
// handles the request
|
|
40
|
-
handle = async (request) => this.getAccessKeys(this.configs).then((access_keys) =>
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
return this.responses.default();
|
|
50
|
-
});
|
|
34
|
+
handle = async (request) => this.getAccessKeys(this.configs).then((access_keys) => Object.keys(this.responses).includes(request.method)
|
|
35
|
+
? this.responses[request.method](request, new access_keys[new URL(request.url).pathname.substring(1)].api({
|
|
36
|
+
...new Config(),
|
|
37
|
+
url: new URL(new URL(request.url).origin),
|
|
38
|
+
handler: this,
|
|
39
|
+
...access_keys[new URL(request.url).pathname.substring(1)],
|
|
40
|
+
}))
|
|
41
|
+
: this.responses.default());
|
|
51
42
|
}
|
package/dist/main/src/libs.d.ts
CHANGED
|
@@ -6,5 +6,5 @@ export declare const log: (obj: any) => any;
|
|
|
6
6
|
export declare const preTagString: (str: string) => string;
|
|
7
7
|
export declare const addSearchParams: (url: URL, params?: Record<string, string>) => URL;
|
|
8
8
|
export declare const responseToJSON: (response: Response) => Promise<Record<string, unknown>>;
|
|
9
|
-
export declare const undefinedEmpty: <T>(obj: T) => T[];
|
|
9
|
+
export declare const undefinedEmpty: <T>(obj: T) => (T & ({} | null))[];
|
|
10
10
|
export declare const fetch_json: (url: URL) => Promise<Response>;
|
package/dist/main/src/libs.js
CHANGED
|
@@ -24,7 +24,7 @@ export const responseToJSON = async (response) => response
|
|
|
24
24
|
.text()
|
|
25
25
|
.then((text) => JSON.parse(text))
|
|
26
26
|
.catch(() => log({ error: "Failed to parse JSON of response" }));
|
|
27
|
-
export const undefinedEmpty = (obj) => (obj === undefined
|
|
27
|
+
export const undefinedEmpty = (obj) => (obj === undefined ? [] : [obj]);
|
|
28
28
|
export const fetch_json = async (url) => fetch(url.href)
|
|
29
29
|
.then((response) => responseToJSON(response))
|
|
30
30
|
.then((json) => JSONResponse(json));
|
|
@@ -1,16 +1,13 @@
|
|
|
1
1
|
import BotApi from "./bot_api";
|
|
2
|
-
import { addSearchParams, log
|
|
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
|
-
inlineQueryUpdate = async (update) => this.executeInlineCommand(update)
|
|
8
|
-
messageUpdate = async (update) =>
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
return this.updates.default;
|
|
13
|
-
};
|
|
7
|
+
inlineQueryUpdate = async (update) => this.executeInlineCommand(update);
|
|
8
|
+
messageUpdate = async (update) => typeof update.message?.text === "string"
|
|
9
|
+
? this.executeCommand(update).then(async () => this.greetUsers(update))
|
|
10
|
+
: this.updates.default;
|
|
14
11
|
updates = {
|
|
15
12
|
inline_query: this.inlineQueryUpdate,
|
|
16
13
|
message: this.messageUpdate,
|
|
@@ -25,25 +22,25 @@ export default class TelegramApi extends BotApi {
|
|
|
25
22
|
this.updates.inline_query(update)) ??
|
|
26
23
|
this.updates.default;
|
|
27
24
|
// greet new users who join
|
|
28
|
-
greetUsers = async (update) =>
|
|
29
|
-
this.sendMessage(update.message.chat.id, `Welcome to ${update.message.chat.title}, ${update.message.from.username}`)
|
|
30
|
-
this.updates.default;
|
|
25
|
+
greetUsers = async (update) => update.message?.new_chat_members
|
|
26
|
+
? this.sendMessage(update.message.chat.id, `Welcome to ${update.message.chat.title}, ${update.message.from.username}`)
|
|
27
|
+
: this.updates.default;
|
|
31
28
|
getCommand = (args) => args[0]?.split("@")[0];
|
|
32
29
|
// run command passed from executeCommand
|
|
33
|
-
_executeCommand = async (update, text, args = []) =>
|
|
34
|
-
|
|
35
|
-
|
|
30
|
+
_executeCommand = async (update, text, args = []) => log({ execute: { text, args } })
|
|
31
|
+
? ((text_args) => ((command) => this.commands[command]
|
|
32
|
+
? this.commands[command]?.(this, update, [...text_args, ...args])
|
|
33
|
+
: log({
|
|
36
34
|
error: `command '${command}' does not exist`,
|
|
37
|
-
}))
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
this.updates.default)(this.getCommand(text_args)))(
|
|
35
|
+
}))(
|
|
36
|
+
// run the command
|
|
37
|
+
this.getCommand(text_args)))(
|
|
41
38
|
// get the command to run
|
|
42
39
|
text
|
|
43
40
|
.trimStart()
|
|
44
41
|
.replace(/^([^\s]*\s)\s*/gm, "$1")
|
|
45
|
-
.split(" "))
|
|
46
|
-
this.updates.default;
|
|
42
|
+
.split(" "))
|
|
43
|
+
: this.updates.default;
|
|
47
44
|
// execute the inline custom bot commands from bot configurations
|
|
48
45
|
executeInlineCommand = async (update) => this._executeCommand(update, update.inline_query?.query ?? "").then(async (command_response) => command_response &&
|
|
49
46
|
this._executeCommand(update, "inline", update.inline_query?.query.trimStart().split(" ")).then((_command_response) => _command_response)) ?? this.updates.default;
|
|
@@ -12,19 +12,18 @@ export default class TelegramBot extends TelegramApi {
|
|
|
12
12
|
this.get_set = config.kv?.get_set;
|
|
13
13
|
}
|
|
14
14
|
// bot command: /code
|
|
15
|
-
code = async (update) => ((url) =>
|
|
16
|
-
this.answerInlineQuery(update.inline_query.id, [
|
|
15
|
+
code = async (update) => ((url) => update.inline_query
|
|
16
|
+
? this.answerInlineQuery(update.inline_query.id, [
|
|
17
17
|
new TelegramInlineQueryResultArticle(url),
|
|
18
|
-
])
|
|
19
|
-
this.sendMessage(update.message?.chat.id ?? 0, url))("https://github.com/codebam/cf-workers-telegram-bot");
|
|
18
|
+
])
|
|
19
|
+
: this.sendMessage(update.message?.chat.id ?? 0, url))("https://github.com/codebam/cf-workers-telegram-bot");
|
|
20
20
|
// bot command: /duckduckgo
|
|
21
|
-
duckduckgo = async (update, args) => ((query) => ((duckduckgo_url) =>
|
|
22
|
-
|
|
23
|
-
this.answerInlineQuery(update.inline_query.id, [
|
|
21
|
+
duckduckgo = async (update, args) => ((query) => ((duckduckgo_url) => update.inline_query && query === ""
|
|
22
|
+
? this.answerInlineQuery(update.inline_query.id, [
|
|
24
23
|
new TelegramInlineQueryResultArticle("https://duckduckgo.com"),
|
|
25
|
-
])
|
|
26
|
-
|
|
27
|
-
fetch(addSearchParams(new URL("https://api.duckduckgo.com"), {
|
|
24
|
+
])
|
|
25
|
+
: update.inline_query
|
|
26
|
+
? fetch(addSearchParams(new URL("https://api.duckduckgo.com"), {
|
|
28
27
|
q: query,
|
|
29
28
|
format: "json",
|
|
30
29
|
t: "telegram_bot",
|
|
@@ -32,78 +31,82 @@ export default class TelegramBot extends TelegramApi {
|
|
|
32
31
|
}).href).then((response) => response
|
|
33
32
|
.json()
|
|
34
33
|
.then((results) => results)
|
|
35
|
-
.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,
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
34
|
+
.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 !== ""
|
|
35
|
+
? [
|
|
36
|
+
new TelegramInlineQueryResultArticle(`${instant_answer_url}\n\n<a href="${addSearchParams(new URL(duckduckgo_url), {
|
|
37
|
+
q: args
|
|
38
|
+
.slice(1)
|
|
39
|
+
.join(" ")
|
|
40
|
+
.replace(/^!\w* /, ""),
|
|
41
|
+
}).href}">Results From DuckDuckGo</a>`, instant_answer_url, "HTML", thumb_url),
|
|
42
|
+
new TelegramInlineQueryResultArticle(duckduckgo_url, duckduckgo_url, "", default_thumb_url),
|
|
43
|
+
]
|
|
44
|
+
: [
|
|
45
|
+
new TelegramInlineQueryResultArticle(duckduckgo_url, duckduckgo_url, "", default_thumb_url),
|
|
46
|
+
], 3600 // 1 hour
|
|
47
|
+
))(ddg_response.Redirect ?? ddg_response.AbstractURL, ddg_response.Redirect === ""
|
|
48
|
+
? `https://duckduckgo.com${ddg_response.Image !== "" && ddg_response.Image
|
|
49
|
+
? ddg_response.Image
|
|
50
|
+
: ddg_response.RelatedTopics.length !== 0 &&
|
|
51
|
+
ddg_response.RelatedTopics[0].Icon.URL !== ""
|
|
52
|
+
? ddg_response.RelatedTopics[0].Icon.URL
|
|
53
|
+
: "/i/f96d4798.png"}`
|
|
54
|
+
: "")))
|
|
55
|
+
: this.sendMessage(update.message?.chat.id ?? 0, duckduckgo_url))(query === ""
|
|
56
|
+
? "https://duckduckgo.com"
|
|
57
|
+
: addSearchParams(new URL("https://duckduckgo.com"), {
|
|
55
58
|
q: query,
|
|
56
59
|
}).href))(args.slice(1).join(" "));
|
|
57
60
|
// bot command: /kanye
|
|
58
61
|
kanye = async (update) => fetch("https://api.kanye.rest")
|
|
59
62
|
.then((response) => responseToJSON(response))
|
|
60
|
-
.then((json) => ((message) =>
|
|
61
|
-
this.answerInlineQuery(update.inline_query.id, [
|
|
63
|
+
.then((json) => ((message) => update.inline_query
|
|
64
|
+
? this.answerInlineQuery(update.inline_query.id, [
|
|
62
65
|
new TelegramInlineQueryResultArticle(message),
|
|
63
|
-
])
|
|
64
|
-
this.sendMessage(update.message?.chat.id ?? 0, message))(`Kanye says... ${json.quote}`))
|
|
66
|
+
])
|
|
67
|
+
: this.sendMessage(update.message?.chat.id ?? 0, message))(`Kanye says... ${json.quote}`))
|
|
65
68
|
.catch(() => new Response("Failed to parse JSON"));
|
|
66
69
|
// bot command: /joke
|
|
67
70
|
joke = async (update) => fetch("https://v2.jokeapi.dev/joke/Any?safe-mode")
|
|
68
71
|
.then((response) => responseToJSON(response))
|
|
69
72
|
.then((joke) => joke)
|
|
70
|
-
.then((joke_response) => ((message) =>
|
|
71
|
-
this.answerInlineQuery(update.inline_query.id, [
|
|
73
|
+
.then((joke_response) => ((message) => update.inline_query
|
|
74
|
+
? this.answerInlineQuery(update.inline_query.id, [
|
|
72
75
|
new TelegramInlineQueryResultArticle(message, joke_response.joke ?? joke_response.setup, "HTML"),
|
|
73
|
-
], 0)
|
|
74
|
-
this.sendMessage(update.message?.chat.id ?? 0, message, "HTML"))(joke_response.joke ??
|
|
76
|
+
], 0)
|
|
77
|
+
: this.sendMessage(update.message?.chat.id ?? 0, message, "HTML"))(joke_response.joke ??
|
|
75
78
|
`${joke_response.setup}\n\n<tg-spoiler>${joke_response.delivery}</tg-spoiler>`));
|
|
76
79
|
// bot command: /dog
|
|
77
80
|
dog = async (update) => fetch("https://shibe.online/api/shibes")
|
|
78
81
|
.then((response) => response.json())
|
|
79
82
|
.then((json) => json)
|
|
80
|
-
.then((shibe_response) =>
|
|
81
|
-
this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultPhoto(shibe_response[0])], 0)
|
|
82
|
-
this.sendPhoto(update.message?.chat.id ?? 0, shibe_response[0]));
|
|
83
|
+
.then((shibe_response) => update.inline_query
|
|
84
|
+
? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultPhoto(shibe_response[0])], 0)
|
|
85
|
+
: this.sendPhoto(update.message?.chat.id ?? 0, shibe_response[0]));
|
|
83
86
|
// bot command: /cat
|
|
84
87
|
cat = async (update) => fetch("https://meow.senither.com/v1/random")
|
|
85
88
|
.then((response) => response.json())
|
|
86
89
|
.then((json) => json)
|
|
87
|
-
.then((json) =>
|
|
88
|
-
this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultPhoto(json.data.url)], 0)
|
|
89
|
-
this.sendPhoto(update.message?.chat.id ?? 0, json.data.url));
|
|
90
|
+
.then((json) => update.inline_query
|
|
91
|
+
? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultPhoto(json.data.url)], 0)
|
|
92
|
+
: this.sendPhoto(update.message?.chat.id ?? 0, json.data.url));
|
|
90
93
|
// bot command: /bored
|
|
91
94
|
bored = async (update) => fetch("https://boredapi.com/api/activity/")
|
|
92
95
|
.then((response) => responseToJSON(response))
|
|
93
96
|
.then((json) => json)
|
|
94
|
-
.then((bored_response) =>
|
|
95
|
-
this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(bored_response.activity)], 0)
|
|
96
|
-
this.sendMessage(update.message?.chat.id ?? 0, bored_response.activity));
|
|
97
|
+
.then((bored_response) => update.inline_query
|
|
98
|
+
? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(bored_response.activity)], 0)
|
|
99
|
+
: this.sendMessage(update.message?.chat.id ?? 0, bored_response.activity));
|
|
97
100
|
// bot command: /epoch
|
|
98
|
-
epoch = async (update) => ((seconds) =>
|
|
99
|
-
this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(seconds)], 0)
|
|
100
|
-
this.sendMessage(update.message?.chat.id ?? 0, seconds))(Math.floor(Date.now() / 1000).toString());
|
|
101
|
+
epoch = async (update) => ((seconds) => update.inline_query
|
|
102
|
+
? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(seconds)], 0)
|
|
103
|
+
: this.sendMessage(update.message?.chat.id ?? 0, seconds))(Math.floor(Date.now() / 1000).toString());
|
|
101
104
|
// bot command: /get
|
|
102
105
|
_get = async (update, args) => ((key) => this.get_set
|
|
103
106
|
.get(key)
|
|
104
|
-
.then((value) =>
|
|
105
|
-
this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(value ?? "")], 0)
|
|
106
|
-
this.sendMessage(update.message?.chat.id ?? 0, value ?? "")))(args[1]);
|
|
107
|
+
.then((value) => update.inline_query
|
|
108
|
+
? this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(value ?? "")], 0)
|
|
109
|
+
: this.sendMessage(update.message?.chat.id ?? 0, value ?? "")))(args[1]);
|
|
107
110
|
// bot command: /set
|
|
108
111
|
_set = async (update, args) => {
|
|
109
112
|
const key = args[1];
|
|
@@ -136,11 +139,11 @@ export default class TelegramBot extends TelegramApi {
|
|
|
136
139
|
// )
|
|
137
140
|
// );
|
|
138
141
|
// bot command: /roll
|
|
139
|
-
roll = async (update, args) => ((outcome, message) =>
|
|
140
|
-
this.answerInlineQuery(update.inline_query.id, [
|
|
142
|
+
roll = async (update, args) => ((outcome, message) => update.inline_query
|
|
143
|
+
? this.answerInlineQuery(update.inline_query.id, [
|
|
141
144
|
new TelegramInlineQueryResultArticle(message(update.inline_query.from.username, update.inline_query.from.first_name, outcome)),
|
|
142
|
-
])
|
|
143
|
-
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}`);
|
|
145
|
+
])
|
|
146
|
+
: 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}`);
|
|
144
147
|
// bot command: /commandList
|
|
145
148
|
commandList = async (update) => this.sendMessage(update.message?.chat.id ?? 0, `<pre>${JSON.stringify(Object.keys(this.commands))}</pre>`, "HTML");
|
|
146
149
|
// bot command: /toss
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codebam/cf-workers-telegram-bot",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.1.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,15 +29,17 @@
|
|
|
29
29
|
"url": "https://github.com/codebam/cf-workers-telegram-bot.git"
|
|
30
30
|
},
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"worker": "^4.
|
|
32
|
+
"worker": "^4.1.0"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
35
|
"@cloudflare/workers-types": "^4.20230221.0",
|
|
36
36
|
"@typescript-eslint/eslint-plugin": "^5.53.0",
|
|
37
37
|
"@typescript-eslint/parser": "^5.53.0",
|
|
38
|
-
"eslint": "^8.
|
|
38
|
+
"eslint": "^8.35.0",
|
|
39
|
+
"eslint-config-prettier": "^8.6.0",
|
|
40
|
+
"lerna": "^6.5.1",
|
|
39
41
|
"prettier": "^2.8.4",
|
|
40
42
|
"typescript": "^4.9.5"
|
|
41
43
|
},
|
|
42
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "06ba99e3db30c7f49b936ad9b4b02b7a4c17bec2"
|
|
43
45
|
}
|