mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2026-07-01 15:45:14 +00:00
improve examples
This commit is contained in:
@@ -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.answers.answer
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
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.sendRichMessage
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.sendRichMessageDraft
|
import dev.inmo.tgbotapi.extensions.api.send.sendRichMessageDraft
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitRichMessage
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitRichMessage
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
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.onBaseInlineQuery
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
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.behaviour_builder.triggers_handling.onRichMessage
|
||||||
import dev.inmo.tgbotapi.extensions.utils.baseSentMessageUpdateOrNull
|
import dev.inmo.tgbotapi.extensions.utils.baseSentMessageUpdateOrNull
|
||||||
import dev.inmo.tgbotapi.extensions.utils.contentMessageOrNull
|
import dev.inmo.tgbotapi.extensions.utils.contentMessageOrNull
|
||||||
import dev.inmo.tgbotapi.extensions.utils.onlyRichMessageContentMessages
|
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.requests.edit.text.EditChatMessageRichText
|
||||||
import dev.inmo.tgbotapi.types.BotCommand
|
import dev.inmo.tgbotapi.types.BotCommand
|
||||||
import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResultArticle
|
import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResultArticle
|
||||||
import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputRichMessageContent
|
import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputRichMessageContent
|
||||||
import dev.inmo.tgbotapi.types.InlineQueryId
|
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.InputRichMessageHTML
|
||||||
import dev.inmo.tgbotapi.types.rich.InputRichMessageMarkdown
|
import dev.inmo.tgbotapi.types.rich.InputRichMessageMarkdown
|
||||||
import dev.inmo.tgbotapi.types.toChatId
|
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)
|
||||||
|

|
||||||
|

|
||||||
|
${'$'}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
|
||||||
|
|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|
|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|

|
||||||
|
|
||||||
|
| 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 <u>underlined text</u>, ==marked text==, and ${'$'}x^2 + y^2$.
|
||||||
|
**Bold _italic <u>underlined italic bold</u> italic_ bold**
|
||||||
|
<u>In inline tags, nested **markdown** is parsed</u>
|
||||||
|
>Quote with **bold text, ~~strikethrough, and <tg-spoiler>spoiler</tg-spoiler>~~**, plus [a link](https://t.me/).
|
||||||
|
|
||||||
|
- List item with `code`, <sup>superscript</sup>, <sub>subscript</sub>, and a footnote[^note]
|
||||||
|
- Another item with **bold <tg-spoiler><code>spoiler code</code></tg-spoiler>**
|
||||||
|
- Another item with ~~strikethrough and <ins>inserted text</ins>~~
|
||||||
|
|
||||||
|
| Metric | Value |
|
||||||
|
|:-------|------:|
|
||||||
|
| Speed | **42** <sup>ms</sup> |
|
||||||
|
| Status | <tg-spoiler>ready</tg-spoiler> |
|
||||||
|
|
||||||
|
[^note]: Footnote with _italic text_ and <u>HTML underline</u>.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
# Details blocks can contain Markdown content:
|
||||||
|
|
||||||
|
<details open><summary>Summary with **bold text**</summary>
|
||||||
|
|
||||||
|
### Details heading
|
||||||
|
- List item with _italic text_
|
||||||
|
- List item with <tg-spoiler>spoiler</tg-spoiler>
|
||||||
|
|
||||||
|
</details>
|
||||||
|
|
||||||
|
# Collages and slideshows can contain Markdown media blocks:
|
||||||
|
|
||||||
|
<tg-collage>
|
||||||
|
|
||||||
|

|
||||||
|

|
||||||
|
|
||||||
|
</tg-collage>
|
||||||
|
|
||||||
|
<tg-slideshow>
|
||||||
|
|
||||||
|

|
||||||
|

|
||||||
|
|
||||||
|
</tg-slideshow>
|
||||||
|
""".trimIndent()
|
||||||
|
|
||||||
telegramBotWithBehaviourAndLongPolling(
|
telegramBotWithBehaviourAndLongPolling(
|
||||||
botToken,
|
botToken,
|
||||||
CoroutineScope(Dispatchers.IO),
|
CoroutineScope(Dispatchers.IO),
|
||||||
@@ -170,130 +298,7 @@ suspend fun main(vararg args: String) {
|
|||||||
it.chat.id,
|
it.chat.id,
|
||||||
// InputRichMessageMarkdown factory — content described using Markdown formatting
|
// InputRichMessageMarkdown factory — content described using Markdown formatting
|
||||||
InputRichMessageMarkdown(
|
InputRichMessageMarkdown(
|
||||||
"""
|
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)
|
|
||||||

|
|
||||||

|
|
||||||
${'$'}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
|
|
||||||
|
|
||||||

|
|
||||||

|
|
||||||

|
|
||||||

|
|
||||||

|
|
||||||
|
|
||||||

|
|
||||||

|
|
||||||

|
|
||||||

|
|
||||||

|
|
||||||
|
|
||||||
| 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 <u>underlined text</u>, ==marked text==, and ${'$'}x^2 + y^2$.
|
|
||||||
**Bold _italic <u>underlined italic bold</u> italic_ bold**
|
|
||||||
<u>In inline tags, nested **markdown** is parsed</u>
|
|
||||||
>Quote with **bold text, ~~strikethrough, and <tg-spoiler>spoiler</tg-spoiler>~~**, plus [a link](https://t.me/).
|
|
||||||
|
|
||||||
- List item with `code`, <sup>superscript</sup>, <sub>subscript</sub>, and a footnote[^note]
|
|
||||||
- Another item with **bold <tg-spoiler><code>spoiler code</code></tg-spoiler>**
|
|
||||||
- Another item with ~~strikethrough and <ins>inserted text</ins>~~
|
|
||||||
|
|
||||||
| Metric | Value |
|
|
||||||
|:-------|------:|
|
|
||||||
| Speed | **42** <sup>ms</sup> |
|
|
||||||
| Status | <tg-spoiler>ready</tg-spoiler> |
|
|
||||||
|
|
||||||
[^note]: Footnote with _italic text_ and <u>HTML underline</u>.
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
# Details blocks can contain Markdown content:
|
|
||||||
|
|
||||||
<details open><summary>Summary with **bold text**</summary>
|
|
||||||
|
|
||||||
### Details heading
|
|
||||||
- List item with _italic text_
|
|
||||||
- List item with <tg-spoiler>spoiler</tg-spoiler>
|
|
||||||
|
|
||||||
</details>
|
|
||||||
|
|
||||||
# Collages and slideshows can contain Markdown media blocks:
|
|
||||||
|
|
||||||
<tg-collage>
|
|
||||||
|
|
||||||

|
|
||||||

|
|
||||||
|
|
||||||
</tg-collage>
|
|
||||||
|
|
||||||
<tg-slideshow>
|
|
||||||
|
|
||||||

|
|
||||||

|
|
||||||
|
|
||||||
</tg-slideshow>
|
|
||||||
""".trimIndent()
|
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
println(sent)
|
println(sent)
|
||||||
@@ -341,6 +346,25 @@ suspend fun main(vararg args: String) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onGuestRequestMessage {
|
||||||
|
val withTextContent = it.withContentOrNull<TextContent>() ?: 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 trigger: incoming messages carrying the new rich_message field
|
||||||
onRichMessage { message ->
|
onRichMessage { message ->
|
||||||
val richMessage = message.content.richMessage
|
val richMessage = message.content.richMessage
|
||||||
|
|||||||
Reference in New Issue
Block a user