mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2026-06-11 13:56:41 +00:00
Compare commits
2 Commits
9981e82a10
...
e660f06edf
| Author | SHA1 | Date | |
|---|---|---|---|
| e660f06edf | |||
| fb6ed8b7ae |
@@ -6,12 +6,19 @@ 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.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.get.getUserProfileAudios
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.replyWithAudio
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.replyWithPlaylist
|
||||||
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.onChannelDirectMessagesConfigurationChanged
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatOwnerChanged
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatOwnerLeft
|
||||||
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.media.AudioMediaGroupMemberTelegramMedia
|
||||||
|
import dev.inmo.tgbotapi.types.media.toTelegramMediaAudio
|
||||||
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
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
@@ -70,6 +77,32 @@ suspend fun main(vararg args: String) {
|
|||||||
println(data.update)
|
println(data.update)
|
||||||
println(data.commonMessage)
|
println(data.commonMessage)
|
||||||
println(getChat(it.chat))
|
println(getChat(it.chat))
|
||||||
|
var currentOffset = 0
|
||||||
|
val pageSize = 2
|
||||||
|
do {
|
||||||
|
val userAudios = getUserProfileAudios(userId = it.chat.id, offset = currentOffset, limit = pageSize)
|
||||||
|
currentOffset += pageSize
|
||||||
|
println(userAudios)
|
||||||
|
when (userAudios.audios.size) {
|
||||||
|
1 -> {
|
||||||
|
replyWithAudio(
|
||||||
|
it,
|
||||||
|
userAudios.audios.first().fileId
|
||||||
|
)
|
||||||
|
}
|
||||||
|
0 -> {
|
||||||
|
// do nothing
|
||||||
|
}
|
||||||
|
else -> {
|
||||||
|
replyWithPlaylist(
|
||||||
|
it,
|
||||||
|
userAudios.audios.map {
|
||||||
|
it.toTelegramMediaAudio()
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} while (currentOffset < userAudios.totalCount && userAudios.audios.isNotEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
onCommand(
|
onCommand(
|
||||||
|
|||||||
@@ -1,29 +0,0 @@
|
|||||||
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.bot.ktor.telegramBot
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This is one of the easiest bots - it will just print information about itself
|
|
||||||
*/
|
|
||||||
suspend fun main(vararg args: 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)
|
|
||||||
|
|
||||||
val me = bot.getMe()
|
|
||||||
println(me)
|
|
||||||
println(bot.getChat(me))
|
|
||||||
}
|
|
||||||
94
MyBot/src/main/kotlin/MyBot.kt
Normal file
94
MyBot/src/main/kotlin/MyBot.kt
Normal file
@@ -0,0 +1,94 @@
|
|||||||
|
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.launchLoggingDropExceptions
|
||||||
|
import dev.inmo.micro_utils.coroutines.runCatchingLogging
|
||||||
|
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.bot.removeMyProfilePhoto
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.bot.setMyProfilePhoto
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.files.downloadFileToTemp
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.sendMessageDraftFlowWithTexts
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitPhotoMessage
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.extensions.sameChat
|
||||||
|
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
|
||||||
|
import dev.inmo.tgbotapi.requests.business_connection.InputProfilePhoto
|
||||||
|
import kotlinx.coroutines.CoroutineScope
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.channels.Channel
|
||||||
|
import kotlinx.coroutines.flow.consumeAsFlow
|
||||||
|
import kotlinx.coroutines.flow.filter
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This is one of the easiest bots - it will just print information about itself
|
||||||
|
*/
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
val bot = telegramBot(botToken)
|
||||||
|
|
||||||
|
telegramBotWithBehaviourAndLongPolling(
|
||||||
|
botToken,
|
||||||
|
CoroutineScope(Dispatchers.Default),
|
||||||
|
testServer = isTestServer,
|
||||||
|
) {
|
||||||
|
val me = bot.getMe()
|
||||||
|
println(me)
|
||||||
|
println(bot.getChat(me))
|
||||||
|
|
||||||
|
onCommand("setMyProfilePhoto") { commandMessage ->
|
||||||
|
reply(commandMessage, "ok, send me new photo")
|
||||||
|
val newPhotoMessage = waitPhotoMessage().filter { potentialPhotoMessage ->
|
||||||
|
potentialPhotoMessage.sameChat(commandMessage)
|
||||||
|
}.first()
|
||||||
|
val draftMessagesChannel = Channel<String>(capacity = 1)
|
||||||
|
|
||||||
|
launchLoggingDropExceptions {
|
||||||
|
sendMessageDraftFlowWithTexts(commandMessage.chat.id, draftMessagesChannel.consumeAsFlow())
|
||||||
|
}.invokeOnCompletion {
|
||||||
|
draftMessagesChannel.close(it)
|
||||||
|
}
|
||||||
|
|
||||||
|
draftMessagesChannel.send("Start downloading photo")
|
||||||
|
val photoFile = downloadFileToTemp(newPhotoMessage.content)
|
||||||
|
|
||||||
|
draftMessagesChannel.send("Photo file have been downloaded. Start set my profile photo")
|
||||||
|
|
||||||
|
val setResult = setMyProfilePhoto(
|
||||||
|
InputProfilePhoto.Static(
|
||||||
|
photoFile.asMultipartFile()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if (setResult) {
|
||||||
|
reply(commandMessage, "New photo have been set")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onCommand("removeMyProfilePhoto") {
|
||||||
|
runCatchingLogging {
|
||||||
|
if (removeMyProfilePhoto()) {
|
||||||
|
reply(it, "Photo have been removed")
|
||||||
|
}
|
||||||
|
}.onFailure { e ->
|
||||||
|
e.printStackTrace()
|
||||||
|
reply(it, "Something web wrong. See logs for details.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.second.join()
|
||||||
|
}
|
||||||
@@ -6,8 +6,8 @@ kotlin.daemon.jvmargs=-Xmx3g -Xms500m
|
|||||||
|
|
||||||
|
|
||||||
kotlin_version=2.2.21
|
kotlin_version=2.2.21
|
||||||
telegram_bot_api_version=31.0.0
|
telegram_bot_api_version=31.0.0-branch_31.0.0-build3109
|
||||||
micro_utils_version=0.26.8
|
micro_utils_version=0.26.9
|
||||||
serialization_version=1.9.0
|
serialization_version=1.9.0
|
||||||
ktor_version=3.3.2
|
ktor_version=3.3.2
|
||||||
compose_version=1.8.2
|
compose_version=1.8.2
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ include ":HelloBot"
|
|||||||
|
|
||||||
include ":PollsBot"
|
include ":PollsBot"
|
||||||
|
|
||||||
include ":GetMeBot"
|
include ":MyBot"
|
||||||
|
|
||||||
include ":DeepLinksBot"
|
include ":DeepLinksBot"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user