Skip to content

Commit

Permalink
simplified web worker launch process + adapted to next client version
Browse files Browse the repository at this point in the history
  • Loading branch information
kondaurovDev committed Feb 3, 2025
1 parent 4a9a291 commit a938a00
Show file tree
Hide file tree
Showing 22 changed files with 327 additions and 338 deletions.
26 changes: 26 additions & 0 deletions docs/telegram-bot-playground/assets/index-BdFVq0_I.js

Large diffs are not rendered by default.

26 changes: 0 additions & 26 deletions docs/telegram-bot-playground/assets/index-D5yhjlgw.js

This file was deleted.

22 changes: 22 additions & 0 deletions docs/telegram-bot-playground/assets/web-worker-BcVFCjdL.js

Large diffs are not rendered by default.

22 changes: 0 additions & 22 deletions docs/telegram-bot-playground/assets/web-worker-DkORvJaQ.js

This file was deleted.

21 changes: 11 additions & 10 deletions docs/telegram-bot-playground/example/command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { BotMessageHandlers } from "@effect-ak/tg-bot-client"
import { BotResponse, defineBot } from "@effect-ak/tg-bot-client"

export default {
export default defineBot({
on_message: (msg) => {

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

if (command == "/bye") {
return {
return BotResponse.make({
type: "message",
text: "See you later!"
}
});
}

if (command == "/echo") {
return {
return BotResponse.make({
type: "message",
text: `<pre language="json">${JSON.stringify(msg, undefined, 2)}</pre>`,
parse_mode: "HTML"
}
})
}

if (msg.text) { // reply with "hey" on any text message
return {
return BotResponse.make({
type: "message",
text: "hey 😇"
}
})
}

},
} satisfies BotMessageHandlers
return BotResponse.ignore

},
})
12 changes: 7 additions & 5 deletions docs/telegram-bot-playground/example/empty.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { BotMessageHandlers } from "@effect-ak/tg-bot-client"
import { BotResponse, defineBot } from "@effect-ak/tg-bot-client"

export default {
export default defineBot({
on_message: (msg) => {

if (msg.text) {
return {
return BotResponse.make({
type: "message",
text: "hey 😀"
}
})
}

return BotResponse.ignore;

},
} satisfies BotMessageHandlers
})
16 changes: 9 additions & 7 deletions docs/telegram-bot-playground/example/file.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import type { BotMessageHandlers } from "@effect-ak/tg-bot-client"
import { BotResponse, defineBot } from "@effect-ak/tg-bot-client"

export default {
export default defineBot({
on_message: (msg) => {
if (msg.text?.includes("+")) {
const numbers = msg.text.split("+");
let result = 0;
for (const num of numbers) {
result += parseInt(num);
}
return {
return BotResponse.make({
type: "document",
caption: "sum result",
document: {
file_content: new TextEncoder().encode(`your sum is ${result}`),
file_name: "hello.txt"
}
}
})
}

if (msg.text) { //reply on any text message
return {
return BotResponse.make({
type: "message",
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"
}
})
}

return BotResponse.ignore;

},
} satisfies BotMessageHandlers
})
2 changes: 1 addition & 1 deletion docs/telegram-bot-playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<link rel="icon" type="image/png" sizes="32x32" href="./favicon.ico">


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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"dependencies": {
"@edge-runtime/vm": "^5.0.0",
"@effect-ak/tg-bot-client": "file://Users/alex/projects/effect-ak-org/tg-bot-client/effect-ak-tg-bot-client-0.3.3.tgz",
"@effect-ak/tg-bot-client": "^0.5.1",
"@monaco-editor/loader": "^1.4.0",
"@types/alpinejs": "^3.13.11",
"@types/node": "^22.10.5",
Expand Down
310 changes: 156 additions & 154 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/common/editor/setup.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import type { Monaco } from "@monaco-editor/loader";
import { fetchText } from "#/common/utils";
import { CDN_PACKAGE_DTS } from "#/tg-bot-playground/const";

export const setupDts = async (
monaco: Monaco
) => {

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

monaco.languages.typescript.typescriptDefaults.setExtraLibs([
{
Expand All @@ -23,4 +24,4 @@ export const setupDts = async (
strict: true
});

}
}
49 changes: 16 additions & 33 deletions src/common/editor/ts-text-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,41 +26,24 @@ export const makeTsTextModel =
getJsCode: async () => {
const output = await getTsCode();
const code = output.outputFiles[0].text;
const defaultExport = await getDefaultExport(code);
return {
defaultExport,
serialized: serialize(defaultExport?.default)
} as const;
return code;
}
} as const

}

async function getDefaultExport<D>(code: string) {
try {
const encodedCode = encodeURIComponent(code);
const module = await import(/* @vite-ignore */`data:text/javascript,${encodedCode}`);
const result = module.default;
return { default: result as D };
} catch (e) {
console.warn("get default error", e);
return undefined;
}

}

const serialize = (input: unknown) => {
if (typeof input != "object" || !input) {
return undefined;
}
const result = [] as [string, string][];

for (const [key, value] of Object.entries(input)) {
if (typeof value != "function") {
continue;
}
result.push([key, value.toString()])
}

return JSON.stringify(Object.fromEntries(result));
}
// const serialize = (input: unknown) => {
// if (typeof input != "object" || !input) {
// return undefined;
// }
// const result = [] as [string, string][];

// for (const [key, value] of Object.entries(input)) {
// if (typeof value != "function") {
// continue;
// }
// result.push([key, value.toString()])
// }

// return JSON.stringify(Object.fromEntries(result));
// }
2 changes: 1 addition & 1 deletion src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { MonacoLoader, Alpine } from "./types";

export const fetchText = (path: string) =>
fetch(path).then(_ => _.text())
fetch(path).then(_ => _.text());

export const getMonacoLoader = () => {
if (!("monaco_loader" in window) || typeof window.monaco_loader != "object" || window.monaco_loader == null) {
Expand Down
7 changes: 1 addition & 6 deletions src/tg-bot-playground/bot-launcher/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@ export const makeRunnableBot =

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

if (!code.serialized) {
console.warn("Serizalized js code not defined");
return;
}

worker.postMessage({
command: 'run-bot',
token: state.bot.token,
code: code.serialized
code
});

state.bot.status = "active"
Expand Down
3 changes: 3 additions & 0 deletions src/tg-bot-playground/const.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const CLIENT_VERSION = "0.5.1";
export const CDN_PACKAGE_MAIN = `@effect-ak/tg-bot-client@${CLIENT_VERSION}/dist/index.min.mjs`;
export const CDN_PACKAGE_DTS = `@effect-ak/tg-bot-client@${CLIENT_VERSION}/dist/index.d.ts`;
2 changes: 1 addition & 1 deletion src/tg-bot-playground/main.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Alpine from "alpinejs";
import { makeBotLauncher } from "#/tg-bot-playground/bot-launcher/_main";
import { makeEditor } from "#/common/editor/_editor";
import { fetchText } from "#/common/utils";
import type { BotState } from "./types";
import Alpine from "alpinejs";

export type GlobalState = ReturnType<typeof makeGlobalState>;

Expand Down
21 changes: 11 additions & 10 deletions src/tg-bot-playground/static/example/command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { BotMessageHandlers } from "@effect-ak/tg-bot-client"
import { BotResponse, defineBot } from "@effect-ak/tg-bot-client"

export default {
export default defineBot({
on_message: (msg) => {

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

if (command == "/bye") {
return {
return BotResponse.make({
type: "message",
text: "See you later!"
}
});
}

if (command == "/echo") {
return {
return BotResponse.make({
type: "message",
text: `<pre language="json">${JSON.stringify(msg, undefined, 2)}</pre>`,
parse_mode: "HTML"
}
})
}

if (msg.text) { // reply with "hey" on any text message
return {
return BotResponse.make({
type: "message",
text: "hey 😇"
}
})
}

},
} satisfies BotMessageHandlers
return BotResponse.ignore

},
})
12 changes: 7 additions & 5 deletions src/tg-bot-playground/static/example/empty.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import type { BotMessageHandlers } from "@effect-ak/tg-bot-client"
import { BotResponse, defineBot } from "@effect-ak/tg-bot-client"

export default {
export default defineBot({
on_message: (msg) => {

if (msg.text) {
return {
return BotResponse.make({
type: "message",
text: "hey 😀"
}
})
}

return BotResponse.ignore;

},
} satisfies BotMessageHandlers
})
16 changes: 9 additions & 7 deletions src/tg-bot-playground/static/example/file.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import type { BotMessageHandlers } from "@effect-ak/tg-bot-client"
import { BotResponse, defineBot } from "@effect-ak/tg-bot-client"

export default {
export default defineBot({
on_message: (msg) => {
if (msg.text?.includes("+")) {
const numbers = msg.text.split("+");
let result = 0;
for (const num of numbers) {
result += parseInt(num);
}
return {
return BotResponse.make({
type: "document",
caption: "sum result",
document: {
file_content: new TextEncoder().encode(`your sum is ${result}`),
file_name: "hello.txt"
}
}
})
}

if (msg.text) { //reply on any text message
return {
return BotResponse.make({
type: "message",
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"
}
})
}

return BotResponse.ignore;

},
} satisfies BotMessageHandlers
})
Loading

0 comments on commit a938a00

Please sign in to comment.