@codebam/cf-workers-telegram-bot 6.4.0 → 6.6.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/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/question.js +1 -1
- package/dist/main/src/commands/translate.js +2 -2
- package/dist/main/src/libs.d.ts +0 -5
- package/dist/main/src/libs.js +0 -18
- package/dist/main/src/telegram_api.js +8 -2
- package/dist/main/src/telegram_webhook.js +4 -4
- 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 +2 -1
- package/dist/main/src/webhook.js +2 -3
- package/package.json +2 -2
|
@@ -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}`))
|
|
@@ -50,7 +50,7 @@ export default async (self, update, args) => {
|
|
|
50
50
|
// @ts-expect-error chat model might not match
|
|
51
51
|
.run(self.chat_model, {
|
|
52
52
|
prompt,
|
|
53
|
-
max_tokens: 596,
|
|
53
|
+
max_tokens: update.inline_query ? 50 : 596,
|
|
54
54
|
})
|
|
55
55
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
56
56
|
.then(({ response }) => response.replace(/(\[|)(\/|)INST(S|)(s|)(\]|)/, '').replace(/<<(\/|)SYS>>/, ''));
|
|
@@ -13,14 +13,14 @@ export default async (self, update, args) => {
|
|
|
13
13
|
if (_prompt === '') {
|
|
14
14
|
_prompt = '';
|
|
15
15
|
}
|
|
16
|
-
const langs = ['french'
|
|
16
|
+
const langs = ['french'];
|
|
17
17
|
const inline_articles = await Promise.all(langs.map(async (lang) => {
|
|
18
18
|
const response = await self.ai.run('@cf/meta/m2m100-1.2b', {
|
|
19
19
|
text: _prompt,
|
|
20
20
|
source_lang: lang,
|
|
21
21
|
target_lang: 'english',
|
|
22
22
|
});
|
|
23
|
-
return new TelegramInlineQueryResultArticle(response.translated_text,
|
|
23
|
+
return new TelegramInlineQueryResultArticle(response.translated_text, response.translated_text);
|
|
24
24
|
}));
|
|
25
25
|
return self.answerInlineQuery(update.inline_query?.id ?? 0, inline_articles);
|
|
26
26
|
};
|
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));
|
|
@@ -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
|
|
@@ -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 @@
|
|
|
1
|
+
export {};
|
package/dist/main/src/types.d.ts
CHANGED
|
@@ -26,4 +26,5 @@ import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPho
|
|
|
26
26
|
import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultArticle';
|
|
27
27
|
import DDGQueryResponse from './types/DDGQueryResponse';
|
|
28
28
|
import ChatPermissions from './types/ChatPermissions';
|
|
29
|
-
|
|
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
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codebam/cf-workers-telegram-bot",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.6.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": "ac79b5c72e1c5379d512ea35dff333c0aca0ff2c"
|
|
45
45
|
}
|