|
| 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.subscribeLoggingDropExceptions |
| 6 | +import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions |
| 7 | +import dev.inmo.tgbotapi.abstracts.FromUser |
| 8 | +import dev.inmo.tgbotapi.extensions.api.bot.getMe |
| 9 | +import dev.inmo.tgbotapi.extensions.api.business.getBusinessAccountGiftsFlow |
| 10 | +import dev.inmo.tgbotapi.extensions.api.chat.members.promoteChatAdministrator |
| 11 | +import dev.inmo.tgbotapi.extensions.api.chat.members.promoteChatMember |
| 12 | +import dev.inmo.tgbotapi.extensions.api.chat.members.setChatMemberTag |
| 13 | +import dev.inmo.tgbotapi.extensions.api.gifts.getChatGiftsFlow |
| 14 | +import dev.inmo.tgbotapi.extensions.api.gifts.getUserGiftsFlow |
| 15 | +import dev.inmo.tgbotapi.extensions.api.send.reply |
| 16 | +import dev.inmo.tgbotapi.extensions.api.send.withTypingAction |
| 17 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling |
| 18 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand |
| 19 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage |
| 20 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayCompleted |
| 21 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayContent |
| 22 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayCreated |
| 23 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayWinners |
| 24 | +import dev.inmo.tgbotapi.extensions.utils.extensions.raw.sender_chat |
| 25 | +import dev.inmo.tgbotapi.extensions.utils.extensions.raw.sender_tag |
| 26 | +import dev.inmo.tgbotapi.extensions.utils.fromUserOrNull |
| 27 | +import dev.inmo.tgbotapi.extensions.utils.groupContentMessageOrNull |
| 28 | +import dev.inmo.tgbotapi.extensions.utils.idChatIdentifierOrNull |
| 29 | +import dev.inmo.tgbotapi.extensions.utils.potentiallyFromUserGroupContentMessageOrNull |
| 30 | +import dev.inmo.tgbotapi.types.UserTag |
| 31 | +import dev.inmo.tgbotapi.types.chat.BusinessChat |
| 32 | +import dev.inmo.tgbotapi.types.chat.PrivateChat |
| 33 | +import dev.inmo.tgbotapi.types.chat.PublicChat |
| 34 | +import dev.inmo.tgbotapi.types.chat.UnknownChatType |
| 35 | +import dev.inmo.tgbotapi.types.gifts.OwnedGift |
| 36 | +import dev.inmo.tgbotapi.types.message.abstracts.OptionallyFromUserMessage |
| 37 | +import dev.inmo.tgbotapi.types.message.textsources.splitForText |
| 38 | +import dev.inmo.tgbotapi.utils.bold |
| 39 | +import dev.inmo.tgbotapi.utils.buildEntities |
| 40 | +import kotlinx.coroutines.CoroutineScope |
| 41 | +import kotlinx.coroutines.Dispatchers |
| 42 | + |
| 43 | +suspend fun main(vararg args: String) { |
| 44 | + val botToken = args.first() |
| 45 | + |
| 46 | + val isDebug = args.any { it == "debug" } |
| 47 | + val isTestServer = args.any { it == "testServer" } |
| 48 | + |
| 49 | + if (isDebug) { |
| 50 | + setDefaultKSLog( |
| 51 | + KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> |
| 52 | + println(defaultMessageFormatter(level, tag, message, throwable)) |
| 53 | + } |
| 54 | + ) |
| 55 | + } |
| 56 | + |
| 57 | + telegramBotWithBehaviourAndLongPolling(botToken, testServer = isTestServer) { |
| 58 | + // start here!! |
| 59 | + val me = getMe() |
| 60 | + println(me) |
| 61 | + |
| 62 | + onCommand("setChatMemberTag", requireOnlyCommandInMessage = false) { |
| 63 | + val reply = it.replyTo ?.groupContentMessageOrNull() ?: return@onCommand |
| 64 | + val title = it.content.text.removePrefix("/setChatMemberTag").removePrefix(" ") |
| 65 | + setChatMemberTag( |
| 66 | + chatId = reply.chat.id, |
| 67 | + userId = reply.fromUserOrNull() ?.user ?.id ?: return@onCommand, |
| 68 | + tag = UserTag(title) |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + onCommand("setCanManageTags", requireOnlyCommandInMessage = false) { |
| 73 | + val reply = it.replyTo ?.groupContentMessageOrNull() ?: return@onCommand |
| 74 | + val setOrUnset = it.content.text.removePrefix("/setCanManageTags").removePrefix(" ") == "true" |
| 75 | + promoteChatAdministrator( |
| 76 | + it.chat.id, |
| 77 | + reply.fromUserOrNull() ?.user ?.id ?: return@onCommand, |
| 78 | + canManageTags = setOrUnset |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + onCommand("removeChatMemberTag") { |
| 83 | + val reply = it.replyTo ?.groupContentMessageOrNull() ?: return@onCommand |
| 84 | + setChatMemberTag( |
| 85 | + chatId = reply.chat.id, |
| 86 | + userId = reply.fromUserOrNull() ?.user ?.id ?: return@onCommand, |
| 87 | + tag = null |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + onContentMessage { |
| 92 | + val groupContentMessage = it.potentiallyFromUserGroupContentMessageOrNull() ?: return@onContentMessage |
| 93 | + reply(it, "Tag after casting: ${groupContentMessage.senderTag}") |
| 94 | + reply(it, "Tag by getting via risk API: ${it.sender_tag}") |
| 95 | + } |
| 96 | + |
| 97 | + allUpdatesFlow.subscribeLoggingDropExceptions(this) { |
| 98 | + println(it) |
| 99 | + } |
| 100 | + }.second.join() |
| 101 | +} |
0 commit comments