From d6a6ad8d37f15fbf9913097a8ee6a3dd0346d130 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Fri, 19 Apr 2024 18:10:46 +0600 Subject: [PATCH] improvements in samples --- BusinessConnectionsBot/README.md | 9 ++ BusinessConnectionsBot/build.gradle | 21 +++++ .../src/main/kotlin/BusinessConnectionsBot.kt | 85 +++++++++++++++++++ .../src/main/kotlin/UserChatShared.kt | 13 +++ settings.gradle | 2 + 5 files changed, 130 insertions(+) create mode 100644 BusinessConnectionsBot/README.md create mode 100644 BusinessConnectionsBot/build.gradle create mode 100644 BusinessConnectionsBot/src/main/kotlin/BusinessConnectionsBot.kt diff --git a/BusinessConnectionsBot/README.md b/BusinessConnectionsBot/README.md new file mode 100644 index 0000000..2461998 --- /dev/null +++ b/BusinessConnectionsBot/README.md @@ -0,0 +1,9 @@ +# BusinessConnectionBotBot + +When bot connected or disconnected to the business chat, it will notify this chat + +## Launch + +```bash +../gradlew run --args="BOT_TOKEN" +``` diff --git a/BusinessConnectionsBot/build.gradle b/BusinessConnectionsBot/build.gradle new file mode 100644 index 0000000..14a48e1 --- /dev/null +++ b/BusinessConnectionsBot/build.gradle @@ -0,0 +1,21 @@ +buildscript { + repositories { + mavenCentral() + } + + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +apply plugin: 'kotlin' +apply plugin: 'application' + +mainClassName="BusinessConnectionsBotKt" + + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + + implementation "dev.inmo:tgbotapi:$telegram_bot_api_version" +} diff --git a/BusinessConnectionsBot/src/main/kotlin/BusinessConnectionsBot.kt b/BusinessConnectionsBot/src/main/kotlin/BusinessConnectionsBot.kt new file mode 100644 index 0000000..49f79a1 --- /dev/null +++ b/BusinessConnectionsBot/src/main/kotlin/BusinessConnectionsBot.kt @@ -0,0 +1,85 @@ +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.extensions.api.bot.getMe +import dev.inmo.tgbotapi.extensions.api.get.getBusinessConnection +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.ifBusinessContentMessage +import dev.inmo.tgbotapi.types.ChatId +import dev.inmo.tgbotapi.types.business_connection.BusinessConnectionId +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.sync.Mutex +import kotlinx.coroutines.sync.withLock + +suspend fun main(args: Array) { + val botToken = args.first() + val isDebug = args.getOrNull(1) == "debug" + + if (isDebug) { + setDefaultKSLog( + KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> + println(defaultMessageFormatter(level, tag, message, throwable)) + } + ) + } + + val businessConnectionsChats = mutableMapOf() + val businessConnectionsChatsMutex = Mutex() + + telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) { + val me = getMe() + println(me) + + onBusinessConnectionEnabled { + businessConnectionsChatsMutex.withLock { + businessConnectionsChats[it.id] = it.userChatId + } + send(it.userChatId, "Business connection ${it.businessConnectionId.string} has been enabled") + } + onBusinessConnectionDisabled { + businessConnectionsChatsMutex.withLock { + businessConnectionsChats.remove(it.id) + } + send(it.userChatId, "Business connection ${it.businessConnectionId.string} has been disabled") + } + + onContentMessage { + it.ifBusinessContentMessage { + val sent = execute(it.content.createResend(it.from.id)) + if (it.sentByBusinessConnectionOwner) { + reply(sent, "You have sent this message to the ${it.businessConnectionId.string} related chat") + } else { + reply(sent, "User have sent this message to you in the ${it.businessConnectionId.string} related chat") + } + } + } + onEditedContentMessage { + it.ifBusinessContentMessage { + val sent = execute(it.content.createResend(it.from.id)) + if (it.sentByBusinessConnectionOwner) { + reply(sent, "You have edited this message in the ${it.businessConnectionId.string} related chat") + } else { + reply(sent, "User have edited this message to you in the ${it.businessConnectionId.string} related chat") + } + } + } + onBusinessMessagesDeleted { + var businessConnectionOwnerChat = businessConnectionsChatsMutex.withLock { + businessConnectionsChats[it.businessConnectionId] + } + if (businessConnectionOwnerChat == null) { + val businessConnection = getBusinessConnection(it.businessConnectionId) + businessConnectionsChatsMutex.withLock { + businessConnectionsChats[businessConnection.businessConnectionId] = businessConnection.userChatId + } + businessConnectionOwnerChat = businessConnection.userChatId + } + send(businessConnectionOwnerChat, "There are several removed messages in chat ${it.chat.id}: ${it.messageIds}") + } + }.second.join() +} \ No newline at end of file diff --git a/UserChatShared/src/main/kotlin/UserChatShared.kt b/UserChatShared/src/main/kotlin/UserChatShared.kt index c55102a..817e61a 100644 --- a/UserChatShared/src/main/kotlin/UserChatShared.kt +++ b/UserChatShared/src/main/kotlin/UserChatShared.kt @@ -1,3 +1,7 @@ +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.micro_utils.coroutines.runCatchingSafely import dev.inmo.tgbotapi.bot.ktor.telegramBot import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands @@ -18,6 +22,15 @@ import dev.inmo.tgbotapi.utils.row suspend fun main(args: Array) { val botToken = args.first() + val isDebug = args.getOrNull(1) == "debug" + + if (isDebug) { + setDefaultKSLog( + KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> + println(defaultMessageFormatter(level, tag, message, throwable)) + } + ) + } val bot = telegramBot(botToken) diff --git a/settings.gradle b/settings.gradle index a5231ca..57b91bf 100644 --- a/settings.gradle +++ b/settings.gradle @@ -47,3 +47,5 @@ include ":ReactionsInfoBot" include ":LinkPreviewsBot" include ":BoostsInfoBot" + +include ":BusinessConnectionsBot"