mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2026-03-03 17:42:21 +00:00
fix of topics according to 9.3
This commit is contained in:
@@ -1,15 +1,26 @@
|
|||||||
import com.benasher44.uuid.uuid4
|
import com.benasher44.uuid.uuid4
|
||||||
|
import dev.inmo.kslog.common.w
|
||||||
|
import dev.inmo.micro_utils.coroutines.runCatchingLogging
|
||||||
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
||||||
|
import dev.inmo.micro_utils.coroutines.subscribeLoggingDropExceptions
|
||||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
|
import dev.inmo.tgbotapi.bot.TelegramBot
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.*
|
import dev.inmo.tgbotapi.extensions.api.chat.forum.*
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.forumChatOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.forumContentMessageOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.privateChatOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.privateForumChatOrNull
|
||||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.flushAccumulatedUpdates
|
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.flushAccumulatedUpdates
|
||||||
import dev.inmo.tgbotapi.types.BotCommand
|
import dev.inmo.tgbotapi.types.BotCommand
|
||||||
import dev.inmo.tgbotapi.types.ForumTopic
|
import dev.inmo.tgbotapi.types.ForumTopic
|
||||||
|
import dev.inmo.tgbotapi.types.chat.PrivateChat
|
||||||
import dev.inmo.tgbotapi.types.commands.BotCommandScope
|
import dev.inmo.tgbotapi.types.commands.BotCommandScope
|
||||||
|
import io.ktor.client.plugins.*
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
@@ -20,13 +31,33 @@ suspend fun main(vararg args: String) {
|
|||||||
CoroutineScope(Dispatchers.Default),
|
CoroutineScope(Dispatchers.Default),
|
||||||
defaultExceptionsHandler = {
|
defaultExceptionsHandler = {
|
||||||
it.printStackTrace()
|
it.printStackTrace()
|
||||||
|
},
|
||||||
|
builder = {
|
||||||
|
client = client.config {
|
||||||
|
install(HttpTimeout) {
|
||||||
|
requestTimeoutMillis = 30000
|
||||||
|
socketTimeoutMillis = 30000
|
||||||
|
connectTimeoutMillis = 30000
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
) {
|
) {
|
||||||
|
suspend fun TelegramBot.isPrivateForumsEnabled(): Boolean {
|
||||||
|
val me = getMe()
|
||||||
|
if (me.hasTopicsEnabled == false) {
|
||||||
|
Log.w("private forums are disabled. That means that they will not work in private chats")
|
||||||
|
}
|
||||||
|
return me.hasTopicsEnabled
|
||||||
|
}
|
||||||
|
println()
|
||||||
flushAccumulatedUpdates()
|
flushAccumulatedUpdates()
|
||||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
allUpdatesFlow.subscribeLoggingDropExceptions(this) {
|
||||||
println(it)
|
println(it)
|
||||||
}
|
}
|
||||||
onCommand("start_test_topics") {
|
onCommand("start_test_topics") {
|
||||||
|
if (it.chat is PrivateChat && isPrivateForumsEnabled() == false) {
|
||||||
|
return@onCommand
|
||||||
|
}
|
||||||
val forumTopic = createForumTopic(
|
val forumTopic = createForumTopic(
|
||||||
it.chat,
|
it.chat,
|
||||||
"Test",
|
"Test",
|
||||||
@@ -44,6 +75,7 @@ suspend fun main(vararg args: String) {
|
|||||||
|
|
||||||
reply(it, "Test topic has changed its name to Test 01")
|
reply(it, "Test topic has changed its name to Test 01")
|
||||||
|
|
||||||
|
if (it.chat.privateChatOrNull() == null) { // For private forums it is prohibited to close or reopen topics
|
||||||
delay(1000L)
|
delay(1000L)
|
||||||
closeForumTopic(
|
closeForumTopic(
|
||||||
it.chat.id,
|
it.chat.id,
|
||||||
@@ -59,6 +91,7 @@ suspend fun main(vararg args: String) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
reply(it, "Test topic has been reopened")
|
reply(it, "Test topic has been reopened")
|
||||||
|
}
|
||||||
|
|
||||||
delay(1000L)
|
delay(1000L)
|
||||||
deleteForumTopic(
|
deleteForumTopic(
|
||||||
@@ -68,6 +101,7 @@ suspend fun main(vararg args: String) {
|
|||||||
|
|
||||||
reply(it, "Test topic has been deleted")
|
reply(it, "Test topic has been deleted")
|
||||||
|
|
||||||
|
if (it.chat.privateChatOrNull() == null) { // For private forums it is prohibited to close or reopen topics
|
||||||
delay(1000L)
|
delay(1000L)
|
||||||
hideGeneralForumTopic(
|
hideGeneralForumTopic(
|
||||||
it.chat.id,
|
it.chat.id,
|
||||||
@@ -123,13 +157,24 @@ suspend fun main(vararg args: String) {
|
|||||||
)
|
)
|
||||||
|
|
||||||
reply(it, "General topic has been renamed")
|
reply(it, "General topic has been renamed")
|
||||||
|
}
|
||||||
|
|
||||||
delay(1000L)
|
delay(1000L)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onCommand("delete_topic") {
|
||||||
|
val chat = it.chat.forumChatOrNull() ?: return@onCommand
|
||||||
|
|
||||||
|
deleteForumTopic(chat, chat.id.threadId ?: return@onCommand)
|
||||||
|
}
|
||||||
|
|
||||||
setMyCommands(
|
setMyCommands(
|
||||||
BotCommand("start_test_topics", "start test topics"),
|
BotCommand("start_test_topics", "start test topics"),
|
||||||
|
BotCommand("delete_topic", "delete topic where message have been sent"),
|
||||||
scope = BotCommandScope.AllGroupChats
|
scope = BotCommandScope.AllGroupChats
|
||||||
)
|
)
|
||||||
|
allUpdatesFlow.subscribeLoggingDropExceptions(this) {
|
||||||
|
println(it)
|
||||||
|
}
|
||||||
}.second.join()
|
}.second.join()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user