Compare commits

..

5 Commits

Author SHA1 Message Date
95a619431c fix of guest bot sample 2026-05-30 15:34:18 +06:00
3b61976734 add guest bot 2026-05-23 00:00:36 +06:00
b7f50b514f update tgbotapi version 2026-05-20 18:06:50 +06:00
89e1eec53e Merge pull request #358 from InsanusMokrassar/33.0.0
33.0.0
2026-04-18 19:34:46 +06:00
00ab078891 build fix 2026-04-18 18:40:16 +06:00
10 changed files with 162 additions and 18 deletions

View File

@@ -213,6 +213,8 @@ suspend fun main(args: Array<String>) {
firstName, firstName,
secondName secondName
) )
}.map {
true
}.getOrElse { false } }.getOrElse { false }
reply(it) { reply(it) {
if (set) { if (set) {
@@ -230,6 +232,8 @@ suspend fun main(args: Array<String>) {
businessConnectionId, businessConnectionId,
username username
) )
}.map {
true
}.getOrElse { }.getOrElse {
it.printStackTrace() it.printStackTrace()
false false
@@ -267,6 +271,8 @@ suspend fun main(args: Array<String>) {
} }
val transferred = runCatching { val transferred = runCatching {
transferBusinessAccountStars(businessConnectionId, count) transferBusinessAccountStars(businessConnectionId, count)
}.map {
true
}.getOrElse { }.getOrElse {
it.printStackTrace() it.printStackTrace()
false false
@@ -310,6 +316,8 @@ suspend fun main(args: Array<String>) {
businessConnectionId, businessConnectionId,
bio bio
) )
}.map {
true
}.getOrElse { }.getOrElse {
it.printStackTrace() it.printStackTrace()
false false
@@ -327,6 +335,8 @@ suspend fun main(args: Array<String>) {
businessConnectionId, businessConnectionId,
initialBio initialBio
) )
}.map {
true
}.getOrElse { }.getOrElse {
it.printStackTrace() it.printStackTrace()
false false
@@ -358,6 +368,8 @@ suspend fun main(args: Array<String>) {
), ),
isPublic = isPublic isPublic = isPublic
) )
}.map {
true
}.getOrElse { }.getOrElse {
it.printStackTrace() it.printStackTrace()
false false
@@ -376,6 +388,8 @@ suspend fun main(args: Array<String>) {
businessConnectionId, businessConnectionId,
isPublic = isPublic isPublic = isPublic
) )
}.map {
true
}.getOrElse { }.getOrElse {
it.printStackTrace() it.printStackTrace()
false false
@@ -461,6 +475,8 @@ suspend fun main(args: Array<String>) {
val deleted = runCatching { val deleted = runCatching {
deleteStory(businessConnectionId, replyTo.content.story.id) deleteStory(businessConnectionId, replyTo.content.story.id)
}.map {
true
}.getOrElse { }.getOrElse {
it.printStackTrace() it.printStackTrace()
false false

View File

@@ -1,3 +1,4 @@
import dev.inmo.micro_utils.coroutines.runCatchingLogging
import dev.inmo.micro_utils.coroutines.runCatchingSafely import dev.inmo.micro_utils.coroutines.runCatchingSafely
import dev.inmo.tgbotapi.bot.ktor.telegramBot import dev.inmo.tgbotapi.bot.ktor.telegramBot
import dev.inmo.tgbotapi.extensions.api.chat.modify.setChatPhoto import dev.inmo.tgbotapi.extensions.api.chat.modify.setChatPhoto
@@ -15,17 +16,13 @@ suspend fun main(args: Array<String>) {
bot.buildBehaviourWithLongPolling(scope = CoroutineScope(Dispatchers.IO)) { bot.buildBehaviourWithLongPolling(scope = CoroutineScope(Dispatchers.IO)) {
onPhoto { onPhoto {
val bytes = downloadFile(it.content) val bytes = downloadFile(it.content)
runCatchingSafely { runCatchingLogging {
setChatPhoto( setChatPhoto(
it.chat.id, it.chat.id,
bytes.asMultipartFile("sample.jpg") bytes.asMultipartFile("sample.jpg")
) )
}.onSuccess { b -> }.onSuccess { _ ->
if (b) { reply(it, "Done")
reply(it, "Done")
} else {
reply(it, "Something went wrong")
}
}.onFailure { e -> }.onFailure { e ->
e.printStackTrace() e.printStackTrace()

View File

@@ -0,0 +1,21 @@
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"
}

View File

@@ -0,0 +1,107 @@
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.onContentMessage
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGuestRequestMessage
import dev.inmo.tgbotapi.extensions.utils.extensions.raw.guest_bot_caller_chat
import dev.inmo.tgbotapi.extensions.utils.extensions.raw.guest_bot_caller_user
import dev.inmo.tgbotapi.extensions.utils.publicChatOrNull
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}")
onGuestRequestMessage { 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")
}
onContentMessage {
println(it)
val userCalledGuestMessage = it.guest_bot_caller_user
val chatCalledGuestMessage = it.guest_bot_caller_chat ?.publicChatOrNull()
if (userCalledGuestMessage != null) {
reply(it) {
+"User called guest bot: ${userCalledGuestMessage.lastName + " " + userCalledGuestMessage.firstName}"
}
}
if (chatCalledGuestMessage != null) {
reply(it) {
+"Chat called guest bot: ${chatCalledGuestMessage.title}"
}
}
}
allUpdatesFlow.subscribeLoggingDropExceptions(scope = this) {
println(it)
}
}.second.join()
}

View File

@@ -70,21 +70,18 @@ suspend fun main(vararg args: String) {
draftMessagesChannel.send("Photo file have been downloaded. Start set my profile photo") draftMessagesChannel.send("Photo file have been downloaded. Start set my profile photo")
val setResult = setMyProfilePhoto( setMyProfilePhoto(
InputProfilePhoto.Static( InputProfilePhoto.Static(
photoFile.asMultipartFile() photoFile.asMultipartFile()
) )
) )
if (setResult) { reply(commandMessage, "New photo have been set")
reply(commandMessage, "New photo have been set")
}
} }
onCommand("removeMyProfilePhoto") { onCommand("removeMyProfilePhoto") {
runCatchingLogging { runCatchingLogging {
if (removeMyProfilePhoto()) { removeMyProfilePhoto()
reply(it, "Photo have been removed") reply(it, "Photo have been removed")
}
}.onFailure { e -> }.onFailure { e ->
e.printStackTrace() e.printStackTrace()
reply(it, "Something web wrong. See logs for details.") reply(it, "Something web wrong. See logs for details.")

View File

@@ -156,7 +156,7 @@ suspend fun main(vararg args: String) {
isAnonymous = false, isAnonymous = false,
replyParameters = ReplyParameters(it), replyParameters = ReplyParameters(it),
correctOptionIds = correctAnswer.sorted(), correctOptionIds = correctAnswer.sorted(),
allowMultipleAnswers = correctAnswer.size > 1, allowsMultipleAnswers = correctAnswer.size > 1,
allowsRevoting = true, allowsRevoting = true,
shuffleOptions = true, shuffleOptions = true,
hideResultsUntilCloses = true, hideResultsUntilCloses = true,

View File

@@ -44,7 +44,11 @@ suspend fun main(args: Array<String>) {
onCommand("delete") { onCommand("delete") {
val deleted = runCatchingSafely { val deleted = runCatchingSafely {
deleteStickerSet(it.chat.stickerSetName()) deleteStickerSet(it.chat.stickerSetName())
}.getOrElse { false } }.map {
true
}.getOrElse {
false
}
if (deleted) { if (deleted) {
reply(it, "Deleted") reply(it, "Deleted")

View File

@@ -26,7 +26,7 @@ allprojects {
} }
} }
maven { url "https://proxy.nexus.inmo.dev/repository/maven-releases/" } // maven { url "https://proxy.nexus.inmo.dev/repository/maven-releases/" }
mavenLocal() mavenLocal()
} }
} }

View File

@@ -6,7 +6,7 @@ kotlin.daemon.jvmargs=-Xmx3g -Xms500m
kotlin_version=2.3.20 kotlin_version=2.3.20
telegram_bot_api_version=33.0.0 telegram_bot_api_version=34.0.0-t6
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.4.1

View File

@@ -71,3 +71,5 @@ include ":GiftsBot"
include ":TagsBot" include ":TagsBot"
include ":ManagedBotsBot" include ":ManagedBotsBot"
include ":GuestQueryBot"