mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-11-22 16:23:54 +00:00
migration onto 3.1.0 and including new example
This commit is contained in:
parent
36273adfd1
commit
068dc79ac8
@ -25,7 +25,9 @@ suspend fun activateResenderBot(
|
|||||||
) {
|
) {
|
||||||
val chat = it.chat
|
val chat = it.chat
|
||||||
withTypingAction(chat) {
|
withTypingAction(chat) {
|
||||||
executeUnsafe(it.content.createResend(chat.id, replyToMessageId = it.messageId))
|
executeUnsafe(it.content.createResend(chat.id, replyToMessageId = it.messageId)) {
|
||||||
|
it.forEach(print)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
onVisualGallery {
|
onVisualGallery {
|
||||||
|
33
StickerInfoBot/StickerInfoBotLib/build.gradle
Normal file
33
StickerInfoBot/StickerInfoBotLib/build.gradle
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
jvm()
|
||||||
|
// js(LEGACY) {
|
||||||
|
js(IR) {
|
||||||
|
browser()
|
||||||
|
binaries.executable()
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
commonMain {
|
||||||
|
dependencies {
|
||||||
|
implementation kotlin('stdlib')
|
||||||
|
|
||||||
|
api "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,71 @@
|
|||||||
|
import dev.inmo.micro_utils.coroutines.defaultSafelyWithoutExceptionHandler
|
||||||
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
|
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.get.getCustomEmojiStickerOrNull
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.get.getStickerSet
|
||||||
|
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.formatting.*
|
||||||
|
import dev.inmo.tgbotapi.types.StickerType
|
||||||
|
import dev.inmo.tgbotapi.types.message.textsources.*
|
||||||
|
import dev.inmo.tgbotapi.types.stickers.StickerSet
|
||||||
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
|
fun StickerSet.buildInfo() = buildEntities {
|
||||||
|
bold("StickerSet name: ") + "${name}\n"
|
||||||
|
bold("StickerSet title: ") + "${title}\n"
|
||||||
|
bold(
|
||||||
|
when (stickerType) {
|
||||||
|
StickerType.CustomEmoji -> "Custom emoji"
|
||||||
|
StickerType.Mask -> "Mask"
|
||||||
|
StickerType.Regular -> "Regular"
|
||||||
|
is StickerType.Unknown -> "Unknown type \"${stickerType.type}\""
|
||||||
|
}
|
||||||
|
) + " sticker set with title " + bold(title) + " and name " + bold(name)
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun activateStickerInfoBot(
|
||||||
|
token: String,
|
||||||
|
print: (Any) -> Unit
|
||||||
|
) {
|
||||||
|
val bot = telegramBot(token)
|
||||||
|
|
||||||
|
print(bot.getMe())
|
||||||
|
|
||||||
|
defaultSafelyWithoutExceptionHandler = {
|
||||||
|
it.printStackTrace()
|
||||||
|
}
|
||||||
|
|
||||||
|
bot.buildBehaviourWithLongPolling(CoroutineScope(currentCoroutineContext() + SupervisorJob())) {
|
||||||
|
onText {
|
||||||
|
withTypingAction(it.chat) {
|
||||||
|
it.content.textSources.mapNotNull {
|
||||||
|
if (it is CustomEmojiTextSource) {
|
||||||
|
getCustomEmojiStickerOrNull(it.customEmojiId) ?.stickerSetName
|
||||||
|
} else {
|
||||||
|
null
|
||||||
|
}
|
||||||
|
}.distinct().map {
|
||||||
|
getStickerSet(it)
|
||||||
|
}.distinct().flatMap {
|
||||||
|
it.buildInfo() + regular("\n")
|
||||||
|
}.separateForText().map { entities ->
|
||||||
|
reply(it, entities)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
onSticker {
|
||||||
|
val stickerSetInfo = getStickerSet(it.content.media)
|
||||||
|
reply(
|
||||||
|
it,
|
||||||
|
stickerSetInfo.buildInfo()
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||||
|
println(it)
|
||||||
|
}
|
||||||
|
}.join()
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
import kotlinx.browser.document
|
||||||
|
import kotlinx.coroutines.*
|
||||||
|
import org.w3c.dom.*
|
||||||
|
|
||||||
|
private val scope = CoroutineScope(Dispatchers.Default)
|
||||||
|
|
||||||
|
fun main() {
|
||||||
|
document.addEventListener(
|
||||||
|
"DOMContentLoaded",
|
||||||
|
{
|
||||||
|
val botsContainer = document.getElementById("bots_container") ?: return@addEventListener
|
||||||
|
|
||||||
|
(document.getElementById("bot_token_form") as? HTMLFormElement) ?.onsubmit = {
|
||||||
|
(document.getElementById("bot_token") as? HTMLInputElement) ?.value ?.let { token ->
|
||||||
|
val botContainer = document.createElement("div") as HTMLDivElement
|
||||||
|
botsContainer.append(botContainer)
|
||||||
|
|
||||||
|
val infoDiv = document.createElement("div") as HTMLDivElement
|
||||||
|
botContainer.append(infoDiv)
|
||||||
|
|
||||||
|
scope.launch {
|
||||||
|
activateStickerInfoBot(token) {
|
||||||
|
infoDiv.innerHTML = it.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
@ -0,0 +1,16 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<title>Resender bot</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<form id="bot_token_form">
|
||||||
|
<input type="text" id="bot_token">
|
||||||
|
<input type="submit" value="Start bot">
|
||||||
|
</form>
|
||||||
|
<div id="start_offer">Type your bot token to the input above to start its work</div>
|
||||||
|
<script type="text/javascript" src="StickerInfoBotLib.js"></script>
|
||||||
|
<div id="bots_container"></div>
|
||||||
|
</body>
|
||||||
|
</html>
|
21
StickerInfoBot/jvm_launcher/build.gradle
Normal file
21
StickerInfoBot/jvm_launcher/build.gradle
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
apply plugin: 'kotlin'
|
||||||
|
apply plugin: 'application'
|
||||||
|
|
||||||
|
mainClassName="StickerInfoBotJvmKt"
|
||||||
|
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
|
|
||||||
|
implementation project(":StickerInfoBot:StickerInfoBotLib")
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
suspend fun main(args: Array<String>) {
|
||||||
|
activateStickerInfoBot(args.first()) {
|
||||||
|
println(it)
|
||||||
|
}
|
||||||
|
}
|
@ -5,7 +5,7 @@ org.gradle.jvmargs=-Xmx768m
|
|||||||
|
|
||||||
|
|
||||||
kotlin_version=1.7.10
|
kotlin_version=1.7.10
|
||||||
telegram_bot_api_version=3.0.2
|
telegram_bot_api_version=3.1.0
|
||||||
micro_utils_version=0.12.0
|
micro_utils_version=0.12.1
|
||||||
serialization_version=1.4.0-RC
|
serialization_version=1.4.0-RC
|
||||||
ktor_version=2.0.3
|
ktor_version=2.1.0
|
||||||
|
@ -14,6 +14,9 @@ include ":ResenderBot:jvm_launcher"
|
|||||||
include ":KeyboardsBot:KeyboardsBotLib"
|
include ":KeyboardsBot:KeyboardsBotLib"
|
||||||
include ":KeyboardsBot:jvm_launcher"
|
include ":KeyboardsBot:jvm_launcher"
|
||||||
|
|
||||||
|
include ":StickerInfoBot:StickerInfoBotLib"
|
||||||
|
include ":StickerInfoBot:jvm_launcher"
|
||||||
|
|
||||||
include ":SlotMachineDetectorBot"
|
include ":SlotMachineDetectorBot"
|
||||||
|
|
||||||
include ":WebApp"
|
include ":WebApp"
|
||||||
|
Loading…
Reference in New Issue
Block a user