+ - bot enable

This commit is contained in:
xds
2025-07-17 13:14:15 +03:00
parent 2e054088ce
commit 6e8f5d112b
2 changed files with 29 additions and 51 deletions

View File

@@ -22,5 +22,4 @@ class TelegramBotConfig {
data class TelegramBotProperties( data class TelegramBotProperties(
val username: String, val username: String,
val token: String, val token: String,
val enable:Int
) )

View File

@@ -41,16 +41,10 @@ class BotService(
private val logger = LoggerFactory.getLogger(javaClass) private val logger = LoggerFactory.getLogger(javaClass)
private suspend fun constructCategoriesButtons( private suspend fun constructCategoriesButtons(
nlp: Boolean, nlp: Boolean, text: String? = null, chatId: Long
text: String? = null,
chatId: Long
): InlineKeyboardMarkup { ): InlineKeyboardMarkup {
val categories = val categories = categoriesService.getCategories(
categoriesService.getCategories( "67af3c0f652da946a7dd9931", "EXPENSE", sortBy = "name", direction = "ASC"
"67af3c0f652da946a7dd9931",
"EXPENSE",
sortBy = "name",
direction = "ASC"
) )
val keyboard = InlineKeyboardMarkup() val keyboard = InlineKeyboardMarkup()
@@ -135,18 +129,14 @@ class BotService(
val chatId = update.callbackQuery.message.chatId.toString() val chatId = update.callbackQuery.message.chatId.toString()
val tgUserId = update.callbackQuery.from.id val tgUserId = update.callbackQuery.from.id
val user = userService.getUserByTelegramId(tgUserId) ?: throw TelegramBotException( val user = userService.getUserByTelegramId(tgUserId) ?: throw TelegramBotException(
"User ${update.callbackQuery.from.userName} not found", "User ${update.callbackQuery.from.userName} not found", chatId = update.callbackQuery.message.chatId
chatId = update.callbackQuery.message.chatId
) )
val state = getState(user.id!!) val state = getState(user.id!!)
if (state != null) { if (state != null) {
when (state.data.first { it.chatId == chatId }.state) { when (state.data.first { it.chatId == chatId }.state) {
BotStates.WAIT_CATEGORY -> BotStates.WAIT_CATEGORY -> if (update.callbackQuery.data.startsWith("category_")) {
if (update.callbackQuery.data.startsWith("category_")) {
confirmTransaction( confirmTransaction(
chatId, chatId, user = userService.getUserByTelegramId(tgUserId)!!, update
user = userService.getUserByTelegramId(tgUserId)!!,
update
) )
} else if (update.callbackQuery.data == "cancel") { } else if (update.callbackQuery.data == "cancel") {
finishState(chatId, user) finishState(chatId, user)
@@ -172,8 +162,7 @@ class BotService(
getState(user.id!!)?.data?.find { it.chatId == update.message.chatId.toString() }?.let { getState(user.id!!)?.data?.find { it.chatId == update.message.chatId.toString() }?.let {
if (it.state == BotStates.WAIT_CATEGORY) { if (it.state == BotStates.WAIT_CATEGORY) {
throw TelegramBotException( throw TelegramBotException(
"Уже есть открытый выбор категории", "Уже есть открытый выбор категории", update.message.chatId
update.message.chatId
) )
} }
} }
@@ -257,17 +246,12 @@ class BotService(
} }
val category = categoriesService.getCategories( val category = categoriesService.getCategories(
"67af3c0f652da946a7dd9931", "67af3c0f652da946a7dd9931", "EXPENSE", sortBy = "name", direction = "ASC"
"EXPENSE", ).first { it.id == update.callbackQuery.data.split("_")[1] }
sortBy = "name",
direction = "ASC"
)
.first { it.id == update.callbackQuery.data.split("_")[1] }
val space = spaceService.getSpace("67af3c0f652da946a7dd9931") val space = spaceService.getSpace("67af3c0f652da946a7dd9931")
val instantType = financialService.getTransactionTypes().first { it.code == "INSTANT" } val instantType = financialService.getTransactionTypes().first { it.code == "INSTANT" }
val transaction = financialService.createTransaction( val transaction = financialService.createTransaction(
space, space, transaction = Transaction(
transaction = Transaction(
space = space, space = space,
type = instantType, type = instantType,
user = user, user = user,
@@ -278,8 +262,7 @@ class BotService(
isDone = true, isDone = true,
parentId = null, parentId = null,
createdAt = LocalDateTime.now() createdAt = LocalDateTime.now()
), ), user = user
user = user
) )
val editMsg = EditMessageText() val editMsg = EditMessageText()
editMsg.chatId = chatId editMsg.chatId = chatId
@@ -302,9 +285,7 @@ class BotService(
val match = match(Criteria.where("userDetails._id").`is`(ObjectId(userId))) val match = match(Criteria.where("userDetails._id").`is`(ObjectId(userId)))
val aggregation = newAggregation(lookup, unwind, match) val aggregation = newAggregation(lookup, unwind, match)
return reactiveMongoTemplate.aggregate(aggregation, "bot-user-states", Document::class.java) return reactiveMongoTemplate.aggregate(aggregation, "bot-user-states", Document::class.java).next().map { doc ->
.next()
.map { doc ->
val dataList = doc.getList("data", Document::class.java) val dataList = doc.getList("data", Document::class.java)
BotUserState( BotUserState(
id = doc.getObjectId("_id").toString(), id = doc.getObjectId("_id").toString(),
@@ -318,14 +299,12 @@ class BotService(
) )
}.toMutableList(), }.toMutableList(),
) )
} }.awaitSingleOrNull()
.awaitSingleOrNull()
} }
private suspend fun setState(userState: BotUserState): BotUserState { private suspend fun setState(userState: BotUserState): BotUserState {
val stateToSave = userState.user.id?.let { userId -> val stateToSave = userState.user.id?.let { userId ->
getState(userId)?.copy(data = userState.data) getState(userId)?.copy(data = userState.data) ?: BotUserState(user = userState.user, data = userState.data)
?: BotUserState(user = userState.user, data = userState.data)
} ?: BotUserState(user = userState.user, data = userState.data) } ?: BotUserState(user = userState.user, data = userState.data)
return botStatesRepo.save(stateToSave).awaitSingle() return botStatesRepo.save(stateToSave).awaitSingle()