mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2026-05-23 00:37:17 +00:00
Compare commits
1 Commits
34.0.0
...
renovate/k
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2cfb2fb028 |
@@ -1,21 +0,0 @@
|
|||||||
buildscript {
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
apply plugin: 'kotlin'
|
|
||||||
apply plugin: 'application'
|
|
||||||
|
|
||||||
mainClassName="GuestQueryBotKt"
|
|
||||||
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
||||||
|
|
||||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
|
||||||
}
|
|
||||||
@@ -1,87 +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.micro_utils.coroutines.subscribeLoggingDropExceptions
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGuestMessage
|
|
||||||
import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResultArticle
|
|
||||||
import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputTextMessageContent
|
|
||||||
import dev.inmo.tgbotapi.types.InlineQueryId
|
|
||||||
import dev.inmo.tgbotapi.utils.buildEntities
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This bot demonstrates guest mode support introduced in Telegram Bot API.
|
|
||||||
*
|
|
||||||
* Guest mode allows bots to receive messages and reply within chats they are not a member of.
|
|
||||||
* To enable guest queries for your bot, set `supports_guest_queries` in BotFather settings.
|
|
||||||
*
|
|
||||||
* Key concepts demonstrated:
|
|
||||||
* - `supportsGuestQueries` field on the bot itself (via getMe())
|
|
||||||
* - `GuestMessageUpdate` — a new update type for messages sent in guest mode
|
|
||||||
* - `guestQueryId` — unique ID used to answer the guest query
|
|
||||||
* - `guestBotCallerUser` — the user who initiated the guest query
|
|
||||||
* - `guestBotCallerChat` — the chat from which the guest query was sent
|
|
||||||
* - `answerGuestQuery` / `reply(GuestMessage, InlineQueryResult)` — how to respond
|
|
||||||
* - `SentGuestMessage` — the result returned after answering, containing the inline_message_id
|
|
||||||
*/
|
|
||||||
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
|
|
||||||
) {
|
|
||||||
val me = getMe()
|
|
||||||
println("Bot info: $me")
|
|
||||||
// supportsGuestQueries reflects the supports_guest_queries field from the Telegram API
|
|
||||||
println("Supports guest queries: ${me.supportsGuestQueries}")
|
|
||||||
|
|
||||||
onGuestMessage { message ->
|
|
||||||
println("=== Guest message received ===")
|
|
||||||
// guestQueryId is the unique ID required to answer this guest query
|
|
||||||
println(" guestQueryId: ${message.guestQueryId}")
|
|
||||||
println(" from: ${message.from}")
|
|
||||||
println(" chat: ${message.chat}")
|
|
||||||
println(" content: ${message.content}")
|
|
||||||
|
|
||||||
// reply() on GuestMessage calls answerGuestQuery internally and returns SentGuestMessage
|
|
||||||
val sentGuestMessage = reply(
|
|
||||||
message,
|
|
||||||
InlineQueryResultArticle(
|
|
||||||
id = InlineQueryId(message.guestQueryId.string),
|
|
||||||
title = "Guest reply",
|
|
||||||
inputMessageContent = InputTextMessageContent(
|
|
||||||
buildEntities {
|
|
||||||
+"Guest mode reply"
|
|
||||||
+"\nQuery ID: "
|
|
||||||
+message.guestQueryId.string
|
|
||||||
}
|
|
||||||
),
|
|
||||||
description = "Reply to guest query from ${message.from.firstName}"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
// SentGuestMessage contains the inline_message_id of the sent reply
|
|
||||||
println(" SentGuestMessage: $sentGuestMessage")
|
|
||||||
}
|
|
||||||
|
|
||||||
allUpdatesFlow.subscribeLoggingDropExceptions(scope = this) {
|
|
||||||
println(it)
|
|
||||||
}
|
|
||||||
}.second.join()
|
|
||||||
}
|
|
||||||
@@ -18,5 +18,5 @@ dependencies {
|
|||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
|
|
||||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||||
implementation 'io.ktor:ktor-client-logging-jvm:3.2.3'
|
implementation 'io.ktor:ktor-client-logging-jvm:3.5.0'
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ kotlin.daemon.jvmargs=-Xmx3g -Xms500m
|
|||||||
|
|
||||||
|
|
||||||
kotlin_version=2.3.20
|
kotlin_version=2.3.20
|
||||||
telegram_bot_api_version=34.0.0-t3
|
telegram_bot_api_version=33.0.0
|
||||||
micro_utils_version=0.29.1
|
micro_utils_version=0.29.1
|
||||||
serialization_version=1.10.0
|
serialization_version=1.10.0
|
||||||
ktor_version=3.4.1
|
ktor_version=3.5.0
|
||||||
compose_version=1.10.2
|
compose_version=1.10.2
|
||||||
|
|||||||
@@ -71,5 +71,3 @@ include ":GiftsBot"
|
|||||||
include ":TagsBot"
|
include ":TagsBot"
|
||||||
|
|
||||||
include ":ManagedBotsBot"
|
include ":ManagedBotsBot"
|
||||||
|
|
||||||
include ":GuestQueryBot"
|
|
||||||
|
|||||||
Reference in New Issue
Block a user