Skip to content

Commit

Permalink
inline query
Browse files Browse the repository at this point in the history
  • Loading branch information
kukume committed Jul 21, 2024
1 parent 18858f7 commit b5945fc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
10 changes: 10 additions & 0 deletions src/main/kotlin/me/kuku/telegram/config/TelegramConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class TelegramBean(

private val telegramSubscribeList = mutableListOf<TelegramSubscribe>()
private val abilitySubscriberList = mutableListOf<AbilitySubscriber>()
private val inlineQuerySubscriberList = mutableListOf<InlineQuerySubscriber>()
private val updateFunction = mutableListOf<UpdateFunction>()
private data class UpdateFunction(val function: KFunction<*>, val any: Any)

Expand All @@ -44,6 +45,7 @@ class TelegramBean(
}
}
val abilitySubscriber = AbilitySubscriber()
val inlineQuerySubscriber = InlineQuerySubscriber()
for (clazz in clazzList) {
val functions = kotlin.runCatching {
clazz.kotlin.declaredMemberExtensionFunctions
Expand Down Expand Up @@ -81,10 +83,15 @@ class TelegramBean(
abilitySubscriberList.addAll(abilities)
telegramSubscribeList.addAll(telegrams)
}
"me.kuku.telegram.context.InlineQuerySubscriber" -> {
val obj = applicationContext.getBean(clazz)
function.call(obj, inlineQuerySubscriber)
}
}
}
}
abilitySubscriberList.add(abilitySubscriber)
inlineQuerySubscriberList.add(inlineQuerySubscriber)
val telegramBot = applicationContext.getBean(TelegramBot::class.java)
telegramBot.setUpdatesListener {
for (update in it) {
Expand All @@ -105,6 +112,9 @@ class TelegramBean(
telegramSubscribe.invoke(telegramBot, update)
}
}
for (single in inlineQuerySubscriberList) {
single.invoke(telegramBot, update)
}
abilitySubscriberList.repeatCheck(telegramBot, update)
}
}
Expand Down
39 changes: 31 additions & 8 deletions src/main/kotlin/me/kuku/telegram/context/Context.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ package me.kuku.telegram.context

import com.pengrad.telegrambot.TelegramBot
import com.pengrad.telegrambot.model.CallbackQuery
import com.pengrad.telegrambot.model.InlineQuery
import com.pengrad.telegrambot.model.Message
import com.pengrad.telegrambot.model.Update
import com.pengrad.telegrambot.model.request.InlineKeyboardMarkup
import com.pengrad.telegrambot.model.request.InputMedia
import com.pengrad.telegrambot.model.request.Keyboard
import com.pengrad.telegrambot.model.request.ParseMode
import com.pengrad.telegrambot.model.request.*
import com.pengrad.telegrambot.request.*
import com.pengrad.telegrambot.response.SendResponse
import me.kuku.telegram.utils.CacheManager
Expand All @@ -22,8 +20,11 @@ import kotlin.collections.ArrayList

abstract class Context {
abstract val tgId: Long
abstract val chatId: Long
abstract val bot: TelegramBot
}

abstract class MessageContext: Context() {
abstract val chatId: Long
abstract val message: Message
abstract val messageThreadId: Int?

Expand Down Expand Up @@ -52,7 +53,7 @@ abstract class Context {
}
}

class AbilityContext(override val bot: TelegramBot, val update: Update): Context() {
class AbilityContext(override val bot: TelegramBot, val update: Update): MessageContext() {

override val message: Message = update.message()

Expand Down Expand Up @@ -89,10 +90,17 @@ private data class History(var message: Message?, var data: String, var text: St
}


class TelegramContext(override val bot: TelegramBot, val update: Update): Context() {
class TelegramContext(override val bot: TelegramBot, val update: Update): MessageContext() {
lateinit var query: CallbackQuery
override val message: Message by lazy {
if (this::query.isInitialized) query.message() else update.message()
if (this::query.isInitialized) {
val tempMessage = query.maybeInaccessibleMessage()
if (tempMessage is Message) {
tempMessage
} else {
error("bot can't access the message")
}
} else update.message()
}
override val tgId: Long by lazy {
if (this::query.isInitialized) query.from().id() else update.chatMember().from().id()
Expand Down Expand Up @@ -233,3 +241,18 @@ class MonitorReturn(
}

}

class InlineQueryContext(override val bot: TelegramBot, val update: Update): Context() {
val inlineQuery: InlineQuery = update.inlineQuery() ?: error("inlineQuery is null")

override val tgId: Long
get() = inlineQuery.from().id()

suspend fun answerInlineQuery(vararg results: InlineQueryResult<*>, cacheTime: Int? = null) {
val answerInlineQuery = AnswerInlineQuery(inlineQuery.id(), *results)
cacheTime?.let {
answerInlineQuery.cacheTime(it)
}
bot.asyncExecute(answerInlineQuery)
}
}
21 changes: 21 additions & 0 deletions src/main/kotlin/me/kuku/telegram/context/TelegramSubscriber.kt
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ data class Ability(val name: String,
private typealias AbilityContextBody = suspend AbilityContext.() -> Unit
private typealias CallbackBody = suspend TelegramContext.() -> Unit
private typealias UpdateBody = Update.() -> Boolean
private typealias InlineQueryBody = suspend InlineQueryContext.() -> Unit

class TelegramSubscribe {

Expand Down Expand Up @@ -269,3 +270,23 @@ class MixSubscribe {
}

}

class InlineQuerySubscriber {

private val inlineQueryMap = mutableMapOf<String, InlineQueryBody>()

operator fun String.invoke(block: InlineQueryBody) {
inlineQueryMap[this] = block
}

suspend fun invoke(bot: TelegramBot, update: Update) {
val inlineQuery = update.inlineQuery() ?: return
val query = inlineQuery.query()
inlineQueryMap[query]?.let {
val inlineQueryContext = InlineQueryContext(bot, update)
it.invoke(inlineQueryContext)
}
}


}

0 comments on commit b5945fc

Please sign in to comment.