mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-11-30 05:58:49 +00:00
Compare commits
2 Commits
7de31addc9
...
4dbe96b505
Author | SHA1 | Date | |
---|---|---|---|
|
4dbe96b505 | ||
|
8c9e27f729 |
@ -63,7 +63,10 @@ suspend fun main(vararg args: String) {
|
|||||||
val bot = telegramBot(telegramBotAPIUrlsKeeper)
|
val bot = telegramBot(telegramBotAPIUrlsKeeper)
|
||||||
createKtorServer(
|
createKtorServer(
|
||||||
"0.0.0.0",
|
"0.0.0.0",
|
||||||
args.getOrNull(2) ?.toIntOrNull() ?: 8080
|
args.getOrNull(2) ?.toIntOrNull() ?: 8080,
|
||||||
|
additionalEngineEnvironmentConfigurator = {
|
||||||
|
parentCoroutineContext += Dispatchers.IO
|
||||||
|
}
|
||||||
) {
|
) {
|
||||||
routing {
|
routing {
|
||||||
val baseJsFolder = File("WebApp/build/dist/js/")
|
val baseJsFolder = File("WebApp/build/dist/js/")
|
||||||
|
@ -1,28 +0,0 @@
|
|||||||
# WebHooks
|
|
||||||
|
|
||||||
Launches webhook-based simple bot. Use `/start` with bot to get simple info about webhooks
|
|
||||||
|
|
||||||
## Launch
|
|
||||||
|
|
||||||
```bash
|
|
||||||
../gradlew run --args="BOT_TOKEN https://sample.com it/is/subpath 8080 debug"
|
|
||||||
```
|
|
||||||
|
|
||||||
Required arguments:
|
|
||||||
|
|
||||||
1. Token
|
|
||||||
2. Arguments starting with `https://`
|
|
||||||
|
|
||||||
Optional arguments:
|
|
||||||
|
|
||||||
* Any argument == `debug` to enable debug mode
|
|
||||||
* Any argument **not** starting with `https://` and **not** equal to `debug` as **subpath** (will be used as
|
|
||||||
subroute to place listening of webhooks)
|
|
||||||
* Any argument as number of port
|
|
||||||
|
|
||||||
Sample: `TOKEN https://sample.com it/is/subpath 8080` will result to:
|
|
||||||
|
|
||||||
* `TOKEN` used as token
|
|
||||||
* Bot will set up its webhook info as `https://sample.com/it/is/subpath`
|
|
||||||
* Bot will set up to listen webhooks on route `it/is/subpath`
|
|
||||||
* Bot will start to listen any incoming request on port `8080` and url `0.0.0.0`
|
|
@ -1,23 +0,0 @@
|
|||||||
buildscript {
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
apply plugin: 'kotlin'
|
|
||||||
apply plugin: 'application'
|
|
||||||
|
|
||||||
mainClassName="WebHooksKt"
|
|
||||||
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
||||||
|
|
||||||
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"
|
|
||||||
}
|
|
@ -1,87 +0,0 @@
|
|||||||
import dev.inmo.kslog.common.KSLog
|
|
||||||
import dev.inmo.kslog.common.LogLevel
|
|
||||||
import dev.inmo.kslog.common.defaultMessageFormatter
|
|
||||||
import dev.inmo.kslog.common.setDefaultKSLog
|
|
||||||
import dev.inmo.micro_utils.ktor.server.createKtorServer
|
|
||||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.webhook.setWebhookInfo
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.includeWebhookHandlingInRoute
|
|
||||||
import dev.inmo.tgbotapi.types.BotCommand
|
|
||||||
import dev.inmo.tgbotapi.types.chat.PrivateChat
|
|
||||||
import dev.inmo.tgbotapi.utils.buildEntities
|
|
||||||
import io.ktor.server.routing.*
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Launches webhook-based simple bot. Required arguments:
|
|
||||||
*
|
|
||||||
* 1. Token
|
|
||||||
* *. Arguments starting with `https://`
|
|
||||||
*
|
|
||||||
* Optional arguments:
|
|
||||||
*
|
|
||||||
* *. Any argument == `debug` to enable debug mode
|
|
||||||
* *. Any argument **not** starting with `https://` and **not** equal to `debug` as **subpath** (will be used as
|
|
||||||
* subroute to place listening of webhooks)
|
|
||||||
* *. Any argument as number of port
|
|
||||||
*
|
|
||||||
* Sample: `TOKEN https://sample.com it/is/subpath 8080` will result to:
|
|
||||||
*
|
|
||||||
* * `TOKEN` used as token
|
|
||||||
* * Bot will set up its webhook info as `https://sample.com/it/is/subpath`
|
|
||||||
* * Bot will set up to listen webhooks on route `it/is/subpath`
|
|
||||||
* * Bot will start to listen any incoming request on port `8080` and url `0.0.0.0`
|
|
||||||
*/
|
|
||||||
suspend fun main(args: Array<String>) {
|
|
||||||
val botToken = args.first()
|
|
||||||
val address = args.first { it.startsWith("https://") }
|
|
||||||
val subpath = args.drop(1).firstOrNull { it != address && it != "debug" }
|
|
||||||
val port = args.firstNotNullOfOrNull { it.toIntOrNull() } ?: 8080
|
|
||||||
val isDebug = args.any { it == "debug" }
|
|
||||||
|
|
||||||
if (isDebug) {
|
|
||||||
setDefaultKSLog(
|
|
||||||
KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
|
|
||||||
println(defaultMessageFormatter(level, tag, message, throwable))
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
val bot = telegramBot(botToken)
|
|
||||||
|
|
||||||
val behaviourContext = bot.buildBehaviour (defaultExceptionsHandler = { it.printStackTrace() }) {
|
|
||||||
onCommand("start", initialFilter = { it.chat is PrivateChat }) {
|
|
||||||
reply(
|
|
||||||
it,
|
|
||||||
buildEntities {
|
|
||||||
+"Url: $address" + "\n"
|
|
||||||
+"Listening server: 0.0.0.0" + "\n"
|
|
||||||
+"Listening port: $port"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
setMyCommands(BotCommand("start", "Get webhook info"))
|
|
||||||
}
|
|
||||||
|
|
||||||
val webhookInfoSubpath = subpath ?.let { "/" + it.removePrefix("/") } ?: "" // drop leading `/` to add it in the beginning for correct construction of subpath
|
|
||||||
bot.setWebhookInfo(address + webhookInfoSubpath)
|
|
||||||
|
|
||||||
createKtorServer(
|
|
||||||
"0.0.0.0",
|
|
||||||
port,
|
|
||||||
) {
|
|
||||||
routing {
|
|
||||||
if (subpath == null) {
|
|
||||||
includeWebhookHandlingInRoute(behaviourContext, block = behaviourContext.asUpdateReceiver)
|
|
||||||
} else {
|
|
||||||
route(subpath) {
|
|
||||||
includeWebhookHandlingInRoute(behaviourContext, block = behaviourContext.asUpdateReceiver)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.start(true)
|
|
||||||
}
|
|
@ -6,7 +6,7 @@ kotlin.daemon.jvmargs=-Xmx3g -Xms500m
|
|||||||
|
|
||||||
|
|
||||||
kotlin_version=2.0.21
|
kotlin_version=2.0.21
|
||||||
telegram_bot_api_version=19.1.0-branch_19.1.0-build2501
|
telegram_bot_api_version=18.2.3
|
||||||
micro_utils_version=0.23.0
|
micro_utils_version=0.22.7
|
||||||
serialization_version=1.7.3
|
serialization_version=1.7.3
|
||||||
ktor_version=3.0.1
|
ktor_version=2.3.12
|
||||||
|
@ -57,5 +57,3 @@ include ":GiveawaysBot"
|
|||||||
include ":CustomBot"
|
include ":CustomBot"
|
||||||
|
|
||||||
include ":MemberUpdatedWatcherBot"
|
include ":MemberUpdatedWatcherBot"
|
||||||
|
|
||||||
include ":WebHooks"
|
|
||||||
|
Loading…
Reference in New Issue
Block a user