mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-09-03 23:29:50 +00:00
add LinkPreviewsBot
This commit is contained in:
93
LinkPreviewsBot/src/main/kotlin/LinkPreviewsBot.kt
Normal file
93
LinkPreviewsBot/src/main/kotlin/LinkPreviewsBot.kt
Normal file
@@ -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<TextedContent>() ?.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<TextContent>() ?.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()
|
||||
}
|
Reference in New Issue
Block a user