mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-10-05 15:09:20 +00:00
update to use resender bot in multiplatform
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
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.coroutines.*
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
|
||||
suspend fun activateResenderBot(
|
||||
token: String,
|
||||
print: (Any) -> Unit
|
||||
) {
|
||||
val bot = telegramBot(token)
|
||||
|
||||
print(bot.getMe())
|
||||
|
||||
supervisorScope {
|
||||
val scope = this
|
||||
bot.startGettingFlowsUpdatesByLongPolling {
|
||||
filterContentMessages<MessageContent>(scope).onEach {
|
||||
it.content.createResends(it.chat.id, replyToMessageId = it.messageId).forEach {
|
||||
bot.executeUnsafe(it) ?.also {
|
||||
print(it)
|
||||
}
|
||||
}
|
||||
}.launchIn(scope)
|
||||
filterMediaGroupMessages<MediaGroupContent>(scope).onEach {
|
||||
safely {
|
||||
bot.sendMediaGroup(
|
||||
it.first().chat,
|
||||
it.map { it.content.toMediaGroupMemberInputMedia() },
|
||||
replyToMessageId = it.first().messageId
|
||||
).also {
|
||||
print(it)
|
||||
}
|
||||
}
|
||||
}.launchIn(scope)
|
||||
}
|
||||
}
|
||||
}
|
32
ResenderBot/ResenderBotLib/src/jsMain/kotlin/ResenderBot.kt
Normal file
32
ResenderBot/ResenderBotLib/src/jsMain/kotlin/ResenderBot.kt
Normal file
@@ -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 {
|
||||
activateResenderBot(token) {
|
||||
infoDiv.innerHTML = it.toString()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
16
ResenderBot/ResenderBotLib/src/jsMain/resources/index.html
Normal file
16
ResenderBot/ResenderBotLib/src/jsMain/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>
|
Reference in New Issue
Block a user