From 94c014b3082d4b1003bfb499e01798f4ba0394db Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 9 Jan 2024 17:56:33 +0600 Subject: [PATCH] add LinkPreviewsBot --- LinkPreviewsBot/README.md | 9 ++ LinkPreviewsBot/build.gradle | 21 +++++ .../src/main/kotlin/LinkPreviewsBot.kt | 93 +++++++++++++++++++ gradle.properties | 2 +- settings.gradle | 2 + 5 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 LinkPreviewsBot/README.md create mode 100644 LinkPreviewsBot/build.gradle create mode 100644 LinkPreviewsBot/src/main/kotlin/LinkPreviewsBot.kt diff --git a/LinkPreviewsBot/README.md b/LinkPreviewsBot/README.md new file mode 100644 index 0000000..c5ae135 --- /dev/null +++ b/LinkPreviewsBot/README.md @@ -0,0 +1,9 @@ +# ReactionsInfoBot + +This bot will resend messages with links with all variants of `LinkPreviewOptions` + +## Launch + +```bash +../gradlew run --args="BOT_TOKEN" +``` diff --git a/LinkPreviewsBot/build.gradle b/LinkPreviewsBot/build.gradle new file mode 100644 index 0000000..a699082 --- /dev/null +++ b/LinkPreviewsBot/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="LinkPreviewsBotKt" + + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" + + implementation "dev.inmo:tgbotapi:$telegram_bot_api_version" +} diff --git a/LinkPreviewsBot/src/main/kotlin/LinkPreviewsBot.kt b/LinkPreviewsBot/src/main/kotlin/LinkPreviewsBot.kt new file mode 100644 index 0000000..ad787f5 --- /dev/null +++ b/LinkPreviewsBot/src/main/kotlin/LinkPreviewsBot.kt @@ -0,0 +1,93 @@ +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.bot.ktor.telegramBot +import dev.inmo.tgbotapi.extensions.api.chat.get.getChat +import dev.inmo.tgbotapi.extensions.api.send.copyMessage +import dev.inmo.tgbotapi.extensions.api.send.reply +import dev.inmo.tgbotapi.extensions.api.send.send +import dev.inmo.tgbotapi.extensions.api.send.setMessageReaction +import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatMessageReactionUpdatedByUser +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatMessageReactionsCountUpdated +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage +import dev.inmo.tgbotapi.extensions.utils.textLinkTextSourceOrNull +import dev.inmo.tgbotapi.extensions.utils.uRLTextSourceOrNull +import dev.inmo.tgbotapi.extensions.utils.withContentOrNull +import dev.inmo.tgbotapi.types.LinkPreviewOptions +import dev.inmo.tgbotapi.types.chat.ExtendedChat +import dev.inmo.tgbotapi.types.message.content.TextContent +import dev.inmo.tgbotapi.types.message.content.TextedContent +import dev.inmo.tgbotapi.types.reactions.Reaction +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 { + onContentMessage { + val url = it.withContentOrNull() ?.let { + it.content.textSources.firstNotNullOfOrNull { + it.textLinkTextSourceOrNull() ?.url ?: it.uRLTextSourceOrNull() ?.source + } + } ?: null.apply { + reply(it) { + regular("I am support only content with text contains url only") + } + } ?: return@onContentMessage + it.withContentOrNull() ?.let { + send( + it.chat, + it.content.textSources, + linkPreviewOptions = LinkPreviewOptions.Disabled + ) + send( + it.chat, + it.content.textSources, + linkPreviewOptions = LinkPreviewOptions.Large(url, showAboveText = true) + ) + send( + it.chat, + it.content.textSources, + linkPreviewOptions = LinkPreviewOptions.Large(url, showAboveText = false) + ) + send( + it.chat, + it.content.textSources, + linkPreviewOptions = LinkPreviewOptions.Small(url, showAboveText = true) + ) + send( + it.chat, + it.content.textSources, + linkPreviewOptions = LinkPreviewOptions.Small(url, showAboveText = false) + ) + send( + it.chat, + it.content.textSources, + linkPreviewOptions = LinkPreviewOptions.Default(url, showAboveText = true) + ) + send( + it.chat, + it.content.textSources, + linkPreviewOptions = LinkPreviewOptions.Default(url, showAboveText = false) + ) + } + } + }.join() +} diff --git a/gradle.properties b/gradle.properties index 25cd87e..04f7c76 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-build2042 +telegram_bot_api_version=10.0.0-branch_10.0.0-build2046 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 53e22d1..a12a93e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -43,3 +43,5 @@ include ":StickerSetHandler" include ":InlineQueriesBot" include ":ReactionsInfoBot" + +include ":LinkPreviewsBot"