mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-12-27 16:39:12 +00:00
Compare commits
1 Commits
15.0.0
...
bcad5e00d3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bcad5e00d3 |
2
.github/workflows/build.yml
vendored
2
.github/workflows/build.yml
vendored
@@ -16,4 +16,4 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
java-version: 17
|
java-version: 17
|
||||||
- name: Build with Gradle
|
- name: Build with Gradle
|
||||||
run: ./gradlew build --no-daemon
|
run: ./gradlew build
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
|||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilterExcludeMediaGroups
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilterExcludeMediaGroups
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||||
import dev.inmo.tgbotapi.extensions.utils.possiblyWithEffectMessageOrNull
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
|
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
|
||||||
import dev.inmo.tgbotapi.extensions.utils.withContentOrNull
|
import dev.inmo.tgbotapi.extensions.utils.withContentOrNull
|
||||||
import dev.inmo.tgbotapi.types.ReplyParameters
|
import dev.inmo.tgbotapi.types.ReplyParameters
|
||||||
@@ -40,8 +39,7 @@ suspend fun activateResenderBot(
|
|||||||
entities = quote ?.textSources ?: emptyList(),
|
entities = quote ?.textSources ?: emptyList(),
|
||||||
quotePosition = quote ?.position
|
quotePosition = quote ?.position
|
||||||
)
|
)
|
||||||
},
|
}
|
||||||
effectId = it.possiblyWithEffectMessageOrNull() ?.effectId
|
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
it.forEach(print)
|
it.forEach(print)
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
# CustomBot
|
|
||||||
|
|
||||||
This bot basically have no any useful behaviour, but you may customize it as a playground
|
|
||||||
|
|
||||||
## Launch
|
|
||||||
|
|
||||||
```bash
|
|
||||||
../gradlew run --args="BOT_TOKEN"
|
|
||||||
```
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
buildscript {
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
apply plugin: 'kotlin'
|
|
||||||
apply plugin: 'application'
|
|
||||||
|
|
||||||
mainClassName="StarTransactionsBotKt"
|
|
||||||
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
||||||
|
|
||||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
|
||||||
}
|
|
||||||
@@ -1,131 +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.coroutines.subscribeSafelyWithoutExceptions
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.answers.payments.answerPreCheckoutQueryOk
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.edit.edit
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.get.getStarTransactions
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.command
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMessageDataCallbackQuery
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onPreCheckoutQuery
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.extensions.sameChat
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.dataButton
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatInlineKeyboard
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineKeyboard
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.payButton
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.withContentOrNull
|
|
||||||
import dev.inmo.tgbotapi.types.RawChatId
|
|
||||||
import dev.inmo.tgbotapi.types.UserId
|
|
||||||
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
|
|
||||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
|
||||||
import dev.inmo.tgbotapi.types.message.textsources.TextSource
|
|
||||||
import dev.inmo.tgbotapi.types.message.textsources.TextSourcesList
|
|
||||||
import dev.inmo.tgbotapi.types.payments.LabeledPrice
|
|
||||||
import dev.inmo.tgbotapi.types.payments.stars.StarTransaction
|
|
||||||
import dev.inmo.tgbotapi.utils.*
|
|
||||||
import kotlinx.coroutines.*
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The main purpose of this bot is just to answer "Oh, hi, " and add user mention here
|
|
||||||
*/
|
|
||||||
@OptIn(PreviewFeature::class)
|
|
||||||
suspend fun main(vararg args: String) {
|
|
||||||
val botToken = args.first()
|
|
||||||
val adminUserId = args.getOrNull(1) ?.toLongOrNull() ?.let(::RawChatId) ?.let(::UserId) ?: error("Pass user-admin for full access to the bot")
|
|
||||||
|
|
||||||
val isDebug = args.any { it == "debug" }
|
|
||||||
val isTestServer = args.any { it == "testServer" }
|
|
||||||
|
|
||||||
if (isDebug) {
|
|
||||||
setDefaultKSLog(
|
|
||||||
KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
|
|
||||||
println(defaultMessageFormatter(level, tag, message, throwable))
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO), testServer = isTestServer) {
|
|
||||||
val me = getMe()
|
|
||||||
|
|
||||||
val payload = "sample payload"
|
|
||||||
command("start") {
|
|
||||||
reply(
|
|
||||||
it,
|
|
||||||
price = LabeledPrice("1", 1L),
|
|
||||||
title = "Sample",
|
|
||||||
description = "Sample description",
|
|
||||||
payload = payload,
|
|
||||||
replyMarkup = flatInlineKeyboard {
|
|
||||||
payButton("Pay")
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
onPreCheckoutQuery(initialFilter = { it.invoicePayload == payload }) {
|
|
||||||
answerPreCheckoutQueryOk(it)
|
|
||||||
}
|
|
||||||
|
|
||||||
val transactionsDataPrefix = "getStarTransactions"
|
|
||||||
fun buildTransactionsData(offset: Int, limit: Int = 10) = "$transactionsDataPrefix $offset $limit"
|
|
||||||
fun parseTransactionsData(data: String): Pair<Int, Int> = data.split(" ").drop(1).let {
|
|
||||||
it.first().toInt() to it.last().toInt()
|
|
||||||
}
|
|
||||||
suspend fun buildStarTransactionsPage(offset: Int, limit: Int = 10): Pair<TextSourcesList, InlineKeyboardMarkup> {
|
|
||||||
val transactions = getStarTransactions(offset, limit)
|
|
||||||
return buildEntities {
|
|
||||||
transactions.transactions.forEach {
|
|
||||||
regular("Transaction Id: ") + bold(it.id.string) + "\n"
|
|
||||||
regular("Date: ") + bold(it.date.asDate.toStringDefault()) + "\n"
|
|
||||||
regular("Amount: ") + bold(it.amount.toString()) + "\n"
|
|
||||||
when (it) {
|
|
||||||
is StarTransaction.Incoming -> {
|
|
||||||
regular("Type: ") + bold("incoming") + "\n"
|
|
||||||
regular("Partner: ") + bold(it.partner.type) + "\n"
|
|
||||||
}
|
|
||||||
is StarTransaction.Outgoing -> {
|
|
||||||
regular("Type: ") + bold("outgoing") + "\n"
|
|
||||||
regular("Partner: ") + bold(it.partner.type) + "\n"
|
|
||||||
}
|
|
||||||
is StarTransaction.Unknown -> {
|
|
||||||
regular("Type: ") + bold("unknown") + "\n"
|
|
||||||
regular("Partner: ") + bold(it.partner.type) + "\n"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} to inlineKeyboard {
|
|
||||||
row {
|
|
||||||
val prevOffset = (offset - limit).coerceAtLeast(0)
|
|
||||||
if (prevOffset < offset) {
|
|
||||||
dataButton("<", buildTransactionsData(prevOffset, limit))
|
|
||||||
}
|
|
||||||
val nextOffset = (offset + limit)
|
|
||||||
dataButton(">", buildTransactionsData(nextOffset, limit))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
onCommand("transactions", initialFilter = { it.sameChat(adminUserId) }) {
|
|
||||||
val (text, keyboard) = buildStarTransactionsPage(0)
|
|
||||||
|
|
||||||
reply(it, text, replyMarkup = keyboard)
|
|
||||||
}
|
|
||||||
|
|
||||||
onMessageDataCallbackQuery(Regex("$transactionsDataPrefix \\d+ \\d+")) {
|
|
||||||
val (offset, limit) = parseTransactionsData(it.data)
|
|
||||||
val (text, keyboard) = buildStarTransactionsPage(offset, limit)
|
|
||||||
edit(
|
|
||||||
it.message.withContentOrNull<TextContent>() ?: return@onMessageDataCallbackQuery,
|
|
||||||
text,
|
|
||||||
replyMarkup = keyboard,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) { println(it) }
|
|
||||||
}.second.join()
|
|
||||||
}
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
||||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
||||||
import dev.inmo.tgbotapi.extensions.api.files.downloadFileToTemp
|
import dev.inmo.tgbotapi.extensions.api.files.downloadFileToTemp
|
||||||
@@ -78,18 +77,10 @@ suspend fun main(args: Array<String>) {
|
|||||||
runCatchingSafely {
|
runCatchingSafely {
|
||||||
getStickerSet(stickerSetName)
|
getStickerSet(stickerSetName)
|
||||||
}.onSuccess { stickerSet ->
|
}.onSuccess { stickerSet ->
|
||||||
runCatching {
|
addStickerToSet(it.chat.id.toChatId(), stickerSet.name, newSticker).also { _ ->
|
||||||
addStickerToSet(it.chat.id.toChatId(), stickerSet.name, newSticker).also { _ ->
|
|
||||||
reply(
|
|
||||||
it,
|
|
||||||
getStickerSet(stickerSetName).stickers.last()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}.onFailure { exception ->
|
|
||||||
exception.printStackTrace()
|
|
||||||
reply(
|
reply(
|
||||||
it,
|
it,
|
||||||
"Unable to add sticker in stickerset"
|
getStickerSet(stickerSetName).stickers.last()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}.onFailure { exception ->
|
}.onFailure { exception ->
|
||||||
@@ -109,9 +100,5 @@ suspend fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
|
||||||
println(it)
|
|
||||||
}
|
|
||||||
}.second.join()
|
}.second.join()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,7 @@
|
|||||||
import dev.inmo.kslog.common.*
|
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.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.answers.answer
|
||||||
@@ -37,7 +40,6 @@ import java.nio.charset.Charset
|
|||||||
*
|
*
|
||||||
* * Telegram Token
|
* * Telegram Token
|
||||||
* * URL where will be placed
|
* * URL where will be placed
|
||||||
* * Port (default 8080)
|
|
||||||
*
|
*
|
||||||
* 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
|
||||||
*/
|
*/
|
||||||
@@ -56,7 +58,6 @@ suspend fun main(vararg args: String) {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
val initiationLogger = KSLog("Initialization")
|
|
||||||
|
|
||||||
val bot = telegramBot(telegramBotAPIUrlsKeeper)
|
val bot = telegramBot(telegramBotAPIUrlsKeeper)
|
||||||
createKtorServer(
|
createKtorServer(
|
||||||
@@ -68,28 +69,12 @@ suspend fun main(vararg args: String) {
|
|||||||
) {
|
) {
|
||||||
routing {
|
routing {
|
||||||
val baseJsFolder = File("WebApp/build/dist/js/")
|
val baseJsFolder = File("WebApp/build/dist/js/")
|
||||||
val prodSubFolder = File(baseJsFolder, "productionExecutable")
|
baseJsFolder.list() ?.forEach {
|
||||||
val devSubFolder = File(baseJsFolder, "developmentExecutable")
|
if (it == "productionExecutable" || it == "developmentExecutable") {
|
||||||
|
staticFiles("", File(baseJsFolder, it)) {
|
||||||
val staticFolder = when {
|
default("WebApp/build/dist/js/$it/index.html")
|
||||||
prodSubFolder.exists() -> {
|
}
|
||||||
initiationLogger.i("Folder for static is ${prodSubFolder.absolutePath}")
|
|
||||||
prodSubFolder
|
|
||||||
}
|
}
|
||||||
devSubFolder.exists() -> {
|
|
||||||
initiationLogger.i("Folder for static is ${devSubFolder.absolutePath}")
|
|
||||||
devSubFolder
|
|
||||||
}
|
|
||||||
else -> error("""
|
|
||||||
Unable to detect any folder with static. Current working directory: ${File("").absolutePath}.
|
|
||||||
Searched paths:
|
|
||||||
* ${prodSubFolder.absolutePath}
|
|
||||||
* ${devSubFolder.absolutePath}
|
|
||||||
""".trimIndent())
|
|
||||||
}
|
|
||||||
|
|
||||||
staticFiles("", staticFolder) {
|
|
||||||
default("${staticFolder.absolutePath}${File.separator}index.html")
|
|
||||||
}
|
}
|
||||||
post("inline") {
|
post("inline") {
|
||||||
val requestBody = call.receiveText()
|
val requestBody = call.receiveText()
|
||||||
|
|||||||
@@ -2,11 +2,10 @@ kotlin.code.style=official
|
|||||||
org.gradle.parallel=true
|
org.gradle.parallel=true
|
||||||
# Due to parallel compilation project require next amount of memory on full build
|
# Due to parallel compilation project require next amount of memory on full build
|
||||||
org.gradle.jvmargs=-Xmx2344m
|
org.gradle.jvmargs=-Xmx2344m
|
||||||
kotlin.daemon.jvmargs=-Xmx2g -Xms500m
|
|
||||||
|
|
||||||
|
|
||||||
kotlin_version=1.9.23
|
kotlin_version=1.9.24
|
||||||
telegram_bot_api_version=15.0.0
|
telegram_bot_api_version=13.0.0
|
||||||
micro_utils_version=0.21.0
|
micro_utils_version=0.20.45
|
||||||
serialization_version=1.6.3
|
serialization_version=1.6.3
|
||||||
ktor_version=2.3.11
|
ktor_version=2.3.10
|
||||||
|
|||||||
@@ -50,6 +50,4 @@ include ":BoostsInfoBot"
|
|||||||
|
|
||||||
include ":BusinessConnectionsBot"
|
include ":BusinessConnectionsBot"
|
||||||
|
|
||||||
include ":StarTransactionsBot"
|
|
||||||
|
|
||||||
include ":CustomBot"
|
include ":CustomBot"
|
||||||
|
|||||||
Reference in New Issue
Block a user