Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions BusinessConnectionsBot/src/main/kotlin/BusinessConnectionsBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ suspend fun main(args: Array<String>) {
firstName,
secondName
)
}.map {
true
}.getOrElse { false }
reply(it) {
if (set) {
Expand All @@ -230,6 +232,8 @@ suspend fun main(args: Array<String>) {
businessConnectionId,
username
)
}.map {
true
}.getOrElse {
it.printStackTrace()
false
Expand Down Expand Up @@ -267,6 +271,8 @@ suspend fun main(args: Array<String>) {
}
val transferred = runCatching {
transferBusinessAccountStars(businessConnectionId, count)
}.map {
true
}.getOrElse {
it.printStackTrace()
false
Expand Down Expand Up @@ -310,6 +316,8 @@ suspend fun main(args: Array<String>) {
businessConnectionId,
bio
)
}.map {
true
}.getOrElse {
it.printStackTrace()
false
Expand All @@ -327,6 +335,8 @@ suspend fun main(args: Array<String>) {
businessConnectionId,
initialBio
)
}.map {
true
}.getOrElse {
it.printStackTrace()
false
Expand Down Expand Up @@ -358,6 +368,8 @@ suspend fun main(args: Array<String>) {
),
isPublic = isPublic
)
}.map {
true
}.getOrElse {
it.printStackTrace()
false
Expand All @@ -376,6 +388,8 @@ suspend fun main(args: Array<String>) {
businessConnectionId,
isPublic = isPublic
)
}.map {
true
}.getOrElse {
it.printStackTrace()
false
Expand Down Expand Up @@ -461,6 +475,8 @@ suspend fun main(args: Array<String>) {

val deleted = runCatching {
deleteStory(businessConnectionId, replyTo.content.story.id)
}.map {
true
}.getOrElse {
it.printStackTrace()
false
Expand Down
11 changes: 4 additions & 7 deletions ChatAvatarSetter/src/main/kotlin/ChatAvatarSetter.kt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import dev.inmo.micro_utils.coroutines.runCatchingLogging
import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.tgbotapi.bot.ktor.telegramBot
import dev.inmo.tgbotapi.extensions.api.chat.modify.setChatPhoto
Expand All @@ -15,17 +16,13 @@ suspend fun main(args: Array<String>) {
bot.buildBehaviourWithLongPolling(scope = CoroutineScope(Dispatchers.IO)) {
onPhoto {
val bytes = downloadFile(it.content)
runCatchingSafely {
runCatchingLogging {
setChatPhoto(
it.chat.id,
bytes.asMultipartFile("sample.jpg")
)
}.onSuccess { b ->
if (b) {
reply(it, "Done")
} else {
reply(it, "Something went wrong")
}
}.onSuccess { _ ->
reply(it, "Done")
}.onFailure { e ->
e.printStackTrace()

Expand Down
21 changes: 21 additions & 0 deletions ManagedBotsBot/build.gradle
Original file line number Diff line number Diff line change
@@ -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 changes: 143 additions & 0 deletions ManagedBotsBot/src/main/kotlin/ManagedBotsBot.kt
Original file line number Diff line number Diff line change
@@ -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()
}
11 changes: 4 additions & 7 deletions MyBot/src/main/kotlin/MyBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,18 @@ suspend fun main(vararg args: String) {

draftMessagesChannel.send("Photo file have been downloaded. Start set my profile photo")

val setResult = setMyProfilePhoto(
setMyProfilePhoto(
InputProfilePhoto.Static(
photoFile.asMultipartFile()
)
)
if (setResult) {
reply(commandMessage, "New photo have been set")
}
reply(commandMessage, "New photo have been set")
}

onCommand("removeMyProfilePhoto") {
runCatchingLogging {
if (removeMyProfilePhoto()) {
reply(it, "Photo have been removed")
}
removeMyProfilePhoto()
reply(it, "Photo have been removed")
}.onFailure { e ->
e.printStackTrace()
reply(it, "Something web wrong. See logs for details.")
Expand Down
Loading
Loading