2024-01-08 13:23:06 +00:00
|
|
|
import dev.inmo.kslog.common.KSLog
|
|
|
|
import dev.inmo.kslog.common.LogLevel
|
|
|
|
import dev.inmo.kslog.common.defaultMessageFormatter
|
|
|
|
import dev.inmo.kslog.common.setDefaultKSLog
|
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
|
2023-04-22 05:06:03 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.answers.answerInlineQuery
|
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.*
|
2023-04-21 18:11:56 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.utils.formatting.makeTelegramStartattach
|
2022-04-29 14:57:16 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
2023-04-22 05:06:03 +00:00
|
|
|
import dev.inmo.tgbotapi.requests.answers.InlineQueryResultsButton
|
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
|
2024-03-18 07:46:15 +00:00
|
|
|
import dev.inmo.tgbotapi.types.InlineQueryId
|
2024-01-08 13:23:06 +00:00
|
|
|
import dev.inmo.tgbotapi.types.LinkPreviewOptions
|
2022-05-04 06:22:49 +00:00
|
|
|
import dev.inmo.tgbotapi.types.webAppQueryIdField
|
2022-05-03 13:55:08 +00:00
|
|
|
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
|
2022-09-19 08:37:01 +00:00
|
|
|
import dev.inmo.tgbotapi.utils.*
|
2022-05-17 12:58:43 +00:00
|
|
|
import io.ktor.http.*
|
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
|
2022-05-17 12:58:43 +00:00
|
|
|
import kotlinx.serialization.json.Json
|
2022-04-29 14:57:16 +00:00
|
|
|
import java.io.File
|
2022-05-17 12:58:43 +00:00
|
|
|
import java.nio.charset.Charset
|
2022-04-29 14:57:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-17 12:58:43 +00:00
|
|
|
val telegramBotAPIUrlsKeeper = TelegramAPIUrlsKeeper(
|
|
|
|
args.first(),
|
|
|
|
testServer = args.any { it == "testServer" }
|
|
|
|
)
|
2024-01-08 13:23:06 +00:00
|
|
|
val isDebug = args.any { it == "debug" }
|
|
|
|
|
|
|
|
if (isDebug) {
|
|
|
|
setDefaultKSLog(
|
|
|
|
KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
|
|
|
|
println(defaultMessageFormatter(level, tag, message, throwable))
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-05-17 12:58:43 +00:00
|
|
|
val bot = telegramBot(telegramBotAPIUrlsKeeper)
|
2022-04-29 14:57:16 +00:00
|
|
|
createKtorServer(
|
|
|
|
"0.0.0.0",
|
2023-04-19 15:46:35 +00:00
|
|
|
args.getOrNull(2) ?.toIntOrNull() ?: 8080,
|
2022-04-29 14:57:16 +00:00
|
|
|
additionalEngineEnvironmentConfigurator = {
|
|
|
|
parentCoroutineContext += Dispatchers.IO
|
|
|
|
}
|
|
|
|
) {
|
|
|
|
routing {
|
2023-10-17 17:28:13 +00:00
|
|
|
val baseJsFolder = File("WebApp/build/dist/js/")
|
|
|
|
baseJsFolder.list() ?.forEach {
|
|
|
|
if (it == "productionExecutable" || it == "developmentExecutable") {
|
|
|
|
staticFiles("", File(baseJsFolder, it)) {
|
|
|
|
default("WebApp/build/dist/js/$it/index.html")
|
|
|
|
}
|
|
|
|
}
|
2022-05-04 06:22:49 +00:00
|
|
|
}
|
|
|
|
post("inline") {
|
|
|
|
val requestBody = call.receiveText()
|
2024-03-18 07:46:15 +00:00
|
|
|
val queryId = call.parameters[webAppQueryIdField] ?.let(::InlineQueryId) ?: error("$webAppQueryIdField should be presented")
|
2022-05-04 06:22:49 +00:00
|
|
|
|
2024-03-18 07:46:15 +00:00
|
|
|
bot.answerInlineQuery(queryId, listOf(InlineQueryResultArticle(queryId, "Result", InputTextMessageContent(requestBody))))
|
2022-05-04 06:22:49 +00:00
|
|
|
call.respond(HttpStatusCode.OK)
|
2022-04-29 14:57:16 +00:00
|
|
|
}
|
2022-05-17 12:58:43 +00:00
|
|
|
post("check") {
|
|
|
|
val requestBody = call.receiveText()
|
|
|
|
val webAppCheckData = Json { }.decodeFromString(WebAppDataWrapper.serializer(), requestBody)
|
|
|
|
|
2022-05-18 11:46:50 +00:00
|
|
|
val isSafe = telegramBotAPIUrlsKeeper.checkWebAppData(webAppCheckData.data, webAppCheckData.hash)
|
2022-05-17 12:58:43 +00:00
|
|
|
|
|
|
|
call.respond(HttpStatusCode.OK, isSafe.toString())
|
|
|
|
}
|
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() }
|
|
|
|
) {
|
2023-04-21 18:11:56 +00:00
|
|
|
val me = getMe()
|
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
|
|
|
}
|
2024-01-08 13:23:06 +00:00
|
|
|
},
|
|
|
|
linkPreviewOptions = LinkPreviewOptions.Small(
|
|
|
|
args[1],
|
|
|
|
showAboveText = false
|
|
|
|
)
|
2022-04-29 14:57:16 +00:00
|
|
|
)
|
|
|
|
}
|
2023-04-21 18:11:56 +00:00
|
|
|
onCommand("attachment_menu") {
|
|
|
|
reply(
|
|
|
|
it,
|
2023-04-22 05:06:03 +00:00
|
|
|
"Button",
|
2023-04-21 18:11:56 +00:00
|
|
|
replyMarkup = inlineKeyboard {
|
|
|
|
row {
|
|
|
|
webAppButton("Open WebApp", WebAppInfo(args[1]))
|
|
|
|
}
|
2024-01-08 13:23:06 +00:00
|
|
|
},
|
|
|
|
linkPreviewOptions = LinkPreviewOptions.Large(
|
|
|
|
args[1],
|
|
|
|
showAboveText = true
|
|
|
|
)
|
2023-04-21 18:11:56 +00:00
|
|
|
)
|
|
|
|
}
|
2023-04-22 05:06:03 +00:00
|
|
|
onBaseInlineQuery {
|
|
|
|
answerInlineQuery(
|
|
|
|
it,
|
|
|
|
button = InlineQueryResultsButton.invoke(
|
|
|
|
"Open webApp",
|
|
|
|
WebAppInfo(args[1])
|
|
|
|
)
|
|
|
|
)
|
|
|
|
}
|
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
|
|
|
}
|
2023-04-21 18:11:56 +00:00
|
|
|
onWriteAccessAllowed(initialFilter = { it.chatEvent.webAppName != null }) {
|
|
|
|
send(it.chat, "Thanks for adding ${it.chatEvent.webAppName} to the attachment menu")
|
|
|
|
}
|
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
|
|
|
}
|