@codebam/cf-workers-telegram-bot 6.2.0 → 6.5.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/commands/ban.d.ts +5 -0
- package/dist/main/src/commands/ban.js +8 -0
- package/dist/main/src/commands/bored.js +1 -2
- package/dist/main/src/commands/getchatinfo.js +1 -2
- package/dist/main/src/commands/joke.js +1 -2
- package/dist/main/src/commands/kanye.js +2 -2
- package/dist/main/src/commands/mute.d.ts +5 -0
- package/dist/main/src/commands/mute.js +8 -0
- package/dist/main/src/commands/start.d.ts +5 -0
- package/dist/main/src/commands/start.js +1 -0
- package/dist/main/src/libs.d.ts +0 -5
- package/dist/main/src/libs.js +0 -18
- package/dist/main/src/telegram_api.d.ts +3 -1
- package/dist/main/src/telegram_api.js +21 -2
- package/dist/main/src/telegram_bot.d.ts +3 -0
- package/dist/main/src/telegram_bot.js +9 -0
- package/dist/main/src/telegram_commands.d.ts +3 -0
- package/dist/main/src/telegram_commands.js +3 -0
- package/dist/main/src/telegram_webhook.js +4 -4
- package/dist/main/src/types/ChatPermissions.d.ts +17 -0
- package/dist/main/src/types/ChatPermissions.js +1 -0
- package/dist/main/src/types/Kanye.d.ts +4 -0
- package/dist/main/src/types/Kanye.js +1 -0
- package/dist/main/src/types/TelegramMessage.d.ts +1 -0
- package/dist/main/src/types.d.ts +3 -1
- package/dist/main/src/webhook.js +2 -3
- package/dist/worker/src/worker.js +3 -1
- package/package.json +2 -2
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export default async (self, update) => {
|
|
2
|
+
const chat_id = update.message?.reply_to_message?.chat.id;
|
|
3
|
+
const user_id = update.message?.reply_to_message?.from.id;
|
|
4
|
+
if (chat_id && user_id) {
|
|
5
|
+
return self.banChatMember(chat_id, user_id, 0, true);
|
|
6
|
+
}
|
|
7
|
+
return new Response('ok');
|
|
8
|
+
};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { responseToJSON } from '../libs';
|
|
2
1
|
import { TelegramInlineQueryResultArticle } from '../types';
|
|
3
2
|
export default async (self, update) => fetch('https://boredapi.com/api/activity/')
|
|
4
|
-
.then((response) =>
|
|
3
|
+
.then((response) => response.json())
|
|
5
4
|
.then((json) => json)
|
|
6
5
|
.then((bored_response) => update.inline_query
|
|
7
6
|
? self.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(bored_response.activity)], 0)
|
|
@@ -1,2 +1 @@
|
|
|
1
|
-
|
|
2
|
-
export default async (self, update) => self.sendMessage(update.message?.chat.id ?? 0, preTagString(prettyJSON(update.message?.chat ?? 0)), 'HTML');
|
|
1
|
+
export default async (self, update) => self.sendMessage(update.message?.chat.id ?? 0, `<pre>${JSON.stringify(update.message?.chat ?? 0)}</pre>`, 'HTML');
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import { responseToJSON } from '../libs';
|
|
2
1
|
import { TelegramInlineQueryResultArticle } from '../types';
|
|
3
2
|
export default async (self, update) => fetch('https://v2.jokeapi.dev/joke/Any?safe-mode')
|
|
4
|
-
.then((response) =>
|
|
3
|
+
.then((response) => response.json())
|
|
5
4
|
.then((joke) => joke)
|
|
6
5
|
.then((joke_response) => ((message) => update.inline_query
|
|
7
6
|
? self.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(message, joke_response.joke ?? joke_response.setup, 'HTML')], 0)
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { responseToJSON } from '../libs';
|
|
2
1
|
import { TelegramInlineQueryResultArticle } from '../types';
|
|
3
2
|
export default async (self, update) => fetch('https://api.kanye.rest')
|
|
4
|
-
.then((response) =>
|
|
3
|
+
.then((response) => response.json())
|
|
4
|
+
.then((json) => json)
|
|
5
5
|
.then((json) => ((message) => update.inline_query
|
|
6
6
|
? self.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(message)])
|
|
7
7
|
: self.sendMessage(update.message?.chat.id ?? 0, message))(`Kanye says... ${json.quote}`))
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export default async (self, update) => {
|
|
2
|
+
const chat_id = update.message?.reply_to_message?.chat.id;
|
|
3
|
+
const user_id = update.message?.reply_to_message?.from.id;
|
|
4
|
+
if (chat_id && user_id) {
|
|
5
|
+
return self.restrictChatMember(chat_id, user_id, { can_send_messages: false }, false, 0);
|
|
6
|
+
}
|
|
7
|
+
return new Response('ok');
|
|
8
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default async (self, update) => self.sendMessage(update.message?.chat.id ?? 0, `Hello, send me a message to start chatting with ${self.chat_model}`);
|
package/dist/main/src/libs.d.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
2
|
export declare const sha256: (text: string) => Promise<string>;
|
|
3
|
-
export declare const prettyJSON: (obj: unknown) => string;
|
|
4
|
-
export declare const JSONResponse: (obj: unknown, status?: number) => Response;
|
|
5
3
|
export declare const log: (obj: any) => any;
|
|
6
|
-
export declare const preTagString: (str: string) => string;
|
|
7
4
|
export declare const addSearchParams: (url: URL, params?: Record<string, string>) => URL;
|
|
8
|
-
export declare const responseToJSON: (response: Response) => Promise<Record<string, unknown>>;
|
|
9
5
|
export declare const undefinedEmpty: <T>(obj: T) => (T & ({} | null))[];
|
|
10
|
-
export declare const fetch_json: (url: URL) => Promise<Response>;
|
package/dist/main/src/libs.js
CHANGED
|
@@ -1,25 +1,7 @@
|
|
|
1
1
|
export const sha256 = async (text) => crypto.subtle.digest('SHA-256', new TextEncoder().encode(text)).then((array_buffer) => Array.from(new Uint8Array(array_buffer))
|
|
2
2
|
.map((b) => b.toString(16).padStart(2, '0'))
|
|
3
3
|
.join(''));
|
|
4
|
-
// format json with line indents and newlines
|
|
5
|
-
export const prettyJSON = (obj) => JSON.stringify(obj, null, 2);
|
|
6
|
-
// Generate JSON response
|
|
7
|
-
export const JSONResponse = (obj, status = 200) => new Response(prettyJSON(obj), {
|
|
8
|
-
status: status,
|
|
9
|
-
headers: {
|
|
10
|
-
'content-type': 'application/json',
|
|
11
|
-
},
|
|
12
|
-
});
|
|
13
4
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
14
5
|
export const log = (obj) => console.log(obj) === undefined && obj;
|
|
15
|
-
export const preTagString = (str) => `<pre>${str}</pre>`;
|
|
16
6
|
export const addSearchParams = (url, params = {}) => new URL(`${url.origin}${url.pathname}?${new URLSearchParams(Object.entries(Object.fromEntries([...Array.from(url.searchParams.entries()), ...Object.entries(params)]))).toString()}`);
|
|
17
|
-
export const responseToJSON = async (response) => response
|
|
18
|
-
.clone()
|
|
19
|
-
.text()
|
|
20
|
-
.then((text) => JSON.parse(text))
|
|
21
|
-
.catch(() => log({ error: 'Failed to parse JSON of response' }));
|
|
22
7
|
export const undefinedEmpty = (obj) => (obj === undefined ? [] : [obj]);
|
|
23
|
-
export const fetch_json = async (url) => fetch(url.href)
|
|
24
|
-
.then((response) => responseToJSON(response))
|
|
25
|
-
.then((json) => JSONResponse(json));
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
2
|
import BotApi from './bot_api';
|
|
3
|
-
import { Commands, TelegramInlineQueryResult, TelegramUpdate, Webhook, Update } from './types';
|
|
3
|
+
import { Commands, TelegramInlineQueryResult, TelegramUpdate, Webhook, Update, ChatPermissions } from './types';
|
|
4
4
|
import Handler from './handler';
|
|
5
5
|
export default class TelegramApi extends BotApi {
|
|
6
6
|
constructor(commands: Commands, webhook: Webhook, handler: Handler);
|
|
@@ -29,4 +29,6 @@ export default class TelegramApi extends BotApi {
|
|
|
29
29
|
sendPoll: (chat_id: number, question: string, options: string[], is_anonymous?: boolean, type?: string, allows_multiple_answers?: boolean, correct_option_id?: number, explanation?: string, explanation_parse_mode?: string, open_period?: number, close_date?: number, is_closed?: boolean, disable_notification?: boolean, reply_to_message_id?: number) => Promise<Response>;
|
|
30
30
|
sendDice: (chat_id: number, emoji?: string, disable_notification?: boolean, reply_to_message_id?: number) => Promise<Response>;
|
|
31
31
|
getUserProfilePhotos: (user_id: number, offset?: number, limit?: number) => Promise<Response>;
|
|
32
|
+
banChatMember: (chat_id: number | string, user_id: number, until_date?: number, revoke_messages?: boolean) => Promise<Response>;
|
|
33
|
+
restrictChatMember: (chat_id: number | string, user_id: number, permissions: ChatPermissions, use_independent_chat_permissions: boolean, until_date: number) => Promise<Response>;
|
|
32
34
|
}
|
|
@@ -5,7 +5,13 @@ export default class TelegramApi extends BotApi {
|
|
|
5
5
|
super({ commands, webhook, handler });
|
|
6
6
|
}
|
|
7
7
|
inlineQueryUpdate = async (update) => this.executeInlineCommand(update);
|
|
8
|
-
messageUpdate = async (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
|
+
};
|
|
9
15
|
updates = {
|
|
10
16
|
inline_query: this.inlineQueryUpdate,
|
|
11
17
|
message: this.messageUpdate,
|
|
@@ -29,7 +35,7 @@ export default class TelegramApi extends BotApi {
|
|
|
29
35
|
};
|
|
30
36
|
// greet new users who join
|
|
31
37
|
greetUsers = async (update) => update.message?.new_chat_members
|
|
32
|
-
? this.sendMessage(update.message.chat.id, `Welcome to ${update.message.chat.title},
|
|
38
|
+
? this.sendMessage(update.message.chat.id, `Welcome to ${update.message.chat.title}, @${update.message.new_chat_member?.username}`)
|
|
33
39
|
: this.updates.default;
|
|
34
40
|
getCommand = (args) => args[0]?.split('@')[0];
|
|
35
41
|
// run command passed from executeCommand
|
|
@@ -170,4 +176,17 @@ export default class TelegramApi extends BotApi {
|
|
|
170
176
|
offset: offset.toString(),
|
|
171
177
|
limit: limit.toString(),
|
|
172
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));
|
|
173
192
|
}
|
|
@@ -19,6 +19,9 @@ export default class TelegramBot extends TelegramApi {
|
|
|
19
19
|
toss: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
20
20
|
ping: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
21
21
|
getChatInfo: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
22
|
+
start: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
23
|
+
ban: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
24
|
+
mute: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
22
25
|
url: URL;
|
|
23
26
|
kv: Kv;
|
|
24
27
|
get_set: KVNamespace;
|
|
@@ -16,6 +16,9 @@ import commandlist from './commands/commandlist';
|
|
|
16
16
|
import toss from './commands/toss';
|
|
17
17
|
import ping from './commands/ping';
|
|
18
18
|
import getchatinfo from './commands/getchatinfo';
|
|
19
|
+
import start from './commands/start';
|
|
20
|
+
import ban from './commands/ban';
|
|
21
|
+
import mute from './commands/mute';
|
|
19
22
|
export default class TelegramBot extends TelegramApi {
|
|
20
23
|
translate;
|
|
21
24
|
clear;
|
|
@@ -34,6 +37,9 @@ export default class TelegramBot extends TelegramApi {
|
|
|
34
37
|
toss;
|
|
35
38
|
ping;
|
|
36
39
|
getChatInfo;
|
|
40
|
+
start;
|
|
41
|
+
ban;
|
|
42
|
+
mute;
|
|
37
43
|
url;
|
|
38
44
|
kv;
|
|
39
45
|
get_set;
|
|
@@ -61,6 +67,9 @@ export default class TelegramBot extends TelegramApi {
|
|
|
61
67
|
this.toss = toss;
|
|
62
68
|
this.ping = ping;
|
|
63
69
|
this.getChatInfo = getchatinfo;
|
|
70
|
+
this.start = start;
|
|
71
|
+
this.ban = ban;
|
|
72
|
+
this.mute = mute;
|
|
64
73
|
this.url = config.url;
|
|
65
74
|
this.kv = config.kv;
|
|
66
75
|
this.get_set = config.kv?.get_set;
|
|
@@ -15,4 +15,7 @@ export default class TelegramCommands {
|
|
|
15
15
|
static commandList = async (bot, update) => bot.commandList(bot, update);
|
|
16
16
|
static image = async (bot, update, args) => bot.image(bot, update, args);
|
|
17
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);
|
|
18
21
|
}
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
import Webhook from './webhook';
|
|
2
|
-
import { sha256, addSearchParams
|
|
2
|
+
import { sha256, addSearchParams } from './libs';
|
|
3
3
|
export default class TelegramWebhook extends Webhook {
|
|
4
4
|
constructor(api, token, url) {
|
|
5
5
|
super(api, token, url);
|
|
6
6
|
}
|
|
7
|
-
set = async (drop_pending_updates = true) => sha256(this.token).then((access_key) =>
|
|
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
8
|
url: new URL(`${this.url.origin}${this.url.pathname}${access_key}`).href,
|
|
9
9
|
max_connections: '100',
|
|
10
10
|
allowed_updates: JSON.stringify(['message', 'inline_query']),
|
|
11
11
|
drop_pending_updates: drop_pending_updates.toString(),
|
|
12
12
|
})));
|
|
13
|
-
get = async () =>
|
|
14
|
-
delete = async () =>
|
|
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
15
|
commands = {
|
|
16
16
|
set: this.set,
|
|
17
17
|
get: this.get,
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
type ChatPermissions = {
|
|
2
|
+
can_send_messages?: boolean;
|
|
3
|
+
can_send_audios?: boolean;
|
|
4
|
+
can_send_documents?: boolean;
|
|
5
|
+
can_send_photos?: boolean;
|
|
6
|
+
can_send_videos?: boolean;
|
|
7
|
+
can_send_video_notes?: boolean;
|
|
8
|
+
can_send_voice_notes?: boolean;
|
|
9
|
+
can_send_polls?: boolean;
|
|
10
|
+
can_send_other_messages?: boolean;
|
|
11
|
+
can_add_web_page_previews?: boolean;
|
|
12
|
+
can_change_info?: boolean;
|
|
13
|
+
can_invite_users?: boolean;
|
|
14
|
+
can_pin_messages?: boolean;
|
|
15
|
+
can_manage_topics?: boolean;
|
|
16
|
+
};
|
|
17
|
+
export default ChatPermissions;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/main/src/types.d.ts
CHANGED
|
@@ -25,4 +25,6 @@ import TelegramInlineQueryResult from './types/TelegramInlineQueryResult';
|
|
|
25
25
|
import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPhoto';
|
|
26
26
|
import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultArticle';
|
|
27
27
|
import DDGQueryResponse from './types/DDGQueryResponse';
|
|
28
|
-
|
|
28
|
+
import ChatPermissions from './types/ChatPermissions';
|
|
29
|
+
import Kanye from './types/Kanye';
|
|
30
|
+
export { Webhook, Command, TelegramCommand, Commands, Kv, Config, localhost, WebhookCommands, Joke, Bored, Balance, TelegramFrom, TelegramChat, TelegramUser, TelegramMessageEntity, TelegramPhotoSize, TelegramMessage, TelegramInputMessageContent, TelegramInlineQuery, Update, TelegramUpdate, PartialTelegramUpdate, TelegramInlineQueryType, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, DDGQueryResponse, ChatPermissions, Kanye, };
|
package/dist/main/src/webhook.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { JSONResponse } from './libs';
|
|
2
1
|
export default class Webhook {
|
|
3
2
|
api;
|
|
4
3
|
token;
|
|
@@ -9,8 +8,8 @@ export default class Webhook {
|
|
|
9
8
|
this.token = token;
|
|
10
9
|
this.url = url;
|
|
11
10
|
this.commands = {
|
|
12
|
-
default: () => new
|
|
11
|
+
default: async () => new Response('Invalid command'),
|
|
13
12
|
};
|
|
14
13
|
}
|
|
15
|
-
process = async (url) => this.commands[url.searchParams.get('command') ?? '']?.() ?? this.commands.default;
|
|
14
|
+
process = async (url) => this.commands[url.searchParams.get('command') ?? '']?.() ?? this.commands.default();
|
|
16
15
|
}
|
|
@@ -23,7 +23,9 @@ export default {
|
|
|
23
23
|
'/clear': TelegramCommands.clear,
|
|
24
24
|
'/help': TelegramCommands.commandList,
|
|
25
25
|
'/image': TelegramCommands.image,
|
|
26
|
-
'/start': TelegramCommands.
|
|
26
|
+
'/start': TelegramCommands.start,
|
|
27
|
+
'/ban': TelegramCommands.ban,
|
|
28
|
+
'/mute': TelegramCommands.mute,
|
|
27
29
|
},
|
|
28
30
|
kv: { get_set: env.KV_GET_SET, uid_data: env.KV_UID_DATA },
|
|
29
31
|
ai: env.AI,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codebam/cf-workers-telegram-bot",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.5.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",
|
|
@@ -41,5 +41,5 @@
|
|
|
41
41
|
"typescript": "^5.4.5",
|
|
42
42
|
"typescript-eslint": "^7.8.0"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "843a4525d055dce257c124a0f0a15b8f552b1a47"
|
|
45
45
|
}
|