2022-04-29 14:57:16 +00:00
|
|
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
|
|
|
import dev.inmo.micro_utils.ktor.server.createKtorServer
|
2022-05-04 06:22:49 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
2022-04-29 14:57:16 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
2022-05-04 06:22:49 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
2022-04-29 14:57:16 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.*
|
2022-05-04 06:22:49 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.telegramBot
|
2022-04-29 14:57:16 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
2022-05-11 06:15:18 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.utils.formatting.botCommand
|
|
|
|
import dev.inmo.tgbotapi.extensions.utils.formatting.buildEntities
|
2022-04-29 14:57:16 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
2022-05-04 06:22:49 +00:00
|
|
|
import dev.inmo.tgbotapi.types.BotCommand
|
|
|
|
import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResultArticle
|
|
|
|
import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputTextMessageContent
|
|
|
|
import dev.inmo.tgbotapi.types.webAppQueryIdField
|
2022-05-03 13:55:08 +00:00
|
|
|
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
|
2022-05-10 18:23:14 +00:00
|
|
|
import dev.inmo.tgbotapi.utils.PreviewFeature
|
2022-05-04 06:22:49 +00:00
|
|
|
import io.ktor.http.HttpStatusCode
|
2022-05-10 18:23:14 +00:00
|
|
|
import io.ktor.server.application.call
|
|
|
|
import io.ktor.server.http.content.*
|
|
|
|
import io.ktor.server.request.receiveText
|
|
|
|
import io.ktor.server.response.respond
|
|
|
|
import io.ktor.server.routing.post
|
|
|
|
import io.ktor.server.routing.routing
|
2022-04-29 14:57:16 +00:00
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import java.io.File
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Accepts two parameters:
|
|
|
|
*
|
|
|
|
* * Telegram Token
|
|
|
|
* * URL where will be placed
|
|
|
|
*
|
|
|
|
* Will start the server to share the static (index.html and WebApp.js) on 0.0.0.0:8080
|
|
|
|
*/
|
2022-05-10 18:23:14 +00:00
|
|
|
@OptIn(PreviewFeature::class)
|
2022-04-29 14:57:16 +00:00
|
|
|
suspend fun main(vararg args: String) {
|
2022-05-04 06:22:49 +00:00
|
|
|
val bot = telegramBot(args.first(), testServer = args.any { it == "testServer" })
|
2022-04-29 14:57:16 +00:00
|
|
|
createKtorServer(
|
|
|
|
"0.0.0.0",
|
|
|
|
8080,
|
|
|
|
additionalEngineEnvironmentConfigurator = {
|
|
|
|
parentCoroutineContext += Dispatchers.IO
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
routing {
|
|
|
|
static {
|
|
|
|
files(File("WebApp/build/distributions"))
|
2022-05-04 06:22:49 +00:00
|
|
|
default("WebApp/build/distributions/index.html")
|
|
|
|
}
|
|
|
|
post("inline") {
|
|
|
|
val requestBody = call.receiveText()
|
|
|
|
val queryId = call.parameters[webAppQueryIdField] ?: error("$webAppQueryIdField should be presented")
|
|
|
|
|
|
|
|
bot.answer(queryId, InlineQueryResultArticle(queryId, "Result", InputTextMessageContent(requestBody)))
|
|
|
|
call.respond(HttpStatusCode.OK)
|
2022-04-29 14:57:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}.start(false)
|
|
|
|
|
2022-05-04 06:22:49 +00:00
|
|
|
bot.buildBehaviourWithLongPolling(
|
2022-04-29 14:57:16 +00:00
|
|
|
defaultExceptionsHandler = { it.printStackTrace() }
|
|
|
|
) {
|
2022-05-04 06:22:49 +00:00
|
|
|
onCommand("reply_markup") {
|
|
|
|
reply(
|
|
|
|
it,
|
|
|
|
"Button",
|
|
|
|
replyMarkup = replyKeyboard(resizeKeyboard = true, oneTimeKeyboard = true) {
|
|
|
|
row {
|
|
|
|
webAppButton("Open WebApp", WebAppInfo(args[1]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
)
|
|
|
|
}
|
|
|
|
onCommand("inline") {
|
2022-04-29 14:57:16 +00:00
|
|
|
reply(
|
|
|
|
it,
|
2022-05-03 13:55:08 +00:00
|
|
|
"Button",
|
2022-05-04 06:22:49 +00:00
|
|
|
replyMarkup = inlineKeyboard {
|
2022-04-29 14:57:16 +00:00
|
|
|
row {
|
2022-05-03 13:55:08 +00:00
|
|
|
webAppButton("Open WebApp", WebAppInfo(args[1]))
|
2022-04-29 14:57:16 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-03 13:55:08 +00:00
|
|
|
|
2022-04-29 14:57:16 +00:00
|
|
|
)
|
|
|
|
}
|
2022-05-07 18:41:13 +00:00
|
|
|
onUnhandledCommand {
|
2022-05-11 06:15:18 +00:00
|
|
|
reply(
|
|
|
|
it,
|
|
|
|
buildEntities {
|
|
|
|
+"Use " + botCommand("inline") + " to get inline web app button\n"
|
|
|
|
+"Use " + botCommand("reply_markup") + " to get reply markup web app button\n"
|
|
|
|
}
|
|
|
|
)
|
2022-05-07 18:41:13 +00:00
|
|
|
}
|
2022-05-04 06:22:49 +00:00
|
|
|
setMyCommands(
|
|
|
|
BotCommand("reply_markup", "Use to get reply markup keyboard with web app trigger"),
|
|
|
|
BotCommand("inline", "Use to get inline keyboard with web app trigger"),
|
|
|
|
)
|
2022-04-29 14:57:16 +00:00
|
|
|
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
|
|
|
println(it)
|
|
|
|
}
|
|
|
|
println(getMe())
|
2022-05-04 06:22:49 +00:00
|
|
|
}.join()
|
2022-04-29 14:57:16 +00:00
|
|
|
}
|