mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-10-13 11:20:03 +00:00
add suggested posts sample
This commit is contained in:
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"
|
||||||
|
}
|
85
SuggestedPosts/src/main/kotlin/SuggestedPostsBot.kt
Normal file
85
SuggestedPosts/src/main/kotlin/SuggestedPostsBot.kt
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
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.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.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.CommonMessage
|
||||||
|
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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
|
||||||
|
|
||||||
|
resend(
|
||||||
|
message.chat.id,
|
||||||
|
message.content,
|
||||||
|
suggestedPostParameters = SuggestedPostParameters()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
onContentMessage {
|
||||||
|
val suggestedPost = it.suggestedChannelDirectMessagesContentMessageOrNull() ?: return@onContentMessage
|
||||||
|
|
||||||
|
for (i in 0 until 3) {
|
||||||
|
delay(1000L)
|
||||||
|
send(suggestedPost.chat, "${3 - i}")
|
||||||
|
}
|
||||||
|
approveSuggestedPost(suggestedPost)
|
||||||
|
}
|
||||||
|
|
||||||
|
allUpdatesFlow.subscribeLoggingDropExceptions(this) {
|
||||||
|
println(it)
|
||||||
|
}
|
||||||
|
}.second.join()
|
||||||
|
}
|
@@ -59,3 +59,5 @@ include ":CustomBot"
|
|||||||
include ":MemberUpdatedWatcherBot"
|
include ":MemberUpdatedWatcherBot"
|
||||||
|
|
||||||
include ":WebHooks"
|
include ":WebHooks"
|
||||||
|
|
||||||
|
include ":SuggestedPosts"
|
||||||
|
Reference in New Issue
Block a user