@codebam/cf-workers-telegram-bot 6.6.0 → 7.4.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.
Files changed (69) hide show
  1. package/README.md +37 -43
  2. package/dist/main/src/main.d.ts +1 -8
  3. package/dist/main/src/main.js +1 -8
  4. package/dist/main/src/telegram_bot.d.ts +18 -32
  5. package/dist/main/src/telegram_bot.js +114 -80
  6. package/dist/main/src/types.d.ts +1 -5
  7. package/dist/main/src/types.js +1 -3
  8. package/dist/worker/src/worker.js +78 -92
  9. package/package.json +3 -3
  10. package/dist/main/src/bot_api.d.ts +0 -10
  11. package/dist/main/src/bot_api.js +0 -11
  12. package/dist/main/src/commands/ban.d.ts +0 -5
  13. package/dist/main/src/commands/ban.js +0 -8
  14. package/dist/main/src/commands/bored.d.ts +0 -5
  15. package/dist/main/src/commands/bored.js +0 -7
  16. package/dist/main/src/commands/clear.d.ts +0 -5
  17. package/dist/main/src/commands/clear.js +0 -14
  18. package/dist/main/src/commands/code.d.ts +0 -5
  19. package/dist/main/src/commands/code.js +0 -4
  20. package/dist/main/src/commands/commandlist.d.ts +0 -5
  21. package/dist/main/src/commands/commandlist.js +0 -1
  22. package/dist/main/src/commands/dog.d.ts +0 -5
  23. package/dist/main/src/commands/dog.js +0 -7
  24. package/dist/main/src/commands/duckduckgo.d.ts +0 -5
  25. package/dist/main/src/commands/duckduckgo.js +0 -42
  26. package/dist/main/src/commands/epoch.d.ts +0 -5
  27. package/dist/main/src/commands/epoch.js +0 -4
  28. package/dist/main/src/commands/getchatinfo.d.ts +0 -5
  29. package/dist/main/src/commands/getchatinfo.js +0 -1
  30. package/dist/main/src/commands/image.d.ts +0 -5
  31. package/dist/main/src/commands/image.js +0 -19
  32. package/dist/main/src/commands/joke.d.ts +0 -5
  33. package/dist/main/src/commands/joke.js +0 -7
  34. package/dist/main/src/commands/kanye.d.ts +0 -5
  35. package/dist/main/src/commands/kanye.js +0 -8
  36. package/dist/main/src/commands/mute.d.ts +0 -5
  37. package/dist/main/src/commands/mute.js +0 -8
  38. package/dist/main/src/commands/ping.d.ts +0 -5
  39. package/dist/main/src/commands/ping.js +0 -1
  40. package/dist/main/src/commands/question.d.ts +0 -5
  41. package/dist/main/src/commands/question.js +0 -74
  42. package/dist/main/src/commands/roll.d.ts +0 -5
  43. package/dist/main/src/commands/roll.js +0 -6
  44. package/dist/main/src/commands/sean.d.ts +0 -5
  45. package/dist/main/src/commands/sean.js +0 -76
  46. package/dist/main/src/commands/start.d.ts +0 -5
  47. package/dist/main/src/commands/start.js +0 -1
  48. package/dist/main/src/commands/toss.d.ts +0 -5
  49. package/dist/main/src/commands/toss.js +0 -1
  50. package/dist/main/src/commands/translate.d.ts +0 -5
  51. package/dist/main/src/commands/translate.js +0 -26
  52. package/dist/main/src/handler.d.ts +0 -12
  53. package/dist/main/src/handler.js +0 -40
  54. package/dist/main/src/libs.d.ts +0 -5
  55. package/dist/main/src/libs.js +0 -7
  56. package/dist/main/src/telegram_api.d.ts +0 -34
  57. package/dist/main/src/telegram_api.js +0 -192
  58. package/dist/main/src/telegram_commands.d.ts +0 -22
  59. package/dist/main/src/telegram_commands.js +0 -21
  60. package/dist/main/src/telegram_webhook.d.ts +0 -10
  61. package/dist/main/src/telegram_webhook.js +0 -20
  62. package/dist/main/src/types/Command.d.ts +0 -5
  63. package/dist/main/src/types/Command.js +0 -1
  64. package/dist/main/src/types/Commands.d.ts +0 -3
  65. package/dist/main/src/types/Commands.js +0 -1
  66. package/dist/main/src/types/Config.d.ts +0 -20
  67. package/dist/main/src/types/Config.js +0 -33
  68. package/dist/main/src/webhook.d.ts +0 -10
  69. package/dist/main/src/webhook.js +0 -15
@@ -1,192 +0,0 @@
1
- import BotApi from './bot_api';
2
- import { addSearchParams, log } from './libs';
3
- export default class TelegramApi extends BotApi {
4
- constructor(commands, webhook, handler) {
5
- super({ commands, webhook, handler });
6
- }
7
- inlineQueryUpdate = async (update) => this.executeInlineCommand(update);
8
- messageUpdate = async (update) => {
9
- if (update.message) {
10
- await this.greetUsers(update);
11
- await this.executeCommand(update);
12
- }
13
- return this.updates.default;
14
- };
15
- updates = {
16
- inline_query: this.inlineQueryUpdate,
17
- message: this.messageUpdate,
18
- default: new Response(),
19
- };
20
- update = async (update) => {
21
- console.log({ update });
22
- if (update) {
23
- if (update.inline_query) {
24
- if (update.inline_query.query !== '') {
25
- return this.updates.inline_query(update);
26
- }
27
- }
28
- else {
29
- if (update.message) {
30
- return this.updates.message(update);
31
- }
32
- }
33
- }
34
- return this.updates.default;
35
- };
36
- // greet new users who join
37
- greetUsers = async (update) => update.message?.new_chat_members
38
- ? this.sendMessage(update.message.chat.id, `Welcome to ${update.message.chat.title}, @${update.message.new_chat_member?.username}`)
39
- : this.updates.default;
40
- getCommand = (args) => args[0]?.split('@')[0];
41
- // run command passed from executeCommand
42
- _executeCommand = async (update, text, args = []) => log({ execute: { text, args } })
43
- ? ((text_args) => ((command) => this.commands[command]
44
- ? this.commands[command]?.(this, update, [...text_args, ...args])
45
- : log({
46
- error: `command '${command}' does not exist, using default`,
47
- }) && this.commands['default']?.(this, update, [...text_args, ...args]))(
48
- // run the command
49
- this.getCommand(text_args)))(
50
- // get the command to run
51
- text
52
- .trimStart()
53
- .replace(/^([^\s]*\s)\s*/gm, '$1')
54
- .split(' '))
55
- : this.updates.default;
56
- // execute the inline custom bot commands from bot configurations
57
- executeInlineCommand = async (update) => this._executeCommand(update, update.inline_query?.query ?? '').then(async (command_response) => command_response
58
- ? this._executeCommand(update, 'inline', update.inline_query?.query.trimStart().split(' ')).then((_command_response) => _command_response)
59
- : this.updates.default);
60
- // execute the custom bot commands from bot configurations
61
- executeCommand = async (update) => this._executeCommand(update, update.message?.text ?? '') ?? this.updates.default;
62
- // trigger answerInlineQuery command of BotAPI
63
- answerInlineQuery = async (inline_query_id, results, cache_time = 0) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/answerInlineQuery`), {
64
- inline_query_id: inline_query_id.toString(),
65
- results: JSON.stringify(results),
66
- cache_time: cache_time.toString(),
67
- }).href));
68
- // trigger editMessage command of BotAPI
69
- editMessageText = async (chat_id, message_id, text) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/editMessageText`), {
70
- chat_id: chat_id.toString(),
71
- message_id: message_id.toString(),
72
- text,
73
- }).href));
74
- // trigger sendMessage command of BotAPI
75
- 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`), {
76
- chat_id: chat_id.toString(),
77
- text,
78
- parse_mode: parse_mode,
79
- disable_web_page_preview: disable_web_page_preview.toString(),
80
- disable_notification: disable_notification.toString(),
81
- reply_to_message_id: reply_to_message_id.toString(),
82
- }).href));
83
- // trigger forwardMessage command of BotAPI
84
- forwardMessage = async (chat_id, from_chat_id, disable_notification = false, message_id) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/sendMessage`), {
85
- chat_id: chat_id.toString(),
86
- from_chat_id: from_chat_id.toString(),
87
- message_id: message_id.toString(),
88
- disable_notification: disable_notification.toString(),
89
- }).href));
90
- // trigger sendPhoto command of BotAPI
91
- sendPhotoRaw = async (chat_id, photo, caption = '', parse_mode = '', disable_notification = false, reply_to_message_id = 0) => {
92
- const formdata = new FormData();
93
- formdata.set('file', photo);
94
- return fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/sendPhoto`), {
95
- chat_id: chat_id.toString(),
96
- caption,
97
- parse_mode,
98
- disable_notification: disable_notification.toString(),
99
- reply_to_message_id: reply_to_message_id.toString(),
100
- }).href), { method: 'POST', body: formdata })
101
- .then((resp) => resp.text())
102
- .then(log);
103
- };
104
- // trigger sendPhoto command of BotAPI
105
- 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`), {
106
- chat_id: chat_id.toString(),
107
- photo,
108
- caption,
109
- parse_mode,
110
- disable_notification: disable_notification.toString(),
111
- reply_to_message_id: reply_to_message_id.toString(),
112
- }).href));
113
- // trigger sendVideo command of BotAPI
114
- 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`), {
115
- chat_id: chat_id.toString(),
116
- video: JSON.stringify(video),
117
- duration: duration.toString(),
118
- width: width.toString(),
119
- height: height.toString(),
120
- thumb: thumb,
121
- caption: caption,
122
- parse_mode: parse_mode,
123
- supports_streaming: supports_streaming.toString(),
124
- disable_notification: disable_notification.toString(),
125
- reply_to_message_id: reply_to_message_id.toString(),
126
- }).href));
127
- // trigger sendAnimation command of BotAPI
128
- 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`), {
129
- chat_id: chat_id.toString(),
130
- animation: JSON.stringify(animation),
131
- duration: duration.toString(),
132
- width: width.toString(),
133
- height: height.toString(),
134
- thumb,
135
- caption,
136
- parse_mode,
137
- disable_notification: disable_notification.toString(),
138
- reply_to_message_id: reply_to_message_id.toString(),
139
- }).href));
140
- // trigger sendLocation command of BotAPI
141
- sendLocation = async (chat_id, latitude, longitude, live_period = 0, disable_notification = false, reply_to_message_id = 0) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/sendLocation`), {
142
- chat_id: chat_id.toString(),
143
- latitude: latitude.toString(),
144
- longitude: longitude.toString(),
145
- live_period: live_period.toString(),
146
- disable_notification: disable_notification.toString(),
147
- reply_to_message_id: reply_to_message_id.toString(),
148
- }).href));
149
- // trigger senPoll command of BotAPI
150
- 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`), {
151
- chat_id: chat_id.toString(),
152
- question,
153
- options: options.toString(),
154
- is_anonymous: is_anonymous.toString(),
155
- type,
156
- allows_multiple_answers: allows_multiple_answers.toString(),
157
- correct_option_id: correct_option_id.toString(),
158
- explanation: explanation,
159
- explanation_parse_mode: explanation_parse_mode,
160
- open_period: open_period.toString(),
161
- close_date: close_date.toString(),
162
- is_closed: is_closed.toString(),
163
- disable_notification: disable_notification.toString(),
164
- reply_to_message_id: reply_to_message_id.toString(),
165
- }).href));
166
- // trigger senDice command of BotAPI
167
- 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`), {
168
- chat_id: chat_id.toString(),
169
- emoji,
170
- disable_notification: disable_notification.toString(),
171
- reply_to_message_id: reply_to_message_id.toString(),
172
- }).href));
173
- // bot api command to get user profile photos
174
- getUserProfilePhotos = async (user_id, offset = 0, limit = 0) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/getUserProfilePhotos`), {
175
- user_id: user_id.toString(),
176
- offset: offset.toString(),
177
- limit: limit.toString(),
178
- }).href));
179
- banChatMember = async (chat_id, user_id, until_date = 0, revoke_messages = false) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/banChatMember`), {
180
- chat_id: chat_id.toString(),
181
- user_id: user_id.toString(),
182
- until_date: until_date.toString(),
183
- revoke_messages: revoke_messages.toString(),
184
- }).href));
185
- restrictChatMember = async (chat_id, user_id, permissions, use_independent_chat_permissions, until_date) => fetch(log(addSearchParams(new URL(`${this.webhook.api.origin}${this.webhook.api.pathname}/restrictChatMember`), {
186
- chat_id: chat_id.toString(),
187
- user_id: user_id.toString(),
188
- permissions: JSON.stringify(permissions),
189
- use_independent_chat_permissions: use_independent_chat_permissions.toString(),
190
- until_date: until_date.toString(),
191
- }).href));
192
- }
@@ -1,22 +0,0 @@
1
- import { TelegramCommand } from './types';
2
- export default class TelegramCommands {
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;
19
- static start: TelegramCommand;
20
- static ban: TelegramCommand;
21
- static mute: TelegramCommand;
22
- }
@@ -1,21 +0,0 @@
1
- export default class TelegramCommands {
2
- static ping = async (bot, update, args) => bot.ping(bot, update, args);
3
- static toss = async (bot, update) => bot.toss(bot, update);
4
- static epoch = async (bot, update) => bot.epoch(bot, update);
5
- static kanye = async (bot, update) => bot.kanye(bot, update);
6
- static bored = async (bot, update) => bot.bored(bot, update);
7
- static joke = async (bot, update) => bot.joke(bot, update);
8
- static dog = async (bot, update) => bot.dog(bot, update);
9
- static roll = async (bot, update, args) => bot.roll(bot, update, args);
10
- static duckduckgo = async (bot, update, args) => bot.duckduckgo(bot, update, args);
11
- static question = async (bot, update, args) => bot.question(bot, update, args);
12
- static sean = async (bot, update, args) => bot.sean(bot, update, args);
13
- static clear = async (bot, update) => bot.clear(bot, update);
14
- static code = async (bot, update) => bot.code(bot, update);
15
- static commandList = async (bot, update) => bot.commandList(bot, update);
16
- static image = async (bot, update, args) => bot.image(bot, update, args);
17
- static translate = async (bot, update, args) => bot.translate(bot, update, args);
18
- static start = async (bot, update) => bot.start(bot, update);
19
- static ban = async (bot, update) => bot.ban(bot, update);
20
- static mute = async (bot, update) => bot.mute(bot, update);
21
- }
@@ -1,10 +0,0 @@
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
- }
@@ -1,20 +0,0 @@
1
- import Webhook from './webhook';
2
- import { sha256, addSearchParams } 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(addSearchParams(new URL(`${this.api.origin}${this.api.pathname}/setWebhook`), {
8
- url: new URL(`${this.url.origin}${this.url.pathname}${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(new URL(`${this.api.origin}${this.api.pathname}/getWebhookInfo`));
14
- delete = async () => fetch(new URL(`${this.api.origin}${this.api.pathname}/deleteWebhook`));
15
- commands = {
16
- set: this.set,
17
- get: this.get,
18
- delete: this.delete,
19
- };
20
- }
@@ -1,5 +0,0 @@
1
- /// <reference types="@cloudflare/workers-types" />
2
- import BotApi from '../bot_api';
3
- import Update from './Update';
4
- type Command = (bot: BotApi, update: Update, args: string[]) => Promise<Response>;
5
- export default Command;
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- import Command from './Command';
2
- type Commands = Record<string, Command>;
3
- export default Commands;
@@ -1 +0,0 @@
1
- export {};
@@ -1,20 +0,0 @@
1
- /// <reference types="@cloudflare/workers-types" />
2
- import Command from './Command';
3
- import Kv from './Kv';
4
- import BotApi from '../bot_api';
5
- import Handler from '../handler';
6
- import Webhook from '../webhook';
7
- export default class Config {
8
- bot_name: string;
9
- api: typeof BotApi;
10
- webhook: Webhook;
11
- commands: Record<string, Command>;
12
- kv: Kv;
13
- url: URL;
14
- handler: Handler;
15
- ai: any;
16
- db: any;
17
- r2: any;
18
- chat_model: string;
19
- constructor(config?: Partial<Config>);
20
- }
@@ -1,33 +0,0 @@
1
- import BotApi from '../bot_api';
2
- import Handler from '../handler';
3
- import Webhook from '../webhook';
4
- import localhost from './localhost';
5
- export default class Config {
6
- bot_name;
7
- api;
8
- webhook;
9
- commands;
10
- kv;
11
- url;
12
- handler;
13
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
14
- ai;
15
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
16
- db;
17
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
- r2;
19
- chat_model;
20
- constructor(config = {}) {
21
- this.bot_name = config.bot_name || '';
22
- this.api = config.api || BotApi;
23
- this.webhook = config.webhook || new Webhook(localhost, '', localhost);
24
- this.commands = config.commands || {};
25
- this.kv = config.kv;
26
- this.url = config.url || new URL(localhost);
27
- this.handler = config.handler || new Handler([]);
28
- this.ai = config.ai;
29
- this.db = config.db;
30
- this.r2 = config.r2;
31
- this.chat_model = config.chat_model;
32
- }
33
- }
@@ -1,10 +0,0 @@
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
- }
@@ -1,15 +0,0 @@
1
- export default class Webhook {
2
- api;
3
- token;
4
- url;
5
- commands;
6
- constructor(api, token, url) {
7
- this.api = api;
8
- this.token = token;
9
- this.url = url;
10
- this.commands = {
11
- default: async () => new Response('Invalid command'),
12
- };
13
- }
14
- process = async (url) => this.commands[url.searchParams.get('command') ?? '']?.() ?? this.commands.default();
15
- }