mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-12-22 00:27:19 +00:00
improvements in samples
This commit is contained in:
parent
c04a367375
commit
d6a6ad8d37
9
BusinessConnectionsBot/README.md
Normal file
9
BusinessConnectionsBot/README.md
Normal file
@ -0,0 +1,9 @@
|
||||
# BusinessConnectionBotBot
|
||||
|
||||
When bot connected or disconnected to the business chat, it will notify this chat
|
||||
|
||||
## Launch
|
||||
|
||||
```bash
|
||||
../gradlew run --args="BOT_TOKEN"
|
||||
```
|
21
BusinessConnectionsBot/build.gradle
Normal file
21
BusinessConnectionsBot/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="BusinessConnectionsBotKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
@ -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.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.get.getBusinessConnection
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.api.send.send
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.ifBusinessContentMessage
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.business_connection.BusinessConnectionId
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
|
||||
suspend fun main(args: Array<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 businessConnectionsChats = mutableMapOf<BusinessConnectionId, ChatId>()
|
||||
val businessConnectionsChatsMutex = Mutex()
|
||||
|
||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||
val me = getMe()
|
||||
println(me)
|
||||
|
||||
onBusinessConnectionEnabled {
|
||||
businessConnectionsChatsMutex.withLock {
|
||||
businessConnectionsChats[it.id] = it.userChatId
|
||||
}
|
||||
send(it.userChatId, "Business connection ${it.businessConnectionId.string} has been enabled")
|
||||
}
|
||||
onBusinessConnectionDisabled {
|
||||
businessConnectionsChatsMutex.withLock {
|
||||
businessConnectionsChats.remove(it.id)
|
||||
}
|
||||
send(it.userChatId, "Business connection ${it.businessConnectionId.string} has been disabled")
|
||||
}
|
||||
|
||||
onContentMessage {
|
||||
it.ifBusinessContentMessage {
|
||||
val sent = execute(it.content.createResend(it.from.id))
|
||||
if (it.sentByBusinessConnectionOwner) {
|
||||
reply(sent, "You have sent this message to the ${it.businessConnectionId.string} related chat")
|
||||
} else {
|
||||
reply(sent, "User have sent this message to you in the ${it.businessConnectionId.string} related chat")
|
||||
}
|
||||
}
|
||||
}
|
||||
onEditedContentMessage {
|
||||
it.ifBusinessContentMessage {
|
||||
val sent = execute(it.content.createResend(it.from.id))
|
||||
if (it.sentByBusinessConnectionOwner) {
|
||||
reply(sent, "You have edited this message in the ${it.businessConnectionId.string} related chat")
|
||||
} else {
|
||||
reply(sent, "User have edited this message to you in the ${it.businessConnectionId.string} related chat")
|
||||
}
|
||||
}
|
||||
}
|
||||
onBusinessMessagesDeleted {
|
||||
var businessConnectionOwnerChat = businessConnectionsChatsMutex.withLock {
|
||||
businessConnectionsChats[it.businessConnectionId]
|
||||
}
|
||||
if (businessConnectionOwnerChat == null) {
|
||||
val businessConnection = getBusinessConnection(it.businessConnectionId)
|
||||
businessConnectionsChatsMutex.withLock {
|
||||
businessConnectionsChats[businessConnection.businessConnectionId] = businessConnection.userChatId
|
||||
}
|
||||
businessConnectionOwnerChat = businessConnection.userChatId
|
||||
}
|
||||
send(businessConnectionOwnerChat, "There are several removed messages in chat ${it.chat.id}: ${it.messageIds}")
|
||||
}
|
||||
}.second.join()
|
||||
}
|
@ -1,3 +1,7 @@
|
||||
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.runCatchingSafely
|
||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||
@ -18,6 +22,15 @@ import dev.inmo.tgbotapi.utils.row
|
||||
|
||||
suspend fun main(args: Array<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)
|
||||
|
||||
|
@ -47,3 +47,5 @@ include ":ReactionsInfoBot"
|
||||
include ":LinkPreviewsBot"
|
||||
|
||||
include ":BoostsInfoBot"
|
||||
|
||||
include ":BusinessConnectionsBot"
|
||||
|
Loading…
Reference in New Issue
Block a user