From 05e289975ab9f194b11caecc23ace83fa6e0be18 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Mon, 8 Jan 2024 15:46:23 +0600 Subject: [PATCH] add reactions info bot --- GetMeBot/src/main/kotlin/GetMeBot.kt | 19 ++++--- ReactionsInfoBot/README.md | 9 ++++ ReactionsInfoBot/build.gradle | 21 ++++++++ .../src/main/kotlin/ReactionsInfoBot.kt | 54 +++++++++++++++++++ .../src/commonMain/kotlin/ResenderBot.kt | 13 ++++- gradle.properties | 2 +- settings.gradle | 2 + 7 files changed, 112 insertions(+), 8 deletions(-) create mode 100644 ReactionsInfoBot/README.md create mode 100644 ReactionsInfoBot/build.gradle create mode 100644 ReactionsInfoBot/src/main/kotlin/ReactionsInfoBot.kt diff --git a/GetMeBot/src/main/kotlin/GetMeBot.kt b/GetMeBot/src/main/kotlin/GetMeBot.kt index c5917cd..460b48c 100644 --- a/GetMeBot/src/main/kotlin/GetMeBot.kt +++ b/GetMeBot/src/main/kotlin/GetMeBot.kt @@ -5,6 +5,7 @@ import dev.inmo.kslog.common.filter.filtered import dev.inmo.kslog.common.setDefaultKSLog import dev.inmo.tgbotapi.bot.ktor.telegramBot import dev.inmo.tgbotapi.extensions.api.bot.getMe +import dev.inmo.tgbotapi.extensions.api.chat.get.getChat import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog /** @@ -12,13 +13,19 @@ import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog */ suspend fun main(vararg args: String) { 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)) + } + ) + } - setDefaultKSLog( - KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> - println(defaultMessageFormatter(level, tag, message, throwable)) - } - ) val bot = telegramBot(botToken) - println(bot.getMe()) + val me = bot.getMe() + println(me) + println(bot.getChat(me)) } diff --git a/ReactionsInfoBot/README.md b/ReactionsInfoBot/README.md new file mode 100644 index 0000000..72b17bf --- /dev/null +++ b/ReactionsInfoBot/README.md @@ -0,0 +1,9 @@ +# ReactionsInfoBot + +This bot will send info about user reactions in his PM with reply to message user reacted to + +## Launch + +```bash +../gradlew run --args="BOT_TOKEN" +``` diff --git a/ReactionsInfoBot/build.gradle b/ReactionsInfoBot/build.gradle new file mode 100644 index 0000000..f0554ac --- /dev/null +++ b/ReactionsInfoBot/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="ReactionsInfoBotKt" + + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + + implementation "dev.inmo:tgbotapi:$telegram_bot_api_version" +} diff --git a/ReactionsInfoBot/src/main/kotlin/ReactionsInfoBot.kt b/ReactionsInfoBot/src/main/kotlin/ReactionsInfoBot.kt new file mode 100644 index 0000000..543fbef --- /dev/null +++ b/ReactionsInfoBot/src/main/kotlin/ReactionsInfoBot.kt @@ -0,0 +1,54 @@ +import dev.inmo.kslog.common.KSLog +import dev.inmo.kslog.common.LogLevel +import dev.inmo.kslog.common.defaultMessageFormatter +import dev.inmo.kslog.common.filter.filtered +import dev.inmo.kslog.common.setDefaultKSLog +import dev.inmo.tgbotapi.bot.ktor.telegramBot +import dev.inmo.tgbotapi.extensions.api.bot.getMe +import dev.inmo.tgbotapi.extensions.api.chat.get.getChat +import dev.inmo.tgbotapi.extensions.api.send.reply +import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatMessageReactionUpdatedByUser +import dev.inmo.tgbotapi.types.reactions.Reaction +import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog +import dev.inmo.tgbotapi.utils.customEmoji +import dev.inmo.tgbotapi.utils.regular + +/** + * This bot will send info about user reactions in his PM with reply to message user reacted to + */ +suspend fun main(vararg args: String) { + 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) + + bot.buildBehaviourWithLongPolling { + onChatMessageReactionUpdatedByUser { + val result = reply( + it.chat.id, + it.messageId, + replyInChat = it.reactedUser.id + ) { + regular("Current reactions for message in reply:\n") + it.new.forEach { + when (it) { + is Reaction.CustomEmoji -> regular("• ") + customEmoji(it.customEmojiId) + regular("(customEmojiId: ${it.customEmojiId})") + is Reaction.Emoji -> regular("• ${it.emoji}") + is Reaction.Unknown -> regular("• Unknown emoji ($it)") + } + regular("\n") + } + } + println(result) + } + }.join() +} diff --git a/ResenderBot/ResenderBotLib/src/commonMain/kotlin/ResenderBot.kt b/ResenderBot/ResenderBotLib/src/commonMain/kotlin/ResenderBot.kt index fe604d8..52e566c 100644 --- a/ResenderBot/ResenderBotLib/src/commonMain/kotlin/ResenderBot.kt +++ b/ResenderBot/ResenderBotLib/src/commonMain/kotlin/ResenderBot.kt @@ -8,6 +8,10 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilte import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.* import dev.inmo.tgbotapi.extensions.utils.shortcuts.* +import dev.inmo.tgbotapi.extensions.utils.withContentOrNull +import dev.inmo.tgbotapi.types.ReplyParameters +import dev.inmo.tgbotapi.types.message.content.TextContent +import dev.inmo.tgbotapi.types.quoteEntitiesField import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull import kotlinx.coroutines.* @@ -26,7 +30,14 @@ suspend fun activateResenderBot( it.content.createResend( chat.id, messageThreadId = it.threadIdOrNull, - replyToMessageId = it.messageId + replyParameters = it.replyInfo ?.messageMeta ?.let { meta -> + val quote = it.withContentOrNull() ?.content ?.quote + ReplyParameters( + meta, + entities = quote ?.textSources ?: emptyList(), + quotePosition = quote ?.position + ) + } ) ) { it.forEach(print) diff --git a/gradle.properties b/gradle.properties index 60a63d6..7ea112e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,7 +5,7 @@ org.gradle.jvmargs=-Xmx2344m kotlin_version=1.9.22 -telegram_bot_api_version=10.0.0-branch_10.0.0-build2030 +telegram_bot_api_version=10.0.0 micro_utils_version=0.20.25 serialization_version=1.6.2 ktor_version=2.3.7 diff --git a/settings.gradle b/settings.gradle index ea37daa..53e22d1 100644 --- a/settings.gradle +++ b/settings.gradle @@ -41,3 +41,5 @@ include ":LiveLocationsBot" include ":StickerSetHandler" include ":InlineQueriesBot" + +include ":ReactionsInfoBot"