mirror of
				https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
				synced 2025-11-03 21:50:09 +00:00 
			
		
		
		
	update to use resender bot in multiplatform
This commit is contained in:
		
							
								
								
									
										35
									
								
								ResenderBot/ResenderBotLib/build.gradle
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										35
									
								
								ResenderBot/ResenderBotLib/build.gradle
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,35 @@
 | 
			
		||||
buildscript {
 | 
			
		||||
    repositories {
 | 
			
		||||
        jcenter()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    dependencies {
 | 
			
		||||
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
plugins {
 | 
			
		||||
    id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version"
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
repositories {
 | 
			
		||||
    jcenter()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
kotlin {
 | 
			
		||||
    jvm()
 | 
			
		||||
    js(IR) {
 | 
			
		||||
        browser()
 | 
			
		||||
        binaries.executable()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    sourceSets {
 | 
			
		||||
        commonMain {
 | 
			
		||||
            dependencies {
 | 
			
		||||
                implementation kotlin('stdlib')
 | 
			
		||||
 | 
			
		||||
                api "com.github.insanusmokrassar:TelegramBotAPI:$telegram_bot_api_version"
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -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
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
@@ -1,22 +0,0 @@
 | 
			
		||||
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"
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								ResenderBot/jvm_launcher/build.gradle
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								ResenderBot/jvm_launcher/build.gradle
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,24 @@
 | 
			
		||||
buildscript {
 | 
			
		||||
    repositories {
 | 
			
		||||
        jcenter()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    dependencies {
 | 
			
		||||
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
apply plugin: 'kotlin'
 | 
			
		||||
apply plugin: 'application'
 | 
			
		||||
 | 
			
		||||
mainClassName="ResenderBotJvmKt"
 | 
			
		||||
 | 
			
		||||
repositories {
 | 
			
		||||
    jcenter()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
dependencies {
 | 
			
		||||
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
 | 
			
		||||
 | 
			
		||||
    implementation project(":ResenderBot:ResenderBotLib")
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,5 @@
 | 
			
		||||
suspend fun main(args: Array<String>) {
 | 
			
		||||
    activateResenderBot(args.first()) {
 | 
			
		||||
        println(it)
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -1,65 +0,0 @@
 | 
			
		||||
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
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    )
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user