mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2026-05-04 15:40:03 +00:00
Compare commits
1 Commits
0.38.21
...
b6606852fa
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b6606852fa |
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
jcenter()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
jcenter()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
jcenter()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
jcenter()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
jcenter()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
jcenter()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
jcenter()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
jcenter()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
jcenter()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
jcenter()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -1,17 +0,0 @@
|
|||||||
# 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"
|
|
||||||
```
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
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-cio:$ktor_version"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
application {
|
|
||||||
mainClassName = "WebAppServerKt"
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.getByName("compileKotlinJvm")
|
|
||||||
.dependsOn(jsBrowserDistribution)
|
|
||||||
tasks.getByName("compileKotlinJvm").configure {
|
|
||||||
mustRunAfter jsBrowserDistribution
|
|
||||||
}
|
|
||||||
@@ -1,55 +0,0 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
|
|
||||||
import dev.inmo.tgbotapi.types.webAppQueryIdField
|
|
||||||
import dev.inmo.tgbotapi.webapps.*
|
|
||||||
import io.ktor.client.HttpClient
|
|
||||||
import io.ktor.client.call.receive
|
|
||||||
import io.ktor.client.request.*
|
|
||||||
import io.ktor.client.statement.HttpResponse
|
|
||||||
import io.ktor.client.statement.readText
|
|
||||||
import io.ktor.http.*
|
|
||||||
import io.ktor.http.content.TextContent
|
|
||||||
import kotlinx.browser.document
|
|
||||||
import kotlinx.browser.window
|
|
||||||
import kotlinx.coroutines.*
|
|
||||||
import kotlinx.dom.appendElement
|
|
||||||
import kotlinx.dom.appendText
|
|
||||||
import org.w3c.dom.HTMLElement
|
|
||||||
|
|
||||||
fun HTMLElement.log(text: String) {
|
|
||||||
appendElement("p", {})
|
|
||||||
appendText(text)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
console.log("Web app started")
|
|
||||||
window.onload = {
|
|
||||||
val scope = CoroutineScope(Dispatchers.Default)
|
|
||||||
runCatching {
|
|
||||||
document.body ?.appendElement("button") {
|
|
||||||
addEventListener("click", {
|
|
||||||
scope.launchSafelyWithoutExceptions {
|
|
||||||
handleResult({ "Clicked" }) {
|
|
||||||
HttpClient().post<HttpResponse>("${window.location.origin.removeSuffix("/")}/inline") {
|
|
||||||
parameter(webAppQueryIdField, it)
|
|
||||||
body = TextContent("Clicked", ContentType.Text.Plain)
|
|
||||||
document.body ?.log(url.build().toString())
|
|
||||||
}.coroutineContext.job.join()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
appendText("Example button")
|
|
||||||
} ?: window.alert("Unable to load body")
|
|
||||||
webApp.apply {
|
|
||||||
onThemeChanged {
|
|
||||||
document.body ?.log("Theme changed: ${webApp.themeParams}")
|
|
||||||
}
|
|
||||||
onViewportChanged {
|
|
||||||
document.body ?.log("Viewport changed: ${it.isStateStable}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
webApp.ready()
|
|
||||||
}.onFailure {
|
|
||||||
window.alert(it.stackTraceToString())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,11 +0,0 @@
|
|||||||
<!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>
|
|
||||||
@@ -1,93 +0,0 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
|
||||||
import dev.inmo.micro_utils.ktor.server.createKtorServer
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.telegramBot
|
|
||||||
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 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
|
|
||||||
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
|
|
||||||
import io.ktor.application.call
|
|
||||||
import io.ktor.http.HttpStatusCode
|
|
||||||
import io.ktor.http.content.*
|
|
||||||
import io.ktor.request.receiveText
|
|
||||||
import io.ktor.response.respond
|
|
||||||
import io.ktor.routing.*
|
|
||||||
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) {
|
|
||||||
val bot = telegramBot(args.first(), testServer = args.any { it == "testServer" })
|
|
||||||
createKtorServer(
|
|
||||||
"0.0.0.0",
|
|
||||||
8080,
|
|
||||||
additionalEngineEnvironmentConfigurator = {
|
|
||||||
parentCoroutineContext += Dispatchers.IO
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
routing {
|
|
||||||
static {
|
|
||||||
files(File("WebApp/build/distributions"))
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.start(false)
|
|
||||||
|
|
||||||
bot.buildBehaviourWithLongPolling(
|
|
||||||
defaultExceptionsHandler = { it.printStackTrace() }
|
|
||||||
) {
|
|
||||||
onCommand("reply_markup") {
|
|
||||||
reply(
|
|
||||||
it,
|
|
||||||
"Button",
|
|
||||||
replyMarkup = replyKeyboard(resizeKeyboard = true, oneTimeKeyboard = true) {
|
|
||||||
row {
|
|
||||||
webAppButton("Open WebApp", WebAppInfo(args[1]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
)
|
|
||||||
}
|
|
||||||
onCommand("inline") {
|
|
||||||
reply(
|
|
||||||
it,
|
|
||||||
"Button",
|
|
||||||
replyMarkup = inlineKeyboard {
|
|
||||||
row {
|
|
||||||
webAppButton("Open WebApp", WebAppInfo(args[1]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
)
|
|
||||||
}
|
|
||||||
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"),
|
|
||||||
)
|
|
||||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
|
||||||
println(it)
|
|
||||||
}
|
|
||||||
println(getMe())
|
|
||||||
}.join()
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
allprojects {
|
allprojects {
|
||||||
repositories {
|
repositories {
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
mavenCentral()
|
jcenter()
|
||||||
if (project.hasProperty("GITHUB_USER") && project.hasProperty("GITHUB_TOKEN")) {
|
if (project.hasProperty("GITHUB_USER") && project.hasProperty("GITHUB_TOKEN")) {
|
||||||
maven {
|
maven {
|
||||||
url "https://maven.pkg.github.com/InsanusMokrassar/TelegramBotAPI"
|
url "https://maven.pkg.github.com/InsanusMokrassar/TelegramBotAPI"
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ kotlin.code.style=official
|
|||||||
org.gradle.parallel=true
|
org.gradle.parallel=true
|
||||||
|
|
||||||
|
|
||||||
kotlin_version=1.6.10
|
kotlin_version=1.6.21
|
||||||
telegram_bot_api_version=0.38.21
|
telegram_bot_api_version=0.38.15
|
||||||
micro_utils_version=0.9.24
|
micro_utils_version=0.9.20
|
||||||
ktor_version=1.6.8
|
|
||||||
|
|||||||
@@ -16,6 +16,4 @@ include ":KeyboardsBot:jvm_launcher"
|
|||||||
|
|
||||||
include ":SlotMachineDetectorBot"
|
include ":SlotMachineDetectorBot"
|
||||||
|
|
||||||
include ":WebApp"
|
|
||||||
|
|
||||||
include ":FSMBot"
|
include ":FSMBot"
|
||||||
|
|||||||
Reference in New Issue
Block a user