gramio 0.0.1 → 0.0.2
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 +0 -0
- package/dist/updates.js +75 -0
- package/package.json +15 -13
package/README.md
CHANGED
|
File without changes
|
package/dist/updates.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Updates = void 0;
|
|
4
|
+
const contexts_1 = require("@gramio/contexts");
|
|
5
|
+
const middleware_io_1 = require("middleware-io");
|
|
6
|
+
class Updates {
|
|
7
|
+
bot;
|
|
8
|
+
isStarted = false;
|
|
9
|
+
offset = 0;
|
|
10
|
+
composer = middleware_io_1.Composer.builder();
|
|
11
|
+
constructor(bot) {
|
|
12
|
+
this.bot = bot;
|
|
13
|
+
}
|
|
14
|
+
on(updateName, handler) {
|
|
15
|
+
return this.use((context, next) => {
|
|
16
|
+
//TODO: fix typings
|
|
17
|
+
if (context.is(updateName))
|
|
18
|
+
handler(context, next);
|
|
19
|
+
else
|
|
20
|
+
next();
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
use(handler) {
|
|
24
|
+
this.composer.use(handler);
|
|
25
|
+
return this;
|
|
26
|
+
}
|
|
27
|
+
async handleUpdate(data) {
|
|
28
|
+
const updateType = Object.keys(data).at(1);
|
|
29
|
+
this.offset = data.update_id + 1;
|
|
30
|
+
try {
|
|
31
|
+
const context = new contexts_1.contextsMappings[updateType]({
|
|
32
|
+
bot: this.bot,
|
|
33
|
+
update: data,
|
|
34
|
+
//TODO: fix
|
|
35
|
+
//@ts-ignore
|
|
36
|
+
payload: data[updateType],
|
|
37
|
+
type: updateType,
|
|
38
|
+
updateId: data.update_id,
|
|
39
|
+
// raw: {
|
|
40
|
+
// update: data,
|
|
41
|
+
// updateId: data.update_id,
|
|
42
|
+
// updateType,
|
|
43
|
+
// },
|
|
44
|
+
});
|
|
45
|
+
this.composer.compose()(
|
|
46
|
+
//TODO: fix typings
|
|
47
|
+
context, middleware_io_1.noopNext);
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
throw new Error(`Update type ${updateType} not supported.`);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
async startPolling() {
|
|
54
|
+
if (this.isStarted)
|
|
55
|
+
throw new Error("[UPDATES] Polling already started!");
|
|
56
|
+
this.isStarted = true;
|
|
57
|
+
this.startFetchLoop();
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
async startFetchLoop() {
|
|
61
|
+
while (this.isStarted) {
|
|
62
|
+
const updates = await this.bot.api.getUpdates({
|
|
63
|
+
offset: this.offset,
|
|
64
|
+
});
|
|
65
|
+
for await (const update of updates) {
|
|
66
|
+
//TODO: update errors
|
|
67
|
+
await this.handleUpdate(update).catch(console.error);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
stopPolling() {
|
|
72
|
+
this.isStarted = false;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
exports.Updates = Updates;
|
package/package.json
CHANGED
|
@@ -1,27 +1,29 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "gramio",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"description": "",
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "WIP",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
6
7
|
"scripts": {
|
|
7
|
-
"
|
|
8
|
-
"lint": "
|
|
9
|
-
"lint:fix": "eslint \"src/**/*.{js,ts}\" --fix"
|
|
8
|
+
"lint": "bun check ./src",
|
|
9
|
+
"lint:fix": "bun lint --apply"
|
|
10
10
|
},
|
|
11
11
|
"author": "kravets",
|
|
12
12
|
"license": "ISC",
|
|
13
13
|
"devDependencies": {
|
|
14
|
-
"@
|
|
15
|
-
"@types
|
|
16
|
-
"
|
|
17
|
-
"eslint-kit": "^10.6.0",
|
|
18
|
-
"prettier": "^3.0.3"
|
|
14
|
+
"@biomejs/biome": "1.5.3",
|
|
15
|
+
"@gramio/types": "^7.0.14",
|
|
16
|
+
"@types/node": "^20.11.17"
|
|
19
17
|
},
|
|
20
18
|
"dependencies": {
|
|
19
|
+
"@gramio/contexts": "^0.0.1",
|
|
20
|
+
"form-data-encoder": "^4.0.2",
|
|
21
21
|
"inspectable": "^2.1.0",
|
|
22
22
|
"middleware-io": "^2.8.1",
|
|
23
|
-
"reflect-metadata": "^0.1
|
|
24
|
-
"undici": "^
|
|
23
|
+
"reflect-metadata": "^0.2.1",
|
|
24
|
+
"undici": "^6.6.2"
|
|
25
25
|
},
|
|
26
|
-
"files": [
|
|
26
|
+
"files": [
|
|
27
|
+
"dist"
|
|
28
|
+
]
|
|
27
29
|
}
|