|
| 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.subscribeLoggingDropExceptions |
| 6 | +import dev.inmo.tgbotapi.extensions.api.bot.getMe |
| 7 | +import dev.inmo.tgbotapi.extensions.api.send.reply |
| 8 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling |
| 9 | +import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGuestMessage |
| 10 | +import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResultArticle |
| 11 | +import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputTextMessageContent |
| 12 | +import dev.inmo.tgbotapi.types.InlineQueryId |
| 13 | +import dev.inmo.tgbotapi.utils.buildEntities |
| 14 | +import kotlinx.coroutines.CoroutineScope |
| 15 | +import kotlinx.coroutines.Dispatchers |
| 16 | + |
| 17 | +/** |
| 18 | + * This bot demonstrates guest mode support introduced in Telegram Bot API. |
| 19 | + * |
| 20 | + * Guest mode allows bots to receive messages and reply within chats they are not a member of. |
| 21 | + * To enable guest queries for your bot, set `supports_guest_queries` in BotFather settings. |
| 22 | + * |
| 23 | + * Key concepts demonstrated: |
| 24 | + * - `supportsGuestQueries` field on the bot itself (via getMe()) |
| 25 | + * - `GuestMessageUpdate` — a new update type for messages sent in guest mode |
| 26 | + * - `guestQueryId` — unique ID used to answer the guest query |
| 27 | + * - `guestBotCallerUser` — the user who initiated the guest query |
| 28 | + * - `guestBotCallerChat` — the chat from which the guest query was sent |
| 29 | + * - `answerGuestQuery` / `reply(GuestMessage, InlineQueryResult)` — how to respond |
| 30 | + * - `SentGuestMessage` — the result returned after answering, containing the inline_message_id |
| 31 | + */ |
| 32 | +suspend fun main(vararg args: String) { |
| 33 | + val botToken = args.first() |
| 34 | + val isDebug = args.any { it == "debug" } |
| 35 | + val isTestServer = args.any { it == "testServer" } |
| 36 | + |
| 37 | + if (isDebug) { |
| 38 | + setDefaultKSLog( |
| 39 | + KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? -> |
| 40 | + println(defaultMessageFormatter(level, tag, message, throwable)) |
| 41 | + } |
| 42 | + ) |
| 43 | + } |
| 44 | + |
| 45 | + telegramBotWithBehaviourAndLongPolling( |
| 46 | + botToken, |
| 47 | + CoroutineScope(Dispatchers.IO), |
| 48 | + testServer = isTestServer |
| 49 | + ) { |
| 50 | + val me = getMe() |
| 51 | + println("Bot info: $me") |
| 52 | + // supportsGuestQueries reflects the supports_guest_queries field from the Telegram API |
| 53 | + println("Supports guest queries: ${me.supportsGuestQueries}") |
| 54 | + |
| 55 | + onGuestMessage { message -> |
| 56 | + println("=== Guest message received ===") |
| 57 | + // guestQueryId is the unique ID required to answer this guest query |
| 58 | + println(" guestQueryId: ${message.guestQueryId}") |
| 59 | + println(" from: ${message.from}") |
| 60 | + println(" chat: ${message.chat}") |
| 61 | + println(" content: ${message.content}") |
| 62 | + |
| 63 | + // reply() on GuestMessage calls answerGuestQuery internally and returns SentGuestMessage |
| 64 | + val sentGuestMessage = reply( |
| 65 | + message, |
| 66 | + InlineQueryResultArticle( |
| 67 | + id = InlineQueryId(message.guestQueryId.string), |
| 68 | + title = "Guest reply", |
| 69 | + inputMessageContent = InputTextMessageContent( |
| 70 | + buildEntities { |
| 71 | + +"Guest mode reply" |
| 72 | + +"\nQuery ID: " |
| 73 | + +message.guestQueryId.string |
| 74 | + } |
| 75 | + ), |
| 76 | + description = "Reply to guest query from ${message.from.firstName}" |
| 77 | + ) |
| 78 | + ) |
| 79 | + // SentGuestMessage contains the inline_message_id of the sent reply |
| 80 | + println(" SentGuestMessage: $sentGuestMessage") |
| 81 | + } |
| 82 | + |
| 83 | + allUpdatesFlow.subscribeLoggingDropExceptions(scope = this) { |
| 84 | + println(it) |
| 85 | + } |
| 86 | + }.second.join() |
| 87 | +} |
0 commit comments