mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2026-11-27 13:52: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
@@ -1,7 +1,7 @@
|
||||
# StarTransactionsBot
|
||||
|
||||
A long-polling Telegram Stars playground for invoices, transaction history, paid
|
||||
media, pre-checkout approval, and refund updates.
|
||||
A long-polling bot for Telegram Stars invoices, transaction history, paid media,
|
||||
refund updates, and TON wallet information.
|
||||
|
||||
## Commands
|
||||
|
||||
@@ -10,6 +10,9 @@ media, pre-checkout approval, and refund updates.
|
||||
automatically.
|
||||
- `/transactions` works only in the chat whose numeric ID was supplied as the
|
||||
second program argument. It replies with the bot's first transaction page.
|
||||
- `/wallet_card` in your private chat with the bot reads the public TON wallet card
|
||||
on your profile with `getChat`. It shows the wallet address, balance in the
|
||||
smallest units, and NFT sticker count, or reports that the card is absent.
|
||||
|
||||
The commands are not registered in Telegram's command menu.
|
||||
|
||||
@@ -34,6 +37,12 @@ Pagination state is encoded in callback data and is not persisted by the bot.
|
||||
paid media, retaining its caption above the media; only photos and videos are used.
|
||||
- Paid-media-info messages are printed to standard output.
|
||||
- Refunded-payment events receive a reply containing the payment information.
|
||||
- `onTonWalletTransfer` replies to a transfer service message with the signed
|
||||
amount, peer wallet address, and transaction ID. The amount stays in Telegram's
|
||||
decimal-string representation. Outgoing amounts are negative, and an empty
|
||||
transaction ID appears as pending.
|
||||
- `onPaidMessagesWaived` acknowledges the service message Telegram sends when
|
||||
someone uses a link that grants free messages to the recipient.
|
||||
- Every received update is printed to standard output.
|
||||
|
||||
## Setup, permissions, and payment safety
|
||||
|
||||
@@ -11,7 +11,7 @@ buildscript {
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'application'
|
||||
|
||||
mainClassName="StarTransactionsBotKt"
|
||||
application.mainClass = "StarTransactionsBotKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
|
||||
@@ -4,6 +4,7 @@ import dev.inmo.kslog.common.defaultMessageFormatter
|
||||
import dev.inmo.kslog.common.setDefaultKSLog
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.tgbotapi.extensions.api.answers.payments.answerPreCheckoutQueryOk
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||
import dev.inmo.tgbotapi.extensions.api.edit.edit
|
||||
import dev.inmo.tgbotapi.extensions.api.files.downloadFileToTemp
|
||||
import dev.inmo.tgbotapi.extensions.api.get.getStarTransactions
|
||||
@@ -11,6 +12,7 @@ import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.api.send.send
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.extendedPrivateChatOrNull
|
||||
import dev.inmo.tgbotapi.extensions.utils.extensions.sameChat
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.withContentOrNull
|
||||
@@ -19,6 +21,7 @@ import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.RawChatId
|
||||
import dev.inmo.tgbotapi.types.UserId
|
||||
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
|
||||
import dev.inmo.tgbotapi.types.chat.PrivateChat
|
||||
import dev.inmo.tgbotapi.types.files.*
|
||||
import dev.inmo.tgbotapi.types.media.TelegramPaidMediaPhoto
|
||||
import dev.inmo.tgbotapi.types.media.TelegramPaidMediaVideo
|
||||
@@ -33,6 +36,7 @@ import dev.inmo.tgbotapi.utils.bold
|
||||
import dev.inmo.tgbotapi.utils.buildEntities
|
||||
import dev.inmo.tgbotapi.utils.regular
|
||||
import dev.inmo.tgbotapi.utils.row
|
||||
import dev.inmo.tgbotapi.utils.tonAddress
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
@@ -42,8 +46,8 @@ import kotlinx.coroutines.Dispatchers
|
||||
* The first element of [args] must be the bot token and the second a numeric admin
|
||||
* user/chat ID authorized for `/transactions`. Optional exact values `debug` and
|
||||
* `testServer` enable diagnostic logging and Telegram's test environment. Other
|
||||
* handlers create a one-Star invoice or paid media and report payment-related
|
||||
* updates.
|
||||
* handlers display the current user's TON wallet card, report TON transfers and
|
||||
* free-message access, or create a one-Star invoice or paid media.
|
||||
*/
|
||||
suspend fun main(vararg args: String) {
|
||||
val botToken = args.first()
|
||||
@@ -79,6 +83,32 @@ suspend fun main(vararg args: String) {
|
||||
answerPreCheckoutQueryOk(it)
|
||||
}
|
||||
|
||||
onCommand("wallet_card", initialFilter = { it.chat is PrivateChat }) { message ->
|
||||
val card = getChat(message.chat.id).extendedPrivateChatOrNull()?.tonWalletCard
|
||||
if (card == null) {
|
||||
reply(message, "Your profile has no public TON wallet card.")
|
||||
return@onCommand
|
||||
}
|
||||
reply(message) {
|
||||
+"Wallet "
|
||||
tonAddress(card.address.value)
|
||||
+"\nBalance of ${card.amount} in the smallest units\n${card.stickers.size} NFT stickers"
|
||||
}
|
||||
}
|
||||
|
||||
onTonWalletTransfer { message ->
|
||||
val transfer = message.chatEvent
|
||||
reply(message) {
|
||||
+"TON transfer of ${transfer.amount} in the smallest units\nPeer wallet "
|
||||
tonAddress(transfer.peerAddress.value)
|
||||
+"\nTransaction ${transfer.transactionId.ifEmpty { "pending" }}"
|
||||
}
|
||||
}
|
||||
|
||||
onPaidMessagesWaived { message ->
|
||||
reply(message, "The sender used a link that grants free messages to this recipient.")
|
||||
}
|
||||
|
||||
val transactionsDataPrefix = "getStarTransactions"
|
||||
fun buildTransactionsData(offset: Int, limit: Int = 10) = "$transactionsDataPrefix $offset $limit"
|
||||
fun parseTransactionsData(data: String): Pair<Int, Int> = data.split(" ").drop(1).let {
|
||||
|
||||
Reference in new issue
Block a user