mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-09-06 08:40:22 +00:00
update tgbotapi and add webapps example
This commit is contained in:
42
WebApp/src/jsMain/kotlin/main.kt
Normal file
42
WebApp/src/jsMain/kotlin/main.kt
Normal file
@@ -0,0 +1,42 @@
|
||||
import dev.inmo.tgbotapi.webapps.*
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.request.get
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.client.statement.readText
|
||||
import io.ktor.http.encodeURLPath
|
||||
import kotlinx.browser.document
|
||||
import kotlinx.browser.window
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.dom.appendElement
|
||||
import kotlinx.dom.appendText
|
||||
|
||||
fun main() {
|
||||
console.log("Web app started")
|
||||
window.onload = {
|
||||
val scope = CoroutineScope(Dispatchers.Default)
|
||||
runCatching {
|
||||
document.body ?.appendElement("button") {
|
||||
addEventListener(
|
||||
"click",
|
||||
{
|
||||
webApp.sendData("Clicked")
|
||||
}
|
||||
)
|
||||
appendText("Example button")
|
||||
} ?: window.alert("Unable to load body")
|
||||
webApp.apply {
|
||||
onThemeChanged {
|
||||
document.body ?.appendText("Theme changed: ${webApp.themeParams}")
|
||||
document.body ?.appendElement("p", {})
|
||||
}
|
||||
onViewportChanged {
|
||||
document.body ?.appendText("Viewport changed: ${it.isStateStable}")
|
||||
document.body ?.appendElement("p", {})
|
||||
}
|
||||
}
|
||||
webApp.ready()
|
||||
}.onFailure {
|
||||
window.alert(it.stackTraceToString())
|
||||
}
|
||||
}
|
||||
}
|
11
WebApp/src/jsMain/resources/index.html
Normal file
11
WebApp/src/jsMain/resources/index.html
Normal file
@@ -0,0 +1,11 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Web App Example</title>
|
||||
</head>
|
||||
<body>
|
||||
<script type="application/javascript" src="https://telegram.org/js/telegram-web-app.js"></script>
|
||||
<script type="application/javascript" src="WebApp.js"></script>
|
||||
</body>
|
||||
</html>
|
61
WebApp/src/jvmMain/kotlin/WebAppServer.kt
Normal file
61
WebApp/src/jvmMain/kotlin/WebAppServer.kt
Normal file
@@ -0,0 +1,61 @@
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.micro_utils.ktor.server.createKtorServer
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.content.files
|
||||
import io.ktor.http.content.static
|
||||
import io.ktor.routing.get
|
||||
import io.ktor.routing.routing
|
||||
import io.ktor.server.tomcat.Tomcat
|
||||
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
|
||||
*/
|
||||
suspend fun main(vararg args: String) {
|
||||
createKtorServer(
|
||||
Tomcat,
|
||||
"0.0.0.0",
|
||||
8080,
|
||||
additionalEngineEnvironmentConfigurator = {
|
||||
parentCoroutineContext += Dispatchers.IO
|
||||
}
|
||||
) {
|
||||
routing {
|
||||
static {
|
||||
files(File("WebApp/build/distributions"))
|
||||
}
|
||||
}
|
||||
}.start(false)
|
||||
|
||||
telegramBotWithBehaviourAndLongPolling(
|
||||
args.first(),
|
||||
defaultExceptionsHandler = { it.printStackTrace() }
|
||||
) {
|
||||
onCommand("start") {
|
||||
reply(
|
||||
it,
|
||||
"Button:",
|
||||
replyMarkup = inlineKeyboard {
|
||||
row {
|
||||
webAppButton("Open", args[1])
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||
println(it)
|
||||
}
|
||||
println(getMe())
|
||||
}.second.join()
|
||||
}
|
Reference in New Issue
Block a user