@codebam/cf-workers-telegram-bot 3.40.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/LICENSE +176 -0
- package/LICENSE_MIT +21 -0
- package/README.md +51 -0
- package/dist/main/src/bot_api.d.ts +10 -0
- package/dist/main/src/bot_api.js +11 -0
- package/dist/main/src/handler.d.ts +12 -0
- package/dist/main/src/handler.js +51 -0
- package/dist/main/src/libs.d.ts +10 -0
- package/dist/main/src/libs.js +30 -0
- package/dist/main/src/main.d.ts +11 -0
- package/dist/main/src/main.js +11 -0
- package/dist/main/src/telegram_api.d.ts +30 -0
- package/dist/main/src/telegram_api.js +153 -0
- package/dist/main/src/telegram_bot.d.ts +26 -0
- package/dist/main/src/telegram_bot.js +152 -0
- package/dist/main/src/telegram_commands.d.ts +19 -0
- package/dist/main/src/telegram_commands.js +16 -0
- package/dist/main/src/telegram_webhook.d.ts +10 -0
- package/dist/main/src/telegram_webhook.js +21 -0
- package/dist/main/src/types.d.ts +220 -0
- package/dist/main/src/types.js +100 -0
- package/dist/main/src/webhook.d.ts +10 -0
- package/dist/main/src/webhook.js +17 -0
- package/dist/worker/src/worker.d.ts +12 -0
- package/dist/worker/src/worker.js +64 -0
- package/package.json +42 -0
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
import { preTagString, prettyJSON, addSearchParams, responseToJSON, } from "./libs";
|
|
2
|
+
import TelegramApi from "./telegram_api";
|
|
3
|
+
import { TelegramInlineQueryResultArticle, TelegramInlineQueryResultPhoto, } from "./types";
|
|
4
|
+
export default class TelegramBot extends TelegramApi {
|
|
5
|
+
url;
|
|
6
|
+
kv;
|
|
7
|
+
get_set;
|
|
8
|
+
constructor(config) {
|
|
9
|
+
super(config.commands, config.webhook, config.handler);
|
|
10
|
+
this.url = config.url;
|
|
11
|
+
this.kv = config.kv;
|
|
12
|
+
this.get_set = config.kv?.get_set;
|
|
13
|
+
}
|
|
14
|
+
// bot command: /code
|
|
15
|
+
code = async (update) => ((url) => (update.inline_query &&
|
|
16
|
+
this.answerInlineQuery(update.inline_query.id, [
|
|
17
|
+
new TelegramInlineQueryResultArticle(url),
|
|
18
|
+
])) ??
|
|
19
|
+
this.sendMessage(update.message?.chat.id ?? 0, url))("https://github.com/codebam/cf-workers-telegram-bot");
|
|
20
|
+
// bot command: /duckduckgo
|
|
21
|
+
duckduckgo = async (update, args) => ((query) => ((duckduckgo_url) => (update.inline_query &&
|
|
22
|
+
query === "" &&
|
|
23
|
+
this.answerInlineQuery(update.inline_query.id, [
|
|
24
|
+
new TelegramInlineQueryResultArticle("https://duckduckgo.com"),
|
|
25
|
+
])) ||
|
|
26
|
+
(update.inline_query &&
|
|
27
|
+
fetch(addSearchParams(new URL("https://api.duckduckgo.com"), {
|
|
28
|
+
q: query,
|
|
29
|
+
format: "json",
|
|
30
|
+
t: "telegram_bot",
|
|
31
|
+
no_redirect: "1",
|
|
32
|
+
}).href).then((response) => response
|
|
33
|
+
.json()
|
|
34
|
+
.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, (instant_answer_url !== "" && [
|
|
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
|
+
new TelegramInlineQueryResultArticle(duckduckgo_url, duckduckgo_url, "", default_thumb_url),
|
|
45
|
+
], 3600 // 1 hour
|
|
46
|
+
))(ddg_response.Redirect || ddg_response.AbstractURL, (ddg_response.Redirect === "" &&
|
|
47
|
+
`https://duckduckgo.com${(ddg_response.Image !== "" && ddg_response.Image) ||
|
|
48
|
+
(ddg_response.RelatedTopics.length !== 0 &&
|
|
49
|
+
ddg_response.RelatedTopics[0].Icon.URL !== "" &&
|
|
50
|
+
ddg_response.RelatedTopics[0].Icon.URL) ||
|
|
51
|
+
"/i/f96d4798.png"}`) ||
|
|
52
|
+
"")))) ||
|
|
53
|
+
this.sendMessage(update.message?.chat.id ?? 0, duckduckgo_url))((query === "" && "https://duckduckgo.com") ||
|
|
54
|
+
addSearchParams(new URL("https://duckduckgo.com"), {
|
|
55
|
+
q: query,
|
|
56
|
+
}).href))(args.slice(1).join(" "));
|
|
57
|
+
// bot command: /kanye
|
|
58
|
+
kanye = async (update) => fetch("https://api.kanye.rest")
|
|
59
|
+
.then((response) => responseToJSON(response))
|
|
60
|
+
.then((json) => ((message) => (update.inline_query !== undefined &&
|
|
61
|
+
this.answerInlineQuery(update.inline_query.id, [
|
|
62
|
+
new TelegramInlineQueryResultArticle(message),
|
|
63
|
+
])) ||
|
|
64
|
+
this.sendMessage(update.message?.chat.id ?? 0, message))(`Kanye says... ${json.quote}`))
|
|
65
|
+
.catch(() => new Response("Failed to parse JSON"));
|
|
66
|
+
// bot command: /joke
|
|
67
|
+
joke = async (update) => fetch("https://v2.jokeapi.dev/joke/Any?safe-mode")
|
|
68
|
+
.then((response) => responseToJSON(response))
|
|
69
|
+
.then((joke) => joke)
|
|
70
|
+
.then((joke_response) => ((message) => (update.inline_query &&
|
|
71
|
+
this.answerInlineQuery(update.inline_query.id, [
|
|
72
|
+
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 ??
|
|
75
|
+
`${joke_response.setup}\n\n<tg-spoiler>${joke_response.delivery}</tg-spoiler>`));
|
|
76
|
+
// bot command: /dog
|
|
77
|
+
dog = async (update) => fetch("https://shibe.online/api/shibes")
|
|
78
|
+
.then((response) => response.json())
|
|
79
|
+
.then((json) => json)
|
|
80
|
+
.then((shibe_response) => (update.inline_query &&
|
|
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
|
+
// bot command: /cat
|
|
84
|
+
cat = async (update) => fetch("https://meow.senither.com/v1/random")
|
|
85
|
+
.then((response) => response.json())
|
|
86
|
+
.then((json) => json)
|
|
87
|
+
.then((json) => (update.inline_query &&
|
|
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
|
+
// bot command: /bored
|
|
91
|
+
bored = async (update) => fetch("https://boredapi.com/api/activity/")
|
|
92
|
+
.then((response) => responseToJSON(response))
|
|
93
|
+
.then((json) => json)
|
|
94
|
+
.then((bored_response) => (update.inline_query &&
|
|
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
|
+
// bot command: /epoch
|
|
98
|
+
epoch = async (update) => ((seconds) => (update.inline_query &&
|
|
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
|
+
// bot command: /get
|
|
102
|
+
_get = async (update, args) => ((key) => this.get_set
|
|
103
|
+
.get(key)
|
|
104
|
+
.then((value) => (update.inline_query &&
|
|
105
|
+
this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(value ?? "")], 0)) ??
|
|
106
|
+
this.sendMessage(update.message?.chat.id ?? 0, value ?? "")))(args[1]);
|
|
107
|
+
// bot command: /set
|
|
108
|
+
_set = async (update, args) => {
|
|
109
|
+
const key = args[1];
|
|
110
|
+
const value = args.slice(2).join(" ");
|
|
111
|
+
const message = `set ${key} to ${value}`;
|
|
112
|
+
this.get_set.put(key, value).then(() => {
|
|
113
|
+
if (update.inline_query) {
|
|
114
|
+
return this.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(message)], 0);
|
|
115
|
+
}
|
|
116
|
+
return this.sendMessage(update.message?.chat.id ?? 0, message);
|
|
117
|
+
});
|
|
118
|
+
return new Response();
|
|
119
|
+
};
|
|
120
|
+
_average = (numbers) => parseFloat((numbers.reduce((prev, cur) => prev + cur, 0) / numbers.length || 0).toFixed(2));
|
|
121
|
+
// bot command: /recursion
|
|
122
|
+
recursion = async (update) => this.sendMessage(update.message?.chat.id ?? 0, "/recursion");
|
|
123
|
+
// .then((response) => responseToJSON(response))
|
|
124
|
+
// .then((result: { ok: boolean; result: { text: string } }) =>
|
|
125
|
+
// this.handler.postResponse(
|
|
126
|
+
// new Request("", {
|
|
127
|
+
// method: "POST",
|
|
128
|
+
// body: JSON.stringify({
|
|
129
|
+
// message: {
|
|
130
|
+
// text: result.result.text,
|
|
131
|
+
// chat: { id: update.message.chat.id },
|
|
132
|
+
// },
|
|
133
|
+
// }),
|
|
134
|
+
// }),
|
|
135
|
+
// this
|
|
136
|
+
// )
|
|
137
|
+
// );
|
|
138
|
+
// bot command: /roll
|
|
139
|
+
roll = async (update, args) => ((outcome, message) => (update.inline_query &&
|
|
140
|
+
this.answerInlineQuery(update.inline_query.id, [
|
|
141
|
+
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}`);
|
|
144
|
+
// bot command: /commandList
|
|
145
|
+
commandList = async (update) => this.sendMessage(update.message?.chat.id ?? 0, `<pre>${JSON.stringify(Object.keys(this.commands))}</pre>`, "HTML");
|
|
146
|
+
// bot command: /toss
|
|
147
|
+
toss = async (update) => this.sendMessage(update.message?.chat.id ?? 0, Math.floor(Math.random() * 2) == 0 ? "heads" : "tails");
|
|
148
|
+
// bot command: /ping
|
|
149
|
+
ping = async (update, args) => this.sendMessage(update.message?.chat.id ?? 0, args.length === 1 ? "pong" : args.slice(1).join(" "));
|
|
150
|
+
// bot command: /chatInfo
|
|
151
|
+
getChatInfo = async (update) => this.sendMessage(update.message?.chat.id ?? 0, preTagString(prettyJSON(update.message?.chat ?? 0)), "HTML");
|
|
152
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
import TelegramBot from "./telegram_bot";
|
|
3
|
+
import { TelegramUpdate } from "./types";
|
|
4
|
+
export default class TelegramCommands {
|
|
5
|
+
static ping: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
6
|
+
static toss: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
7
|
+
static epoch: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
8
|
+
static kanye: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
9
|
+
static bored: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
10
|
+
static joke: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
11
|
+
static dog: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
12
|
+
static cat: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
13
|
+
static roll: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
14
|
+
static _get: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
15
|
+
static _set: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
16
|
+
static duckduckgo: (bot: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
17
|
+
static code: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
18
|
+
static commandList: (bot: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export default class TelegramCommands {
|
|
2
|
+
static ping = async (bot, update, args) => bot.ping(update, args);
|
|
3
|
+
static toss = async (bot, update) => bot.toss(update);
|
|
4
|
+
static epoch = async (bot, update) => bot.epoch(update);
|
|
5
|
+
static kanye = async (bot, update) => bot.kanye(update);
|
|
6
|
+
static bored = async (bot, update) => bot.bored(update);
|
|
7
|
+
static joke = async (bot, update) => bot.joke(update);
|
|
8
|
+
static dog = async (bot, update) => bot.dog(update);
|
|
9
|
+
static cat = async (bot, update) => bot.cat(update);
|
|
10
|
+
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
|
+
static duckduckgo = async (bot, update, args) => bot.duckduckgo(update, args);
|
|
14
|
+
static code = async (bot, update) => bot.code(update);
|
|
15
|
+
static commandList = async (bot, update) => bot.commandList(update);
|
|
16
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
import Webhook from "./webhook";
|
|
3
|
+
import { WebhookCommands } from "./types";
|
|
4
|
+
export default class TelegramWebhook extends Webhook {
|
|
5
|
+
constructor(api: URL, token: string, url: URL);
|
|
6
|
+
set: (drop_pending_updates?: boolean) => Promise<Response>;
|
|
7
|
+
get: () => Promise<Response>;
|
|
8
|
+
delete: () => Promise<Response>;
|
|
9
|
+
commands: WebhookCommands;
|
|
10
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import Webhook from "./webhook";
|
|
2
|
+
import { sha256, addSearchParams, fetch_json } from "./libs";
|
|
3
|
+
export default class TelegramWebhook extends Webhook {
|
|
4
|
+
constructor(api, token, url) {
|
|
5
|
+
super(api, token, url);
|
|
6
|
+
}
|
|
7
|
+
set = async (drop_pending_updates = true) => sha256(this.token).then((access_key) => fetch_json(addSearchParams(new URL(`${this.api.href}/setWebhook`), {
|
|
8
|
+
url: new URL(`${this.url.href}/${access_key}`).href,
|
|
9
|
+
max_connections: "100",
|
|
10
|
+
allowed_updates: JSON.stringify(["message", "inline_query"]),
|
|
11
|
+
drop_pending_updates: drop_pending_updates.toString(),
|
|
12
|
+
})));
|
|
13
|
+
get = async () => fetch_json(new URL(`${this.api.href}/getWebhookInfo`));
|
|
14
|
+
delete = async () => fetch_json(new URL(`${this.api.href}/deleteWebhook`));
|
|
15
|
+
commands = {
|
|
16
|
+
...this.commands,
|
|
17
|
+
set: this.set,
|
|
18
|
+
get: this.get,
|
|
19
|
+
delete: this.delete,
|
|
20
|
+
};
|
|
21
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
import BotApi from "./bot_api";
|
|
3
|
+
import Handler from "./handler";
|
|
4
|
+
import TelegramApi from "./telegram_api";
|
|
5
|
+
import Webhook from "./webhook";
|
|
6
|
+
export { Webhook };
|
|
7
|
+
export type Command = (bot: BotApi, update: Update, args?: string[]) => Promise<Response>;
|
|
8
|
+
export type TelegramCommand = (bot: TelegramApi, update: TelegramUpdate, args?: string[]) => Promise<Response>;
|
|
9
|
+
export type Commands = Record<string, Command>;
|
|
10
|
+
export type Kv = Record<string, KVNamespace> | undefined;
|
|
11
|
+
export declare class Config {
|
|
12
|
+
bot_name: string;
|
|
13
|
+
api: object;
|
|
14
|
+
webhook: Webhook;
|
|
15
|
+
commands: Record<string, Command>;
|
|
16
|
+
kv: Kv | undefined;
|
|
17
|
+
url: URL;
|
|
18
|
+
handler: Handler | undefined;
|
|
19
|
+
constructor(config?: PartialConfig);
|
|
20
|
+
}
|
|
21
|
+
export type PartialConfig = {
|
|
22
|
+
bot_name?: string;
|
|
23
|
+
api?: object;
|
|
24
|
+
webhook?: Webhook;
|
|
25
|
+
commands?: Record<string, Command>;
|
|
26
|
+
kv?: Kv | undefined;
|
|
27
|
+
url?: URL;
|
|
28
|
+
handler?: Handler;
|
|
29
|
+
};
|
|
30
|
+
export declare const localhost: URL;
|
|
31
|
+
export declare class WebhookCommands {
|
|
32
|
+
[key: string]: () => Promise<Response>;
|
|
33
|
+
}
|
|
34
|
+
export type Joke = {
|
|
35
|
+
error: boolean;
|
|
36
|
+
category: string;
|
|
37
|
+
type: string;
|
|
38
|
+
setup?: string;
|
|
39
|
+
delivery?: string;
|
|
40
|
+
joke?: string;
|
|
41
|
+
flags: {
|
|
42
|
+
nsfw: boolean;
|
|
43
|
+
religious: boolean;
|
|
44
|
+
political: boolean;
|
|
45
|
+
racist: boolean;
|
|
46
|
+
sexist: boolean;
|
|
47
|
+
explicit: boolean;
|
|
48
|
+
};
|
|
49
|
+
id: number;
|
|
50
|
+
safe: boolean;
|
|
51
|
+
lang: string;
|
|
52
|
+
};
|
|
53
|
+
export type Bored = {
|
|
54
|
+
activity: string;
|
|
55
|
+
type: string;
|
|
56
|
+
participants: number;
|
|
57
|
+
price: number;
|
|
58
|
+
link: string;
|
|
59
|
+
key: string;
|
|
60
|
+
accessibility: 0;
|
|
61
|
+
};
|
|
62
|
+
export type Balance = Record<string, {
|
|
63
|
+
final_balance: number;
|
|
64
|
+
n_tx: number;
|
|
65
|
+
total_received: number;
|
|
66
|
+
}>;
|
|
67
|
+
export type TelegramFrom = {
|
|
68
|
+
first_name: string;
|
|
69
|
+
id: number;
|
|
70
|
+
is_bot: boolean;
|
|
71
|
+
language_code: string;
|
|
72
|
+
username: string;
|
|
73
|
+
};
|
|
74
|
+
export type TelegramChat = {
|
|
75
|
+
id: number;
|
|
76
|
+
type: string;
|
|
77
|
+
title?: string;
|
|
78
|
+
username?: string;
|
|
79
|
+
first_name?: string;
|
|
80
|
+
last_name?: string;
|
|
81
|
+
bio?: string;
|
|
82
|
+
has_private_forwards: boolean;
|
|
83
|
+
description?: string;
|
|
84
|
+
invite_link?: string;
|
|
85
|
+
pinned_message?: TelegramMessage;
|
|
86
|
+
slow_mode_delay?: number;
|
|
87
|
+
message_auto_delete_time?: number;
|
|
88
|
+
has_protected_content?: boolean;
|
|
89
|
+
sticker_set_name?: string;
|
|
90
|
+
can_set_sticker_set?: boolean;
|
|
91
|
+
linked_chat_id?: number;
|
|
92
|
+
};
|
|
93
|
+
export type TelegramUser = {
|
|
94
|
+
id: number;
|
|
95
|
+
is_bot: boolean;
|
|
96
|
+
first_name: string;
|
|
97
|
+
last_name?: string;
|
|
98
|
+
username?: string;
|
|
99
|
+
language_code?: string;
|
|
100
|
+
can_join_groups?: boolean;
|
|
101
|
+
can_read_all_group_messages?: boolean;
|
|
102
|
+
supports_inline_queries: boolean;
|
|
103
|
+
};
|
|
104
|
+
export type TelegramMessageEntity = {
|
|
105
|
+
type: string;
|
|
106
|
+
offset: number;
|
|
107
|
+
length: number;
|
|
108
|
+
url?: string;
|
|
109
|
+
user?: TelegramUser;
|
|
110
|
+
language?: string;
|
|
111
|
+
};
|
|
112
|
+
export type TelegramPhotoSize = {
|
|
113
|
+
file_id: string;
|
|
114
|
+
file_unique_id: string;
|
|
115
|
+
width: number;
|
|
116
|
+
height: number;
|
|
117
|
+
file_size?: number;
|
|
118
|
+
};
|
|
119
|
+
export type TelegramMessage = {
|
|
120
|
+
message_id: number;
|
|
121
|
+
from: TelegramFrom;
|
|
122
|
+
sender_chat?: TelegramChat;
|
|
123
|
+
date: number;
|
|
124
|
+
chat: TelegramChat;
|
|
125
|
+
forward_from?: TelegramUser;
|
|
126
|
+
forward_from_chat?: TelegramChat;
|
|
127
|
+
forward_from_message_id?: number;
|
|
128
|
+
forward_signature?: string;
|
|
129
|
+
forward_sender_name?: string;
|
|
130
|
+
forward_date?: number;
|
|
131
|
+
is_automatic_forward?: boolean;
|
|
132
|
+
reply_to_message?: TelegramMessage;
|
|
133
|
+
via_bot?: TelegramUser;
|
|
134
|
+
edit_date?: number;
|
|
135
|
+
has_protected_content?: boolean;
|
|
136
|
+
media_group_id?: string;
|
|
137
|
+
author_signature?: string;
|
|
138
|
+
text?: string;
|
|
139
|
+
entities?: TelegramMessageEntity[];
|
|
140
|
+
photo?: TelegramPhotoSize[];
|
|
141
|
+
caption?: string;
|
|
142
|
+
caption_entities?: TelegramMessageEntity[];
|
|
143
|
+
new_chat_members?: TelegramUser[];
|
|
144
|
+
left_chat_member?: TelegramUser;
|
|
145
|
+
new_chat_title?: string;
|
|
146
|
+
delete_chat_photo?: boolean;
|
|
147
|
+
group_chat_created?: boolean;
|
|
148
|
+
supergroup_chat_created?: boolean;
|
|
149
|
+
channel_chat_created?: boolean;
|
|
150
|
+
migrate_to_chat_id?: number;
|
|
151
|
+
migrate_from_chat_id?: number;
|
|
152
|
+
pinned_message?: TelegramMessage;
|
|
153
|
+
connected_website?: string;
|
|
154
|
+
};
|
|
155
|
+
export type TelegramInputMessageContent = {
|
|
156
|
+
message_text: string;
|
|
157
|
+
parse_mode: string;
|
|
158
|
+
};
|
|
159
|
+
export type TelegramInlineQuery = {
|
|
160
|
+
chat_type: "sender" | "private" | "group" | "supergroup" | "channel";
|
|
161
|
+
from: TelegramFrom;
|
|
162
|
+
id: number;
|
|
163
|
+
offset: string;
|
|
164
|
+
query: string;
|
|
165
|
+
};
|
|
166
|
+
export declare class Update {
|
|
167
|
+
[key: string]: unknown;
|
|
168
|
+
}
|
|
169
|
+
export declare class TelegramUpdate extends Update {
|
|
170
|
+
update_id: number;
|
|
171
|
+
message?: TelegramMessage;
|
|
172
|
+
edited_message?: TelegramMessage;
|
|
173
|
+
channel_post?: TelegramMessage;
|
|
174
|
+
edited_channel_post?: TelegramMessage;
|
|
175
|
+
inline_query?: TelegramInlineQuery;
|
|
176
|
+
constructor(update: PartialTelegramUpdate);
|
|
177
|
+
}
|
|
178
|
+
export type PartialTelegramUpdate = {
|
|
179
|
+
update_id?: number;
|
|
180
|
+
message?: TelegramMessage;
|
|
181
|
+
edited_message?: TelegramMessage;
|
|
182
|
+
channel_post?: TelegramMessage;
|
|
183
|
+
edited_channel_post?: TelegramMessage;
|
|
184
|
+
inline_query?: TelegramInlineQuery;
|
|
185
|
+
};
|
|
186
|
+
export type TelegramInlineQueryType = "article" | "photo" | "gif" | "mpeg4_gif" | "video" | "audio" | "voice" | "document" | "location" | "venue" | "contact" | "game" | "sticker";
|
|
187
|
+
export declare class TelegramInlineQueryResult {
|
|
188
|
+
type: TelegramInlineQueryType;
|
|
189
|
+
id: string;
|
|
190
|
+
constructor(type: TelegramInlineQueryType);
|
|
191
|
+
}
|
|
192
|
+
export declare class TelegramInlineQueryResultPhoto extends TelegramInlineQueryResult {
|
|
193
|
+
photo_url: string;
|
|
194
|
+
thumb_url: string;
|
|
195
|
+
photo_width?: number;
|
|
196
|
+
photo_height?: number;
|
|
197
|
+
title?: string;
|
|
198
|
+
description?: string;
|
|
199
|
+
caption?: string;
|
|
200
|
+
parse_mode?: string;
|
|
201
|
+
caption_entities?: string;
|
|
202
|
+
constructor(photo: string);
|
|
203
|
+
}
|
|
204
|
+
export declare class TelegramInlineQueryResultArticle extends TelegramInlineQueryResult {
|
|
205
|
+
title: string;
|
|
206
|
+
input_message_content: TelegramInputMessageContent;
|
|
207
|
+
thumb_url: string;
|
|
208
|
+
constructor(content: string, title?: string, parse_mode?: string, thumb_url?: string);
|
|
209
|
+
}
|
|
210
|
+
export type DDGQueryResponse = {
|
|
211
|
+
AbstractSource: string;
|
|
212
|
+
AbstractURL: string;
|
|
213
|
+
Redirect: string;
|
|
214
|
+
Image: string;
|
|
215
|
+
RelatedTopics: {
|
|
216
|
+
Icon: {
|
|
217
|
+
URL: string;
|
|
218
|
+
};
|
|
219
|
+
}[];
|
|
220
|
+
};
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import Webhook from "./webhook";
|
|
2
|
+
export { Webhook };
|
|
3
|
+
export class Config {
|
|
4
|
+
bot_name;
|
|
5
|
+
api;
|
|
6
|
+
webhook;
|
|
7
|
+
commands;
|
|
8
|
+
kv;
|
|
9
|
+
url;
|
|
10
|
+
handler;
|
|
11
|
+
constructor(config = {}) {
|
|
12
|
+
this.bot_name = config.bot_name || "";
|
|
13
|
+
this.api = config.api || {};
|
|
14
|
+
this.webhook = config.webhook || new Webhook(localhost, "", localhost);
|
|
15
|
+
this.commands = config.commands || {};
|
|
16
|
+
this.kv = config.kv;
|
|
17
|
+
this.url = config.url || new URL(localhost);
|
|
18
|
+
this.handler = config.handler;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
export const localhost = new URL("http://localhost");
|
|
22
|
+
export class WebhookCommands {
|
|
23
|
+
}
|
|
24
|
+
export class Update {
|
|
25
|
+
}
|
|
26
|
+
export class TelegramUpdate extends Update {
|
|
27
|
+
update_id;
|
|
28
|
+
message;
|
|
29
|
+
edited_message;
|
|
30
|
+
channel_post;
|
|
31
|
+
edited_channel_post;
|
|
32
|
+
inline_query;
|
|
33
|
+
// chosen_inline_result?: TelegramChosenInlineResult;
|
|
34
|
+
// callback_query?: TelegramCallbackQuery;
|
|
35
|
+
// shipping_query?: TelegramShippingQuery;
|
|
36
|
+
// pre_checkout_query?: TelegramPreCheckoutQuery;
|
|
37
|
+
// poll?: TelegramPoll;
|
|
38
|
+
// poll_answer?: TelegramPollAnswer;
|
|
39
|
+
// my_chat_member?: TelegramChatMemberUpdated;
|
|
40
|
+
// chat_member?: TelegramChatMemberUpdated;
|
|
41
|
+
// chat_join_request: TelegramChatJoinRequest;
|
|
42
|
+
constructor(update) {
|
|
43
|
+
super();
|
|
44
|
+
this.update_id = update.update_id || 0;
|
|
45
|
+
this.message = update.message;
|
|
46
|
+
this.edited_message = update.edited_message;
|
|
47
|
+
this.channel_post = update.channel_post;
|
|
48
|
+
this.edited_channel_post = update.edited_channel_post;
|
|
49
|
+
this.inline_query = update.inline_query;
|
|
50
|
+
// chosen_inline_result = update.chosen_inline_result;
|
|
51
|
+
// callback_query = update.callback_query;
|
|
52
|
+
// shipping_query = update.shipping_query;
|
|
53
|
+
// pre_checkout_query = update.pre_checkout_query;
|
|
54
|
+
// poll = update.poll;
|
|
55
|
+
// poll_answer = update.poll_answer;
|
|
56
|
+
// my_chat_member = update.my_chat_member;
|
|
57
|
+
// chat_member = update.chat_member;
|
|
58
|
+
// chat_join_request = update.chat_join_request;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export class TelegramInlineQueryResult {
|
|
62
|
+
type;
|
|
63
|
+
id;
|
|
64
|
+
constructor(type) {
|
|
65
|
+
this.type = type;
|
|
66
|
+
this.id = crypto.randomUUID();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
export class TelegramInlineQueryResultPhoto extends TelegramInlineQueryResult {
|
|
70
|
+
photo_url; // must be a jpg
|
|
71
|
+
thumb_url;
|
|
72
|
+
photo_width;
|
|
73
|
+
photo_height;
|
|
74
|
+
title;
|
|
75
|
+
description;
|
|
76
|
+
caption;
|
|
77
|
+
parse_mode;
|
|
78
|
+
caption_entities;
|
|
79
|
+
// reply_markup?: TelegramInlineKeyboardMarkup;
|
|
80
|
+
// input_message_content?: TelegramInputMessageContent;
|
|
81
|
+
constructor(photo) {
|
|
82
|
+
super("photo");
|
|
83
|
+
this.photo_url = photo;
|
|
84
|
+
this.thumb_url = photo;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
export class TelegramInlineQueryResultArticle extends TelegramInlineQueryResult {
|
|
88
|
+
title;
|
|
89
|
+
input_message_content;
|
|
90
|
+
thumb_url;
|
|
91
|
+
constructor(content, title = content, parse_mode = "", thumb_url = "") {
|
|
92
|
+
super("article");
|
|
93
|
+
this.title = title;
|
|
94
|
+
this.input_message_content = {
|
|
95
|
+
message_text: content.toString(),
|
|
96
|
+
parse_mode,
|
|
97
|
+
};
|
|
98
|
+
this.thumb_url = thumb_url;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
import { WebhookCommands } from "./types";
|
|
3
|
+
export default class Webhook {
|
|
4
|
+
api: URL;
|
|
5
|
+
token: string;
|
|
6
|
+
url: URL;
|
|
7
|
+
commands: WebhookCommands;
|
|
8
|
+
constructor(api: URL, token: string, url: URL);
|
|
9
|
+
process: (url: URL) => Promise<Response>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { JSONResponse } from "./libs";
|
|
2
|
+
export default class Webhook {
|
|
3
|
+
api;
|
|
4
|
+
token;
|
|
5
|
+
url;
|
|
6
|
+
commands;
|
|
7
|
+
constructor(api, token, url) {
|
|
8
|
+
this.api = api;
|
|
9
|
+
this.token = token;
|
|
10
|
+
this.url = url;
|
|
11
|
+
this.commands = {
|
|
12
|
+
default: () => new Promise(() => JSONResponse({ error: "Invalid command" }, 400)),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
process = async (url) => this.commands[url.searchParams.get("command") ?? ""]?.() ??
|
|
16
|
+
this.commands.default;
|
|
17
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
interface Environment {
|
|
3
|
+
SECRET_TELEGRAM_API_TOKEN: string;
|
|
4
|
+
KV_GET_SET: KVNamespace;
|
|
5
|
+
KV_UID_DATA: KVNamespace;
|
|
6
|
+
SECRET_TELEGRAM_API_TOKEN2: string;
|
|
7
|
+
SECRET_TELEGRAM_API_TOKEN3: string;
|
|
8
|
+
}
|
|
9
|
+
declare const _default: {
|
|
10
|
+
fetch: (request: Request, env: Environment) => Promise<Response>;
|
|
11
|
+
};
|
|
12
|
+
export default _default;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
////////////////////////////////////////////////////////////////////
|
|
2
|
+
//// Telegram Bot using Cloudflare Worker ////
|
|
3
|
+
////////////////////////////////////////////////////////////////////
|
|
4
|
+
//// Author: Nikhil John ////
|
|
5
|
+
//// Repo: https://github.com/nikhiljohn10/telegram-bot-worker ////
|
|
6
|
+
//// License: MIT ////
|
|
7
|
+
//// ////
|
|
8
|
+
//// Author: Sean Behan ////
|
|
9
|
+
//// Repo: https://github.com/codebam/cf-workers-telegram-bot ////
|
|
10
|
+
//// License: Apache-2.0 ////
|
|
11
|
+
////////////////////////////////////////////////////////////////////
|
|
12
|
+
import { TelegramCommands, Handler, TelegramWebhook, TelegramBot, } from "../../main/src/main";
|
|
13
|
+
export default {
|
|
14
|
+
fetch: async (request, env) => new Handler([
|
|
15
|
+
{
|
|
16
|
+
bot_name: "cf-workers-telegram-bot",
|
|
17
|
+
api: TelegramBot,
|
|
18
|
+
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)),
|
|
19
|
+
commands: {
|
|
20
|
+
"/ping": TelegramCommands.ping,
|
|
21
|
+
"/toss": TelegramCommands.toss,
|
|
22
|
+
"/epoch": TelegramCommands.epoch,
|
|
23
|
+
"/kanye": TelegramCommands.kanye,
|
|
24
|
+
"/bored": TelegramCommands.bored,
|
|
25
|
+
"/joke": TelegramCommands.joke,
|
|
26
|
+
"/dog": TelegramCommands.dog,
|
|
27
|
+
"/cat": TelegramCommands.cat,
|
|
28
|
+
"/roll": TelegramCommands.roll,
|
|
29
|
+
"/get": TelegramCommands._get,
|
|
30
|
+
"/set": TelegramCommands._set,
|
|
31
|
+
"/duckduckgo": TelegramCommands.duckduckgo,
|
|
32
|
+
"/code": TelegramCommands.code,
|
|
33
|
+
"/commands": TelegramCommands.commandList,
|
|
34
|
+
"/help": TelegramCommands.commandList,
|
|
35
|
+
"/start": TelegramCommands.commandList,
|
|
36
|
+
},
|
|
37
|
+
kv: { get_set: env.KV_GET_SET, uid_data: env.KV_UID_DATA },
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
bot_name: "@duckduckbot",
|
|
41
|
+
api: TelegramBot,
|
|
42
|
+
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)),
|
|
43
|
+
commands: {
|
|
44
|
+
inline: TelegramCommands.duckduckgo,
|
|
45
|
+
"/duckduckgo": TelegramCommands.duckduckgo,
|
|
46
|
+
"/code": TelegramCommands.code,
|
|
47
|
+
"/commands": TelegramCommands.commandList,
|
|
48
|
+
"/start": TelegramCommands.commandList,
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
bot_name: "@ddggbot",
|
|
53
|
+
api: TelegramBot,
|
|
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)),
|
|
55
|
+
commands: {
|
|
56
|
+
inline: TelegramCommands.duckduckgo,
|
|
57
|
+
"/duckduckgo": TelegramCommands.duckduckgo,
|
|
58
|
+
"/code": TelegramCommands.code,
|
|
59
|
+
"/commands": TelegramCommands.commandList,
|
|
60
|
+
"/start": TelegramCommands.commandList,
|
|
61
|
+
},
|
|
62
|
+
},
|
|
63
|
+
]).handle(request),
|
|
64
|
+
};
|