@codebam/cf-workers-telegram-bot 6.5.0 → 7.3.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/README.md +9 -23
- package/dist/main/src/main.d.ts +1 -8
- package/dist/main/src/main.js +1 -8
- package/dist/main/src/telegram_bot.d.ts +18 -32
- package/dist/main/src/telegram_bot.js +112 -80
- package/dist/main/src/types.d.ts +1 -5
- package/dist/main/src/types.js +1 -3
- package/dist/worker/src/worker.js +79 -92
- package/package.json +2 -2
- package/dist/main/src/bot_api.d.ts +0 -10
- package/dist/main/src/bot_api.js +0 -11
- package/dist/main/src/commands/ban.d.ts +0 -5
- package/dist/main/src/commands/ban.js +0 -8
- package/dist/main/src/commands/bored.d.ts +0 -5
- package/dist/main/src/commands/bored.js +0 -7
- package/dist/main/src/commands/clear.d.ts +0 -5
- package/dist/main/src/commands/clear.js +0 -14
- package/dist/main/src/commands/code.d.ts +0 -5
- package/dist/main/src/commands/code.js +0 -4
- package/dist/main/src/commands/commandlist.d.ts +0 -5
- package/dist/main/src/commands/commandlist.js +0 -1
- package/dist/main/src/commands/dog.d.ts +0 -5
- package/dist/main/src/commands/dog.js +0 -7
- package/dist/main/src/commands/duckduckgo.d.ts +0 -5
- package/dist/main/src/commands/duckduckgo.js +0 -42
- package/dist/main/src/commands/epoch.d.ts +0 -5
- package/dist/main/src/commands/epoch.js +0 -4
- package/dist/main/src/commands/getchatinfo.d.ts +0 -5
- package/dist/main/src/commands/getchatinfo.js +0 -1
- package/dist/main/src/commands/image.d.ts +0 -5
- package/dist/main/src/commands/image.js +0 -19
- package/dist/main/src/commands/joke.d.ts +0 -5
- package/dist/main/src/commands/joke.js +0 -7
- package/dist/main/src/commands/kanye.d.ts +0 -5
- package/dist/main/src/commands/kanye.js +0 -8
- package/dist/main/src/commands/mute.d.ts +0 -5
- package/dist/main/src/commands/mute.js +0 -8
- package/dist/main/src/commands/ping.d.ts +0 -5
- package/dist/main/src/commands/ping.js +0 -1
- package/dist/main/src/commands/question.d.ts +0 -5
- package/dist/main/src/commands/question.js +0 -74
- package/dist/main/src/commands/roll.d.ts +0 -5
- package/dist/main/src/commands/roll.js +0 -6
- package/dist/main/src/commands/sean.d.ts +0 -5
- package/dist/main/src/commands/sean.js +0 -76
- package/dist/main/src/commands/start.d.ts +0 -5
- package/dist/main/src/commands/start.js +0 -1
- package/dist/main/src/commands/toss.d.ts +0 -5
- package/dist/main/src/commands/toss.js +0 -1
- package/dist/main/src/commands/translate.d.ts +0 -5
- package/dist/main/src/commands/translate.js +0 -26
- package/dist/main/src/handler.d.ts +0 -12
- package/dist/main/src/handler.js +0 -40
- package/dist/main/src/libs.d.ts +0 -5
- package/dist/main/src/libs.js +0 -7
- package/dist/main/src/telegram_api.d.ts +0 -34
- package/dist/main/src/telegram_api.js +0 -192
- package/dist/main/src/telegram_commands.d.ts +0 -22
- package/dist/main/src/telegram_commands.js +0 -21
- package/dist/main/src/telegram_webhook.d.ts +0 -10
- package/dist/main/src/telegram_webhook.js +0 -20
- package/dist/main/src/types/Command.d.ts +0 -5
- package/dist/main/src/types/Command.js +0 -1
- package/dist/main/src/types/Commands.d.ts +0 -3
- package/dist/main/src/types/Commands.js +0 -1
- package/dist/main/src/types/Config.d.ts +0 -20
- package/dist/main/src/types/Config.js +0 -33
- package/dist/main/src/webhook.d.ts +0 -10
- package/dist/main/src/webhook.js +0 -15
package/README.md
CHANGED
|
@@ -6,15 +6,16 @@
|
|
|
6
6
|
|
|
7
7
|
serverless telegram bot on cf workers
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
My modifications are licensed under the Apache license.
|
|
9
|
+
To get continuous conversation with AI working make sure you add a database
|
|
10
|
+
to your wrangler.toml and initailize it with the schema.sql
|
|
12
11
|
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
```sh
|
|
13
|
+
npm i @codebam/cf-workers-telegram-bot
|
|
14
|
+
```
|
|
15
15
|
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
See `worker.ts` and follow the instructions below.
|
|
17
|
+
|
|
18
|
+
---
|
|
18
19
|
|
|
19
20
|
To use the deploy button:
|
|
20
21
|
|
|
@@ -45,19 +46,4 @@ To fork this repo and use wrangler:
|
|
|
45
46
|
|
|
46
47
|
## Getting started with cf-workers-telegram-bot
|
|
47
48
|
|
|
48
|
-
Once you've deployed the bot you can get your Webhook command URL by
|
|
49
|
-
of the following.
|
|
50
|
-
|
|
51
|
-
- sha256sum(SECRET_TELEGRAM_API_TOKEN) is the path to your webhook commands and
|
|
52
|
-
should be put at the end of your worker URL to access commands such as
|
|
53
|
-
setting your webhook
|
|
54
|
-
- Use `echo -n yoursecretkey | sha256sum` to get the path
|
|
55
|
-
- Open the Cloudflare Worker Logs under **Workers > cf-workers-telegram-bot
|
|
56
|
-
> Logs > Begin log stream** and make a GET request (open it in your browser)
|
|
57
|
-
to your Worker URL and look at the logs to see your Access URL
|
|
58
|
-
- Run `wrangler tail --format pretty` from inside your git repository and make
|
|
59
|
-
a GET request to your Worker URL
|
|
60
|
-
|
|
61
|
-
Example URL for setting the Webhook and dropping pending updates:
|
|
62
|
-
|
|
63
|
-
`https://cf-workers-telegram-bot.codebam.workers.dev/a948904f2f0f479b8f8197694b30184b0d2ed1c1cd2a1ec0fb85d299a192a447?command=set`
|
|
49
|
+
Once you've deployed the bot you can get your Webhook command URL by calling `await bot.webhook.set()`
|
package/dist/main/src/main.d.ts
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
import BotApi from './bot_api';
|
|
2
|
-
import Handler from './handler';
|
|
3
|
-
import * as Libs from './libs';
|
|
4
|
-
import TelegramApi from './telegram_api';
|
|
5
1
|
import TelegramBot from './telegram_bot';
|
|
6
|
-
import TelegramCommands from './telegram_commands';
|
|
7
|
-
import TelegramWebhook from './telegram_webhook';
|
|
8
2
|
import * as Types from './types';
|
|
9
|
-
import Webhook from './webhook';
|
|
10
3
|
import worker from '../../worker/src/worker';
|
|
11
|
-
export {
|
|
4
|
+
export { TelegramBot, Types, worker };
|
package/dist/main/src/main.js
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
import BotApi from './bot_api';
|
|
2
|
-
import Handler from './handler';
|
|
3
|
-
import * as Libs from './libs';
|
|
4
|
-
import TelegramApi from './telegram_api';
|
|
5
1
|
import TelegramBot from './telegram_bot';
|
|
6
|
-
import TelegramCommands from './telegram_commands';
|
|
7
|
-
import TelegramWebhook from './telegram_webhook';
|
|
8
2
|
import * as Types from './types';
|
|
9
|
-
import Webhook from './webhook';
|
|
10
3
|
import worker from '../../worker/src/worker';
|
|
11
|
-
export {
|
|
4
|
+
export { TelegramBot, Types, worker };
|
|
@@ -1,34 +1,20 @@
|
|
|
1
1
|
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
joke: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
14
|
-
dog: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
15
|
-
bored: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
16
|
-
epoch: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
17
|
-
roll: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
18
|
-
commandList: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
19
|
-
toss: (self: TelegramBot, update: TelegramUpdate) => Promise<Response>;
|
|
20
|
-
ping: (self: TelegramBot, update: TelegramUpdate, args: string[]) => Promise<Response>;
|
|
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>;
|
|
25
|
-
url: URL;
|
|
26
|
-
kv: Kv;
|
|
27
|
-
get_set: KVNamespace;
|
|
28
|
-
ai: Ai;
|
|
29
|
-
db: D1Database;
|
|
30
|
-
r2: R2Bucket;
|
|
31
|
-
bot_name: string;
|
|
32
|
-
chat_model: string;
|
|
33
|
-
constructor(config: Config);
|
|
2
|
+
import { TelegramUpdate } from './types';
|
|
3
|
+
export default class TelegramBot {
|
|
4
|
+
token: string;
|
|
5
|
+
webhook: Webhook;
|
|
6
|
+
update: TelegramUpdate;
|
|
7
|
+
api: URL;
|
|
8
|
+
update_type: string;
|
|
9
|
+
constructor(token: string);
|
|
10
|
+
on(event: string, callback: () => Promise<Response>): this;
|
|
11
|
+
handle(request: Request): Promise<any>;
|
|
12
|
+
reply(message: string): Promise<void>;
|
|
34
13
|
}
|
|
14
|
+
declare class Webhook {
|
|
15
|
+
api: URL;
|
|
16
|
+
webhook: URL;
|
|
17
|
+
constructor(token: string, request: Request);
|
|
18
|
+
set(): Promise<Response>;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
@@ -1,82 +1,114 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
this.
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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
|
+
// eslint-disable-next-line
|
|
17
|
+
// @ts-ignore TS7053
|
|
18
|
+
this[event] = callback;
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
async handle(request) {
|
|
22
|
+
this.webhook = new Webhook(this.token, request);
|
|
23
|
+
if (request.method === 'POST') {
|
|
24
|
+
this.update = await request.json();
|
|
25
|
+
}
|
|
26
|
+
const url = new URL(request.url);
|
|
27
|
+
if (`/${this.token}` === url.pathname) {
|
|
28
|
+
switch (url.searchParams.get('command')) {
|
|
29
|
+
case 'set':
|
|
30
|
+
return this.webhook.set();
|
|
31
|
+
default:
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
console.log(this.update);
|
|
35
|
+
if (this.update.message?.text) {
|
|
36
|
+
this.update_type = 'message';
|
|
37
|
+
}
|
|
38
|
+
else if (this.update.inline_query?.query) {
|
|
39
|
+
this.update_type = 'inline';
|
|
40
|
+
}
|
|
41
|
+
let command = 'default';
|
|
42
|
+
let args = [];
|
|
43
|
+
switch (this.update_type) {
|
|
44
|
+
case 'message': {
|
|
45
|
+
// @ts-expect-error already checked above
|
|
46
|
+
args = this.update.message.text.split(' ');
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
case 'inline': {
|
|
50
|
+
// @ts-expect-error already checked above
|
|
51
|
+
args = this.update.inline_query.query.split(' ');
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
default:
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
if (args.at(0)?.startsWith('/')) {
|
|
58
|
+
// @ts-expect-error already checked above
|
|
59
|
+
command = args.at(0).slice(1);
|
|
60
|
+
}
|
|
61
|
+
// eslint-disable-next-line
|
|
62
|
+
// @ts-ignore
|
|
63
|
+
if (!this[command]) {
|
|
64
|
+
command = 'default';
|
|
65
|
+
}
|
|
66
|
+
// eslint-disable-next-line
|
|
67
|
+
// @ts-ignore
|
|
68
|
+
return this[command]?.();
|
|
69
|
+
}
|
|
70
|
+
return new Response('ok');
|
|
71
|
+
}
|
|
72
|
+
async reply(message) {
|
|
73
|
+
switch (this.update_type) {
|
|
74
|
+
case 'message': {
|
|
75
|
+
const request = new URL(this.api + '/sendMessage');
|
|
76
|
+
const params = new URLSearchParams();
|
|
77
|
+
params.append('chat_id', this.update.message?.chat.id.toString() ?? '');
|
|
78
|
+
params.append('reply_to_message_id', this.update.message?.message_id.toString() ?? '');
|
|
79
|
+
params.append('text', message);
|
|
80
|
+
console.log(`${request}?${params}`);
|
|
81
|
+
await fetch(`${request}?${params}`);
|
|
82
|
+
break;
|
|
83
|
+
}
|
|
84
|
+
case 'inline': {
|
|
85
|
+
const inline_request = new URL(this.api + '/answerInlineQuery');
|
|
86
|
+
const inline_params = new URLSearchParams();
|
|
87
|
+
inline_params.append('inline_query_id', this.update.inline_query?.id.toString() ?? '');
|
|
88
|
+
inline_params.append('results', JSON.stringify([new TelegramInlineQueryResultArticle(message)]));
|
|
89
|
+
console.log(`${inline_request}?${inline_params}`);
|
|
90
|
+
await fetch(`${inline_request}?${inline_params}`);
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
default:
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
class Webhook {
|
|
99
|
+
api;
|
|
100
|
+
webhook;
|
|
101
|
+
constructor(token, request) {
|
|
102
|
+
this.api = new URL('https://api.telegram.org/bot' + token);
|
|
103
|
+
this.webhook = new URL(new URL(request.url).origin + `/${token}`);
|
|
104
|
+
}
|
|
105
|
+
async set() {
|
|
106
|
+
const url = new URL(`${this.api.origin}${this.api.pathname}/setWebhook`);
|
|
107
|
+
const params = url.searchParams;
|
|
108
|
+
params.append('url', this.webhook.toString());
|
|
109
|
+
params.append('max_connections', '100');
|
|
110
|
+
params.append('allowed_updates', JSON.stringify(['message', 'inline_query']));
|
|
111
|
+
params.append('drop_pending_updates', 'true');
|
|
112
|
+
return await fetch(`${url}?${params}`);
|
|
81
113
|
}
|
|
82
114
|
}
|
package/dist/main/src/types.d.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
import Webhook from './webhook';
|
|
2
|
-
import Command from './types/Command';
|
|
3
1
|
import TelegramCommand from './types/TelegramCommand';
|
|
4
|
-
import Commands from './types/Commands';
|
|
5
2
|
import Kv from './types/Kv';
|
|
6
|
-
import Config from './types/Config';
|
|
7
3
|
import localhost from './types/localhost';
|
|
8
4
|
import WebhookCommands from './types/WebhookCommands';
|
|
9
5
|
import Joke from './types/Joke';
|
|
@@ -27,4 +23,4 @@ import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultA
|
|
|
27
23
|
import DDGQueryResponse from './types/DDGQueryResponse';
|
|
28
24
|
import ChatPermissions from './types/ChatPermissions';
|
|
29
25
|
import Kanye from './types/Kanye';
|
|
30
|
-
export {
|
|
26
|
+
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, };
|
package/dist/main/src/types.js
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import Webhook from './webhook';
|
|
2
|
-
import Config from './types/Config';
|
|
3
1
|
import localhost from './types/localhost';
|
|
4
2
|
import WebhookCommands from './types/WebhookCommands';
|
|
5
3
|
import Update from './types/Update';
|
|
@@ -7,4 +5,4 @@ import TelegramUpdate from './types/TelegramUpdate';
|
|
|
7
5
|
import TelegramInlineQueryResult from './types/TelegramInlineQueryResult';
|
|
8
6
|
import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPhoto';
|
|
9
7
|
import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultArticle';
|
|
10
|
-
export {
|
|
8
|
+
export { localhost, WebhookCommands, Update, TelegramUpdate, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, };
|
|
@@ -1,94 +1,81 @@
|
|
|
1
|
-
import
|
|
1
|
+
import TelegramBot from '../../main/src/telegram_bot';
|
|
2
2
|
export default {
|
|
3
|
-
fetch: async (request, env) =>
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
'
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
'
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
'
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
},
|
|
82
|
-
{
|
|
83
|
-
bot_name: '@TranslatePartyBot',
|
|
84
|
-
api: TelegramBot,
|
|
85
|
-
webhook: new TelegramWebhook(new URL(`https://api.telegram.org/bot${env.SECRET_TELEGRAM_API_TOKEN5}`), env.SECRET_TELEGRAM_API_TOKEN5, new URL(new URL(request.url).origin)),
|
|
86
|
-
commands: {
|
|
87
|
-
default: TelegramCommands.translate,
|
|
88
|
-
inline: TelegramCommands.translate,
|
|
89
|
-
'/start': TelegramCommands.commandList,
|
|
90
|
-
},
|
|
91
|
-
ai: env.AI,
|
|
92
|
-
},
|
|
93
|
-
]).handle(request),
|
|
3
|
+
fetch: async (request, env) => {
|
|
4
|
+
const bot = new TelegramBot(env.SECRET_TELEGRAM_API_TOKEN);
|
|
5
|
+
await bot
|
|
6
|
+
.on('default', async function () {
|
|
7
|
+
switch (bot.update_type) {
|
|
8
|
+
case 'message': {
|
|
9
|
+
const messages = [
|
|
10
|
+
{ role: 'system', content: 'You are a friendly assistant' },
|
|
11
|
+
{
|
|
12
|
+
role: 'user',
|
|
13
|
+
content: bot.update.message?.text?.toString() ?? '',
|
|
14
|
+
},
|
|
15
|
+
];
|
|
16
|
+
const response = await env.AI.run('@cf/meta/llama-3-8b-instruct', { messages });
|
|
17
|
+
if ('response' in response) {
|
|
18
|
+
await bot.reply(response.response ?? '');
|
|
19
|
+
}
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
case 'inline': {
|
|
23
|
+
const inline_messages = [
|
|
24
|
+
{ role: 'system', content: 'You are a friendly assistant' },
|
|
25
|
+
{
|
|
26
|
+
role: 'user',
|
|
27
|
+
content: bot.update.inline_query?.query.toString() ?? '',
|
|
28
|
+
},
|
|
29
|
+
];
|
|
30
|
+
const inline_response = await env.AI.run('@cf/meta/llama-3-8b-instruct', { messages: inline_messages, max_tokens: 50 });
|
|
31
|
+
if ('response' in inline_response) {
|
|
32
|
+
await bot.reply(inline_response.response ?? '');
|
|
33
|
+
}
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
default:
|
|
37
|
+
break;
|
|
38
|
+
}
|
|
39
|
+
return new Response('ok');
|
|
40
|
+
})
|
|
41
|
+
.handle(request.clone());
|
|
42
|
+
const bot2 = new TelegramBot(env.SECRET_TELEGRAM_API_TOKEN2);
|
|
43
|
+
await bot2
|
|
44
|
+
.on('default', async function () {
|
|
45
|
+
switch (bot2.update_type) {
|
|
46
|
+
case 'message': {
|
|
47
|
+
await bot2.reply('https://duckduckgo.com/?q=' + encodeURIComponent(bot.update.message?.text?.toString() ?? ''));
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
case 'inline': {
|
|
51
|
+
await bot2.reply('https://duckduckgo.com/?q=' + encodeURIComponent(bot.update.inline_query?.query ?? ''));
|
|
52
|
+
break;
|
|
53
|
+
}
|
|
54
|
+
default:
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
return new Response('ok');
|
|
58
|
+
})
|
|
59
|
+
.handle(request.clone());
|
|
60
|
+
const bot3 = new TelegramBot(env.SECRET_TELEGRAM_API_TOKEN3);
|
|
61
|
+
await bot3
|
|
62
|
+
.on('default', async function () {
|
|
63
|
+
switch (bot3.update_type) {
|
|
64
|
+
case 'inline': {
|
|
65
|
+
const { translated_text } = await env.AI.run('@cf/meta/m2m100-1.2b', {
|
|
66
|
+
text: bot3.update.inline_query?.query.toString() ?? '',
|
|
67
|
+
source_lang: 'french',
|
|
68
|
+
target_lang: 'english',
|
|
69
|
+
});
|
|
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
|
+
},
|
|
94
81
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codebam/cf-workers-telegram-bot",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.3.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": "55015e53958e69fc128e0b1e8116abb249291b0f"
|
|
45
45
|
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
/// <reference types="@cloudflare/workers-types" />
|
|
2
|
-
import Handler from './handler';
|
|
3
|
-
import { Commands, Config, Update } from './types';
|
|
4
|
-
export default class BotApi {
|
|
5
|
-
commands: Commands;
|
|
6
|
-
webhook: Config['webhook'];
|
|
7
|
-
handler: Handler;
|
|
8
|
-
update: (update: Update) => Promise<Response>;
|
|
9
|
-
constructor(config: Partial<Config>);
|
|
10
|
-
}
|
package/dist/main/src/bot_api.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
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 +0,0 @@
|
|
|
1
|
-
import { TelegramInlineQueryResultArticle } from '../types';
|
|
2
|
-
export default async (self, update) => fetch('https://boredapi.com/api/activity/')
|
|
3
|
-
.then((response) => response.json())
|
|
4
|
-
.then((json) => json)
|
|
5
|
-
.then((bored_response) => update.inline_query
|
|
6
|
-
? self.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle(bored_response.activity)], 0)
|
|
7
|
-
: self.sendMessage(update.message?.chat.id ?? 0, bored_response.activity));
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { TelegramInlineQueryResultArticle } from '../types';
|
|
2
|
-
export default async (self, update) => {
|
|
3
|
-
const { success } = await self.db
|
|
4
|
-
.prepare('DELETE FROM Messages WHERE userId=?')
|
|
5
|
-
.bind(update.inline_query ? update.inline_query.from.id : update.message?.from.id)
|
|
6
|
-
.run();
|
|
7
|
-
if (success) {
|
|
8
|
-
if (update.inline_query) {
|
|
9
|
-
return self.answerInlineQuery(update.inline_query.id, [new TelegramInlineQueryResultArticle('_')]);
|
|
10
|
-
}
|
|
11
|
-
return self.sendMessage(update.message?.chat.id ?? 0, '_');
|
|
12
|
-
}
|
|
13
|
-
return self.sendMessage(update.message?.chat.id ?? 0, 'failed');
|
|
14
|
-
};
|