From b4dddeed748a7a7924108fec6fd38d09d30e32f0 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 30 Jun 2026 15:57:34 +0600 Subject: [PATCH] improve examples --- .../src/main/kotlin/RichMessagesBot.kt | 274 ++++++++++-------- 1 file changed, 149 insertions(+), 125 deletions(-) diff --git a/RichMessagesBot/src/main/kotlin/RichMessagesBot.kt b/RichMessagesBot/src/main/kotlin/RichMessagesBot.kt index 4205ddd..9b641ef 100644 --- a/RichMessagesBot/src/main/kotlin/RichMessagesBot.kt +++ b/RichMessagesBot/src/main/kotlin/RichMessagesBot.kt @@ -6,22 +6,25 @@ import dev.inmo.micro_utils.coroutines.subscribeLoggingDropExceptions import dev.inmo.tgbotapi.extensions.api.answers.answer import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands import dev.inmo.tgbotapi.extensions.api.send.reply -import dev.inmo.tgbotapi.extensions.api.send.send import dev.inmo.tgbotapi.extensions.api.send.sendRichMessage import dev.inmo.tgbotapi.extensions.api.send.sendRichMessageDraft import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitRichMessage import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onBaseInlineQuery import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGuestRequestMessage import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onRichMessage import dev.inmo.tgbotapi.extensions.utils.baseSentMessageUpdateOrNull import dev.inmo.tgbotapi.extensions.utils.contentMessageOrNull import dev.inmo.tgbotapi.extensions.utils.onlyRichMessageContentMessages +import dev.inmo.tgbotapi.extensions.utils.withContentOrNull import dev.inmo.tgbotapi.requests.edit.text.EditChatMessageRichText import dev.inmo.tgbotapi.types.BotCommand import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResultArticle import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputRichMessageContent import dev.inmo.tgbotapi.types.InlineQueryId +import dev.inmo.tgbotapi.types.message.content.TextContent +import dev.inmo.tgbotapi.types.message.textsources.BotCommandTextSource import dev.inmo.tgbotapi.types.rich.InputRichMessageHTML import dev.inmo.tgbotapi.types.rich.InputRichMessageMarkdown import dev.inmo.tgbotapi.types.toChatId @@ -63,6 +66,131 @@ suspend fun main(vararg args: String) { ) } + val testMarkdownText = """ + **bold text** + __bold text__ + *italic text* + _italic text_ + ~~strikethrough text~~ + `inline fixed-width code` + ==marked text== + ||spoiler|| + + [inline URL](https://t.me/) + [inline e-mail](mailto:user@example.com) + [inline phone number](tel:+123456789) + [inline mention of a user](tg://user?id=123456789) + ![👍](tg://emoji?id=5368324170671202286) + ![22:45 tomorrow](tg://time?unix=1647531900&format=wDT) + ${'$'}x^2 + y^2$ + \#hashtag ${'$'}USD +12345678901, card: 4242 4242 4242 4242, https://t.me t.me a@t.me /command @username + all the text above was on the same line + + # Heading 1 + ## Heading 2 + ### Heading 3 + #### Heading 4 + ##### Heading 5 + ###### Heading 6 + + Paragraph text + + ```python + print('pre-formatted fixed-width code block written in the Python programming language') + ``` + + --- + + - unordered list item + * unordered list item + + unordered list item + + 1. ordered list item + 2. ordered list item + + - [ ] task list item + - [x] completed task list item + + >Block quotation started + > + >Block quotation continued on the next line + >Block quotation continued on the same line + > + >The last line of the block quotation + + ![](https://telegram.org/example/photo.jpg) + ![](https://telegram.org/example/video.mp4) + ![](https://telegram.org/example/audio.mp3) + ![](https://telegram.org/example/audio.ogg) + ![](https://telegram.org/example/animation.gif) + + ![](https://telegram.org/example/photo.jpg "Photo caption") + ![](https://telegram.org/example/video.mp4 "Video caption") + ![](https://telegram.org/example/audio.mp3 "Audio caption") + ![](https://telegram.org/example/audio.ogg "Voice note caption") + ![](https://telegram.org/example/animation.gif "Animation caption") + + | Header 1 | Header 2 | + |:---------|:--------:| + | left | center | + + Text with a reference[^id1] and another one[^id2]. + + [^id1]: Definition of the first footnote. + [^id2]: Definition of the second footnote. + + $${'$'}E = mc^2$$ + + ```math + E = mc^2 + ``` + + ## Example Nested Syntax Report for _Q1_ + Intro with underlined text, ==marked text==, and ${'$'}x^2 + y^2$. + **Bold _italic underlined italic bold italic_ bold** + In inline tags, nested **markdown** is parsed + >Quote with **bold text, ~~strikethrough, and spoiler~~**, plus [a link](https://t.me/). + + - List item with `code`, superscript, subscript, and a footnote[^note] + - Another item with **bold spoiler code** + - Another item with ~~strikethrough and inserted text~~ + + | Metric | Value | + |:-------|------:| + | Speed | **42** ms | + | Status | ready | + + [^note]: Footnote with _italic text_ and HTML underline. + + --- + + # Details blocks can contain Markdown content: + +
Summary with **bold text** + + ### Details heading + - List item with _italic text_ + - List item with spoiler + +
+ + # Collages and slideshows can contain Markdown media blocks: + + + + ![](https://telegram.org/example/photo.jpg) + ![](https://telegram.org/example/video.mp4) + + + + + + ![](https://telegram.org/example/photo.jpg) + ![](https://telegram.org/example/video.mp4) + + + """.trimIndent() + telegramBotWithBehaviourAndLongPolling( botToken, CoroutineScope(Dispatchers.IO), @@ -170,130 +298,7 @@ suspend fun main(vararg args: String) { it.chat.id, // InputRichMessageMarkdown factory — content described using Markdown formatting InputRichMessageMarkdown( - """ - **bold text** - __bold text__ - *italic text* - _italic text_ - ~~strikethrough text~~ - `inline fixed-width code` - ==marked text== - ||spoiler|| - - [inline URL](https://t.me/) - [inline e-mail](mailto:user@example.com) - [inline phone number](tel:+123456789) - [inline mention of a user](tg://user?id=123456789) - ![👍](tg://emoji?id=5368324170671202286) - ![22:45 tomorrow](tg://time?unix=1647531900&format=wDT) - ${'$'}x^2 + y^2$ - \#hashtag ${'$'}USD +12345678901, card: 4242 4242 4242 4242, https://t.me t.me a@t.me /command @username - all the text above was on the same line - - # Heading 1 - ## Heading 2 - ### Heading 3 - #### Heading 4 - ##### Heading 5 - ###### Heading 6 - - Paragraph text - - ```python - print('pre-formatted fixed-width code block written in the Python programming language') - ``` - - --- - - - unordered list item - * unordered list item - + unordered list item - - 1. ordered list item - 2. ordered list item - - - [ ] task list item - - [x] completed task list item - - >Block quotation started - > - >Block quotation continued on the next line - >Block quotation continued on the same line - > - >The last line of the block quotation - - ![](https://telegram.org/example/photo.jpg) - ![](https://telegram.org/example/video.mp4) - ![](https://telegram.org/example/audio.mp3) - ![](https://telegram.org/example/audio.ogg) - ![](https://telegram.org/example/animation.gif) - - ![](https://telegram.org/example/photo.jpg "Photo caption") - ![](https://telegram.org/example/video.mp4 "Video caption") - ![](https://telegram.org/example/audio.mp3 "Audio caption") - ![](https://telegram.org/example/audio.ogg "Voice note caption") - ![](https://telegram.org/example/animation.gif "Animation caption") - - | Header 1 | Header 2 | - |:---------|:--------:| - | left | center | - - Text with a reference[^id1] and another one[^id2]. - - [^id1]: Definition of the first footnote. - [^id2]: Definition of the second footnote. - - $${'$'}E = mc^2$$ - - ```math - E = mc^2 - ``` - - ## Example Nested Syntax Report for _Q1_ - Intro with underlined text, ==marked text==, and ${'$'}x^2 + y^2$. - **Bold _italic underlined italic bold italic_ bold** - In inline tags, nested **markdown** is parsed - >Quote with **bold text, ~~strikethrough, and spoiler~~**, plus [a link](https://t.me/). - - - List item with `code`, superscript, subscript, and a footnote[^note] - - Another item with **bold spoiler code** - - Another item with ~~strikethrough and inserted text~~ - - | Metric | Value | - |:-------|------:| - | Speed | **42** ms | - | Status | ready | - - [^note]: Footnote with _italic text_ and HTML underline. - - --- - - # Details blocks can contain Markdown content: - -
Summary with **bold text** - - ### Details heading - - List item with _italic text_ - - List item with spoiler - -
- - # Collages and slideshows can contain Markdown media blocks: - - - - ![](https://telegram.org/example/photo.jpg) - ![](https://telegram.org/example/video.mp4) - - - - - - ![](https://telegram.org/example/photo.jpg) - ![](https://telegram.org/example/video.mp4) - - - """.trimIndent() + testMarkdownText ) ) println(sent) @@ -341,6 +346,25 @@ suspend fun main(vararg args: String) { ) } + onGuestRequestMessage { + val withTextContent = it.withContentOrNull() ?: return@onGuestRequestMessage + val haveCommand = withTextContent.content.text.contains("/rich_guest") + if (haveCommand) { + answer( + it.guestQueryId, + InlineQueryResultArticle( + InlineQueryId("rich_content"), + "Send rich message", + InputRichMessageContent( + InputRichMessageMarkdown( + testMarkdownText + ) + ) + ) + ) + } + } + // onRichMessage trigger: incoming messages carrying the new rich_message field onRichMessage { message -> val richMessage = message.content.richMessage