Skip to content

MKE-overseas/mke-tg

Repository files navigation

MKE tg utils

Admin menu

  1. Create feature instance in implementation of BaseLocationConfig
val adminsFeature by lazy {
    AdminsFeature(
        tgUser = tgUser,
        userSelector = { userId: Long -> transaction { ComplaintsTgUser.findById(userId) } },
        usersSelector = { transaction { ComplaintsTgUser.all() } }
    )
}
  1. Use feature in global CallbackQueryHandler and CommandHandler
global {
    handleCallbackQuery(handlerId = "global") {
        if (tgUser.isAdmin) {
            setupFeatures(adminsFeature)
        }
    }

    handleCommand(handlerId = "global") {
        if (tgUser.isAdmin) {
            setupFeatures(adminsFeature)
        }
    }
}

Auth via Google Sheets

  1. Create private static base instance in companion object of BaseLocationConfig
    companion object {
        private val baseAuthGoogleSheetsFeature = AuthGoogleSheetsFeature(
            tgUser = TgUser(EntityID(0, TgUsers)),
            sheetService = SheetService,
            phonesSpreadsheetId = phonesSpreadsheetId,
            getPhone = { message.contact?.phoneNumber?.phoneFormatted() },
            onFail = { send("Вашего телефона нет среди разрешенных. ${contactEmail?.let { "Обратитесь на почту $it, чтобы вас добавили в систему." }}") },
            onSuccess = { phone, _ ->
                suspendTransaction {
                    tgUser.phone = phone
                    tgUser.isRegistered = true
                    tgUser.location = Location.MENU
                    tgUser.provideCommands()
                    sendWelcomeMessage()
                }
            }
        )
    }
  1. Create feature instance in implementation of BaseLocationConfig
val authGoogleSheetsFeature by lazy {
    baseAuthGoogleSheetsFeature.copy(tgUser = tgUser)
}
  1. Use feature in global MessageHandler
global {
    handleMessage(handlerId = "global") {
        if (!tgUser.isRegistered) {
            setupFeatures(authGoogleSheetsFeature)
        }
    }
}

About

MKE utilities for raysmith/tg-bot =)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages