@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.
- package/README.md +37 -43
- 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 +114 -80
- package/dist/main/src/types.d.ts +1 -5
- package/dist/main/src/types.js +1 -3
- package/dist/worker/src/worker.js +78 -92
- package/package.json +3 -3
- 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
|
@@ -1,22 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
<h3 align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/codebam/cf-workers-telegram-bot/master/assets/logo.png" width="100" />
|
|
3
|
+
<br/>
|
|
4
|
+
CF workers telegram bot
|
|
5
|
+
<br/>
|
|
6
|
+
</h3>
|
|
7
|
+
|
|
8
|
+
<h6 align="center">
|
|
9
|
+
<a href="https://github.com/codebam/cf-workers-telegram-bot/wiki">Wiki</a>
|
|
10
|
+
·
|
|
11
|
+
<a href="https://codebam.github.io/cf-workers-telegram-bot-docs/">Docs</a>
|
|
12
|
+
</h6>
|
|
13
|
+
|
|
14
|
+
<p align="center">
|
|
15
|
+
<a href="https://github.com/codebam/cf-workers-telegram-bot/stargazers"> <img src="https://img.shields.io/github/stars/codebam/cf-workers-telegram-bot?style=for-the-badge&logo=starship&color=111111&logoColor=ffffff&labelColor=000000" alt="GitHub stars"/></a>
|
|
16
|
+
<a href="https://github.com/codebam/cf-workers-telegram-bot/issues">
|
|
17
|
+
<img src="https://img.shields.io/github/issues/codebam/cf-workers-telegram-bot?style=for-the-badge&logo=gitbook&color=111111&logoColor=ffffff&labelColor=000000" alt="GitHub issues"/></a>
|
|
18
|
+
<a href="https://github.com/codebam/cf-workers-telegram-bot"> <img src="https://img.shields.io/github/forks/codebam/cf-workers-telegram-bot?style=for-the-badge&logo=git&color=111111&logoColor=ffffff&labelColor=000000" alt="GitHub forks"/></a>
|
|
19
|
+
<a href="https://www.npmjs.com/package/@codebam/cf-workers-telegram-bot"> <img src="https://img.shields.io/npm/v/@codebam/cf-workers-telegram-bot?style=for-the-badge&logo=npm&color=111111&logoColor=ffffff&labelColor=000000" alt="npm version" /></a>
|
|
20
|
+
</p>
|
|
4
21
|
|
|
5
22
|

|
|
6
23
|
|
|
7
|
-
|
|
24
|
+
```sh
|
|
25
|
+
npm i @codebam/cf-workers-telegram-bot
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
See `worker.ts` for an example of what a bot might look like. Just import from `@codebam/cf-workers-telegram-bot`.
|
|
8
29
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
30
|
+
- `npm create cloudflare@latest`
|
|
31
|
+
- `npx wrangler login`
|
|
32
|
+
- `npx wrangler secret put SECRET_TELEGRAM_API_TOKEN`, set it to your telegram bot token that you got from `@BotFather`
|
|
33
|
+
- `npx wrangler deploy`
|
|
34
|
+
- Open this url in your browser to set your webhook `https://your-worker.username.workers.dev/SECRET_TELEGRAM_API_TOKEN?command=set`
|
|
12
35
|
|
|
13
|
-
|
|
14
|
-
messages with llama2 if a command isn't found.
|
|
36
|
+
To set up GitHub actions to deploy when you push, see https://github.com/cloudflare/wrangler-action
|
|
15
37
|
|
|
16
|
-
|
|
17
|
-
to your wrangler.toml and initailize it with the schema.sql
|
|
38
|
+
---
|
|
18
39
|
|
|
19
|
-
|
|
40
|
+
These instructions are for if you want to deploy a copy of the bot along with
|
|
41
|
+
the library. Such as if you need extra API requests that haven't been
|
|
42
|
+
implemented yet.
|
|
43
|
+
|
|
44
|
+
[](https://deploy.workers.cloudflare.com/?url=https://github.com/codebam/cf-workers-telegram-bot)
|
|
20
45
|
|
|
21
46
|
- Click the deploy button
|
|
22
47
|
- Navigate to your new **GitHub repository > Settings > Secrets** and add the following secrets:
|
|
@@ -30,34 +55,3 @@ To use the deploy button:
|
|
|
30
55
|
```
|
|
31
56
|
|
|
32
57
|
- Push to `master` to trigger a deploy
|
|
33
|
-
|
|
34
|
-
To fork this repo and use wrangler:
|
|
35
|
-
|
|
36
|
-
- Click fork
|
|
37
|
-
- `npm i -g wrangler`
|
|
38
|
-
- `wrangler secret put SECRET_TELEGRAM_API_TOKEN` and set it to your telegram
|
|
39
|
-
bot token
|
|
40
|
-
- `wrangler d1 create llama2`
|
|
41
|
-
- put the database block from the command in your wrangler.toml
|
|
42
|
-
- `wrangler d1 execute --remote llama2 --file ./schema.sql`
|
|
43
|
-
- `wrangler deploy`
|
|
44
|
-
- Done!
|
|
45
|
-
|
|
46
|
-
## Getting started with cf-workers-telegram-bot
|
|
47
|
-
|
|
48
|
-
Once you've deployed the bot you can get your Webhook command URL by doing any
|
|
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`
|
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,116 @@
|
|
|
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
|
-
|
|
74
|
-
|
|
75
|
-
this.
|
|
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
|
+
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}`);
|
|
81
115
|
}
|
|
82
116
|
}
|
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,80 @@
|
|
|
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
|
-
db: env.DB,
|
|
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 fetch('https://clients5.google.com/translate_a/t?client=at&sl=auto&tl=en&q=' +
|
|
66
|
+
encodeURIComponent(bot3.update.inline_query?.query.toString() ?? ''))
|
|
67
|
+
.then((r) => r.json())
|
|
68
|
+
.then((json) => json[0].slice(0, -1).join(' '));
|
|
69
|
+
await bot3.reply(translated_text ?? '');
|
|
70
|
+
break;
|
|
71
|
+
}
|
|
72
|
+
default:
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
return new Response('ok');
|
|
76
|
+
})
|
|
77
|
+
.handle(request.clone());
|
|
78
|
+
return new Response('ok');
|
|
79
|
+
},
|
|
94
80
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codebam/cf-workers-telegram-bot",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.4.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",
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
"@typescript-eslint/parser": "^7.8.0",
|
|
36
36
|
"eslint": "^9.2.0",
|
|
37
37
|
"eslint-config-prettier": "^9.1.0",
|
|
38
|
-
"globals": "^15.
|
|
38
|
+
"globals": "^15.2.0",
|
|
39
39
|
"lerna": "^8.1.2",
|
|
40
40
|
"prettier": "^3.2.5",
|
|
41
41
|
"typescript": "^5.4.5",
|
|
42
42
|
"typescript-eslint": "^7.8.0"
|
|
43
43
|
},
|
|
44
|
-
"gitHead": "
|
|
44
|
+
"gitHead": "5a413513b05d2cde80b76d4e901dbe334e95ed25"
|
|
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