Skip to content

Commit

Permalink
small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kondaurovDev committed Jan 23, 2025
1 parent 01e642b commit 4a9a291
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 48 deletions.
3 changes: 2 additions & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@
<div class="container">
<div class="links">
<a href="/telegram-bot-api/">Telegram Bot API Documentation</a>
<a href="/telegram-bot-playground/">Bot Editor</a>
<a href="/telegram-bot-playground/">Bot Playground</a>

</div>
</div>
</body>
Expand Down
26 changes: 26 additions & 0 deletions docs/telegram-bot-playground/assets/index-D5yhjlgw.js

Large diffs are not rendered by default.

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

This file was deleted.

11 changes: 6 additions & 5 deletions docs/telegram-bot-playground/example/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ export default {
}
}

console.log("got a message", msg.text)

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

},
} satisfies BotMessageHandlers

9 changes: 6 additions & 3 deletions docs/telegram-bot-playground/example/empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import type { BotMessageHandlers } from "@effect-ak/tg-bot-client"
export default {
on_message: (msg) => {

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

},
} satisfies BotMessageHandlers
9 changes: 6 additions & 3 deletions docs/telegram-bot-playground/example/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ export default {
}
}

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

},
} satisfies BotMessageHandlers
4 changes: 2 additions & 2 deletions 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-jDxP3pDx.js"></script>
<script type="module" crossorigin src="./assets/index-D5yhjlgw.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-BzQa5oof.css">
</head>

Expand Down Expand Up @@ -53,7 +53,7 @@ <h2>Telegram Bot Playground 🤖 </h2>
x-model="$store.state.selectedExample"
@change="$dispatch('change-example')"
>
<option value="empty.ts">Empty</option>
<option value="empty.ts">Simple Bot</option>
<option value="command.ts">Command Bot</option>
<option value="file.ts">File Bot</option>
</select>
Expand Down
2 changes: 1 addition & 1 deletion src/common/editor/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const setupDts = async (
monaco: Monaco
) => {

const dts = await fetchText("https://cdn.jsdelivr.net/npm/@effect-ak/tg-bot-client@0.2.2/dist/index.d.ts");
const dts = await fetchText("https://cdn.jsdelivr.net/npm/@effect-ak/tg-bot-client@0.3.3/dist/index.d.ts");

monaco.languages.typescript.typescriptDefaults.setExtraLibs([
{
Expand Down
37 changes: 22 additions & 15 deletions src/tg-bot-playground/bot-launcher/run.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { makeTgBotClient } from "@effect-ak/tg-bot-client";
import type { GlobalState } from "#/tg-bot-playground/main";
import type { TsTextModel } from "#/common/editor/ts-text-model";

Expand All @@ -14,8 +15,6 @@ export const makeRunnableBot =
return;
}

console.log("code", code.serialized)

worker.postMessage({
command: 'run-bot',
token: state.bot.token,
Expand All @@ -28,7 +27,7 @@ export const makeRunnableBot =
})

export const checkTokenAndRun =
(state: GlobalState, runnableBot: RunnableBot) => {
async (state: GlobalState, runnableBot: RunnableBot) => {

const token = state.bot.token;

Expand All @@ -37,21 +36,29 @@ export const checkTokenAndRun =
return
};

fetch(`https://api.telegram.org/bot${token}/getMe`)
.then(_ => _.json())
const client =
makeTgBotClient({
bot_token: token
});

await client.execute("get_me", {})
.then(info => {
if (info.ok) {
state.bot.name = info.result.first_name;
state.bot.isReachable = true;
console.log("Running bot")
runnableBot(state);
} else {
state.bot.name = "nameless";
state.bot.isReachable = false;
}
state.bot.name = info.first_name;
state.bot.isReachable = true;
console.log("Running bot")
return runnableBot(state);
}).catch(error => {
console.warn("check token error", error);
state.botUpdates.push(error);
state.bot.name = "nameless";
state.bot.isReachable = false;
});

const webhook =
await client.execute("get_webhook_info", {});

if (webhook.url) {
state.botUpdates.push("Cannot work with webhooks, delete it first");
state.bot.isReachable = false;
}

}
2 changes: 1 addition & 1 deletion src/tg-bot-playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ <h2>Telegram Bot Playground 🤖 </h2>
x-model="$store.state.selectedExample"
@change="$dispatch('change-example')"
>
<option value="empty.ts">Empty</option>
<option value="empty.ts">Simple Bot</option>
<option value="command.ts">Command Bot</option>
<option value="file.ts">File Bot</option>
</select>
Expand Down
2 changes: 1 addition & 1 deletion src/tg-bot-playground/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ async function setup() {

botLauncher.worker.onmessage = (event: MessageEvent) => {
const data = event.data
console.log('got message from worker', data);
// console.log('got message from worker', data);
if (!data) return;
if (data.botState) {
Object.assign(state.bot, data.botState)
Expand Down
11 changes: 6 additions & 5 deletions src/tg-bot-playground/static/example/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ export default {
}
}

console.log("got a message", msg.text)

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

},
} satisfies BotMessageHandlers

9 changes: 6 additions & 3 deletions src/tg-bot-playground/static/example/empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import type { BotMessageHandlers } from "@effect-ak/tg-bot-client"
export default {
on_message: (msg) => {

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

},
} satisfies BotMessageHandlers
9 changes: 6 additions & 3 deletions src/tg-bot-playground/static/example/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ export default {
}
}

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

},
} satisfies BotMessageHandlers

0 comments on commit 4a9a291

Please sign in to comment.