@codebam/cf-workers-telegram-bot 7.6.0 → 7.7.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/api.d.ts +14 -0
- package/dist/api.js +21 -0
- package/dist/ctx.d.ts +16 -0
- package/dist/ctx.js +56 -0
- package/dist/main.d.ts +4 -0
- package/dist/main.js +4 -0
- package/dist/{main/src/telegram_bot.d.ts → telegram_bot.d.ts} +6 -10
- package/dist/telegram_bot.js +74 -0
- package/dist/types/SerializableData.d.ts +2 -0
- package/dist/types/TelegramUser.js +1 -0
- package/dist/{main/src/types.d.ts → types.d.ts} +2 -1
- package/dist/webhook.d.ts +7 -0
- package/dist/webhook.js +17 -0
- package/package.json +2 -2
- package/LICENSE_MIT +0 -21
- package/dist/main/src/main.d.ts +0 -4
- package/dist/main/src/main.js +0 -4
- package/dist/main/src/telegram_bot.js +0 -116
- package/dist/worker/src/worker.d.ts +0 -18
- package/dist/worker/src/worker.js +0 -81
- /package/dist/{main/src/types → types}/Balance.d.ts +0 -0
- /package/dist/{main/src/types → types}/Balance.js +0 -0
- /package/dist/{main/src/types → types}/Bored.d.ts +0 -0
- /package/dist/{main/src/types → types}/Bored.js +0 -0
- /package/dist/{main/src/types → types}/ChatPermissions.d.ts +0 -0
- /package/dist/{main/src/types → types}/ChatPermissions.js +0 -0
- /package/dist/{main/src/types → types}/DDGQueryResponse.d.ts +0 -0
- /package/dist/{main/src/types → types}/DDGQueryResponse.js +0 -0
- /package/dist/{main/src/types → types}/Joke.d.ts +0 -0
- /package/dist/{main/src/types → types}/Joke.js +0 -0
- /package/dist/{main/src/types → types}/Kanye.d.ts +0 -0
- /package/dist/{main/src/types → types}/Kanye.js +0 -0
- /package/dist/{main/src/types → types}/Kv.d.ts +0 -0
- /package/dist/{main/src/types → types}/Kv.js +0 -0
- /package/dist/{main/src/types → types}/PartialTelegramUpdate.d.ts +0 -0
- /package/dist/{main/src/types → types}/PartialTelegramUpdate.js +0 -0
- /package/dist/{main/src/types/TelegramChat.js → types/SerializableData.js} +0 -0
- /package/dist/{main/src/types → types}/TelegramChat.d.ts +0 -0
- /package/dist/{main/src/types/TelegramCommand.js → types/TelegramChat.js} +0 -0
- /package/dist/{main/src/types → types}/TelegramCommand.d.ts +0 -0
- /package/dist/{main/src/types/TelegramFrom.js → types/TelegramCommand.js} +0 -0
- /package/dist/{main/src/types → types}/TelegramFrom.d.ts +0 -0
- /package/dist/{main/src/types/TelegramInlineQuery.js → types/TelegramFrom.js} +0 -0
- /package/dist/{main/src/types → types}/TelegramInlineQuery.d.ts +0 -0
- /package/dist/{main/src/types/TelegramInlineQueryType.js → types/TelegramInlineQuery.js} +0 -0
- /package/dist/{main/src/types → types}/TelegramInlineQueryResult.d.ts +0 -0
- /package/dist/{main/src/types → types}/TelegramInlineQueryResult.js +0 -0
- /package/dist/{main/src/types → types}/TelegramInlineQueryResultArticle.d.ts +0 -0
- /package/dist/{main/src/types → types}/TelegramInlineQueryResultArticle.js +0 -0
- /package/dist/{main/src/types → types}/TelegramInlineQueryResultPhoto.d.ts +0 -0
- /package/dist/{main/src/types → types}/TelegramInlineQueryResultPhoto.js +0 -0
- /package/dist/{main/src/types → types}/TelegramInlineQueryType.d.ts +0 -0
- /package/dist/{main/src/types/TelegramInputMessageContent.js → types/TelegramInlineQueryType.js} +0 -0
- /package/dist/{main/src/types → types}/TelegramInputMessageContent.d.ts +0 -0
- /package/dist/{main/src/types/TelegramMessage.js → types/TelegramInputMessageContent.js} +0 -0
- /package/dist/{main/src/types → types}/TelegramMessage.d.ts +0 -0
- /package/dist/{main/src/types/TelegramMessageEntity.js → types/TelegramMessage.js} +0 -0
- /package/dist/{main/src/types → types}/TelegramMessageEntity.d.ts +0 -0
- /package/dist/{main/src/types/TelegramPhotoSize.js → types/TelegramMessageEntity.js} +0 -0
- /package/dist/{main/src/types → types}/TelegramPhotoSize.d.ts +0 -0
- /package/dist/{main/src/types/TelegramUser.js → types/TelegramPhotoSize.js} +0 -0
- /package/dist/{main/src/types → types}/TelegramUpdate.d.ts +0 -0
- /package/dist/{main/src/types → types}/TelegramUpdate.js +0 -0
- /package/dist/{main/src/types → types}/TelegramUser.d.ts +0 -0
- /package/dist/{main/src/types → types}/Update.d.ts +0 -0
- /package/dist/{main/src/types → types}/Update.js +0 -0
- /package/dist/{main/src/types → types}/WebhookCommands.d.ts +0 -0
- /package/dist/{main/src/types → types}/WebhookCommands.js +0 -0
- /package/dist/{main/src/types → types}/localhost.d.ts +0 -0
- /package/dist/{main/src/types → types}/localhost.js +0 -0
- /package/dist/{main/src/types.js → types.js} +0 -0
package/dist/api.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
import { SerializableData, TelegramInlineQueryResultArticle } from "./types";
|
|
3
|
+
export default class API {
|
|
4
|
+
static getApiUrl(botApi: string, slug: string, data: Record<string, SerializableData>): string;
|
|
5
|
+
static sendMessage(botApi: string, data: {
|
|
6
|
+
reply_to_message_id: number | string;
|
|
7
|
+
chat_id: number | string;
|
|
8
|
+
text: string;
|
|
9
|
+
}): Promise<Response>;
|
|
10
|
+
static answerInline(botApi: string, data: {
|
|
11
|
+
inline_query_id: number | string;
|
|
12
|
+
results: TelegramInlineQueryResultArticle[];
|
|
13
|
+
}): Promise<Response>;
|
|
14
|
+
}
|
package/dist/api.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export default class API {
|
|
2
|
+
static getApiUrl(botApi, slug, data) {
|
|
3
|
+
const request = new URL(botApi + (slug.startsWith('/') || botApi.endsWith('/') ? '' : '/') + slug);
|
|
4
|
+
const params = new URLSearchParams();
|
|
5
|
+
for (const i in data) {
|
|
6
|
+
params.append(i, data[i].toString());
|
|
7
|
+
}
|
|
8
|
+
return `${request}?${params}`;
|
|
9
|
+
}
|
|
10
|
+
static async sendMessage(botApi, data) {
|
|
11
|
+
const url = this.getApiUrl(botApi, 'sendMessage', data);
|
|
12
|
+
return await fetch(url);
|
|
13
|
+
}
|
|
14
|
+
static async answerInline(botApi, data) {
|
|
15
|
+
const url = this.getApiUrl(botApi, 'answerInlineQuery', {
|
|
16
|
+
inline_query_id: data.inline_query_id,
|
|
17
|
+
results: JSON.stringify(data.results)
|
|
18
|
+
});
|
|
19
|
+
return await fetch(url);
|
|
20
|
+
}
|
|
21
|
+
}
|
package/dist/ctx.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
+
import TelegramBot from "./telegram_bot";
|
|
3
|
+
import { SerializableData, TelegramUpdate } from "./types";
|
|
4
|
+
export default class ExecutionContext {
|
|
5
|
+
bot: TelegramBot;
|
|
6
|
+
update: TelegramUpdate;
|
|
7
|
+
update_type: string;
|
|
8
|
+
private data;
|
|
9
|
+
constructor(bot: TelegramBot, update: TelegramUpdate);
|
|
10
|
+
getText(): string;
|
|
11
|
+
next(): Response;
|
|
12
|
+
setData(key: string, value: SerializableData): this;
|
|
13
|
+
deleteData(key: string): this;
|
|
14
|
+
getData(key: string): SerializableData;
|
|
15
|
+
reply(message: string): Promise<void>;
|
|
16
|
+
}
|
package/dist/ctx.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import API from "./api";
|
|
2
|
+
import { TelegramInlineQueryResultArticle } from "./types";
|
|
3
|
+
export default class ExecutionContext {
|
|
4
|
+
bot;
|
|
5
|
+
update;
|
|
6
|
+
update_type = '';
|
|
7
|
+
data = {};
|
|
8
|
+
constructor(bot, update) {
|
|
9
|
+
this.bot = bot;
|
|
10
|
+
this.update = update;
|
|
11
|
+
if (this.update.message?.text) {
|
|
12
|
+
this.update_type = 'message';
|
|
13
|
+
}
|
|
14
|
+
else if (this.update.inline_query?.query) {
|
|
15
|
+
this.update_type = 'inline';
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
getText() {
|
|
19
|
+
return this.update.message?.text || this.update.inline_query?.query || "";
|
|
20
|
+
}
|
|
21
|
+
next() {
|
|
22
|
+
return new Response('ok');
|
|
23
|
+
}
|
|
24
|
+
setData(key, value) {
|
|
25
|
+
this.data[key] = value;
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
deleteData(key) {
|
|
29
|
+
delete this.data[key];
|
|
30
|
+
return this;
|
|
31
|
+
}
|
|
32
|
+
getData(key) {
|
|
33
|
+
return this.data[key];
|
|
34
|
+
}
|
|
35
|
+
async reply(message) {
|
|
36
|
+
switch (this.update_type) {
|
|
37
|
+
case 'message': {
|
|
38
|
+
await API.sendMessage(this.bot.api.toString(), {
|
|
39
|
+
chat_id: this.update.message?.chat.id.toString() ?? '',
|
|
40
|
+
reply_to_message_id: this.update.message?.message_id.toString() ?? '',
|
|
41
|
+
text: message
|
|
42
|
+
});
|
|
43
|
+
break;
|
|
44
|
+
}
|
|
45
|
+
case 'inline': {
|
|
46
|
+
await API.answerInline(this.bot.api.toString(), {
|
|
47
|
+
inline_query_id: this.update.inline_query?.id.toString() ?? '',
|
|
48
|
+
results: [new TelegramInlineQueryResultArticle(message)]
|
|
49
|
+
});
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
default:
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
package/dist/main.d.ts
ADDED
package/dist/main.js
ADDED
|
@@ -1,20 +1,16 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
2
|
import { TelegramUpdate } from './types';
|
|
3
|
+
import ExecutionContext from './ctx';
|
|
4
|
+
import Webhook from './webhook';
|
|
3
5
|
export default class TelegramBot {
|
|
4
6
|
token: string;
|
|
5
7
|
webhook: Webhook;
|
|
6
|
-
update: TelegramUpdate;
|
|
7
8
|
api: URL;
|
|
9
|
+
update: TelegramUpdate;
|
|
8
10
|
update_type: string;
|
|
11
|
+
commands: Record<string, (ctx: ExecutionContext) => Promise<Response>>;
|
|
12
|
+
currentContext: ExecutionContext;
|
|
9
13
|
constructor(token: string);
|
|
10
|
-
on(event: string, callback: () => Promise<Response>): this;
|
|
14
|
+
on(event: string, callback: (ctx: ExecutionContext) => Promise<Response>): this;
|
|
11
15
|
handle(request: Request): Promise<Response>;
|
|
12
|
-
reply(message: string): Promise<void>;
|
|
13
|
-
}
|
|
14
|
-
declare class Webhook {
|
|
15
|
-
api: URL;
|
|
16
|
-
webhook: URL;
|
|
17
|
-
constructor(token: string, request: Request);
|
|
18
|
-
set(): Promise<Response>;
|
|
19
16
|
}
|
|
20
|
-
export {};
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { TelegramUpdate } from './types';
|
|
2
|
+
import ExecutionContext from './ctx';
|
|
3
|
+
import Webhook from './webhook';
|
|
4
|
+
export default class TelegramBot {
|
|
5
|
+
token;
|
|
6
|
+
webhook;
|
|
7
|
+
api;
|
|
8
|
+
update;
|
|
9
|
+
update_type;
|
|
10
|
+
commands = {};
|
|
11
|
+
currentContext;
|
|
12
|
+
constructor(token) {
|
|
13
|
+
this.token = token;
|
|
14
|
+
this.webhook = new Webhook('', new Request('http://127.0.0.1'));
|
|
15
|
+
this.api = new URL('https://api.telegram.org/bot' + token);
|
|
16
|
+
this.update = new TelegramUpdate({});
|
|
17
|
+
this.update_type = '';
|
|
18
|
+
}
|
|
19
|
+
on(event, callback) {
|
|
20
|
+
if (event !== 'on') {
|
|
21
|
+
// eslint-disable-next-line
|
|
22
|
+
// @ts-ignore TS7053
|
|
23
|
+
this.commands[event] = callback;
|
|
24
|
+
}
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
async handle(request) {
|
|
28
|
+
this.webhook = new Webhook(this.token, request);
|
|
29
|
+
if (request.method === 'POST') {
|
|
30
|
+
this.update = await request.json();
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
this.update = new TelegramUpdate({});
|
|
34
|
+
}
|
|
35
|
+
const url = new URL(request.url);
|
|
36
|
+
if (`/${this.token}` === url.pathname) {
|
|
37
|
+
switch (url.searchParams.get('command')) {
|
|
38
|
+
case 'set':
|
|
39
|
+
return this.webhook.set();
|
|
40
|
+
default:
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
console.log(this.update);
|
|
44
|
+
let command = 'default';
|
|
45
|
+
let args = [];
|
|
46
|
+
const ctx = new ExecutionContext(this, this.update);
|
|
47
|
+
this.currentContext = ctx;
|
|
48
|
+
switch (ctx.update_type) {
|
|
49
|
+
case 'message': {
|
|
50
|
+
// @ts-expect-error already checked above
|
|
51
|
+
args = this.update.message.text.split(' ');
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
case 'inline': {
|
|
55
|
+
// @ts-expect-error already checked above
|
|
56
|
+
args = this.update.inline_query.query.split(' ');
|
|
57
|
+
break;
|
|
58
|
+
}
|
|
59
|
+
default:
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
if (args.at(0)?.startsWith('/')) {
|
|
63
|
+
// @ts-expect-error already checked above
|
|
64
|
+
command = args.at(0).slice(1);
|
|
65
|
+
}
|
|
66
|
+
this.commands['any']?.(ctx);
|
|
67
|
+
if (!this.commands[command]) {
|
|
68
|
+
command = 'default';
|
|
69
|
+
}
|
|
70
|
+
return await this.commands[command]?.(ctx);
|
|
71
|
+
}
|
|
72
|
+
return new Response('ok');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -23,4 +23,5 @@ import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultA
|
|
|
23
23
|
import DDGQueryResponse from './types/DDGQueryResponse';
|
|
24
24
|
import ChatPermissions from './types/ChatPermissions';
|
|
25
25
|
import Kanye from './types/Kanye';
|
|
26
|
-
|
|
26
|
+
import SerializableData from './types/SerializableData';
|
|
27
|
+
export { TelegramCommand, Kv, localhost, WebhookCommands, Joke, Bored, Balance, TelegramFrom, TelegramChat, TelegramUser, TelegramMessageEntity, TelegramPhotoSize, TelegramMessage, TelegramInputMessageContent, TelegramInlineQuery, Update, TelegramUpdate, PartialTelegramUpdate, TelegramInlineQueryType, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, DDGQueryResponse, ChatPermissions, Kanye, SerializableData, };
|
package/dist/webhook.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export default class Webhook {
|
|
2
|
+
api;
|
|
3
|
+
webhook;
|
|
4
|
+
constructor(token, request) {
|
|
5
|
+
this.api = new URL('https://api.telegram.org/bot' + token);
|
|
6
|
+
this.webhook = new URL(new URL(request.url).origin + `/${token}`);
|
|
7
|
+
}
|
|
8
|
+
async set() {
|
|
9
|
+
const url = new URL(`${this.api.origin}${this.api.pathname}/setWebhook`);
|
|
10
|
+
const params = url.searchParams;
|
|
11
|
+
params.append('url', this.webhook.toString());
|
|
12
|
+
params.append('max_connections', '100');
|
|
13
|
+
params.append('allowed_updates', JSON.stringify(['message', 'inline_query']));
|
|
14
|
+
params.append('drop_pending_updates', 'true');
|
|
15
|
+
return await fetch(`${url}?${params}`);
|
|
16
|
+
}
|
|
17
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codebam/cf-workers-telegram-bot",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.7.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",
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"typescript-eslint": "^7.8.0",
|
|
44
44
|
"vitest": "^1.6.0"
|
|
45
45
|
},
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "11d5f60c49614aaeaa1e980790be6c2c3ef2057a"
|
|
47
47
|
}
|
package/LICENSE_MIT
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2020 Nikhil John
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
package/dist/main/src/main.d.ts
DELETED
package/dist/main/src/main.js
DELETED
|
@@ -1,116 +0,0 @@
|
|
|
1
|
-
import { TelegramInlineQueryResultArticle, TelegramUpdate } from './types';
|
|
2
|
-
export default class TelegramBot {
|
|
3
|
-
token;
|
|
4
|
-
webhook;
|
|
5
|
-
update;
|
|
6
|
-
api;
|
|
7
|
-
update_type;
|
|
8
|
-
constructor(token) {
|
|
9
|
-
this.token = token;
|
|
10
|
-
this.webhook = new Webhook('', new Request('http://127.0.0.1'));
|
|
11
|
-
this.update = new TelegramUpdate({});
|
|
12
|
-
this.api = new URL('https://api.telegram.org/bot' + token);
|
|
13
|
-
this.update_type = '';
|
|
14
|
-
}
|
|
15
|
-
on(event, callback) {
|
|
16
|
-
if (event !== 'on') {
|
|
17
|
-
// eslint-disable-next-line
|
|
18
|
-
// @ts-ignore TS7053
|
|
19
|
-
this[event] = callback;
|
|
20
|
-
}
|
|
21
|
-
return this;
|
|
22
|
-
}
|
|
23
|
-
async handle(request) {
|
|
24
|
-
this.webhook = new Webhook(this.token, request);
|
|
25
|
-
if (request.method === 'POST') {
|
|
26
|
-
this.update = await request.json();
|
|
27
|
-
}
|
|
28
|
-
const url = new URL(request.url);
|
|
29
|
-
if (`/${this.token}` === url.pathname) {
|
|
30
|
-
switch (url.searchParams.get('command')) {
|
|
31
|
-
case 'set':
|
|
32
|
-
return this.webhook.set();
|
|
33
|
-
default:
|
|
34
|
-
break;
|
|
35
|
-
}
|
|
36
|
-
console.log(this.update);
|
|
37
|
-
if (this.update.message?.text) {
|
|
38
|
-
this.update_type = 'message';
|
|
39
|
-
}
|
|
40
|
-
else if (this.update.inline_query?.query) {
|
|
41
|
-
this.update_type = 'inline';
|
|
42
|
-
}
|
|
43
|
-
let command = 'default';
|
|
44
|
-
let args = [];
|
|
45
|
-
switch (this.update_type) {
|
|
46
|
-
case 'message': {
|
|
47
|
-
// @ts-expect-error already checked above
|
|
48
|
-
args = this.update.message.text.split(' ');
|
|
49
|
-
break;
|
|
50
|
-
}
|
|
51
|
-
case 'inline': {
|
|
52
|
-
// @ts-expect-error already checked above
|
|
53
|
-
args = this.update.inline_query.query.split(' ');
|
|
54
|
-
break;
|
|
55
|
-
}
|
|
56
|
-
default:
|
|
57
|
-
break;
|
|
58
|
-
}
|
|
59
|
-
if (args.at(0)?.startsWith('/')) {
|
|
60
|
-
// @ts-expect-error already checked above
|
|
61
|
-
command = args.at(0).slice(1);
|
|
62
|
-
}
|
|
63
|
-
// eslint-disable-next-line
|
|
64
|
-
// @ts-ignore
|
|
65
|
-
if (!this[command] || command === 'on') {
|
|
66
|
-
command = 'default';
|
|
67
|
-
}
|
|
68
|
-
// eslint-disable-next-line
|
|
69
|
-
// @ts-ignore
|
|
70
|
-
return this[command]?.();
|
|
71
|
-
}
|
|
72
|
-
return new Response('ok');
|
|
73
|
-
}
|
|
74
|
-
async reply(message) {
|
|
75
|
-
switch (this.update_type) {
|
|
76
|
-
case 'message': {
|
|
77
|
-
const request = new URL(this.api + '/sendMessage');
|
|
78
|
-
const params = new URLSearchParams();
|
|
79
|
-
params.append('chat_id', this.update.message?.chat.id.toString() ?? '');
|
|
80
|
-
params.append('reply_to_message_id', this.update.message?.message_id.toString() ?? '');
|
|
81
|
-
params.append('text', message);
|
|
82
|
-
console.log(`${request}?${params}`);
|
|
83
|
-
await fetch(`${request}?${params}`);
|
|
84
|
-
break;
|
|
85
|
-
}
|
|
86
|
-
case 'inline': {
|
|
87
|
-
const inline_request = new URL(this.api + '/answerInlineQuery');
|
|
88
|
-
const inline_params = new URLSearchParams();
|
|
89
|
-
inline_params.append('inline_query_id', this.update.inline_query?.id.toString() ?? '');
|
|
90
|
-
inline_params.append('results', JSON.stringify([new TelegramInlineQueryResultArticle(message)]));
|
|
91
|
-
console.log(`${inline_request}?${inline_params}`);
|
|
92
|
-
await fetch(`${inline_request}?${inline_params}`);
|
|
93
|
-
break;
|
|
94
|
-
}
|
|
95
|
-
default:
|
|
96
|
-
break;
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
class Webhook {
|
|
101
|
-
api;
|
|
102
|
-
webhook;
|
|
103
|
-
constructor(token, request) {
|
|
104
|
-
this.api = new URL('https://api.telegram.org/bot' + token);
|
|
105
|
-
this.webhook = new URL(new URL(request.url).origin + `/${token}`);
|
|
106
|
-
}
|
|
107
|
-
async set() {
|
|
108
|
-
const url = new URL(`${this.api.origin}${this.api.pathname}/setWebhook`);
|
|
109
|
-
const params = url.searchParams;
|
|
110
|
-
params.append('url', this.webhook.toString());
|
|
111
|
-
params.append('max_connections', '100');
|
|
112
|
-
params.append('allowed_updates', JSON.stringify(['message', 'inline_query']));
|
|
113
|
-
params.append('drop_pending_updates', 'true');
|
|
114
|
-
return await fetch(`${url}?${params}`);
|
|
115
|
-
}
|
|
116
|
-
}
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
export 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
|
-
SECRET_TELEGRAM_API_TOKEN4: string;
|
|
9
|
-
SECRET_TELEGRAM_API_TOKEN5: string;
|
|
10
|
-
AI: Ai;
|
|
11
|
-
DB: D1Database;
|
|
12
|
-
R2: R2Bucket;
|
|
13
|
-
CHAT_MODEL: string;
|
|
14
|
-
}
|
|
15
|
-
declare const _default: {
|
|
16
|
-
fetch: (request: Request, env: Environment, ctx: ExecutionContext) => Promise<Response>;
|
|
17
|
-
};
|
|
18
|
-
export default _default;
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import TelegramBot from '../../main/src/telegram_bot';
|
|
2
|
-
export default {
|
|
3
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
4
|
-
fetch: async (request, env, ctx) => {
|
|
5
|
-
const bot = new TelegramBot(env.SECRET_TELEGRAM_API_TOKEN);
|
|
6
|
-
await bot
|
|
7
|
-
.on('default', async function () {
|
|
8
|
-
switch (bot.update_type) {
|
|
9
|
-
case 'message': {
|
|
10
|
-
const messages = [
|
|
11
|
-
{ role: 'system', content: 'You are a friendly assistant' },
|
|
12
|
-
{
|
|
13
|
-
role: 'user',
|
|
14
|
-
content: bot.update.message?.text?.toString() ?? '',
|
|
15
|
-
},
|
|
16
|
-
];
|
|
17
|
-
const response = await env.AI.run('@cf/meta/llama-3-8b-instruct', { messages });
|
|
18
|
-
if ('response' in response) {
|
|
19
|
-
await bot.reply(response.response ?? '');
|
|
20
|
-
}
|
|
21
|
-
break;
|
|
22
|
-
}
|
|
23
|
-
case 'inline': {
|
|
24
|
-
const inline_messages = [
|
|
25
|
-
{ role: 'system', content: 'You are a friendly assistant' },
|
|
26
|
-
{
|
|
27
|
-
role: 'user',
|
|
28
|
-
content: bot.update.inline_query?.query.toString() ?? '',
|
|
29
|
-
},
|
|
30
|
-
];
|
|
31
|
-
const inline_response = await env.AI.run('@cf/meta/llama-3-8b-instruct', { messages: inline_messages, max_tokens: 50 });
|
|
32
|
-
if ('response' in inline_response) {
|
|
33
|
-
await bot.reply(inline_response.response ?? '');
|
|
34
|
-
}
|
|
35
|
-
break;
|
|
36
|
-
}
|
|
37
|
-
default:
|
|
38
|
-
break;
|
|
39
|
-
}
|
|
40
|
-
return new Response('ok');
|
|
41
|
-
})
|
|
42
|
-
.handle(request.clone());
|
|
43
|
-
const bot2 = new TelegramBot(env.SECRET_TELEGRAM_API_TOKEN2);
|
|
44
|
-
await bot2
|
|
45
|
-
.on('default', async function () {
|
|
46
|
-
switch (bot2.update_type) {
|
|
47
|
-
case 'message': {
|
|
48
|
-
await bot2.reply('https://duckduckgo.com/?q=' + encodeURIComponent(bot.update.message?.text?.toString() ?? ''));
|
|
49
|
-
break;
|
|
50
|
-
}
|
|
51
|
-
case 'inline': {
|
|
52
|
-
await bot2.reply('https://duckduckgo.com/?q=' + encodeURIComponent(bot.update.inline_query?.query ?? ''));
|
|
53
|
-
break;
|
|
54
|
-
}
|
|
55
|
-
default:
|
|
56
|
-
break;
|
|
57
|
-
}
|
|
58
|
-
return new Response('ok');
|
|
59
|
-
})
|
|
60
|
-
.handle(request.clone());
|
|
61
|
-
const bot3 = new TelegramBot(env.SECRET_TELEGRAM_API_TOKEN3);
|
|
62
|
-
await bot3
|
|
63
|
-
.on('default', async function () {
|
|
64
|
-
switch (bot3.update_type) {
|
|
65
|
-
case 'inline': {
|
|
66
|
-
const translated_text = await fetch('https://clients5.google.com/translate_a/t?client=at&sl=auto&tl=en&q=' +
|
|
67
|
-
encodeURIComponent(bot3.update.inline_query?.query.toString() ?? ''))
|
|
68
|
-
.then((r) => r.json())
|
|
69
|
-
.then((json) => json[0].slice(0, -1).join(' '));
|
|
70
|
-
await bot3.reply(translated_text ?? '');
|
|
71
|
-
break;
|
|
72
|
-
}
|
|
73
|
-
default:
|
|
74
|
-
break;
|
|
75
|
-
}
|
|
76
|
-
return new Response('ok');
|
|
77
|
-
})
|
|
78
|
-
.handle(request.clone());
|
|
79
|
-
return new Response('ok');
|
|
80
|
-
},
|
|
81
|
-
};
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
/package/dist/{main/src/types/TelegramInputMessageContent.js → types/TelegramInlineQueryType.js}
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|