mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2026-11-28 14:22:58 +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
+7
-4
@@ -1,6 +1,6 @@
|
||||
# GiftsBot
|
||||
|
||||
Demonstrates the paginated owned-gift APIs by listing gifts for the chat in which the command is received.
|
||||
Lists gifts owned by the current chat and looks up individual collectible gifts by name.
|
||||
|
||||
## Behavior
|
||||
|
||||
@@ -19,10 +19,13 @@ The bot also handles `UniqueGiftInfo` service messages with `onUniqueGiftSentOrR
|
||||
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
|
||||
## Commands
|
||||
|
||||
- `/start` — lists the owned gifts selected by the current chat type. It must be the only command in the message and
|
||||
takes no arguments.
|
||||
- `/unique_gift <name>` calls `getUniqueGift` and replies with the collectible's number, model, symbol, backdrop,
|
||||
and Telegram link. Use the name after `t.me/nft/` in a collectible's link, or copy the name from a `/start` result.
|
||||
The lookup works independently of gift ownership.
|
||||
|
||||
Other commands and ordinary non-command messages are ignored; unique-gift service messages are handled separately.
|
||||
|
||||
@@ -34,8 +37,8 @@ Other commands and ordinary non-command messages are ignored; unique-gift servic
|
||||
4. For business-chat use, enable the bot's Business/Secretary mode, connect it to the business account, and grant the
|
||||
**View gifts and Stars** (`can_view_gifts_and_stars`) business right.
|
||||
|
||||
The example performs no access checks or error recovery, so Telegram API or permission errors end that command
|
||||
handler.
|
||||
The lookup reports Telegram's error description when available. The owned-gift listing requires the applicable chat
|
||||
or business access.
|
||||
|
||||
## Arguments and launch
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ buildscript {
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'application'
|
||||
|
||||
mainClassName="GiftsBotKt"
|
||||
application.mainClass = "GiftsBotKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
|
||||
@@ -2,20 +2,24 @@ import dev.inmo.kslog.common.KSLog
|
||||
import dev.inmo.kslog.common.LogLevel
|
||||
import dev.inmo.kslog.common.defaultMessageFormatter
|
||||
import dev.inmo.kslog.common.setDefaultKSLog
|
||||
import dev.inmo.tgbotapi.bot.exceptions.BotException
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.business.getBusinessAccountGiftsFlow
|
||||
import dev.inmo.tgbotapi.extensions.api.gifts.getChatGiftsFlow
|
||||
import dev.inmo.tgbotapi.extensions.api.gifts.getUniqueGift
|
||||
import dev.inmo.tgbotapi.extensions.api.gifts.getUserGiftsFlow
|
||||
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.onCommandWithArgs
|
||||
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
|
||||
import dev.inmo.tgbotapi.types.chat.UnknownChatType
|
||||
import dev.inmo.tgbotapi.types.gifts.OwnedGift
|
||||
import dev.inmo.tgbotapi.types.gifts.UniqueGiftName
|
||||
import dev.inmo.tgbotapi.types.message.textsources.splitForText
|
||||
import dev.inmo.tgbotapi.utils.bold
|
||||
import dev.inmo.tgbotapi.utils.buildEntities
|
||||
@@ -24,7 +28,7 @@ import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
/**
|
||||
* Starts a long-polling bot whose standalone `/start` command lists all owned gifts for the current chat.
|
||||
* Lists owned gifts with `/start` and looks up an individual collectible by name with `/unique_gift`.
|
||||
*
|
||||
* 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
|
||||
@@ -54,6 +58,26 @@ suspend fun main(vararg args: String) {
|
||||
val me = getMe()
|
||||
println(me)
|
||||
|
||||
onCommandWithArgs("unique_gift") { message, arguments ->
|
||||
val name = arguments.singleOrNull()?.takeIf { it.isNotBlank() }
|
||||
if (name == null) {
|
||||
reply(message, "Use /unique_gift <name> with the name from a t.me/nft/ link.")
|
||||
return@onCommandWithArgs
|
||||
}
|
||||
val gift = try {
|
||||
getUniqueGift(UniqueGiftName(name))
|
||||
} catch (error: BotException) {
|
||||
reply(message, error.response?.description ?: "Telegram could not look up this gift.")
|
||||
return@onCommandWithArgs
|
||||
}
|
||||
reply(
|
||||
message,
|
||||
"${gift.baseName} #${gift.number}\n" +
|
||||
"${gift.model.name} model, ${gift.symbol.name} symbol, ${gift.backdrop.name} backdrop\n" +
|
||||
gift.name.nftLink
|
||||
)
|
||||
}
|
||||
|
||||
onUniqueGiftSentOrReceived { message ->
|
||||
val uniqueGiftInfo = message.chatEvent
|
||||
println(
|
||||
|
||||
Reference in new issue
Block a user