mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2026-09-29 11:55:01 +00:00
update examples for ktgbotapi 37.0.0
This commit is contained in:
@@ -15,12 +15,16 @@ Regular gifts are shown with their ID, optional text, and Stars cost. Unique gif
|
||||
name, model, and number. Long results are split into multiple Telegram messages; an empty result produces
|
||||
`This chat have no any gifts`.
|
||||
|
||||
The bot also handles `UniqueGiftInfo` service messages with `onUniqueGiftSentOrReceived`. It logs and replies with
|
||||
Bot API 10.3's `text`, parsed `textSources` (`entities`), and `isPrivate` fields. When entities are present, the reply
|
||||
reuses them so the gift text keeps its formatting.
|
||||
|
||||
## Command
|
||||
|
||||
- `/start` — lists the owned gifts selected by the current chat type. It must be the only command in the message and
|
||||
takes no arguments.
|
||||
|
||||
Other commands and non-command messages are ignored.
|
||||
Other commands and ordinary non-command messages are ignored; unique-gift service messages are handled separately.
|
||||
|
||||
## Setup and permissions
|
||||
|
||||
|
||||
@@ -10,10 +10,7 @@ import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.api.send.withTypingAction
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayCompleted
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayContent
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayCreated
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayWinners
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onUniqueGiftSentOrReceived
|
||||
import dev.inmo.tgbotapi.types.chat.BusinessChat
|
||||
import dev.inmo.tgbotapi.types.chat.PrivateChat
|
||||
import dev.inmo.tgbotapi.types.chat.PublicChat
|
||||
@@ -22,6 +19,7 @@ import dev.inmo.tgbotapi.types.gifts.OwnedGift
|
||||
import dev.inmo.tgbotapi.types.message.textsources.splitForText
|
||||
import dev.inmo.tgbotapi.utils.bold
|
||||
import dev.inmo.tgbotapi.utils.buildEntities
|
||||
import dev.inmo.tgbotapi.utils.regular
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
@@ -30,7 +28,8 @@ import kotlinx.coroutines.Dispatchers
|
||||
*
|
||||
* Business chats are queried through their business connection, private chats through their user ID, and public or
|
||||
* unknown chat types through their chat ID. Regular and unique gifts are rendered as formatted text and long results
|
||||
* are split across replies. The bot also prints its [getMe] result at startup.
|
||||
* are split across replies. Unique-gift service messages also expose the Bot API 10.3 text, entities and privacy flag.
|
||||
* The bot prints its [getMe] result at startup.
|
||||
*
|
||||
* @param args the bot token followed by optional, case-sensitive `debug` and `testServer` flags; unknown trailing
|
||||
* arguments are ignored
|
||||
@@ -55,6 +54,28 @@ suspend fun main(vararg args: String) {
|
||||
val me = getMe()
|
||||
println(me)
|
||||
|
||||
onUniqueGiftSentOrReceived { message ->
|
||||
val uniqueGiftInfo = message.chatEvent
|
||||
println(
|
||||
"Unique gift ${uniqueGiftInfo.gift.name.value}: " +
|
||||
"text=${uniqueGiftInfo.text}, textSources=${uniqueGiftInfo.textSources}, " +
|
||||
"isPrivate=${uniqueGiftInfo.isPrivate}"
|
||||
)
|
||||
reply(
|
||||
message,
|
||||
buildEntities {
|
||||
bold("Unique gift") + ": ${uniqueGiftInfo.gift.name.value}\n"
|
||||
bold("Private") + ": ${uniqueGiftInfo.isPrivate}\n"
|
||||
bold("Text") + ": "
|
||||
if (uniqueGiftInfo.textSources.isEmpty()) {
|
||||
regular(uniqueGiftInfo.text ?: "(None)")
|
||||
} else {
|
||||
+uniqueGiftInfo.textSources
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
onCommand("start") {
|
||||
val giftsFlow = when (val chat = it.chat) {
|
||||
is BusinessChat -> {
|
||||
|
||||
Reference in New Issue
Block a user