com/example/myproject/holder/MessageTemplateHolder.kt
@ConfigurationProperties("telegram-bot.template")
class MessageTemplateHolder {
// from application.yml telegram-bot.template.chain = "Let's play a game. Write any positive integer."
lateinit var chain: String
// from application.yml telegram-bot.template.chain-start-sum = "I will now add the numbers entered until the sum of them is greater than or equal to ${target}."
lateinit var chainStartSum: String
// from application.yml telegram-bot.template.chain-one-more = "The current amount is ${sum}. Target: ${target}."
lateinit var chainOneMore: String
// from application.yml telegram-bot.template.chain-end = "Let's play a game. Write any positive integer."
lateinit var chainEnd: String
// from application.yml telegram-bot.template.chain-integer-is-expected = "Integer is expected."
lateinit var chainIntegerIsExpected: String
// from application.yml telegram-bot.template.chain-positive-is-expected = "Positive integer is expected."
lateinit var chainPositiveIsExpected: String
}
com/example/myproject/handler/GameChainHandler.kt
@HandlerComponent
class GameChainHandler(
private val template: MessageTemplateHolder,
) : BotHandler({
val startSum = 0
command("/chain", next = "get_target") {
sendMessage(template.chain)
}
step("get_target", next = "sum_numbers") {
val target = text.toIntOrNull() ?: throw ChatException(template.chainIntegerIsExpected)
if (target < 1) throw ChatException(template.chainPositiveIsExpected)
sendMessage(template.chainStartSum with ("target" to target))
transfer(target to startSum)
}
step("sum_numbers") {
val value = text.toIntOrNull() ?: throw ChatException(template.chainIntegerIsExpected)
var (target, sum) = transferred<Pair<Int, Int>>()
sum += value
if (sum < target) {
sendMessage(template.chainOneMore with mapOf("sum" to sum, "target" to target))
next("sum_numbers", target to sum)
return@step
}
sendMessage(template.chainEnd with mapOf("sum" to sum, "target" to target))
}
})
com/example/myproject/handler/PurchaseHandler.kt
@Factory
class PurchaseHandler(
private val itemService: ItemService,
private val feedbackService: FeedbackService,
) : BotHandler({
command("/buy") {
sendMessage("Make your choice", replyMarkup = inlineKeyboard(
callbackButton("Buy slippers", next = "buy", content = "some id 1"),
callbackButton("Buy hats", next = "buy", content = "some id 2"),
callbackButton("Give feedback", next = "get_feedback_intro")))
}
callback("buy") {
val itemId = transferred<String>()
val item = itemService.get(itemId)
sendInvoice(
title = item.title,
description = item.description,
payload = "...",
providerToken = "...",
currency = "rub",
prices = item.prices
)
}
callback("get_feedback_intro", next = "get_feedback") {
sendMessage("Please write us what you think about our company")
}
step("get_feedback") {
feedbackService.save(chatId, text)
sendMessage("Thanks for the feedback!")
sendSticker(...)
}
})
com/example/myproject/handler/RegistrationHandler.kt
fun BotHandling.registrationHandler() {
val phonePattern = <phone regex>
command("/register", next = "get_contact") {
sendMessage("Enter a number or share your contact to register",
replyMarkup = contactKeyboard("Share contact"))
}
step("get_contact", type = CONTACT) {
sendMessage("${contact.firstName}, You have successfully registered with number ${contact.phoneNumber}!",
replyMarkup = removeKeyboard())
}
step("get_contact", type = TEXT, next = "get_firstname") {
phonePattern.find(text) ?: throw ChatException("Incorrect phone number format")
val phone: String = text
sendMessage("What is your name?", replyMarkup = removeKeyboard())
transfer(phone)
}
step("get_firstname") {
val phone = transferred<String>()
sendMessage("${text}, You have successfully registered with number ${phone}!")
}
}