mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-12-01 22:40:13 +00:00
Compare commits
1 Commits
169483c93d
...
6eed9edf11
Author | SHA1 | Date | |
---|---|---|---|
|
6eed9edf11 |
@ -38,7 +38,7 @@ kotlin {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||||
implementation "dev.inmo:micro_utils.ktor.server:$micro_utils_version"
|
implementation "dev.inmo:micro_utils.ktor.server:$micro_utils_version"
|
||||||
implementation "io.ktor:ktor-server-cio:$ktor_version"
|
implementation "io.ktor:ktor-server-tomcat:$ktor_version"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,24 +1,14 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
|
|
||||||
import dev.inmo.tgbotapi.types.webAppQueryIdField
|
|
||||||
import dev.inmo.tgbotapi.webapps.*
|
import dev.inmo.tgbotapi.webapps.*
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import io.ktor.client.call.receive
|
import io.ktor.client.request.get
|
||||||
import io.ktor.client.request.*
|
|
||||||
import io.ktor.client.statement.HttpResponse
|
import io.ktor.client.statement.HttpResponse
|
||||||
import io.ktor.client.statement.readText
|
import io.ktor.client.statement.readText
|
||||||
import io.ktor.http.*
|
import io.ktor.http.encodeURLPath
|
||||||
import io.ktor.http.content.TextContent
|
|
||||||
import kotlinx.browser.document
|
import kotlinx.browser.document
|
||||||
import kotlinx.browser.window
|
import kotlinx.browser.window
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.dom.appendElement
|
import kotlinx.dom.appendElement
|
||||||
import kotlinx.dom.appendText
|
import kotlinx.dom.appendText
|
||||||
import org.w3c.dom.HTMLElement
|
|
||||||
|
|
||||||
fun HTMLElement.log(text: String) {
|
|
||||||
appendElement("p", {})
|
|
||||||
appendText(text)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
console.log("Web app started")
|
console.log("Web app started")
|
||||||
@ -26,25 +16,22 @@ fun main() {
|
|||||||
val scope = CoroutineScope(Dispatchers.Default)
|
val scope = CoroutineScope(Dispatchers.Default)
|
||||||
runCatching {
|
runCatching {
|
||||||
document.body ?.appendElement("button") {
|
document.body ?.appendElement("button") {
|
||||||
addEventListener("click", {
|
addEventListener(
|
||||||
scope.launchSafelyWithoutExceptions {
|
"click",
|
||||||
handleResult({ "Clicked" }) {
|
{
|
||||||
HttpClient().post<HttpResponse>("${window.location.origin.removeSuffix("/")}/inline") {
|
webApp.sendData("Clicked")
|
||||||
parameter(webAppQueryIdField, it)
|
|
||||||
body = TextContent("Clicked", ContentType.Text.Plain)
|
|
||||||
document.body ?.log(url.build().toString())
|
|
||||||
}.coroutineContext.job.join()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
appendText("Example button")
|
appendText("Example button")
|
||||||
} ?: window.alert("Unable to load body")
|
} ?: window.alert("Unable to load body")
|
||||||
webApp.apply {
|
webApp.apply {
|
||||||
onThemeChanged {
|
onThemeChanged {
|
||||||
document.body ?.log("Theme changed: ${webApp.themeParams}")
|
document.body ?.appendText("Theme changed: ${webApp.themeParams}")
|
||||||
|
document.body ?.appendElement("p", {})
|
||||||
}
|
}
|
||||||
onViewportChanged {
|
onViewportChanged {
|
||||||
document.body ?.log("Viewport changed: ${it.isStateStable}")
|
document.body ?.appendText("Viewport changed: ${it.isStateStable}")
|
||||||
|
document.body ?.appendElement("p", {})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
webApp.ready()
|
webApp.ready()
|
||||||
|
@ -1,24 +1,16 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
import dev.inmo.micro_utils.ktor.server.createKtorServer
|
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.getMe
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
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.*
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
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.application.call
|
||||||
import io.ktor.http.HttpStatusCode
|
import io.ktor.http.content.files
|
||||||
import io.ktor.http.content.*
|
import io.ktor.http.content.static
|
||||||
import io.ktor.request.receiveText
|
import io.ktor.routing.get
|
||||||
import io.ktor.response.respond
|
import io.ktor.routing.routing
|
||||||
import io.ktor.routing.*
|
import io.ktor.server.tomcat.Tomcat
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
@ -31,8 +23,8 @@ import java.io.File
|
|||||||
* Will start the server to share the static (index.html and WebApp.js) on 0.0.0.0:8080
|
* 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) {
|
suspend fun main(vararg args: String) {
|
||||||
val bot = telegramBot(args.first(), testServer = args.any { it == "testServer" })
|
|
||||||
createKtorServer(
|
createKtorServer(
|
||||||
|
Tomcat,
|
||||||
"0.0.0.0",
|
"0.0.0.0",
|
||||||
8080,
|
8080,
|
||||||
additionalEngineEnvironmentConfigurator = {
|
additionalEngineEnvironmentConfigurator = {
|
||||||
@ -42,52 +34,28 @@ suspend fun main(vararg args: String) {
|
|||||||
routing {
|
routing {
|
||||||
static {
|
static {
|
||||||
files(File("WebApp/build/distributions"))
|
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)
|
}.start(false)
|
||||||
|
|
||||||
bot.buildBehaviourWithLongPolling(
|
telegramBotWithBehaviourAndLongPolling(
|
||||||
|
args.first(),
|
||||||
defaultExceptionsHandler = { it.printStackTrace() }
|
defaultExceptionsHandler = { it.printStackTrace() }
|
||||||
) {
|
) {
|
||||||
onCommand("reply_markup") {
|
onCommand("start") {
|
||||||
reply(
|
reply(
|
||||||
it,
|
it,
|
||||||
"Button",
|
"Button:",
|
||||||
replyMarkup = replyKeyboard(resizeKeyboard = true, oneTimeKeyboard = true) {
|
|
||||||
row {
|
|
||||||
webAppButton("Open WebApp", WebAppInfo(args[1]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
)
|
|
||||||
}
|
|
||||||
onCommand("inline") {
|
|
||||||
reply(
|
|
||||||
it,
|
|
||||||
"Button",
|
|
||||||
replyMarkup = inlineKeyboard {
|
replyMarkup = inlineKeyboard {
|
||||||
row {
|
row {
|
||||||
webAppButton("Open WebApp", WebAppInfo(args[1]))
|
webAppButton("Open", 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) {
|
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||||
println(it)
|
println(it)
|
||||||
}
|
}
|
||||||
println(getMe())
|
println(getMe())
|
||||||
}.join()
|
}.second.join()
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,6 @@ org.gradle.parallel=true
|
|||||||
|
|
||||||
|
|
||||||
kotlin_version=1.6.21
|
kotlin_version=1.6.21
|
||||||
telegram_bot_api_version=0.38.21
|
telegram_bot_api_version=0.38.19
|
||||||
micro_utils_version=0.9.24
|
micro_utils_version=0.9.24
|
||||||
ktor_version=1.6.8
|
ktor_version=1.6.8
|
||||||
|
Loading…
Reference in New Issue
Block a user