@codebam/cf-workers-telegram-bot 7.9.0 → 7.10.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/telegram_bot.d.ts +1 -2
- package/dist/telegram_bot.js +42 -42
- package/package.json +3 -2
package/dist/telegram_bot.d.ts
CHANGED
|
@@ -4,10 +4,9 @@ import TelegramExecutionContext from './ctx';
|
|
|
4
4
|
import Webhook from './webhook';
|
|
5
5
|
export default class TelegramBot {
|
|
6
6
|
token: string;
|
|
7
|
-
webhook: Webhook;
|
|
8
7
|
api: URL;
|
|
8
|
+
webhook: Webhook;
|
|
9
9
|
update: TelegramUpdate;
|
|
10
|
-
update_type: string;
|
|
11
10
|
commands: Record<string, (ctx: TelegramExecutionContext) => Promise<Response>>;
|
|
12
11
|
currentContext: TelegramExecutionContext;
|
|
13
12
|
constructor(token: string);
|
package/dist/telegram_bot.js
CHANGED
|
@@ -3,70 +3,70 @@ import TelegramExecutionContext from './ctx';
|
|
|
3
3
|
import Webhook from './webhook';
|
|
4
4
|
export default class TelegramBot {
|
|
5
5
|
token;
|
|
6
|
-
webhook;
|
|
7
6
|
api;
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
webhook = new Webhook('', new Request('http://127.0.0.1'));
|
|
8
|
+
update = new TelegramUpdate({});
|
|
10
9
|
commands = {};
|
|
11
10
|
currentContext;
|
|
12
11
|
constructor(token) {
|
|
13
12
|
this.token = token;
|
|
14
|
-
this.webhook = new Webhook('', new Request('http://127.0.0.1'));
|
|
15
13
|
this.api = new URL('https://api.telegram.org/bot' + token);
|
|
16
|
-
this.update = new TelegramUpdate({});
|
|
17
|
-
this.update_type = '';
|
|
18
14
|
}
|
|
19
15
|
on(event, callback) {
|
|
20
|
-
if (
|
|
16
|
+
if (['on', 'handle'].indexOf(event) === -1) {
|
|
21
17
|
this.commands[event] = callback;
|
|
22
18
|
}
|
|
23
19
|
return this;
|
|
24
20
|
}
|
|
25
21
|
async handle(request) {
|
|
26
22
|
this.webhook = new Webhook(this.token, request);
|
|
27
|
-
if (request.method === 'POST') {
|
|
28
|
-
this.update = await request.json();
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
this.update = new TelegramUpdate({});
|
|
32
|
-
}
|
|
33
23
|
const url = new URL(request.url);
|
|
34
24
|
if (`/${this.token}` === url.pathname) {
|
|
35
|
-
switch (
|
|
36
|
-
case '
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
25
|
+
switch (request.method) {
|
|
26
|
+
case 'POST': {
|
|
27
|
+
this.update = await request.json();
|
|
28
|
+
console.log(this.update);
|
|
29
|
+
let command = 'default';
|
|
30
|
+
let args = [];
|
|
31
|
+
const ctx = new TelegramExecutionContext(this, this.update);
|
|
32
|
+
this.currentContext = ctx;
|
|
33
|
+
switch (ctx.update_type) {
|
|
34
|
+
case 'message': {
|
|
35
|
+
args = this.update.message?.text?.split(' ') ?? [];
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
case 'inline': {
|
|
39
|
+
args = this.update.inline_query?.query.split(' ') ?? [];
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
case 'photo': {
|
|
43
|
+
command = ':photo';
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
default:
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
if (args.at(0)?.startsWith('/')) {
|
|
50
|
+
command = args.at(0)?.slice(1) ?? 'default';
|
|
51
|
+
}
|
|
52
|
+
this.commands['any']?.(ctx);
|
|
53
|
+
if (!this.commands[command]) {
|
|
54
|
+
command = 'default';
|
|
55
|
+
}
|
|
56
|
+
return await this.commands[command]?.(ctx);
|
|
54
57
|
}
|
|
55
|
-
case '
|
|
56
|
-
|
|
58
|
+
case 'GET': {
|
|
59
|
+
switch (url.searchParams.get('command')) {
|
|
60
|
+
case 'set':
|
|
61
|
+
return this.webhook.set();
|
|
62
|
+
default:
|
|
63
|
+
break;
|
|
64
|
+
}
|
|
57
65
|
break;
|
|
58
66
|
}
|
|
59
67
|
default:
|
|
60
68
|
break;
|
|
61
69
|
}
|
|
62
|
-
if (args.at(0)?.startsWith('/')) {
|
|
63
|
-
command = args.at(0)?.slice(1) ?? 'default';
|
|
64
|
-
}
|
|
65
|
-
this.commands['any']?.(ctx);
|
|
66
|
-
if (!this.commands[command]) {
|
|
67
|
-
command = 'default';
|
|
68
|
-
}
|
|
69
|
-
return await this.commands[command]?.(ctx);
|
|
70
70
|
}
|
|
71
71
|
return new Response('ok');
|
|
72
72
|
}
|
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@codebam/cf-workers-telegram-bot",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.10.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",
|
|
7
|
+
"types": "./dist/main/src/types.js",
|
|
7
8
|
"files": [
|
|
8
9
|
"dist",
|
|
9
10
|
"LICENSE",
|
|
@@ -43,5 +44,5 @@
|
|
|
43
44
|
"typescript-eslint": "^7.8.0",
|
|
44
45
|
"vitest": "^1.6.0"
|
|
45
46
|
},
|
|
46
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "b3cd6c5001d4d82bd8affb32d4bc3da41e64185a"
|
|
47
48
|
}
|