mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-12-23 00:57:18 +00:00
add example for resender bot and JS
This commit is contained in:
parent
74e1d2e1a0
commit
b72921a44c
@ -20,5 +20,5 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||||
|
|
||||||
implementation "com.github.insanusmokrassar:TelegramBotAPI-all:$telegram_bot_api_version"
|
implementation "com.github.insanusmokrassar:TelegramBotAPI:$telegram_bot_api_version"
|
||||||
}
|
}
|
||||||
|
@ -20,5 +20,5 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||||
|
|
||||||
implementation "com.github.insanusmokrassar:TelegramBotAPI-all:$telegram_bot_api_version"
|
implementation "com.github.insanusmokrassar:TelegramBotAPI:$telegram_bot_api_version"
|
||||||
}
|
}
|
||||||
|
@ -20,5 +20,5 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||||
|
|
||||||
implementation "com.github.insanusmokrassar:TelegramBotAPI-all:$telegram_bot_api_version"
|
implementation "com.github.insanusmokrassar:TelegramBotAPI:$telegram_bot_api_version"
|
||||||
}
|
}
|
||||||
|
@ -20,5 +20,5 @@ repositories {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||||
|
|
||||||
implementation "com.github.insanusmokrassar:TelegramBotAPI-all:$telegram_bot_api_version"
|
implementation "com.github.insanusmokrassar:TelegramBotAPI:$telegram_bot_api_version"
|
||||||
}
|
}
|
||||||
|
22
ResenderBot/build.gradle
Normal file
22
ResenderBot/build.gradle
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
plugins {
|
||||||
|
id 'org.jetbrains.kotlin.js' version "$kotlin_version"
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
jcenter()
|
||||||
|
mavenCentral()
|
||||||
|
mavenLocal()
|
||||||
|
}
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
js(IR) {
|
||||||
|
browser()
|
||||||
|
binaries.executable()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation "org.jetbrains.kotlin:kotlin-stdlib-js"
|
||||||
|
|
||||||
|
implementation "com.github.insanusmokrassar:TelegramBotAPI:$telegram_bot_api_version"
|
||||||
|
}
|
65
ResenderBot/src/main/kotlin/ResenderBot.kt
Normal file
65
ResenderBot/src/main/kotlin/ResenderBot.kt
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.bot.getMe
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.send.media.sendMediaGroup
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.telegramBot
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.safely
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.shortcuts.*
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MediaGroupContent
|
||||||
|
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.abstracts.MessageContent
|
||||||
|
import kotlinx.browser.document
|
||||||
|
import kotlinx.coroutines.*
|
||||||
|
import kotlinx.coroutines.flow.*
|
||||||
|
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 = {
|
||||||
|
val botContainer = document.createElement("div") as HTMLDivElement
|
||||||
|
botsContainer.append(botContainer)
|
||||||
|
|
||||||
|
val statusDiv = document.createElement("div") as HTMLDivElement
|
||||||
|
botContainer.append(statusDiv)
|
||||||
|
|
||||||
|
val lastRequestAnswerDiv = document.createElement("div") as HTMLDivElement
|
||||||
|
botContainer.append(lastRequestAnswerDiv)
|
||||||
|
|
||||||
|
val token = (document.getElementById("bot_token") as? HTMLInputElement) ?.value
|
||||||
|
if (token != null) {
|
||||||
|
val bot = telegramBot(token)
|
||||||
|
scope.launch {
|
||||||
|
statusDiv.innerHTML = "Loaded bot: ${bot.getMe()}"
|
||||||
|
|
||||||
|
bot.startGettingFlowsUpdatesByLongPolling {
|
||||||
|
filterContentMessages<MessageContent>(scope).onEach {
|
||||||
|
it.content.createResends(it.chat.id, replyToMessageId = it.messageId).forEach {
|
||||||
|
bot.executeUnsafe(it) ?.also {
|
||||||
|
lastRequestAnswerDiv.innerHTML = it.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.launchIn(scope)
|
||||||
|
filterMediaGroupMessages<MediaGroupContent>(scope).onEach {
|
||||||
|
safely {
|
||||||
|
bot.sendMediaGroup(
|
||||||
|
it.first().chat,
|
||||||
|
it.map { it.content.toMediaGroupMemberInputMedia() },
|
||||||
|
replyToMessageId = it.first().messageId
|
||||||
|
).also {
|
||||||
|
lastRequestAnswerDiv.innerHTML = it.toString()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.launchIn(scope)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
16
ResenderBot/src/main/resources/index.html
Normal file
16
ResenderBot/src/main/resources/index.html
Normal file
@ -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="ResenderBot.js"></script>
|
||||||
|
<div id="bots_container"></div>
|
||||||
|
</body>
|
||||||
|
</html>
|
@ -1,3 +1,5 @@
|
|||||||
kotlin_version=1.3.72
|
kotlin.code.style=official
|
||||||
|
org.gradle.parallel=true
|
||||||
|
|
||||||
telegram_bot_api_version=0.27.11
|
kotlin_version=1.4.0
|
||||||
|
telegram_bot_api_version=0.28.0-rc
|
||||||
|
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
|
|||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.4.1-all.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6-bin.zip
|
||||||
|
@ -2,3 +2,4 @@ include ":ForwarderBot"
|
|||||||
include ":RandomFileSenderBot"
|
include ":RandomFileSenderBot"
|
||||||
include ":HelloBot"
|
include ":HelloBot"
|
||||||
include ":GetMeBot"
|
include ":GetMeBot"
|
||||||
|
include ":ResenderBot"
|
||||||
|
Loading…
Reference in New Issue
Block a user