-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.kt
96 lines (85 loc) · 3.73 KB
/
Main.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import dev.inmo.tgbotapi.extensions.api.answers.answerCallbackQuery
import dev.inmo.tgbotapi.extensions.api.edit.edit
import dev.inmo.tgbotapi.extensions.api.send.reply
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
import dev.inmo.tgbotapi.extensions.api.telegramBot
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onDataCallbackQuery
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMessageDataCallbackQuery
import dev.inmo.tgbotapi.extensions.utils.botCommandTextSourceOrNull
import dev.inmo.tgbotapi.extensions.utils.extensions.raw.text
import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatInlineKeyboard
import dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineKeyboard
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.CallbackDataInlineKeyboardButton
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardButtons.URLInlineKeyboardButton
import dev.inmo.tgbotapi.types.message.HTMLParseMode
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
import dev.inmo.tgbotapi.types.message.content.MessageContent
import dev.inmo.tgbotapi.types.message.content.TextContent
import dev.inmo.tgbotapi.utils.row
private val firstMenu = "<b>Menu 1</b>\n\nA beautiful menu with a shiny inline button.";
private val secondMenu = "<b>Menu 2</b>\n\nA better menu with even more shiny inline buttons.";
private val next = CallbackDataInlineKeyboardButton("Next", "next")
private val back = CallbackDataInlineKeyboardButton("Back", "back")
private val url = URLInlineKeyboardButton("Tutorial", "https://madhead.me/posts/from-botfather-to-hello-world")
private val firstMenuMarkup = flatInlineKeyboard { next }
private val secondMenuMarkup = inlineKeyboard {
row { back }
row { url }
}
suspend fun main(args: Array<String>) {
val token = args.first()
val bot = telegramBot(token)
var screaming = false
bot.buildBehaviourWithLongPolling() {
onCommand("scream") {
screaming = true
}
onCommand("whisper") {
screaming = false
}
onCommand("menu") {
bot.sendMessage(
chat = it.chat,
text = firstMenu,
parseMode = HTMLParseMode,
replyMarkup = firstMenuMarkup
)
}
onContentMessage(
initialFilter = CommonMessage<MessageContent>::isNotCommand
) {
val text = it.text
if (text != null) {
bot.reply(it, if (screaming) text.uppercase() else text)
} else {
bot.reply(it, it)
}
}
onMessageDataCallbackQuery("next") {
bot.edit(
chatId = it.message.chat.id,
messageId = it.message.messageId,
text = secondMenu,
parseMode = HTMLParseMode,
replyMarkup = secondMenuMarkup,
)
}
onMessageDataCallbackQuery("back") {
bot.edit(
message = it.message as ContentMessage<TextContent>,
text = firstMenu,
parseMode = HTMLParseMode,
replyMarkup = firstMenuMarkup,
)
}
onDataCallbackQuery {
bot.answerCallbackQuery(it)
}
}.join()
}
fun CommonMessage<*>.isNotCommand(): Boolean =
(this.content as? TextContent)?.textSources?.none { it.botCommandTextSourceOrNull() != null } ?: true