|
| 1 | +import dev.inmo.kslog.common.KSLog |
| 2 | +import dev.inmo.kslog.common.LogLevel |
| 3 | +import dev.inmo.kslog.common.defaultMessageFormatter |
| 4 | +import dev.inmo.kslog.common.setDefaultKSLog |
| 5 | +import dev.inmo.micro_utils.coroutines.launchLoggingDropExceptions |
| 6 | +import dev.inmo.micro_utils.coroutines.runCatchingLogging |
| 7 | +import dev.inmo.tgbotapi.bot.ktor.telegramBot |
| 8 | +import dev.inmo.tgbotapi.extensions.api.bot.getMe |
| 9 | +import dev.inmo.tgbotapi.extensions.api.bot.removeMyProfilePhoto |
| 10 | +import dev.inmo.tgbotapi.extensions.api.bot.setMyProfilePhoto |
| 11 | +import dev.inmo.tgbotapi.extensions.api.chat.get.getChat |
| 12 | +import dev.inmo.tgbotapi.extensions.api.files.downloadFileToTemp |
| 13 | +import dev.inmo.tgbotapi.extensions.api.send.reply |
| 14 | +import dev.inmo.tgbotapi.extensions.api.send.sendMessageDraftFlowWithTexts |
| 15 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitPhotoMessage |
| 16 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling |
| 17 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand |
| 18 | +import dev.inmo.tgbotapi.extensions.utils.extensions.sameChat |
| 19 | +import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile |
| 20 | +import dev.inmo.tgbotapi.requests.business_connection.InputProfilePhoto |
| 21 | +import kotlinx.coroutines.CoroutineScope |
| 22 | +import kotlinx.coroutines.Dispatchers |
| 23 | +import kotlinx.coroutines.channels.Channel |
| 24 | +import kotlinx.coroutines.flow.consumeAsFlow |
| 25 | +import kotlinx.coroutines.flow.filter |
| 26 | +import kotlinx.coroutines.flow.first |
| 27 | + |
| 28 | +/** |
| 29 | + * This is one of the easiest bots - it will just print information about itself |
| 30 | + */ |
| 31 | +suspend fun main(vararg args: String) { |
| 32 | + val botToken = args.first() |
| 33 | + val isDebug = args.any { it == "debug" } |
| 34 | + val isTestServer = args.any { it == "testServer" } |
| 35 | + |
| 36 | + if (isDebug) { |
| 37 | + setDefaultKSLog( |
| 38 | + KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> |
| 39 | + println(defaultMessageFormatter(level, tag, message, throwable)) |
| 40 | + } |
| 41 | + ) |
| 42 | + } |
| 43 | + |
| 44 | + val bot = telegramBot(botToken) |
| 45 | + |
| 46 | + telegramBotWithBehaviourAndLongPolling( |
| 47 | + botToken, |
| 48 | + CoroutineScope(Dispatchers.Default), |
| 49 | + testServer = isTestServer, |
| 50 | + ) { |
| 51 | + val me = bot.getMe() |
| 52 | + println(me) |
| 53 | + println(bot.getChat(me)) |
| 54 | + |
| 55 | + onCommand("setMyProfilePhoto") { commandMessage -> |
| 56 | + reply(commandMessage, "ok, send me new photo") |
| 57 | + val newPhotoMessage = waitPhotoMessage().filter { potentialPhotoMessage -> |
| 58 | + potentialPhotoMessage.sameChat(commandMessage) |
| 59 | + }.first() |
| 60 | + val draftMessagesChannel = Channel<String>(capacity = 1) |
| 61 | + |
| 62 | + launchLoggingDropExceptions { |
| 63 | + sendMessageDraftFlowWithTexts(commandMessage.chat.id, draftMessagesChannel.consumeAsFlow()) |
| 64 | + }.invokeOnCompletion { |
| 65 | + draftMessagesChannel.close(it) |
| 66 | + } |
| 67 | + |
| 68 | + draftMessagesChannel.send("Start downloading photo") |
| 69 | + val photoFile = downloadFileToTemp(newPhotoMessage.content) |
| 70 | + |
| 71 | + draftMessagesChannel.send("Photo file have been downloaded. Start set my profile photo") |
| 72 | + |
| 73 | + val setResult = setMyProfilePhoto( |
| 74 | + InputProfilePhoto.Static( |
| 75 | + photoFile.asMultipartFile() |
| 76 | + ) |
| 77 | + ) |
| 78 | + if (setResult) { |
| 79 | + reply(commandMessage, "New photo have been set") |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + onCommand("removeMyProfilePhoto") { |
| 84 | + runCatchingLogging { |
| 85 | + if (removeMyProfilePhoto()) { |
| 86 | + reply(it, "Photo have been removed") |
| 87 | + } |
| 88 | + }.onFailure { e -> |
| 89 | + e.printStackTrace() |
| 90 | + reply(it, "Something web wrong. See logs for details.") |
| 91 | + } |
| 92 | + } |
| 93 | + }.second.join() |
| 94 | +} |
0 commit comments