update readmes

This commit is contained in:
2026-08-21 15:51:06 +06:00
parent 016400a821
commit ce48393895
104 changed files with 2615 additions and 333 deletions

View File

@@ -25,6 +25,11 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.currentCoroutineContext
/**
* Parses pagination callback data whose first two space-separated fields are the page and total page count.
*
* @return the parsed page and count, or `null` when either field is missing or is not an integer
*/
fun String.parsePageAndCount(): Pair<Int, Int>? {
val (pageString, countString) = split(" ").takeIf { it.count() > 1 } ?: return null
return Pair(
@@ -33,6 +38,15 @@ fun String.parsePageAndCount(): Pair<Int, Int>? {
)
}
/**
* Adds the pagination controls used by command replies and inline-query results.
*
* The controls include nearby page callbacks, first/last-page jumps when applicable, a button that copies the
* corresponding `/inline` command, and a button that starts inline mode for a user-selected chat.
*
* @param page the current page; callers should keep it within `1..count`
* @param count the total number of pages; callers should pass a positive value
*/
fun InlineKeyboardBuilder.includePageButtons(page: Int, count: Int) {
val numericButtons = listOfNotNull(
page - 1,
@@ -78,6 +92,15 @@ fun InlineKeyboardBuilder.includePageButtons(page: Int, count: Int) {
}
}
/**
* Creates and runs the shared KeyboardsBot behavior using long polling.
*
* The bot serves `/inline` pagination keyboards, edits them in response to callback queries, answers compatible
* inline queries, offers an `/inline` reply-keyboard button for unhandled commands, and logs every received update.
*
* @param token the Telegram bot token
* @param print receives the bot information returned by the startup `getMe` request
*/
@OptIn(PreviewFeature::class)
suspend fun activateKeyboardsBot(
token: String,

View File

@@ -4,6 +4,12 @@ import org.w3c.dom.*
private val scope = CoroutineScope(Dispatchers.Default)
/**
* Installs the browser launch form after `DOMContentLoaded`.
*
* Every submission reads the token from `bot_token`, appends a result container under `bots_container`, and launches
* [activateKeyboardsBot]. The result of its startup `getMe` request is rendered in that new container.
*/
fun main() {
document.addEventListener(
"DOMContentLoaded",

66
KeyboardsBot/README.md Normal file
View File

@@ -0,0 +1,66 @@
# KeyboardsBot
A multiplatform long-polling example that demonstrates Telegram reply keyboards, inline keyboards, callback queries, copy-text buttons, inline-mode buttons, and keyboard button styles. The shared bot behavior lives in `KeyboardsBotLib`; the project provides a browser/JS entry point and a separate JVM launcher.
## Bot behavior
At startup, the bot calls `getMe`, reports the returned bot information through the platform launcher, registers `/inline` with Telegram, and starts long polling. Every received update is also printed to the JVM terminal or browser developer console.
### Commands
| Command | Result |
| --- | --- |
| `/inline` | Opens page `1` of a `10`-page inline keyboard. |
| `/inline <count>` | Opens page `1` with the supplied total page count. |
| `/inline <page> <count>` | Opens the supplied page with the supplied total page count. |
Only numeric command arguments are considered. Use positive integers with `page <= count`; the example does not validate the count or clamp the page to the upper bound.
The generated inline keyboard contains:
- numbered buttons for the current page and any adjacent pages that are within `1..count`;
- styled jump buttons for moving toward the first or last page when applicable;
- a **Command copy button** that copies `/inline <page> <count>`;
- a **Send somebody page** button that starts inline mode and lets the user choose a user, bot, group, or channel.
Pagination callbacks edit the original message and replace its text with `This is <page> of <count>`. This works for both ordinary bot messages and messages sent through inline mode. Unsupported callback data or an unsupported message type is answered with a callback notification instead.
Any command not handled above, including `/start`, receives a one-time reply keyboard containing a styled `/inline` button. Ordinary non-command messages are ignored.
### Inline mode
With inline mode enabled, a query beginning with a page and count, such as `@YourBot 2 10`, returns one **Send buttons** article. Sending that result posts an inline-mode message with the same pagination keyboard.
## Telegram setup
1. Create a bot with [@BotFather](https://t.me/BotFather) and obtain its token.
2. Enable inline mode for the bot with BotFather's `/setinline` command. Direct `/inline` commands work without it, but inline queries and **Send somebody page** require it.
3. Run only one launcher for a token at a time. The bot receives updates through long polling and automatically removes an existing webhook when it starts.
The browser launcher handles the token in client-side code. Use it only from a trusted local page, do not expose the page publicly with a token filled in, and close the page when the bot should stop.
## Launch
Run the commands below from the repository root.
### JVM
The first argument is the required bot token. An optional argument exactly equal to `debug` enables formatted KSLog output; the token must remain first.
```bash
./gradlew :KeyboardsBot:jvm_launcher:run --args="<BOT_TOKEN>"
```
```bash
./gradlew :KeyboardsBot:jvm_launcher:run --args="<BOT_TOKEN> debug"
```
### Browser/JS
Start the Kotlin/JS browser development run:
```bash
./gradlew :KeyboardsBot:KeyboardsBotLib:jsBrowserDevelopmentRun
```
Enter the bot token in the displayed form and press **Start bot**. The page displays the result of `getMe`; raw updates and other console output appear in the browser developer console. Each form submission starts another bot instance, so submit the token only once.

View File

@@ -5,6 +5,12 @@ import dev.inmo.kslog.common.setDefaultKSLog
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
/**
* Runs [activateKeyboardsBot] on the JVM and prints its startup bot information to standard output.
*
* @param args the bot token as the first element and, optionally, `debug` in a later element to enable formatted
* KSLog output
*/
suspend fun main(args: Array<String>) {
val isDebug = args.any { it == "debug" }