2020-11-08 12:54:22 +00:00
|
|
|
package telegram_bot
|
|
|
|
|
2022-05-13 20:05:25 +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
|
2022-06-16 21:24:35 +00:00
|
|
|
import java.io.File
|
2021-01-29 10:51:08 +00:00
|
|
|
import kotlinx.coroutines.*
|
2022-06-16 21:24:35 +00:00
|
|
|
import kotlinx.serialization.json.Json
|
2020-11-08 12:54:22 +00:00
|
|
|
|
|
|
|
/**
|
2022-06-16 21:24:35 +00:00
|
|
|
* This method by default expects one argument in [args] field: telegram bot condiguration
|
2020-11-08 12:54:22 +00:00
|
|
|
*/
|
|
|
|
suspend fun main(args: Array<String>) {
|
2022-06-16 21:24:35 +00:00
|
|
|
// create json to decode config
|
|
|
|
val json = Json { ignoreUnknownKeys = true }
|
|
|
|
// decode config
|
2022-06-16 21:26:56 +00:00
|
|
|
val config: Config = json.decodeFromString(Config.serializer(), File(args.first()).readText())
|
2021-01-29 10:51:08 +00:00
|
|
|
// that is your bot
|
2022-06-16 21:24:35 +00:00
|
|
|
val bot = telegramBot(config.token)
|
2020-11-08 12:54:22 +00:00
|
|
|
|
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}")
|
|
|
|
}
|
2021-11-21 14:51:56 +00:00
|
|
|
|
|
|
|
// That will be called on the end of bot initiation. After that prinln will be started long polling and bot will
|
|
|
|
// react on your commands
|
|
|
|
println(me)
|
2021-04-22 11:15:25 +00:00
|
|
|
}.join()
|
2020-11-08 12:54:22 +00:00
|
|
|
}
|