Skip to content

Commit 3b61976

Browse files
add guest bot
1 parent b7f50b5 commit 3b61976

4 files changed

Lines changed: 111 additions & 1 deletion

File tree

GuestQueryBot/build.gradle

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
buildscript {
2+
repositories {
3+
mavenCentral()
4+
}
5+
6+
dependencies {
7+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
8+
}
9+
}
10+
11+
apply plugin: 'kotlin'
12+
apply plugin: 'application'
13+
14+
mainClassName="GuestQueryBotKt"
15+
16+
17+
dependencies {
18+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
19+
20+
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
21+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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+
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ kotlin.daemon.jvmargs=-Xmx3g -Xms500m
66

77

88
kotlin_version=2.3.20
9-
telegram_bot_api_version=34.0.0
9+
telegram_bot_api_version=34.0.0-t3
1010
micro_utils_version=0.29.1
1111
serialization_version=1.10.0
1212
ktor_version=3.4.1

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,5 @@ include ":GiftsBot"
7171
include ":TagsBot"
7272

7373
include ":ManagedBotsBot"
74+
75+
include ":GuestQueryBot"

0 commit comments

Comments
 (0)