mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2026-11-27 05:43:14 +00:00
update examples for ktgbotapi 38.0.0
This commit is contained in:
1 parent
d0da6c851b
commit
97b4391495
86 files changed
+888
-370
No files matched your search
@@ -1,15 +1,14 @@
|
||||
# RichMessagesBot
|
||||
|
||||
RichMessagesBot is a long-polling showcase of Telegram rich messages. It sends
|
||||
rich content from HTML, Markdown, and the typed `InputRichMessageBlocks` DSL;
|
||||
streams drafts; edits rich text; handles incoming rich messages; and supplies
|
||||
rich content from inline and guest queries. Bot API 10.3 coverage includes rich
|
||||
buttons, compact tables, expandable quotations, document blocks, document
|
||||
references, direct document uploads, and stoppable drafts.
|
||||
HTML, Markdown, and typed `InputRichMessageBlocks`, streams drafts, edits rich
|
||||
text, and answers inline and guest queries. Its examples include buttons, compact
|
||||
tables, document uploads, expandable quotations with nested blocks, TON addresses,
|
||||
and replies that preserve a user's rich-text selection.
|
||||
|
||||
## Commands
|
||||
|
||||
The bot installs eight commands in Telegram's default command menu. Three
|
||||
The bot installs nine commands in Telegram's default command menu. Three
|
||||
additional handlers can be invoked by typing their commands manually.
|
||||
|
||||
| Command | In menu | Demonstration |
|
||||
@@ -21,11 +20,18 @@ additional handlers can be invoked by typing their commands manually.
|
||||
| `/rich_markdown_medialess_blocks` | No | Sends and logs the same typed block tree without its media section. |
|
||||
| `/rich_blocks` | Yes | Sends a smaller, directly constructed block tree with headings, formatted paragraphs, ordered and unordered checkbox lists, a divider, preformatted Kotlin, and a quotation. |
|
||||
| `/rich_10_3` | Yes | Sends an inline `RichTextButton`, three aligned `InputRichBlockButtons` rows with URL, callback, inline-query, copy-text, and disabled actions, a compact table, an expandable quotation, and a document block. |
|
||||
| `/rich_quote` | Yes | Sends an expandable quotation containing a formatted paragraph and a list with a typed TON address. Send the command as a quoted reply to reproduce the selected passage. |
|
||||
| `/rich_draft` | Yes | In a private chat, streams three Markdown revisions with `canStop` and `keepOnStop`, one second apart, then sends a normal final rich message unless generation is stopped. |
|
||||
| `/rich_blocks_draft` | Yes | In a private chat, streams two draft-only `thinking()` blocks with `canStop` and `keepOnStop`, then sends a normal typed-block answer unless generation is stopped. |
|
||||
| `/rich_edit` | Yes | Sends a Markdown rich message, waits two seconds, and replaces its rich content with `EditChatMessageRichText`. |
|
||||
| `/wait_rich` | Yes | Prompts for a rich message, waits for the next matching content, and reports its block count. |
|
||||
|
||||
To try a rich-text selection, send `/rich_quote`, expand the address book, select
|
||||
some text, and reply to that selection with `/rich_quote`. The bot replies to the
|
||||
original address book using the received `TextQuote.richTexts` as `quoteRichTexts`
|
||||
and the received UTF-16 offset as `quotePosition`. Telegram counts UTF-16 code
|
||||
units across quotable texts separated by line feeds.
|
||||
|
||||
The two draft examples allocate a unique draft ID and subscribe to
|
||||
`waitMessageGenerationStopped` before sending the first revision. They finalize
|
||||
by sending a new normal rich message only if no matching stop update arrives;
|
||||
|
||||
@@ -11,7 +11,7 @@ buildscript {
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'application'
|
||||
|
||||
mainClassName="RichMessagesBotKt"
|
||||
application.mainClass = "RichMessagesBotKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
|
||||
@@ -7,6 +7,7 @@ import dev.inmo.tgbotapi.extensions.api.answers.answer
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
|
||||
import dev.inmo.tgbotapi.extensions.api.send.sendRichMessage
|
||||
import dev.inmo.tgbotapi.extensions.api.send.sendRichMessageDraft
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitMessageGenerationStopped
|
||||
@@ -14,9 +15,9 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitRichMessa
|
||||
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.onDataCallbackQuery
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onDocument
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGuestRequestMessage
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMessageDataCallbackQuery
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onPhoto
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onRichMessage
|
||||
import dev.inmo.tgbotapi.extensions.utils.baseSentMessageUpdateOrNull
|
||||
@@ -33,6 +34,7 @@ import dev.inmo.tgbotapi.types.DateTimeEntityFormatting
|
||||
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.ReplyParameters
|
||||
import dev.inmo.tgbotapi.types.TelegramDate
|
||||
import dev.inmo.tgbotapi.types.chat.PrivateChat
|
||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||
@@ -43,6 +45,7 @@ import dev.inmo.tgbotapi.types.media.TelegramMediaDocument
|
||||
import dev.inmo.tgbotapi.types.media.TelegramMediaPhoto
|
||||
import dev.inmo.tgbotapi.types.media.TelegramMediaVideo
|
||||
import dev.inmo.tgbotapi.types.media.TelegramMediaVoiceNote
|
||||
import dev.inmo.tgbotapi.types.payments.ton.TonAddress
|
||||
import dev.inmo.tgbotapi.types.rich.InputRichMessage
|
||||
import dev.inmo.tgbotapi.types.rich.InputRichMessageBlocks
|
||||
import dev.inmo.tgbotapi.types.rich.InputRichMessageHTML
|
||||
@@ -74,7 +77,7 @@ import kotlinx.coroutines.withTimeoutOrNull
|
||||
private val richDraftIds = DraftIdAllocator()
|
||||
|
||||
/**
|
||||
* Runs a long-polling showcase of the rich-message APIs introduced in Telegram Bot API 10.1 through 10.3.
|
||||
* Runs a long-polling showcase of Telegram's rich-message APIs.
|
||||
*
|
||||
* Outgoing [dev.inmo.tgbotapi.types.rich.InputRichMessage] values use one of three representations:
|
||||
* [InputRichMessageHTML], [InputRichMessageMarkdown], or a typed [InputRichMessageBlocks] tree of
|
||||
@@ -84,7 +87,8 @@ private val richDraftIds = DraftIdAllocator()
|
||||
* `tg://photo?id=...` and as typed blocks; [dev.inmo.tgbotapi.requests.send.SendRichMessage] also turns
|
||||
* multipart files inside an input tree into `attach://` uploads. Bot API 10.3 button rows, inline rich-text buttons,
|
||||
* compact tables, expandable quotations and document blocks are demonstrated by `/rich_10_3` and the document
|
||||
* trigger.
|
||||
* trigger. `/rich_quote` demonstrates nested expandable quotations, a TON address, and replying to a
|
||||
* user-selected passage using its rich texts and UTF-16 position.
|
||||
*
|
||||
* Incoming [dev.inmo.tgbotapi.types.message.content.RichMessageContent] and user-selected content covers
|
||||
* [onRichMessage], [waitRichMessage], [onlyRichMessageContentMessages], photo reuse, and
|
||||
@@ -924,10 +928,49 @@ suspend fun main(vararg args: String) {
|
||||
)
|
||||
}
|
||||
|
||||
onMessageDataCallbackQuery(Regex("rich_10_3_callback")) { query ->
|
||||
onDataCallbackQuery("rich_10_3_callback") { query ->
|
||||
answer(query, "Rich-message callback received")
|
||||
}
|
||||
|
||||
onCommand("rich_quote") { command ->
|
||||
val quote = command.content.quote
|
||||
val original = command.replyTo
|
||||
if (original != null && quote != null && !quote.richTexts.isNullOrEmpty()) {
|
||||
sendMessage(
|
||||
command.chat.id,
|
||||
"Your selection starts at UTF-16 offset ${quote.position}.",
|
||||
replyParameters = ReplyParameters(
|
||||
messageId = original.messageId,
|
||||
quoteRichTexts = quote.richTexts,
|
||||
quotePosition = quote.position,
|
||||
),
|
||||
)
|
||||
return@onCommand
|
||||
}
|
||||
|
||||
val zeroAddress = TonAddress("EQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAM9c")
|
||||
sendRichMessage(
|
||||
command.chat.id,
|
||||
InputRichMessageBlocks {
|
||||
paragraph("Expand the quotation, select a passage, and reply with /rich_quote.")
|
||||
expandableBlockQuotation(
|
||||
blocks = {
|
||||
paragraph { bold("📘 Sample address book") }
|
||||
unorderedList {
|
||||
item {
|
||||
paragraph {
|
||||
plain("The TON zero address is ")
|
||||
tonAddress(zeroAddress.value, zeroAddress)
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
credit = RichTextPlain("Address book example"),
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
// sendRichMessageDraft with blocks: the thinking() block is only valid inside a draft and is used
|
||||
// to stream a model's reasoning before the finalized rich message is sent via sendRichMessage.
|
||||
onCommand("rich_blocks_draft", initialFilter = { it.chat is PrivateChat }) { origin ->
|
||||
@@ -1120,6 +1163,7 @@ suspend fun main(vararg args: String) {
|
||||
BotCommand("rich_markdown", "Send a rich message described with Markdown"),
|
||||
BotCommand("rich_blocks", "Send a rich message built from the InputRichBlocks DSL"),
|
||||
BotCommand("rich_10_3", "Show Bot API 10.3 rich blocks and buttons"),
|
||||
BotCommand("rich_quote", "Select and quote a passage from a rich message"),
|
||||
BotCommand("rich_draft", "Stream a rich message draft, then finalize it"),
|
||||
BotCommand("rich_blocks_draft", "Stream a blocks draft with thinking(), then finalize it"),
|
||||
BotCommand("rich_edit", "Send a rich message and edit it with new rich content"),
|
||||
|
||||
Reference in new issue
Block a user