|
| 1 | +import dev.inmo.kslog.common.KSLog |
| 2 | +import dev.inmo.kslog.common.LogLevel |
| 3 | +import dev.inmo.kslog.common.defaultMessageFormatter |
| 4 | +import dev.inmo.kslog.common.setDefaultKSLog |
| 5 | +import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions |
| 6 | +import dev.inmo.tgbotapi.extensions.api.bot.getMe |
| 7 | +import dev.inmo.tgbotapi.extensions.api.business.getBusinessAccountGiftsFlow |
| 8 | +import dev.inmo.tgbotapi.extensions.api.gifts.getChatGiftsFlow |
| 9 | +import dev.inmo.tgbotapi.extensions.api.gifts.getUserGiftsFlow |
| 10 | +import dev.inmo.tgbotapi.extensions.api.send.reply |
| 11 | +import dev.inmo.tgbotapi.extensions.api.send.withTypingAction |
| 12 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling |
| 13 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand |
| 14 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayCompleted |
| 15 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayContent |
| 16 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayCreated |
| 17 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayWinners |
| 18 | +import dev.inmo.tgbotapi.types.chat.BusinessChat |
| 19 | +import dev.inmo.tgbotapi.types.chat.PrivateChat |
| 20 | +import dev.inmo.tgbotapi.types.chat.PublicChat |
| 21 | +import dev.inmo.tgbotapi.types.chat.UnknownChatType |
| 22 | +import dev.inmo.tgbotapi.types.gifts.OwnedGift |
| 23 | +import dev.inmo.tgbotapi.types.message.textsources.splitForText |
| 24 | +import dev.inmo.tgbotapi.utils.bold |
| 25 | +import dev.inmo.tgbotapi.utils.buildEntities |
| 26 | +import kotlinx.coroutines.CoroutineScope |
| 27 | +import kotlinx.coroutines.Dispatchers |
| 28 | + |
| 29 | +suspend fun main(vararg args: String) { |
| 30 | + val botToken = args.first() |
| 31 | + |
| 32 | + val isDebug = args.any { it == "debug" } |
| 33 | + val isTestServer = args.any { it == "testServer" } |
| 34 | + |
| 35 | + if (isDebug) { |
| 36 | + setDefaultKSLog( |
| 37 | + KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> |
| 38 | + println(defaultMessageFormatter(level, tag, message, throwable)) |
| 39 | + } |
| 40 | + ) |
| 41 | + } |
| 42 | + |
| 43 | + telegramBotWithBehaviourAndLongPolling(botToken, testServer = isTestServer) { |
| 44 | + // start here!! |
| 45 | + val me = getMe() |
| 46 | + println(me) |
| 47 | + |
| 48 | + onCommand("start") { |
| 49 | + val giftsFlow = when (val chat = it.chat) { |
| 50 | + is BusinessChat -> { |
| 51 | + getBusinessAccountGiftsFlow( |
| 52 | + chat.id.businessConnectionId |
| 53 | + ) |
| 54 | + } |
| 55 | + is PrivateChat -> { |
| 56 | + getUserGiftsFlow(it.chat.id) |
| 57 | + } |
| 58 | + is UnknownChatType, |
| 59 | + is PublicChat -> { |
| 60 | + getChatGiftsFlow(it.chat.id) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + withTypingAction(it.chat) { |
| 65 | + val texts = buildEntities { |
| 66 | + giftsFlow.collect { ownedGifts -> |
| 67 | + ownedGifts.gifts.forEach { |
| 68 | + when (it) { |
| 69 | + is OwnedGift.Regular.Common -> { |
| 70 | + bold("Type") + ": Regular common\n" |
| 71 | + bold("Id") + ": ${it.gift.id.string}\n" |
| 72 | + bold("Text") + ": ${it.text ?: "(None)"}\n" |
| 73 | + bold("Stars cost") + ": ${it.gift.starCount}\n" |
| 74 | + } |
| 75 | + is OwnedGift.Unique.Common -> { |
| 76 | + bold("Type") + ": Unique common\n" |
| 77 | + bold("Id") + ": ${it.gift.id ?.string ?: "(None)"}\n" |
| 78 | + bold("Name") + ": ${it.gift.name.value}\n" |
| 79 | + bold("Model") + ": ${it.gift.model.name}\n" |
| 80 | + bold("Number") + ": ${it.gift.number}\n" |
| 81 | + } |
| 82 | + is OwnedGift.Regular.OwnedByBusinessAccount -> { |
| 83 | + bold("Type") + ": Regular owned by business\n" |
| 84 | + bold("Id") + ": ${it.gift.id.string}\n" |
| 85 | + bold("Text") + ": ${it.text ?: "(None)"}\n" |
| 86 | + bold("Stars cost") + ": ${it.gift.starCount}\n" |
| 87 | + } |
| 88 | + is OwnedGift.Unique.OwnedByBusinessAccount -> { |
| 89 | + bold("Type") + ": Unique owned by business\n" |
| 90 | + bold("Id") + ": ${it.gift.id ?.string ?: "(None)"}\n" |
| 91 | + bold("Name") + ": ${it.gift.name.value}\n" |
| 92 | + bold("Model") + ": ${it.gift.model.name}\n" |
| 93 | + bold("Number") + ": ${it.gift.number}\n" |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + val preparedTexts = texts.splitForText() |
| 100 | + if (preparedTexts.isEmpty()) { |
| 101 | + reply(it, "This chat have no any gifts") |
| 102 | + } else { |
| 103 | + preparedTexts.forEach { preparedText -> reply(it, preparedText) } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +// allUpdatesFlow.subscribeSafelyWithoutExceptions(this) { |
| 109 | +// println(it) |
| 110 | +// } |
| 111 | + }.second.join() |
| 112 | +} |
0 commit comments