mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-11-26 12:08:49 +00:00
Compare commits
5 Commits
382a8757f1
...
37bd7696eb
Author | SHA1 | Date | |
---|---|---|---|
|
37bd7696eb | ||
1c16a9f868 | |||
00c3aba12b | |||
a547bbce65 | |||
0b7d8c087f |
@ -1,6 +1,6 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
@ -1,6 +1,6 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
@ -1,6 +1,6 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
@ -1,6 +1,6 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
@ -1,6 +1,6 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
@ -1,6 +1,6 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
@ -1,6 +1,6 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
@ -1,6 +1,6 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
@ -1,6 +1,6 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
@ -1,6 +1,6 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
|
17
WebApp/README.md
Normal file
17
WebApp/README.md
Normal file
@ -0,0 +1,17 @@
|
||||
# WebApp
|
||||
|
||||
Here you may find simple example of `WebApp`. For work of this example you will need one of two things:
|
||||
|
||||
* Your own domain with SSL (letsencrypt is okay)
|
||||
* Test account in telegram
|
||||
|
||||
What is there in this module:
|
||||
|
||||
* JVM part of this example is a server with simple static webapp sharing and bot which just gives the webapp button to open webapp
|
||||
* JS part is the WebApp with one button and reacting to chaged user theme and app viewport
|
||||
|
||||
## How to run
|
||||
|
||||
```kotlin
|
||||
./gradlew run --args="TOKEN WEB_APP_ADDRESS"
|
||||
```
|
55
WebApp/build.gradle
Normal file
55
WebApp/build.gradle
Normal file
@ -0,0 +1,55 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version"
|
||||
}
|
||||
|
||||
apply plugin: 'application'
|
||||
|
||||
kotlin {
|
||||
jvm()
|
||||
js(IR) {
|
||||
browser()
|
||||
binaries.executable()
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
implementation kotlin('stdlib')
|
||||
}
|
||||
}
|
||||
|
||||
jsMain {
|
||||
dependencies {
|
||||
implementation "dev.inmo:tgbotapi.webapps:$telegram_bot_api_version"
|
||||
}
|
||||
}
|
||||
|
||||
jvmMain {
|
||||
dependencies {
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
implementation "dev.inmo:micro_utils.ktor.server:$micro_utils_version"
|
||||
implementation "io.ktor:ktor-server-tomcat:$ktor_version"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
application {
|
||||
mainClassName = "WebAppServerKt"
|
||||
}
|
||||
|
||||
tasks.getByName("compileKotlinJvm")
|
||||
.dependsOn(jsBrowserDistribution)
|
||||
tasks.getByName("compileKotlinJvm").configure {
|
||||
mustRunAfter jsBrowserDistribution
|
||||
}
|
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()
|
||||
}
|
@ -1,7 +1,7 @@
|
||||
allprojects {
|
||||
repositories {
|
||||
mavenLocal()
|
||||
jcenter()
|
||||
mavenCentral()
|
||||
if (project.hasProperty("GITHUB_USER") && project.hasProperty("GITHUB_TOKEN")) {
|
||||
maven {
|
||||
url "https://maven.pkg.github.com/InsanusMokrassar/TelegramBotAPI"
|
||||
|
@ -2,6 +2,7 @@ kotlin.code.style=official
|
||||
org.gradle.parallel=true
|
||||
|
||||
|
||||
kotlin_version=1.6.10
|
||||
telegram_bot_api_version=0.38.15
|
||||
micro_utils_version=0.9.20
|
||||
kotlin_version=1.6.21
|
||||
telegram_bot_api_version=0.38.16
|
||||
micro_utils_version=0.9.24
|
||||
ktor_version=1.6.8
|
||||
|
@ -16,4 +16,6 @@ include ":KeyboardsBot:jvm_launcher"
|
||||
|
||||
include ":SlotMachineDetectorBot"
|
||||
|
||||
include ":WebApp"
|
||||
|
||||
include ":FSMBot"
|
||||
|
Loading…
Reference in New Issue
Block a user