Skip to content

Commit 4a9a291

Browse files
committed
small refactoring
1 parent 01e642b commit 4a9a291

File tree

14 files changed

+91
-48
lines changed

14 files changed

+91
-48
lines changed

docs/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@
5454
<div class="container">
5555
<div class="links">
5656
<a href="/telegram-bot-api/">Telegram Bot API Documentation</a>
57-
<a href="/telegram-bot-playground/">Bot Editor</a>
57+
<a href="/telegram-bot-playground/">Bot Playground</a>
58+
5859
</div>
5960
</div>
6061
</body>

docs/telegram-bot-playground/assets/index-D5yhjlgw.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-jDxP3pDx.js

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

docs/telegram-bot-playground/example/command.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ export default {
2323
}
2424
}
2525

26-
console.log("got a message", msg.text)
27-
28-
return {
29-
type: "message",
30-
text: "hey :)"
26+
if (msg.text) { // reply with "hey" on any text message
27+
return {
28+
type: "message",
29+
text: "hey 😇"
30+
}
3131
}
32+
3233
},
3334
} satisfies BotMessageHandlers
3435

docs/telegram-bot-playground/example/empty.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import type { BotMessageHandlers } from "@effect-ak/tg-bot-client"
33
export default {
44
on_message: (msg) => {
55

6-
return {
7-
type: "message",
8-
text: "hey :)"
6+
if (msg.text) {
7+
return {
8+
type: "message",
9+
text: "hey 😀"
10+
}
911
}
12+
1013
},
1114
} satisfies BotMessageHandlers

docs/telegram-bot-playground/example/file.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ export default {
1818
}
1919
}
2020

21-
return {
22-
type: "message",
23-
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"
21+
if (msg.text) { //reply on any text message
22+
return {
23+
type: "message",
24+
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+
}
2426
}
27+
2528
},
2629
} satisfies BotMessageHandlers

docs/telegram-bot-playground/index.html

Lines changed: 2 additions & 2 deletions
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-jDxP3pDx.js"></script>
12+
<script type="module" crossorigin src="./assets/index-D5yhjlgw.js"></script>
1313
<link rel="stylesheet" crossorigin href="./assets/index-BzQa5oof.css">
1414
</head>
1515

@@ -53,7 +53,7 @@ <h2>Telegram Bot Playground 🤖 </h2>
5353
x-model="$store.state.selectedExample"
5454
@change="$dispatch('change-example')"
5555
>
56-
<option value="empty.ts">Empty</option>
56+
<option value="empty.ts">Simple Bot</option>
5757
<option value="command.ts">Command Bot</option>
5858
<option value="file.ts">File Bot</option>
5959
</select>

src/common/editor/setup.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const setupDts = async (
55
monaco: Monaco
66
) => {
77

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

1010
monaco.languages.typescript.typescriptDefaults.setExtraLibs([
1111
{
Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { makeTgBotClient } from "@effect-ak/tg-bot-client";
12
import type { GlobalState } from "#/tg-bot-playground/main";
23
import type { TsTextModel } from "#/common/editor/ts-text-model";
34

@@ -14,8 +15,6 @@ export const makeRunnableBot =
1415
return;
1516
}
1617

17-
console.log("code", code.serialized)
18-
1918
worker.postMessage({
2019
command: 'run-bot',
2120
token: state.bot.token,
@@ -28,7 +27,7 @@ export const makeRunnableBot =
2827
})
2928

3029
export const checkTokenAndRun =
31-
(state: GlobalState, runnableBot: RunnableBot) => {
30+
async (state: GlobalState, runnableBot: RunnableBot) => {
3231

3332
const token = state.bot.token;
3433

@@ -37,21 +36,29 @@ export const checkTokenAndRun =
3736
return
3837
};
3938

40-
fetch(`https://api.telegram.org/bot${token}/getMe`)
41-
.then(_ => _.json())
39+
const client =
40+
makeTgBotClient({
41+
bot_token: token
42+
});
43+
44+
await client.execute("get_me", {})
4245
.then(info => {
43-
if (info.ok) {
44-
state.bot.name = info.result.first_name;
45-
state.bot.isReachable = true;
46-
console.log("Running bot")
47-
runnableBot(state);
48-
} else {
49-
state.bot.name = "nameless";
50-
state.bot.isReachable = false;
51-
}
46+
state.bot.name = info.first_name;
47+
state.bot.isReachable = true;
48+
console.log("Running bot")
49+
return runnableBot(state);
5250
}).catch(error => {
53-
console.warn("check token error", error);
51+
state.botUpdates.push(error);
52+
state.bot.name = "nameless";
5453
state.bot.isReachable = false;
5554
});
5655

56+
const webhook =
57+
await client.execute("get_webhook_info", {});
58+
59+
if (webhook.url) {
60+
state.botUpdates.push("Cannot work with webhooks, delete it first");
61+
state.bot.isReachable = false;
62+
}
63+
5764
}

src/tg-bot-playground/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ <h2>Telegram Bot Playground 🤖 </h2>
5353
x-model="$store.state.selectedExample"
5454
@change="$dispatch('change-example')"
5555
>
56-
<option value="empty.ts">Empty</option>
56+
<option value="empty.ts">Simple Bot</option>
5757
<option value="command.ts">Command Bot</option>
5858
<option value="file.ts">File Bot</option>
5959
</select>

src/tg-bot-playground/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async function setup() {
7373

7474
botLauncher.worker.onmessage = (event: MessageEvent) => {
7575
const data = event.data
76-
console.log('got message from worker', data);
76+
// console.log('got message from worker', data);
7777
if (!data) return;
7878
if (data.botState) {
7979
Object.assign(state.bot, data.botState)

src/tg-bot-playground/static/example/command.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ export default {
2323
}
2424
}
2525

26-
console.log("got a message", msg.text)
27-
28-
return {
29-
type: "message",
30-
text: "hey :)"
26+
if (msg.text) { // reply with "hey" on any text message
27+
return {
28+
type: "message",
29+
text: "hey 😇"
30+
}
3131
}
32+
3233
},
3334
} satisfies BotMessageHandlers
3435

src/tg-bot-playground/static/example/empty.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ import type { BotMessageHandlers } from "@effect-ak/tg-bot-client"
33
export default {
44
on_message: (msg) => {
55

6-
return {
7-
type: "message",
8-
text: "hey :)"
6+
if (msg.text) {
7+
return {
8+
type: "message",
9+
text: "hey 😀"
10+
}
911
}
12+
1013
},
1114
} satisfies BotMessageHandlers

src/tg-bot-playground/static/example/file.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ export default {
1818
}
1919
}
2020

21-
return {
22-
type: "message",
23-
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"
21+
if (msg.text) { //reply on any text message
22+
return {
23+
type: "message",
24+
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+
}
2426
}
27+
2528
},
2629
} satisfies BotMessageHandlers

0 commit comments

Comments
 (0)