TelegramBotAPI-bot_template/src/main/kotlin/telegram_bot/App.kt

32 lines
1.1 KiB
Kotlin
Raw Normal View History

2020-11-08 12:54:22 +00:00
package telegram_bot
2020-12-08 09:31:26 +00:00
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
2020-11-08 12:54:22 +00:00
import dev.inmo.tgbotapi.extensions.api.bot.getMe
2021-01-29 10:51:08 +00:00
import dev.inmo.tgbotapi.extensions.api.send.reply
2021-10-30 15:07:31 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
2021-01-29 10:51:08 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
import kotlinx.coroutines.*
2020-11-08 12:54:22 +00:00
/**
* This method by default expects one argument in [args] field: telegram bot token
*/
suspend fun main(args: Array<String>) {
2021-01-29 10:51:08 +00:00
// that is your bot
2020-11-08 12:54:22 +00:00
val bot = telegramBot(args.first())
2021-01-29 10:51:08 +00:00
// that is kotlin coroutine scope which will be used in requests and parallel works under the hood
val scope = CoroutineScope(Dispatchers.Default)
// here should be main logic of your bot
2021-10-30 15:07:31 +00:00
bot.buildBehaviourWithLongPolling(scope) {
2021-01-29 10:51:08 +00:00
// in this lambda you will be able to call methods without "bot." prefix
val me = getMe()
// this method will create point to react on each /start command
2021-01-31 11:42:15 +00:00
onCommand("start", requireOnlyCommandInMessage = true) {
2021-01-29 10:51:08 +00:00
// simply reply :)
reply(it, "Hello, I am ${me.firstName}")
}
}.join()
2020-11-08 12:54:22 +00:00
}