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
@@ -22,6 +22,7 @@ This is a feature demonstration, not a production-ready bot. Several commands ch
|
||||
| Edit bio (`can_edit_bio`) | `/set_business_account_bio` |
|
||||
| Edit profile photo (`can_edit_profile_photo`) | Both profile-photo commands |
|
||||
| View gifts and Stars (`can_view_gifts_and_stars`) | Balance and gift-list commands |
|
||||
| Convert gifts to Stars (`can_convert_gifts_to_stars`) | `/delete_business_account_gift` |
|
||||
| Transfer Stars (`can_transfer_stars`) | `/transfer_business_account_stars` |
|
||||
| Manage stories (`can_manage_stories`) | `/post_story` and `/delete_story` |
|
||||
|
||||
@@ -58,7 +59,7 @@ The inline **Read message** button calls `readBusinessMessage`. **Delete message
|
||||
|
||||
## Private-chat commands
|
||||
|
||||
These commands must be sent by the connected account owner in their private chat with the bot. Except for `/get_business_account_info`, handlers silently stop when that chat is not associated with an in-memory business connection.
|
||||
Send these commands from the connected account owner's private chat with the bot. The account-info and gift-deletion commands report a missing connection. The other commands require a connection already recorded in memory.
|
||||
|
||||
| Command | Behavior |
|
||||
| --- | --- |
|
||||
@@ -67,7 +68,8 @@ These commands must be sent by the connected account owner in their private chat
|
||||
| `/set_business_account_username <username>` | Changes the account username to the single supplied argument. |
|
||||
| `/get_business_account_star_balance` | Prints the account's current Telegram Stars balance. |
|
||||
| `/transfer_business_account_stars <count>` | Transfers an integer number of Stars from the business account to the bot. Telegram accepts values from 1 through 10,000; this example leaves range validation to Telegram. |
|
||||
| `/get_business_account_gifts` | Fetches every page of owned gifts and prints their Kotlin representations, splitting long output across messages. |
|
||||
| `/get_business_account_gifts` | Lists each gift with its owned gift ID when Telegram supplies one, followed by the full gift details. Long results span multiple messages. |
|
||||
| `/delete_business_account_gift <owned_gift_id>` | Permanently deletes the specified regular, unlimited, unrefunded gift. Type the command directly in the owner's bot chat using the owned gift ID from the listing. The bot checks the active connection, sender and business right before calling `deleteBusinessAccountGift`. Deletion credits no Stars. |
|
||||
| `/set_business_account_bio <text>` | Saves the current bio, sets the complete text after the command as the new bio, waits 15 seconds, and attempts to restore the saved value. An empty text clears it temporarily. |
|
||||
| `/set_business_account_profile_photo` | Reply to a photo with this command to set it as the main profile photo; after 15 seconds the bot removes that photo. |
|
||||
| `/set_business_account_profile_photo_public` | Reply to a photo to set it as the public profile photo; after 15 seconds the bot removes it. |
|
||||
|
||||
@@ -11,7 +11,7 @@ buildscript {
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'application'
|
||||
|
||||
mainClassName="BusinessConnectionsBotKt"
|
||||
application.mainClass = "BusinessConnectionsBotKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
|
||||
@@ -3,9 +3,10 @@ import dev.inmo.kslog.common.LogLevel
|
||||
import dev.inmo.kslog.common.defaultMessageFormatter
|
||||
import dev.inmo.kslog.common.setDefaultKSLog
|
||||
import dev.inmo.micro_utils.common.Percentage
|
||||
import dev.inmo.tgbotapi.types.chat.PreviewBot
|
||||
import dev.inmo.tgbotapi.bot.exceptions.BotException
|
||||
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.business.deleteBusinessAccountGift
|
||||
import dev.inmo.tgbotapi.extensions.api.business.getBusinessAccountStarBalance
|
||||
import dev.inmo.tgbotapi.extensions.api.business.deleteBusinessMessages
|
||||
import dev.inmo.tgbotapi.extensions.api.business.getBusinessAccountGifts
|
||||
@@ -41,23 +42,28 @@ import dev.inmo.tgbotapi.extensions.utils.withContentOrNull
|
||||
import dev.inmo.tgbotapi.requests.abstracts.multipartFile
|
||||
import dev.inmo.tgbotapi.requests.business_connection.InputProfilePhoto
|
||||
import dev.inmo.tgbotapi.requests.stories.PostStory
|
||||
import dev.inmo.tgbotapi.types.AreaPosition
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.GiftId
|
||||
import dev.inmo.tgbotapi.types.MessageId
|
||||
import dev.inmo.tgbotapi.types.RawChatId
|
||||
import dev.inmo.tgbotapi.types.business_connection.BusinessConnectionId
|
||||
import dev.inmo.tgbotapi.types.chat.PreviewBot
|
||||
import dev.inmo.tgbotapi.types.chat.PrivateChat
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.ChatContentMessage
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.FromUserMessage
|
||||
import dev.inmo.tgbotapi.types.message.content.LivePhotoContent
|
||||
import dev.inmo.tgbotapi.types.message.content.PhotoContent
|
||||
import dev.inmo.tgbotapi.types.message.content.StoryContent
|
||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||
import dev.inmo.tgbotapi.types.message.content.VideoContent
|
||||
import dev.inmo.tgbotapi.types.message.content.VisualMediaGroupPartContent
|
||||
import dev.inmo.tgbotapi.types.message.textsources.BotCommandTextSource
|
||||
import dev.inmo.tgbotapi.types.stories.InputStoryContent
|
||||
import dev.inmo.tgbotapi.types.stories.InputStoryContent.*
|
||||
import dev.inmo.tgbotapi.types.stories.StoryArea
|
||||
import dev.inmo.tgbotapi.types.stories.StoryAreaPosition
|
||||
import dev.inmo.tgbotapi.types.stories.StoryAreaType
|
||||
import dev.inmo.tgbotapi.types.toChatId
|
||||
import dev.inmo.tgbotapi.utils.botCommand
|
||||
import dev.inmo.tgbotapi.utils.code
|
||||
import dev.inmo.tgbotapi.utils.extensions.splitForText
|
||||
@@ -324,14 +330,51 @@ suspend fun main(args: Array<String>) {
|
||||
}
|
||||
} else {
|
||||
giftsFlow.collect { giftsPage ->
|
||||
giftsPage.gifts.joinToString {
|
||||
it.toString()
|
||||
giftsPage.gifts.joinToString("\n\n") { gift ->
|
||||
buildString {
|
||||
gift.ownedGiftId?.let { appendLine("Owned gift ID ${it.string}") }
|
||||
append(gift)
|
||||
}
|
||||
}.splitForText().forEach { message ->
|
||||
reply(it, message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
val deleteGiftCommand = "delete_business_account_gift"
|
||||
onCommandWithArgs(deleteGiftCommand, initialFilter = { message ->
|
||||
val command = message.content.textSources.firstOrNull() as? BotCommandTextSource
|
||||
message.chat is PrivateChat && message.forwardInfo == null && command?.command == deleteGiftCommand
|
||||
}) { message, arguments ->
|
||||
val ownedGiftId = arguments.singleOrNull()?.takeIf { it.isNotBlank() }
|
||||
if (ownedGiftId == null) {
|
||||
reply(message, "Use /$deleteGiftCommand <owned_gift_id> to permanently delete one gift.")
|
||||
return@onCommandWithArgs
|
||||
}
|
||||
val connectionId = businessConnectionsChatsMutex.withLock {
|
||||
chatsBusinessConnections[message.chat.id.toChatId()]
|
||||
}
|
||||
if (connectionId == null) {
|
||||
reply(message, "No business account is connected to this chat.")
|
||||
return@onCommandWithArgs
|
||||
}
|
||||
val result = try {
|
||||
val connection = getBusinessConnection(connectionId)
|
||||
when {
|
||||
!connection.isEnabled || connection.user.id != (message as? FromUserMessage)?.from?.id ->
|
||||
"Only the owner of an active business connection can delete a gift."
|
||||
!connection.rights.canConvertGiftsToStars ->
|
||||
"Grant the Convert gifts to Stars business right to delete a gift."
|
||||
else -> {
|
||||
deleteBusinessAccountGift(connectionId, GiftId(ownedGiftId))
|
||||
"Deleted owned gift $ownedGiftId."
|
||||
}
|
||||
}
|
||||
} catch (error: BotException) {
|
||||
error.response?.description ?: "Telegram could not delete this gift."
|
||||
}
|
||||
reply(message, result)
|
||||
}
|
||||
onCommand("set_business_account_bio", requireOnlyCommandInMessage = false, initialFilter = { it.chat is PrivateChat }) {
|
||||
val initialBio = getChat(it.chat).extendedPrivateChatOrThrow().bio
|
||||
val bio = it.content.text.removePrefix("/set_business_account_bio").trim()
|
||||
@@ -463,7 +506,7 @@ suspend fun main(args: Array<String>) {
|
||||
activePeriod = PostStory.ACTIVE_PERIOD_6_HOURS,
|
||||
areas = listOf(
|
||||
StoryArea(
|
||||
StoryAreaPosition(
|
||||
AreaPosition(
|
||||
x = Percentage.of100(50.0),
|
||||
y = Percentage.of100(50.0),
|
||||
width = Percentage.of100(8.0),
|
||||
|
||||
Reference in new issue
Block a user