11import com.benasher44.uuid.uuid4
2+ import dev.inmo.kslog.common.w
3+ import dev.inmo.micro_utils.coroutines.runCatchingLogging
24import dev.inmo.micro_utils.coroutines.runCatchingSafely
5+ import dev.inmo.micro_utils.coroutines.subscribeLoggingDropExceptions
36import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
7+ import dev.inmo.tgbotapi.bot.TelegramBot
8+ import dev.inmo.tgbotapi.extensions.api.bot.getMe
49import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
510import dev.inmo.tgbotapi.extensions.api.chat.forum.*
611import dev.inmo.tgbotapi.extensions.api.send.reply
712import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
813import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
14+ import dev.inmo.tgbotapi.extensions.utils.forumChatOrNull
15+ import dev.inmo.tgbotapi.extensions.utils.forumContentMessageOrNull
16+ import dev.inmo.tgbotapi.extensions.utils.privateChatOrNull
17+ import dev.inmo.tgbotapi.extensions.utils.privateForumChatOrNull
918import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.flushAccumulatedUpdates
1019import dev.inmo.tgbotapi.types.BotCommand
1120import dev.inmo.tgbotapi.types.ForumTopic
21+ import dev.inmo.tgbotapi.types.chat.PrivateChat
1222import dev.inmo.tgbotapi.types.commands.BotCommandScope
23+ import io.ktor.client.plugins.*
1324import kotlinx.coroutines.CoroutineScope
1425import kotlinx.coroutines.Dispatchers
1526import kotlinx.coroutines.delay
@@ -20,13 +31,33 @@ suspend fun main(vararg args: String) {
2031 CoroutineScope (Dispatchers .Default ),
2132 defaultExceptionsHandler = {
2233 it.printStackTrace()
34+ },
35+ builder = {
36+ client = client.config {
37+ install(HttpTimeout ) {
38+ requestTimeoutMillis = 30000
39+ socketTimeoutMillis = 30000
40+ connectTimeoutMillis = 30000
41+ }
42+ }
2343 }
2444 ) {
45+ suspend fun TelegramBot.isPrivateForumsEnabled (): Boolean {
46+ val me = getMe()
47+ if (me.hasTopicsEnabled == false ) {
48+ Log .w(" private forums are disabled. That means that they will not work in private chats" )
49+ }
50+ return me.hasTopicsEnabled
51+ }
52+ println ()
2553 flushAccumulatedUpdates()
26- allUpdatesFlow.subscribeSafelyWithoutExceptions (this ) {
54+ allUpdatesFlow.subscribeLoggingDropExceptions (this ) {
2755 println (it)
2856 }
2957 onCommand(" start_test_topics" ) {
58+ if (it.chat is PrivateChat && isPrivateForumsEnabled() == false ) {
59+ return @onCommand
60+ }
3061 val forumTopic = createForumTopic(
3162 it.chat,
3263 " Test" ,
@@ -44,21 +75,23 @@ suspend fun main(vararg args: String) {
4475
4576 reply(it, " Test topic has changed its name to Test 01" )
4677
47- delay(1000L )
48- closeForumTopic(
49- it.chat.id,
50- forumTopic.messageThreadId,
51- )
78+ if (it.chat.privateChatOrNull() == null ) { // For private forums it is prohibited to close or reopen topics
79+ delay(1000L )
80+ closeForumTopic(
81+ it.chat.id,
82+ forumTopic.messageThreadId,
83+ )
5284
53- reply(it, " Test topic has been closed" )
85+ reply(it, " Test topic has been closed" )
5486
55- delay(1000L )
56- reopenForumTopic(
57- it.chat.id,
58- forumTopic.messageThreadId,
59- )
87+ delay(1000L )
88+ reopenForumTopic(
89+ it.chat.id,
90+ forumTopic.messageThreadId,
91+ )
6092
61- reply(it, " Test topic has been reopened" )
93+ reply(it, " Test topic has been reopened" )
94+ }
6295
6396 delay(1000L )
6497 deleteForumTopic(
@@ -68,68 +101,80 @@ suspend fun main(vararg args: String) {
68101
69102 reply(it, " Test topic has been deleted" )
70103
71- delay(1000L )
72- hideGeneralForumTopic(
73- it.chat.id,
74- )
104+ if (it.chat.privateChatOrNull() == null ) { // For private forums it is prohibited to close or reopen topics
105+ delay(1000L )
106+ hideGeneralForumTopic(
107+ it.chat.id,
108+ )
75109
76- reply(it, " General topic has been hidden" )
110+ reply(it, " General topic has been hidden" )
77111
78- delay(1000L )
79- unhideGeneralForumTopic(
80- it.chat.id
81- )
112+ delay(1000L )
113+ unhideGeneralForumTopic(
114+ it.chat.id
115+ )
82116
83- reply(it, " General topic has been shown" )
117+ reply(it, " General topic has been shown" )
84118
85- delay(1000L )
86- runCatchingSafely(
87- { _ ->
88- reopenGeneralForumTopic(
89- it.chat.id
90- )
119+ delay(1000L )
120+ runCatchingSafely(
121+ { _ ->
122+ reopenGeneralForumTopic(
123+ it.chat.id
124+ )
91125
126+ closeGeneralForumTopic(
127+ it.chat.id
128+ )
129+ }
130+ ) {
92131 closeGeneralForumTopic(
93132 it.chat.id
94133 )
95134 }
96- ) {
97- closeGeneralForumTopic(
135+
136+ reply(it, " General topic has been closed" )
137+
138+ delay(1000L )
139+ reopenGeneralForumTopic(
98140 it.chat.id
99141 )
100- }
101142
102- reply(it, " General topic has been closed " )
143+ reply(it, " General topic has been opened " )
103144
104- delay(1000L )
105- reopenGeneralForumTopic(
106- it.chat.id
107- )
145+ delay(1000L )
146+ editGeneralForumTopic(
147+ it.chat.id,
148+ uuid4().toString().take(10 )
149+ )
108150
109- reply(it, " General topic has been opened " )
151+ reply(it, " General topic has been renamed " )
110152
111- delay(1000L )
112- editGeneralForumTopic(
113- it.chat.id,
114- uuid4().toString().take( 10 )
115- )
153+ delay(1000L )
154+ editGeneralForumTopic(
155+ it.chat.id,
156+ " Main topic "
157+ )
116158
117- reply(it, " General topic has been renamed" )
159+ reply(it, " General topic has been renamed" )
160+ }
118161
119162 delay(1000L )
120- editGeneralForumTopic(
121- it.chat.id,
122- " Main topic"
123- )
163+ }
124164
125- reply(it, " General topic has been renamed" )
165+ onCommand(" delete_topic" ) {
166+ val chat = it.chat.forumChatOrNull() ? : return @onCommand
126167
127- delay( 1000L )
168+ deleteForumTopic(chat, chat.id.threadId ? : return @onCommand )
128169 }
129170
130171 setMyCommands(
131172 BotCommand (" start_test_topics" , " start test topics" ),
173+ BotCommand (" delete_topic" , " delete topic where message have been sent" ),
132174 scope = BotCommandScope .AllGroupChats
133175 )
176+ allUpdatesFlow.subscribeLoggingDropExceptions(this ) {
177+ println (it)
178+ }
134179 }.second.join()
135180}
0 commit comments