Skip to content

Commit a938a00

Browse files
committed
simplified web worker launch process + adapted to next client version
1 parent 4a9a291 commit a938a00

File tree

22 files changed

+327
-338
lines changed

22 files changed

+327
-338
lines changed

docs/telegram-bot-playground/assets/index-BdFVq0_I.js

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/telegram-bot-playground/assets/index-D5yhjlgw.js

Lines changed: 0 additions & 26 deletions
This file was deleted.

docs/telegram-bot-playground/assets/web-worker-BcVFCjdL.js

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/telegram-bot-playground/assets/web-worker-DkORvJaQ.js

Lines changed: 0 additions & 22 deletions
This file was deleted.
Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { BotMessageHandlers } from "@effect-ak/tg-bot-client"
1+
import { BotResponse, defineBot } from "@effect-ak/tg-bot-client"
22

3-
export default {
3+
export default defineBot({
44
on_message: (msg) => {
55

66
const commandEntity = msg.entities?.find(_ => _.type == "bot_command");
@@ -9,27 +9,28 @@ export default {
99
console.log("command", command);
1010

1111
if (command == "/bye") {
12-
return {
12+
return BotResponse.make({
1313
type: "message",
1414
text: "See you later!"
15-
}
15+
});
1616
}
1717

1818
if (command == "/echo") {
19-
return {
19+
return BotResponse.make({
2020
type: "message",
2121
text: `<pre language="json">${JSON.stringify(msg, undefined, 2)}</pre>`,
2222
parse_mode: "HTML"
23-
}
23+
})
2424
}
2525

2626
if (msg.text) { // reply with "hey" on any text message
27-
return {
27+
return BotResponse.make({
2828
type: "message",
2929
text: "hey 😇"
30-
}
30+
})
3131
}
3232

33-
},
34-
} satisfies BotMessageHandlers
33+
return BotResponse.ignore
3534

35+
},
36+
})
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import type { BotMessageHandlers } from "@effect-ak/tg-bot-client"
1+
import { BotResponse, defineBot } from "@effect-ak/tg-bot-client"
22

3-
export default {
3+
export default defineBot({
44
on_message: (msg) => {
55

66
if (msg.text) {
7-
return {
7+
return BotResponse.make({
88
type: "message",
99
text: "hey 😀"
10-
}
10+
})
1111
}
1212

13+
return BotResponse.ignore;
14+
1315
},
14-
} satisfies BotMessageHandlers
16+
})
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
import type { BotMessageHandlers } from "@effect-ak/tg-bot-client"
1+
import { BotResponse, defineBot } from "@effect-ak/tg-bot-client"
22

3-
export default {
3+
export default defineBot({
44
on_message: (msg) => {
55
if (msg.text?.includes("+")) {
66
const numbers = msg.text.split("+");
77
let result = 0;
88
for (const num of numbers) {
99
result += parseInt(num);
1010
}
11-
return {
11+
return BotResponse.make({
1212
type: "document",
1313
caption: "sum result",
1414
document: {
1515
file_content: new TextEncoder().encode(`your sum is ${result}`),
1616
file_name: "hello.txt"
1717
}
18-
}
18+
})
1919
}
2020

2121
if (msg.text) { //reply on any text message
22-
return {
22+
return BotResponse.make({
2323
type: "message",
2424
text: "hey 🙃, send me a message in the format '3+3+3' and I will return you the sum of it in a text file"
25-
}
25+
})
2626
}
2727

28+
return BotResponse.ignore;
29+
2830
},
29-
} satisfies BotMessageHandlers
31+
})

docs/telegram-bot-playground/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<link rel="icon" type="image/png" sizes="32x32" href="./favicon.ico">
1010

1111

12-
<script type="module" crossorigin src="./assets/index-D5yhjlgw.js"></script>
12+
<script type="module" crossorigin src="./assets/index-BdFVq0_I.js"></script>
1313
<link rel="stylesheet" crossorigin href="./assets/index-BzQa5oof.css">
1414
</head>
1515

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"type": "module",
44
"dependencies": {
55
"@edge-runtime/vm": "^5.0.0",
6-
"@effect-ak/tg-bot-client": "file://Users/alex/projects/effect-ak-org/tg-bot-client/effect-ak-tg-bot-client-0.3.3.tgz",
6+
"@effect-ak/tg-bot-client": "^0.5.1",
77
"@monaco-editor/loader": "^1.4.0",
88
"@types/alpinejs": "^3.13.11",
99
"@types/node": "^22.10.5",

pnpm-lock.yaml

Lines changed: 156 additions & 154 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/common/editor/setup.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import type { Monaco } from "@monaco-editor/loader";
22
import { fetchText } from "#/common/utils";
3+
import { CDN_PACKAGE_DTS } from "#/tg-bot-playground/const";
34

45
export const setupDts = async (
56
monaco: Monaco
67
) => {
78

8-
const dts = await fetchText("https://cdn.jsdelivr.net/npm/@effect-ak/[email protected]/dist/index.d.ts");
9+
const dts = await fetchText(`https://cdn.jsdelivr.net/npm/${CDN_PACKAGE_DTS}`);
910

1011
monaco.languages.typescript.typescriptDefaults.setExtraLibs([
1112
{
@@ -23,4 +24,4 @@ export const setupDts = async (
2324
strict: true
2425
});
2526

26-
}
27+
}

src/common/editor/ts-text-model.ts

Lines changed: 16 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -26,41 +26,24 @@ export const makeTsTextModel =
2626
getJsCode: async () => {
2727
const output = await getTsCode();
2828
const code = output.outputFiles[0].text;
29-
const defaultExport = await getDefaultExport(code);
30-
return {
31-
defaultExport,
32-
serialized: serialize(defaultExport?.default)
33-
} as const;
29+
return code;
3430
}
3531
} as const
3632

3733
}
3834

39-
async function getDefaultExport<D>(code: string) {
40-
try {
41-
const encodedCode = encodeURIComponent(code);
42-
const module = await import(/* @vite-ignore */`data:text/javascript,${encodedCode}`);
43-
const result = module.default;
44-
return { default: result as D };
45-
} catch (e) {
46-
console.warn("get default error", e);
47-
return undefined;
48-
}
49-
50-
}
51-
52-
const serialize = (input: unknown) => {
53-
if (typeof input != "object" || !input) {
54-
return undefined;
55-
}
56-
const result = [] as [string, string][];
57-
58-
for (const [key, value] of Object.entries(input)) {
59-
if (typeof value != "function") {
60-
continue;
61-
}
62-
result.push([key, value.toString()])
63-
}
64-
65-
return JSON.stringify(Object.fromEntries(result));
66-
}
35+
// const serialize = (input: unknown) => {
36+
// if (typeof input != "object" || !input) {
37+
// return undefined;
38+
// }
39+
// const result = [] as [string, string][];
40+
41+
// for (const [key, value] of Object.entries(input)) {
42+
// if (typeof value != "function") {
43+
// continue;
44+
// }
45+
// result.push([key, value.toString()])
46+
// }
47+
48+
// return JSON.stringify(Object.fromEntries(result));
49+
// }

src/common/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { MonacoLoader, Alpine } from "./types";
22

33
export const fetchText = (path: string) =>
4-
fetch(path).then(_ => _.text())
4+
fetch(path).then(_ => _.text());
55

66
export const getMonacoLoader = () => {
77
if (!("monaco_loader" in window) || typeof window.monaco_loader != "object" || window.monaco_loader == null) {

src/tg-bot-playground/bot-launcher/run.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,10 @@ export const makeRunnableBot =
1010

1111
if (!state.bot.token || state.bot.token.length < 10) return;
1212

13-
if (!code.serialized) {
14-
console.warn("Serizalized js code not defined");
15-
return;
16-
}
17-
1813
worker.postMessage({
1914
command: 'run-bot',
2015
token: state.bot.token,
21-
code: code.serialized
16+
code
2217
});
2318

2419
state.bot.status = "active"

src/tg-bot-playground/const.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const CLIENT_VERSION = "0.5.1";
2+
export const CDN_PACKAGE_MAIN = `@effect-ak/tg-bot-client@${CLIENT_VERSION}/dist/index.min.mjs`;
3+
export const CDN_PACKAGE_DTS = `@effect-ak/tg-bot-client@${CLIENT_VERSION}/dist/index.d.ts`;

src/tg-bot-playground/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import Alpine from "alpinejs";
12
import { makeBotLauncher } from "#/tg-bot-playground/bot-launcher/_main";
23
import { makeEditor } from "#/common/editor/_editor";
34
import { fetchText } from "#/common/utils";
45
import type { BotState } from "./types";
5-
import Alpine from "alpinejs";
66

77
export type GlobalState = ReturnType<typeof makeGlobalState>;
88

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { BotMessageHandlers } from "@effect-ak/tg-bot-client"
1+
import { BotResponse, defineBot } from "@effect-ak/tg-bot-client"
22

3-
export default {
3+
export default defineBot({
44
on_message: (msg) => {
55

66
const commandEntity = msg.entities?.find(_ => _.type == "bot_command");
@@ -9,27 +9,28 @@ export default {
99
console.log("command", command);
1010

1111
if (command == "/bye") {
12-
return {
12+
return BotResponse.make({
1313
type: "message",
1414
text: "See you later!"
15-
}
15+
});
1616
}
1717

1818
if (command == "/echo") {
19-
return {
19+
return BotResponse.make({
2020
type: "message",
2121
text: `<pre language="json">${JSON.stringify(msg, undefined, 2)}</pre>`,
2222
parse_mode: "HTML"
23-
}
23+
})
2424
}
2525

2626
if (msg.text) { // reply with "hey" on any text message
27-
return {
27+
return BotResponse.make({
2828
type: "message",
2929
text: "hey 😇"
30-
}
30+
})
3131
}
3232

33-
},
34-
} satisfies BotMessageHandlers
33+
return BotResponse.ignore
3534

35+
},
36+
})
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import type { BotMessageHandlers } from "@effect-ak/tg-bot-client"
1+
import { BotResponse, defineBot } from "@effect-ak/tg-bot-client"
22

3-
export default {
3+
export default defineBot({
44
on_message: (msg) => {
55

66
if (msg.text) {
7-
return {
7+
return BotResponse.make({
88
type: "message",
99
text: "hey 😀"
10-
}
10+
})
1111
}
1212

13+
return BotResponse.ignore;
14+
1315
},
14-
} satisfies BotMessageHandlers
16+
})
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
1-
import type { BotMessageHandlers } from "@effect-ak/tg-bot-client"
1+
import { BotResponse, defineBot } from "@effect-ak/tg-bot-client"
22

3-
export default {
3+
export default defineBot({
44
on_message: (msg) => {
55
if (msg.text?.includes("+")) {
66
const numbers = msg.text.split("+");
77
let result = 0;
88
for (const num of numbers) {
99
result += parseInt(num);
1010
}
11-
return {
11+
return BotResponse.make({
1212
type: "document",
1313
caption: "sum result",
1414
document: {
1515
file_content: new TextEncoder().encode(`your sum is ${result}`),
1616
file_name: "hello.txt"
1717
}
18-
}
18+
})
1919
}
2020

2121
if (msg.text) { //reply on any text message
22-
return {
22+
return BotResponse.make({
2323
type: "message",
2424
text: "hey 🙃, send me a message in the format '3+3+3' and I will return you the sum of it in a text file"
25-
}
25+
})
2626
}
2727

28+
return BotResponse.ignore;
29+
2830
},
29-
} satisfies BotMessageHandlers
31+
})

0 commit comments

Comments
 (0)