Created
May 27, 2024 20:00
-
-
Save centralhardware/1add5b0f86c2dad222907ac9a17bc038 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package me.centralhardware.znatoki.telegram.statistic | |
import dev.inmo.tgbotapi.bot.TelegramBot | |
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands | |
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour | |
import dev.inmo.tgbotapi.extensions.behaviour_builder.createSubContext | |
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling | |
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.* | |
import dev.inmo.tgbotapi.extensions.utils.updates.flowsUpdatesFilter | |
import dev.inmo.tgbotapi.types.BotCommand | |
import kotlinx.coroutines.CoroutineScope | |
import kotlinx.coroutines.Dispatchers | |
import kotlinx.coroutines.flow.filter | |
import me.centralhardware.znatoki.telegram.statistic.mapper.UserMapper | |
import me.centralhardware.znatoki.telegram.statistic.telegram.callbackHandler.statistic.paymentDeleteCallback | |
import me.centralhardware.znatoki.telegram.statistic.telegram.callbackHandler.statistic.paymentRestoreCallback | |
import me.centralhardware.znatoki.telegram.statistic.telegram.callbackHandler.statistic.timeDeleteCallback | |
import me.centralhardware.znatoki.telegram.statistic.telegram.callbackHandler.statistic.timeRestoreCallback | |
import me.centralhardware.znatoki.telegram.statistic.telegram.callbackHandler.student.deleteUserCallback | |
import me.centralhardware.znatoki.telegram.statistic.telegram.callbackHandler.student.userInfoCallback | |
import me.centralhardware.znatoki.telegram.statistic.telegram.commandHandler.debug.dailyReportCommand | |
import me.centralhardware.znatoki.telegram.statistic.telegram.commandHandler.organization.grafanaCommand | |
import me.centralhardware.znatoki.telegram.statistic.telegram.commandHandler.resetCommand | |
import me.centralhardware.znatoki.telegram.statistic.telegram.commandHandler.startCommand | |
import me.centralhardware.znatoki.telegram.statistic.telegram.commandHandler.statisticCommand.addPaymentCommand | |
import me.centralhardware.znatoki.telegram.statistic.telegram.commandHandler.statisticCommand.addTimeCommand | |
import me.centralhardware.znatoki.telegram.statistic.telegram.commandHandler.statisticCommand.reportCommand | |
import me.centralhardware.znatoki.telegram.statistic.telegram.commandHandler.statisticCommand.reportPreviousCommand | |
import me.centralhardware.znatoki.telegram.statistic.telegram.commandHandler.studentCommand.addClientCommand | |
import me.centralhardware.znatoki.telegram.statistic.telegram.commandHandler.studentCommand.searchCommand | |
import me.centralhardware.znatoki.telegram.statistic.telegram.commandHandler.studentCommand.userInfoCommand | |
import me.centralhardware.znatoki.telegram.statistic.telegram.fsm.Storage | |
import me.centralhardware.znatoki.telegram.statistic.telegram.processInline | |
import org.slf4j.LoggerFactory | |
val log = LoggerFactory.getLogger("bot") | |
lateinit var bot: TelegramBot | |
suspend fun main() { | |
val res = telegramBotWithBehaviourAndLongPolling( | |
Config.Telegram.token, | |
defaultExceptionsHandler = { log.info("", it) }, | |
scope = CoroutineScope(Dispatchers.IO) | |
) { | |
setMyCommands( | |
BotCommand("addtime", "Добавить запись"), | |
BotCommand("addpayment", "Добавить оплату"), | |
BotCommand("report", "Отчет за текущий месяц"), | |
BotCommand("reportprevious", "Отчет за предыдущий месяц"), | |
BotCommand("reset", "Сбросить состояние") | |
) | |
onCommand("/start") { startCommand(it) } | |
onCommand("/reset") { resetCommand(it) } | |
createSubContext(buildBehaviour(defaultExceptionsHandler = { log.info("", it) }) { | |
flowsUpdatesFilter { | |
allUpdatesFlow.filter { UserMapper.isAdmin(it.userId()) } | |
} | |
onCommand("grafana") { grafanaCommand(it) } | |
onCommandWithArgs("dailyReport") { it, args -> dailyReportCommand(it, args) } | |
onDataCallbackQuery(Regex("\\/delete_user\\d+\$")) { deleteUserCallback(it) } | |
onDataCallbackQuery(Regex("timeRestore-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) { | |
timeRestoreCallback(it) | |
} | |
onDataCallbackQuery(Regex("timeDelete-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) { | |
timeDeleteCallback(it) | |
} | |
onDataCallbackQuery(Regex("paymentRestore-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) { | |
paymentRestoreCallback(it) | |
} | |
onDataCallbackQuery(Regex("paymentRestore-[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) { | |
paymentDeleteCallback(it) | |
} | |
}) | |
createSubContext(buildBehaviour(defaultExceptionsHandler = { log.info("", it) }) { | |
flowsUpdatesFilter { | |
allUpdatesFlow.filter { UserMapper.hasReadRight(it.userId()) } | |
} | |
onCommand("report") { reportCommand(it) } | |
onCommand("reportPrevious") { reportPreviousCommand(it) } | |
onCommandWithArgs("i") { it, args -> userInfoCommand(it, args) } | |
onCommandWithArgs("s") { it, args -> searchCommand(it, args) } | |
onDataCallbackQuery(Regex("\\/user_info\\d+\$")) { userInfoCallback(it) } | |
onBaseInlineQuery { processInline(it) } | |
}) | |
createSubContext(buildBehaviour(defaultExceptionsHandler = { log.info("", it) }) { | |
flowsUpdatesFilter { | |
allUpdatesFlow.filter { UserMapper.hasWriteRight(it.userId()) } | |
} | |
onCommand("addPupil") { addClientCommand(it) } | |
onCommand("addTime") { addTimeCommand(it) } | |
onCommand("addPayment") { addPaymentCommand(it) } | |
}) | |
createSubContext(buildBehaviour(defaultExceptionsHandler = { log.info("", it) }) { | |
flowsUpdatesFilter { | |
allUpdatesFlow.filter { Storage.contain(it.userId()) } | |
} | |
onContentMessage { | |
Storage.process(it) | |
} | |
}) | |
} | |
bot = res.first | |
res.second.join() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment