mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2026-06-04 22:57:17 +00:00
Compare commits
13 Commits
31.0.0
...
9746a068b7
| Author | SHA1 | Date | |
|---|---|---|---|
| 9746a068b7 | |||
| 9903e0e323 | |||
| 8268cd9bf4 | |||
| b4e2d52e7e | |||
| f829ce7281 | |||
| 20b2ae8175 | |||
| 29ad52b506 | |||
|
|
b848c6bfad | ||
| 6642b95af2 | |||
| 828ab43317 | |||
| 1a4533221c | |||
| e304a5ecab | |||
| 600ac8ebbf |
21
ManagedBotsBot/build.gradle
Normal file
21
ManagedBotsBot/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="CustomBotKt"
|
||||||
|
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
|
|
||||||
|
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||||
|
}
|
||||||
143
ManagedBotsBot/src/main/kotlin/ManagedBotsBot.kt
Normal file
143
ManagedBotsBot/src/main/kotlin/ManagedBotsBot.kt
Normal file
@@ -0,0 +1,143 @@
|
|||||||
|
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.subscribeLoggingDropExceptions
|
||||||
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.managed_bots.getManagedBotToken
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.managed_bots.replaceManagedBotToken
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.send
|
||||||
|
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.onCommand
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onManagedBotCreated
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onManagedBotUpdated
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.chatEventMessageOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.groupContentMessageOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.managedBotCreatedOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatReplyKeyboard
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.replyKeyboard
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.requestManagedBotButton
|
||||||
|
import dev.inmo.tgbotapi.types.Username
|
||||||
|
import dev.inmo.tgbotapi.types.buttons.KeyboardButtonRequestManagedBot
|
||||||
|
import dev.inmo.tgbotapi.types.buttons.PreparedKeyboardButtonId
|
||||||
|
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
||||||
|
import dev.inmo.tgbotapi.types.request.RequestId
|
||||||
|
import dev.inmo.tgbotapi.types.toChatId
|
||||||
|
import dev.inmo.tgbotapi.types.update.abstracts.Update
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
|
||||||
|
private var BehaviourContextData.update: Update?
|
||||||
|
get() = get("update") as? Update
|
||||||
|
set(value) = set("update", value)
|
||||||
|
|
||||||
|
private var BehaviourContextData.commonMessage: CommonMessage<*>?
|
||||||
|
get() = get("commonMessage") as? CommonMessage<*>
|
||||||
|
set(value) = set("commonMessage", value)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.IO),
|
||||||
|
testServer = isTestServer,
|
||||||
|
builder = {
|
||||||
|
includeMiddlewares {
|
||||||
|
addMiddleware {
|
||||||
|
doOnRequestReturnResult { result, request, _ ->
|
||||||
|
println("Result of $request:\n\n$result")
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
subcontextInitialAction = buildSubcontextInitialAction {
|
||||||
|
add {
|
||||||
|
data.update = it
|
||||||
|
}
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
// start here!!
|
||||||
|
val me = getMe()
|
||||||
|
println(me)
|
||||||
|
|
||||||
|
onCommand("start") {
|
||||||
|
println(data.update)
|
||||||
|
println(data.commonMessage)
|
||||||
|
println(getChat(it.chat))
|
||||||
|
}
|
||||||
|
|
||||||
|
onCommand("canManageBots") {
|
||||||
|
val me = getMe()
|
||||||
|
reply(it, if (me.canManageBots) "Yes" else "No")
|
||||||
|
}
|
||||||
|
|
||||||
|
val requestId = RequestId(0)
|
||||||
|
onCommand("keyboard") {
|
||||||
|
reply(
|
||||||
|
it,
|
||||||
|
"Keyboard",
|
||||||
|
replyMarkup = flatReplyKeyboard(
|
||||||
|
resizeKeyboard = true,
|
||||||
|
oneTimeKeyboard = true,
|
||||||
|
) {
|
||||||
|
requestManagedBotButton(
|
||||||
|
"Add managed bot",
|
||||||
|
KeyboardButtonRequestManagedBot(
|
||||||
|
requestId = requestId,
|
||||||
|
suggestedName = "SampleName",
|
||||||
|
suggestedUsername = Username("@some_sample_bot")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
onManagedBotCreated {
|
||||||
|
reply(it, "Managed bot created successfully: ${it.chatEvent.bot}")
|
||||||
|
val token = getManagedBotToken(
|
||||||
|
it.chatEvent.bot.id.toChatId()
|
||||||
|
)
|
||||||
|
reply(it, "Token: $token")
|
||||||
|
}
|
||||||
|
|
||||||
|
onManagedBotUpdated {
|
||||||
|
send(it.user, "Managed bot has been updated: ${it.bot}")
|
||||||
|
val token = getManagedBotToken(
|
||||||
|
it.bot.id.toChatId()
|
||||||
|
)
|
||||||
|
send(it.user, "Token: $token")
|
||||||
|
}
|
||||||
|
|
||||||
|
onCommand("replaceToken") {
|
||||||
|
val reply = it.replyTo ?.chatEventMessageOrNull() ?: return@onCommand
|
||||||
|
val managedBotCreated = reply.chatEvent.managedBotCreatedOrNull() ?: return@onCommand
|
||||||
|
|
||||||
|
reply(it, "Token in replace update: ${replaceManagedBotToken(managedBotCreated.bot.id.toChatId())}")
|
||||||
|
}
|
||||||
|
|
||||||
|
allUpdatesFlow.subscribeLoggingDropExceptions(this) {
|
||||||
|
println(it)
|
||||||
|
}
|
||||||
|
}.second.join()
|
||||||
|
}
|
||||||
21
TagsBot/build.gradle
Normal file
21
TagsBot/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="TagsBotKt"
|
||||||
|
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
|
|
||||||
|
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||||
|
}
|
||||||
101
TagsBot/src/main/kotlin/TagsBot.kt
Normal file
101
TagsBot/src/main/kotlin/TagsBot.kt
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
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.subscribeLoggingDropExceptions
|
||||||
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
|
import dev.inmo.tgbotapi.abstracts.FromUser
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.business.getBusinessAccountGiftsFlow
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.chat.members.promoteChatAdministrator
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.chat.members.promoteChatMember
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.chat.members.setChatMemberTag
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.gifts.getChatGiftsFlow
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.gifts.getUserGiftsFlow
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.withTypingAction
|
||||||
|
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.onContentMessage
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayCompleted
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayContent
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayCreated
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayWinners
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.extensions.raw.sender_chat
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.extensions.raw.sender_tag
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.fromUserOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.groupContentMessageOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.idChatIdentifierOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.potentiallyFromUserGroupContentMessageOrNull
|
||||||
|
import dev.inmo.tgbotapi.types.UserTag
|
||||||
|
import dev.inmo.tgbotapi.types.chat.BusinessChat
|
||||||
|
import dev.inmo.tgbotapi.types.chat.PrivateChat
|
||||||
|
import dev.inmo.tgbotapi.types.chat.PublicChat
|
||||||
|
import dev.inmo.tgbotapi.types.chat.UnknownChatType
|
||||||
|
import dev.inmo.tgbotapi.types.gifts.OwnedGift
|
||||||
|
import dev.inmo.tgbotapi.types.message.abstracts.OptionallyFromUserMessage
|
||||||
|
import dev.inmo.tgbotapi.types.message.textsources.splitForText
|
||||||
|
import dev.inmo.tgbotapi.utils.bold
|
||||||
|
import dev.inmo.tgbotapi.utils.buildEntities
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
|
||||||
|
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, testServer = isTestServer) {
|
||||||
|
// start here!!
|
||||||
|
val me = getMe()
|
||||||
|
println(me)
|
||||||
|
|
||||||
|
onCommand("setChatMemberTag", requireOnlyCommandInMessage = false) {
|
||||||
|
val reply = it.replyTo ?.groupContentMessageOrNull() ?: return@onCommand
|
||||||
|
val title = it.content.text.removePrefix("/setChatMemberTag").removePrefix(" ")
|
||||||
|
setChatMemberTag(
|
||||||
|
chatId = reply.chat.id,
|
||||||
|
userId = reply.fromUserOrNull() ?.user ?.id ?: return@onCommand,
|
||||||
|
tag = UserTag(title)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
onCommand("setCanManageTags", requireOnlyCommandInMessage = false) {
|
||||||
|
val reply = it.replyTo ?.groupContentMessageOrNull() ?: return@onCommand
|
||||||
|
val setOrUnset = it.content.text.removePrefix("/setCanManageTags").removePrefix(" ") == "true"
|
||||||
|
promoteChatAdministrator(
|
||||||
|
it.chat.id,
|
||||||
|
reply.fromUserOrNull() ?.user ?.id ?: return@onCommand,
|
||||||
|
canManageTags = setOrUnset
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
onCommand("removeChatMemberTag") {
|
||||||
|
val reply = it.replyTo ?.groupContentMessageOrNull() ?: return@onCommand
|
||||||
|
setChatMemberTag(
|
||||||
|
chatId = reply.chat.id,
|
||||||
|
userId = reply.fromUserOrNull() ?.user ?.id ?: return@onCommand,
|
||||||
|
tag = null
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
onContentMessage {
|
||||||
|
val groupContentMessage = it.potentiallyFromUserGroupContentMessageOrNull() ?: return@onContentMessage
|
||||||
|
reply(it, "Tag after casting: ${groupContentMessage.senderTag}")
|
||||||
|
reply(it, "Tag by getting via risk API: ${it.sender_tag}")
|
||||||
|
}
|
||||||
|
|
||||||
|
allUpdatesFlow.subscribeLoggingDropExceptions(this) {
|
||||||
|
println(it)
|
||||||
|
}
|
||||||
|
}.second.join()
|
||||||
|
}
|
||||||
@@ -1,5 +1,6 @@
|
|||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.*
|
||||||
import dev.inmo.micro_utils.coroutines.launchLoggingDropExceptions
|
import dev.inmo.micro_utils.coroutines.launchLoggingDropExceptions
|
||||||
|
import dev.inmo.tgbotapi.types.CustomEmojiId
|
||||||
import dev.inmo.tgbotapi.types.userIdField
|
import dev.inmo.tgbotapi.types.userIdField
|
||||||
import dev.inmo.tgbotapi.types.webAppQueryIdField
|
import dev.inmo.tgbotapi.types.webAppQueryIdField
|
||||||
import dev.inmo.tgbotapi.webapps.*
|
import dev.inmo.tgbotapi.webapps.*
|
||||||
@@ -395,6 +396,11 @@ fun main() {
|
|||||||
}
|
}
|
||||||
mainButton.apply {
|
mainButton.apply {
|
||||||
setText("Main button")
|
setText("Main button")
|
||||||
|
setParams(
|
||||||
|
BottomButtonParams(
|
||||||
|
iconCustomEmojiId = CustomEmojiId("5370976574969486150") // 😏
|
||||||
|
)
|
||||||
|
)
|
||||||
onClick {
|
onClick {
|
||||||
logsState.add("Main button clicked")
|
logsState.add("Main button clicked")
|
||||||
hapticFeedback.notificationOccurred(
|
hapticFeedback.notificationOccurred(
|
||||||
@@ -405,6 +411,11 @@ fun main() {
|
|||||||
}
|
}
|
||||||
secondaryButton.apply {
|
secondaryButton.apply {
|
||||||
setText("Secondary button")
|
setText("Secondary button")
|
||||||
|
setParams(
|
||||||
|
BottomButtonParams(
|
||||||
|
iconCustomEmojiId = CustomEmojiId("5370763368497944736") // 😒
|
||||||
|
)
|
||||||
|
)
|
||||||
onClick {
|
onClick {
|
||||||
logsState.add("Secondary button clicked")
|
logsState.add("Secondary button clicked")
|
||||||
hapticFeedback.notificationOccurred(
|
hapticFeedback.notificationOccurred(
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ org.gradle.jvmargs=-Xmx3148m
|
|||||||
kotlin.daemon.jvmargs=-Xmx3g -Xms500m
|
kotlin.daemon.jvmargs=-Xmx3g -Xms500m
|
||||||
|
|
||||||
|
|
||||||
kotlin_version=2.2.21
|
kotlin_version=2.3.20
|
||||||
telegram_bot_api_version=31.0.0
|
telegram_bot_api_version=33.0.0
|
||||||
micro_utils_version=0.26.9
|
micro_utils_version=0.29.1
|
||||||
serialization_version=1.9.0
|
serialization_version=1.10.0
|
||||||
ktor_version=3.3.2
|
ktor_version=3.4.1
|
||||||
compose_version=1.8.2
|
compose_version=1.10.2
|
||||||
|
|||||||
@@ -67,3 +67,7 @@ include ":ChecklistsBot"
|
|||||||
include ":DraftsBot"
|
include ":DraftsBot"
|
||||||
|
|
||||||
include ":GiftsBot"
|
include ":GiftsBot"
|
||||||
|
|
||||||
|
include ":TagsBot"
|
||||||
|
|
||||||
|
include ":ManagedBotsBot"
|
||||||
|
|||||||
Reference in New Issue
Block a user