mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-12-25 15:45:38 +00:00
Compare commits
34 Commits
25.0.0
...
renovate/k
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f5b2355ea6 | ||
| 0e8714cf2b | |||
| ea74f884bf | |||
| 69cbc257b5 | |||
| d3e6014e06 | |||
| 1d260f82e9 | |||
| 5d0b48c4b7 | |||
| 34be1a25b2 | |||
| 990614e257 | |||
| 0d1bcf05fd | |||
| 7d5cb58a3f | |||
| 261df14412 | |||
| 81ba5831c3 | |||
| 0b9c715e25 | |||
| 0216919145 | |||
| e2d56a4d80 | |||
| 70aca52960 | |||
| 6c0d961339 | |||
|
|
a3cdf693f2 | ||
|
|
e378c6630c | ||
| 707ad9a160 | |||
| 68e9830a8f | |||
| 55ebdeadbc | |||
|
|
d4f3d4bc68 | ||
| b3d06c9773 | |||
| e6e3eabf97 | |||
| 47efedf311 | |||
| 8423b1377b | |||
| d0029603ce | |||
| 8d8fa74779 | |||
| 459a70c47b | |||
| 88102f3afa | |||
| a621058fdd | |||
| 56e072aabe |
@@ -34,6 +34,7 @@ import dev.inmo.tgbotapi.extensions.utils.ifBusinessContentMessage
|
|||||||
import dev.inmo.tgbotapi.extensions.utils.textContentOrNull
|
import dev.inmo.tgbotapi.extensions.utils.textContentOrNull
|
||||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.dataButton
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.dataButton
|
||||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineKeyboard
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineKeyboard
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.flushAccumulatedUpdates
|
||||||
import dev.inmo.tgbotapi.extensions.utils.withContentOrNull
|
import dev.inmo.tgbotapi.extensions.utils.withContentOrNull
|
||||||
import dev.inmo.tgbotapi.requests.abstracts.multipartFile
|
import dev.inmo.tgbotapi.requests.abstracts.multipartFile
|
||||||
import dev.inmo.tgbotapi.requests.business_connection.InputProfilePhoto
|
import dev.inmo.tgbotapi.requests.business_connection.InputProfilePhoto
|
||||||
@@ -84,6 +85,7 @@ suspend fun main(args: Array<String>) {
|
|||||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||||
val me = getMe()
|
val me = getMe()
|
||||||
println(me)
|
println(me)
|
||||||
|
flushAccumulatedUpdates()
|
||||||
|
|
||||||
onBusinessConnectionEnabled {
|
onBusinessConnectionEnabled {
|
||||||
businessConnectionsChatsMutex.withLock {
|
businessConnectionsChatsMutex.withLock {
|
||||||
@@ -471,5 +473,15 @@ suspend fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Will work when some premium user sending to some other user checklist
|
||||||
|
onChecklistContent {
|
||||||
|
execute(
|
||||||
|
it.content.createResend(
|
||||||
|
it.chat.id,
|
||||||
|
businessConnectionId = it.chat.id.businessConnectionId ?: chatsBusinessConnections[it.chat.id] ?: return@onChecklistContent
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
}.second.join()
|
}.second.join()
|
||||||
}
|
}
|
||||||
21
ChecklistsBot/build.gradle
Normal file
21
ChecklistsBot/build.gradle
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
apply plugin: 'kotlin'
|
||||||
|
apply plugin: 'application'
|
||||||
|
|
||||||
|
mainClassName="ChecklistsBotKt"
|
||||||
|
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
|
|
||||||
|
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||||
|
}
|
||||||
120
ChecklistsBot/src/main/kotlin/ChecklistsBot.kt
Normal file
120
ChecklistsBot/src/main/kotlin/ChecklistsBot.kt
Normal file
@@ -0,0 +1,120 @@
|
|||||||
|
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.micro_utils.coroutines.runCatchingLogging
|
||||||
|
import dev.inmo.micro_utils.coroutines.subscribeLoggingDropExceptions
|
||||||
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.bot.getMyStarBalance
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.resend
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.send
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.suggested.approveSuggestedPost
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextData
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildSubcontextInitialAction
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitSuggestedPostApproved
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitSuggestedPostDeclined
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChannelDirectMessagesConfigurationChanged
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChecklistContent
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChecklistTasksAdded
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChecklistTasksDone
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostApprovalFailed
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostApproved
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostDeclined
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostPaid
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostRefunded
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.channelDirectMessagesContentMessageOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.previewChannelDirectMessagesChatOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.suggestedChannelDirectMessagesContentMessageOrNull
|
||||||
|
import dev.inmo.tgbotapi.types.checklists.ChecklistTaskId
|
||||||
|
import dev.inmo.tgbotapi.types.message.SuggestedPostParameters
|
||||||
|
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
||||||
|
import dev.inmo.tgbotapi.types.message.content.ChecklistContent
|
||||||
|
import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList
|
||||||
|
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
||||||
|
import dev.inmo.tgbotapi.utils.bold
|
||||||
|
import dev.inmo.tgbotapi.utils.buildEntities
|
||||||
|
import dev.inmo.tgbotapi.utils.code
|
||||||
|
import dev.inmo.tgbotapi.utils.firstOf
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.flow.filter
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
|
|
||||||
|
suspend fun main(vararg args: String) {
|
||||||
|
val botToken = args.first()
|
||||||
|
|
||||||
|
val isDebug = args.any { it == "debug" }
|
||||||
|
val isTestServer = args.any { it == "testServer" }
|
||||||
|
|
||||||
|
if (isDebug) {
|
||||||
|
setDefaultKSLog(
|
||||||
|
KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
|
||||||
|
println(defaultMessageFormatter(level, tag, message, throwable))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
telegramBotWithBehaviourAndLongPolling(
|
||||||
|
botToken,
|
||||||
|
CoroutineScope(Dispatchers.Default),
|
||||||
|
testServer = isTestServer,
|
||||||
|
) {
|
||||||
|
// start here!!
|
||||||
|
val me = getMe()
|
||||||
|
println(me)
|
||||||
|
|
||||||
|
fun ChecklistContent.textBuilderTextSources(): TextSourcesList {
|
||||||
|
return buildEntities {
|
||||||
|
+checklist.textSources + "\n\n"
|
||||||
|
checklist.tasks.forEach { task ->
|
||||||
|
+"• "
|
||||||
|
code(
|
||||||
|
if (task.completionDate != null) {
|
||||||
|
"[x] "
|
||||||
|
} else {
|
||||||
|
"[ ] "
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
bold(task.textSources) + "\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onChecklistContent { messageWithContent ->
|
||||||
|
reply(messageWithContent) {
|
||||||
|
+messageWithContent.content.textBuilderTextSources()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onChecklistTasksDone { eventMessage ->
|
||||||
|
reply(
|
||||||
|
eventMessage,
|
||||||
|
checklistTaskId = eventMessage.chatEvent.markedAsDone ?.firstOrNull()
|
||||||
|
) {
|
||||||
|
eventMessage.chatEvent.checklistMessage.content.checklist
|
||||||
|
+eventMessage.chatEvent.checklistMessage.content.textBuilderTextSources()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onChecklistTasksAdded { messageWithContent ->
|
||||||
|
reply(
|
||||||
|
messageWithContent.chatEvent.checklistMessage,
|
||||||
|
checklistTaskId = messageWithContent.chatEvent.tasks.firstOrNull() ?.id
|
||||||
|
) {
|
||||||
|
+messageWithContent.chatEvent.checklistMessage.content.textBuilderTextSources()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
allUpdatesFlow.subscribeLoggingDropExceptions(this) {
|
||||||
|
println(it)
|
||||||
|
}
|
||||||
|
}.second.join()
|
||||||
|
}
|
||||||
@@ -4,10 +4,13 @@ import dev.inmo.kslog.common.defaultMessageFormatter
|
|||||||
import dev.inmo.kslog.common.setDefaultKSLog
|
import dev.inmo.kslog.common.setDefaultKSLog
|
||||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.bot.getMyStarBalance
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextData
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextData
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildSubcontextInitialAction
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildSubcontextInitialAction
|
||||||
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.onChannelDirectMessagesConfigurationChanged
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||||
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
||||||
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
||||||
@@ -79,6 +82,17 @@ suspend fun main(vararg args: String) {
|
|||||||
println(data.commonMessage)
|
println(data.commonMessage)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onCommand("getMyStarBalance") {
|
||||||
|
reply(
|
||||||
|
to = it,
|
||||||
|
text = getMyStarBalance().toString()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
onChannelDirectMessagesConfigurationChanged {
|
||||||
|
println(it.chatEvent)
|
||||||
|
}
|
||||||
|
|
||||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||||
println(it)
|
println(it)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
|
import dev.inmo.micro_utils.coroutines.awaitFirst
|
||||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
import dev.inmo.micro_utils.fsm.common.State
|
import dev.inmo.micro_utils.fsm.common.State
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.send
|
import dev.inmo.tgbotapi.extensions.api.send.send
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitAnyContentMessage
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitAnyContentMessage
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitCommandMessage
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndFSMAndStartLongPolling
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndFSMAndStartLongPolling
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.command
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.command
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.utils.containsCommand
|
||||||
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithArgs
|
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithArgs
|
||||||
import dev.inmo.tgbotapi.extensions.utils.extensions.sameThread
|
import dev.inmo.tgbotapi.extensions.utils.extensions.sameThread
|
||||||
import dev.inmo.tgbotapi.extensions.utils.textContentOrNull
|
import dev.inmo.tgbotapi.extensions.utils.textContentOrNull
|
||||||
@@ -13,10 +16,12 @@ import dev.inmo.tgbotapi.types.IdChatIdentifier
|
|||||||
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
||||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||||
import dev.inmo.tgbotapi.utils.botCommand
|
import dev.inmo.tgbotapi.utils.botCommand
|
||||||
|
import dev.inmo.tgbotapi.utils.firstOf
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.flow.filter
|
import kotlinx.coroutines.flow.filter
|
||||||
import kotlinx.coroutines.flow.first
|
import kotlinx.coroutines.flow.first
|
||||||
|
import kotlinx.coroutines.flow.map
|
||||||
|
|
||||||
sealed interface BotState : State
|
sealed interface BotState : State
|
||||||
data class ExpectContentOrStopState(override val context: IdChatIdentifier, val sourceMessage: CommonMessage<TextContent>) : BotState
|
data class ExpectContentOrStopState(override val context: IdChatIdentifier, val sourceMessage: CommonMessage<TextContent>) : BotState
|
||||||
@@ -48,20 +53,30 @@ suspend fun main(args: Array<String>) {
|
|||||||
+"Send me some content or " + botCommand("stop") + " if you want to stop sending"
|
+"Send me some content or " + botCommand("stop") + " if you want to stop sending"
|
||||||
}
|
}
|
||||||
|
|
||||||
val contentMessage = waitAnyContentMessage().filter { message ->
|
val contentMessage = firstOf(
|
||||||
|
{
|
||||||
|
waitCommandMessage("stop").filter { message ->
|
||||||
message.sameThread(it.sourceMessage)
|
message.sameThread(it.sourceMessage)
|
||||||
}.first()
|
}.first()
|
||||||
|
null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
waitAnyContentMessage().filter { message ->
|
||||||
|
message.sameThread(it.sourceMessage)
|
||||||
|
}.filter {
|
||||||
|
containsCommand(
|
||||||
|
"stop",
|
||||||
|
it.withContentOrNull<TextContent>() ?.content ?.textSources ?: return@filter false
|
||||||
|
) == false
|
||||||
|
}.first()
|
||||||
|
}
|
||||||
|
) ?: return@strictlyOn StopState(it.context)
|
||||||
|
|
||||||
val content = contentMessage.content
|
val content = contentMessage.content
|
||||||
|
|
||||||
when {
|
|
||||||
content is TextContent && content.text == "/stop"
|
|
||||||
|| content is TextContent && content.parseCommandsWithArgs().keys.contains("stop") -> StopState(it.context)
|
|
||||||
else -> {
|
|
||||||
execute(content.createResend(it.context))
|
execute(content.createResend(it.context))
|
||||||
it
|
it
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
strictlyOn<StopState> {
|
strictlyOn<StopState> {
|
||||||
send(it.context) { +"You have stopped sending of content" }
|
send(it.context) { +"You have stopped sending of content" }
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
import dev.inmo.kslog.common.KSLog
|
||||||
|
import dev.inmo.kslog.common.defaultMessageFormatter
|
||||||
|
import dev.inmo.kslog.common.filter.filtered
|
||||||
import dev.inmo.micro_utils.coroutines.subscribeLoggingDropExceptions
|
import dev.inmo.micro_utils.coroutines.subscribeLoggingDropExceptions
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.withTypingAction
|
import dev.inmo.tgbotapi.extensions.api.send.withTypingAction
|
||||||
@@ -10,7 +13,9 @@ import dev.inmo.tgbotapi.extensions.utils.withContentOrNull
|
|||||||
import dev.inmo.tgbotapi.types.ReplyParameters
|
import dev.inmo.tgbotapi.types.ReplyParameters
|
||||||
import dev.inmo.tgbotapi.types.message.abstracts.BusinessContentMessage
|
import dev.inmo.tgbotapi.types.message.abstracts.BusinessContentMessage
|
||||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||||
|
import dev.inmo.tgbotapi.utils.DefaultKTgBotAPIKSLog
|
||||||
import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull
|
import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull
|
||||||
|
import kotlinx.coroutines.CancellationException
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.currentCoroutineContext
|
import kotlinx.coroutines.currentCoroutineContext
|
||||||
@@ -19,7 +24,10 @@ suspend fun activateResenderBot(
|
|||||||
token: String,
|
token: String,
|
||||||
print: (Any) -> Unit
|
print: (Any) -> Unit
|
||||||
) {
|
) {
|
||||||
telegramBotWithBehaviourAndLongPolling(token, scope = CoroutineScope(currentCoroutineContext() + SupervisorJob())) {
|
telegramBotWithBehaviourAndLongPolling(
|
||||||
|
token,
|
||||||
|
scope = CoroutineScope(currentCoroutineContext() + SupervisorJob()),
|
||||||
|
) {
|
||||||
onContentMessage(
|
onContentMessage(
|
||||||
subcontextUpdatesFilter = MessageFilterByChat,
|
subcontextUpdatesFilter = MessageFilterByChat,
|
||||||
initialFilter = { it !is BusinessContentMessage<*> || !it.sentByBusinessConnectionOwner }
|
initialFilter = { it !is BusinessContentMessage<*> || !it.sentByBusinessConnectionOwner }
|
||||||
|
|||||||
@@ -1,4 +1,19 @@
|
|||||||
|
import dev.inmo.kslog.common.KSLog
|
||||||
|
import dev.inmo.kslog.common.LogLevel
|
||||||
|
import dev.inmo.kslog.common.defaultMessageFormatter
|
||||||
|
import dev.inmo.kslog.common.setDefaultKSLog
|
||||||
|
|
||||||
suspend fun main(args: Array<String>) {
|
suspend fun main(args: Array<String>) {
|
||||||
|
val isDebug = args.getOrNull(1) == "debug"
|
||||||
|
|
||||||
|
if (isDebug) {
|
||||||
|
setDefaultKSLog(
|
||||||
|
KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
|
||||||
|
println(defaultMessageFormatter(level, tag, message, throwable))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
activateResenderBot(args.first()) {
|
activateResenderBot(args.first()) {
|
||||||
println(it)
|
println(it)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,5 +18,5 @@ dependencies {
|
|||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
|
|
||||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||||
implementation 'io.ktor:ktor-client-logging-jvm:3.1.0'
|
implementation 'io.ktor:ktor-client-logging-jvm:3.3.3'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
# CustomBot
|
# StarTransactionsBot
|
||||||
|
|
||||||
This bot basically have no any useful behaviour, but you may customize it as a playground
|
This bot basically have no any useful behaviour, but you may customize it as a playground
|
||||||
|
|
||||||
|
|||||||
9
SuggestedPosts/README.md
Normal file
9
SuggestedPosts/README.md
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# StickerSetHandler
|
||||||
|
|
||||||
|
Send sticker to this bot to form your own stickers set. Send /delete to delete this sticker set
|
||||||
|
|
||||||
|
## How to run
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./gradlew run --args="TOKEN"
|
||||||
|
```
|
||||||
21
SuggestedPosts/build.gradle
Normal file
21
SuggestedPosts/build.gradle
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
apply plugin: 'kotlin'
|
||||||
|
apply plugin: 'application'
|
||||||
|
|
||||||
|
mainClassName="SuggestedPostsBotKt"
|
||||||
|
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
|
|
||||||
|
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||||
|
}
|
||||||
140
SuggestedPosts/src/main/kotlin/SuggestedPostsBot.kt
Normal file
140
SuggestedPosts/src/main/kotlin/SuggestedPostsBot.kt
Normal file
@@ -0,0 +1,140 @@
|
|||||||
|
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.micro_utils.coroutines.runCatchingLogging
|
||||||
|
import dev.inmo.micro_utils.coroutines.subscribeLoggingDropExceptions
|
||||||
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.bot.getMyStarBalance
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.resend
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.send
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.suggested.approveSuggestedPost
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.suggested.declineSuggestedPost
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextData
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildSubcontextInitialAction
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitSuggestedPostApproved
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitSuggestedPostDeclined
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChannelDirectMessagesConfigurationChanged
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostApprovalFailed
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostApproved
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostDeclined
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostPaid
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSuggestedPostRefunded
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.channelDirectMessagesContentMessageOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.previewChannelDirectMessagesChatOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.suggestedChannelDirectMessagesContentMessageOrNull
|
||||||
|
import dev.inmo.tgbotapi.types.message.SuggestedPostParameters
|
||||||
|
import dev.inmo.tgbotapi.types.message.abstracts.ChannelPaidPost
|
||||||
|
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
||||||
|
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
||||||
|
import dev.inmo.tgbotapi.utils.firstOf
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.flow.filter
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This place can be the playground for your code.
|
||||||
|
*/
|
||||||
|
suspend fun main(vararg args: String) {
|
||||||
|
val botToken = args.first()
|
||||||
|
|
||||||
|
val isDebug = args.any { it == "debug" }
|
||||||
|
val isTestServer = args.any { it == "testServer" }
|
||||||
|
|
||||||
|
if (isDebug) {
|
||||||
|
setDefaultKSLog(
|
||||||
|
KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
|
||||||
|
println(defaultMessageFormatter(level, tag, message, throwable))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
telegramBotWithBehaviourAndLongPolling(
|
||||||
|
botToken,
|
||||||
|
CoroutineScope(Dispatchers.Default),
|
||||||
|
testServer = isTestServer,
|
||||||
|
) {
|
||||||
|
// start here!!
|
||||||
|
val me = getMe()
|
||||||
|
println(me)
|
||||||
|
|
||||||
|
onCommand("start") {
|
||||||
|
println(getChat(it.chat))
|
||||||
|
}
|
||||||
|
|
||||||
|
onContentMessage {
|
||||||
|
val message = it.channelDirectMessagesContentMessageOrNull() ?: return@onContentMessage
|
||||||
|
val chat = getChat(it.chat)
|
||||||
|
println(chat)
|
||||||
|
|
||||||
|
resend(
|
||||||
|
message.chat.id,
|
||||||
|
message.content,
|
||||||
|
suggestedPostParameters = SuggestedPostParameters()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
onContentMessage(
|
||||||
|
subcontextUpdatesFilter = { _, _ -> true } // important to not miss updates in channel for waitSuggestedPost events
|
||||||
|
) { message ->
|
||||||
|
val suggestedPost = message.suggestedChannelDirectMessagesContentMessageOrNull() ?: return@onContentMessage
|
||||||
|
|
||||||
|
firstOf(
|
||||||
|
{
|
||||||
|
waitSuggestedPostApproved().filter {
|
||||||
|
it.suggestedPostMessage ?.chat ?.id == message.chat.id
|
||||||
|
}.first()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
waitSuggestedPostDeclined().filter {
|
||||||
|
it.suggestedPostMessage ?.chat ?.id == message.chat.id
|
||||||
|
}.first()
|
||||||
|
},
|
||||||
|
{
|
||||||
|
for (i in 0 until 3) {
|
||||||
|
delay(1000L)
|
||||||
|
send(suggestedPost.chat, "${3 - i}")
|
||||||
|
}
|
||||||
|
declineSuggestedPost(suggestedPost)
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
onContentMessage(initialFilter = { it is ChannelPaidPost<*> }) {
|
||||||
|
println(it)
|
||||||
|
}
|
||||||
|
|
||||||
|
onSuggestedPostPaid {
|
||||||
|
println(it)
|
||||||
|
reply(it, "Paid")
|
||||||
|
}
|
||||||
|
onSuggestedPostApproved {
|
||||||
|
println(it)
|
||||||
|
reply(it, "Approved")
|
||||||
|
}
|
||||||
|
onSuggestedPostDeclined {
|
||||||
|
println(it)
|
||||||
|
reply(it, "Declined")
|
||||||
|
}
|
||||||
|
onSuggestedPostRefunded {
|
||||||
|
println(it)
|
||||||
|
reply(it, "Refunded")
|
||||||
|
}
|
||||||
|
onSuggestedPostApprovalFailed {
|
||||||
|
println(it)
|
||||||
|
reply(it, "Approval failed")
|
||||||
|
}
|
||||||
|
|
||||||
|
allUpdatesFlow.subscribeLoggingDropExceptions(this) {
|
||||||
|
println(it)
|
||||||
|
}
|
||||||
|
}.second.join()
|
||||||
|
}
|
||||||
@@ -163,6 +163,34 @@ fun main() {
|
|||||||
Text("Answer in chat button")
|
Text("Answer in chat button")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
H3 { Text("Hide keyboard") }
|
||||||
|
val hideCountdown = remember { mutableStateOf<Int?>(null) }
|
||||||
|
Button({
|
||||||
|
onClick {
|
||||||
|
hideCountdown.value = 5
|
||||||
|
}
|
||||||
|
}) {
|
||||||
|
if (hideCountdown.value == null) {
|
||||||
|
Text("Hide")
|
||||||
|
} else {
|
||||||
|
Text("Hide in ${hideCountdown.value} seconds")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LaunchedEffect(hideCountdown.value) {
|
||||||
|
val value = hideCountdown.value
|
||||||
|
when {
|
||||||
|
value == null -> return@LaunchedEffect
|
||||||
|
value > 0 -> {
|
||||||
|
delay(1000)
|
||||||
|
hideCountdown.value = hideCountdown.value ?.minus(1)
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
webApp.hideKeyboard()
|
||||||
|
hideCountdown.value = null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
P()
|
P()
|
||||||
H3 { Text("User info") }
|
H3 { Text("User info") }
|
||||||
Text("Allow to write in private messages: ${webApp.initDataUnsafe.user ?.allowsWriteToPM ?: "User unavailable"}")
|
Text("Allow to write in private messages: ${webApp.initDataUnsafe.user ?.allowsWriteToPM ?: "User unavailable"}")
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ org.gradle.jvmargs=-Xmx3148m
|
|||||||
kotlin.daemon.jvmargs=-Xmx3g -Xms500m
|
kotlin.daemon.jvmargs=-Xmx3g -Xms500m
|
||||||
|
|
||||||
|
|
||||||
kotlin_version=2.1.20
|
kotlin_version=2.2.10
|
||||||
telegram_bot_api_version=25.0.0
|
telegram_bot_api_version=29.0.0
|
||||||
micro_utils_version=0.25.3
|
micro_utils_version=0.26.3
|
||||||
serialization_version=1.8.0
|
serialization_version=1.9.0
|
||||||
ktor_version=3.1.1
|
ktor_version=3.3.3
|
||||||
compose_version=1.7.3
|
compose_version=1.8.2
|
||||||
|
|||||||
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
|
|||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.13-bin.zip
|
||||||
|
|||||||
@@ -59,3 +59,7 @@ include ":CustomBot"
|
|||||||
include ":MemberUpdatedWatcherBot"
|
include ":MemberUpdatedWatcherBot"
|
||||||
|
|
||||||
include ":WebHooks"
|
include ":WebHooks"
|
||||||
|
|
||||||
|
include ":SuggestedPosts"
|
||||||
|
|
||||||
|
include ":ChecklistsBot"
|
||||||
|
|||||||
Reference in New Issue
Block a user