add target and telegram target
This commit is contained in:
features
content
binary
common
common
src
commonMain
kotlin
dev
inmo
postssystem
features
content
common
server
src
jvmMain
kotlin
dev
inmo
postssystem
features
publication
server
settings.gradletargets/telegram/publication/server
22
targets/telegram/publication/server/build.gradle
Normal file
22
targets/telegram/publication/server/build.gradle
Normal file
@ -0,0 +1,22 @@
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
}
|
||||
|
||||
apply from: "$mppJavaProjectPresetPath"
|
||||
|
||||
kotlin {
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
api project(":postssystem.features.common.server")
|
||||
api project(":postssystem.features.publication.server")
|
||||
|
||||
api project(":postssystem.features.content.binary.server")
|
||||
api project(":postssystem.features.content.text.server")
|
||||
|
||||
api "dev.inmo:tgbotapi:$tgbotapi_version"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package dev.inmo.postssystem.targets.telegram.publication.server
|
||||
|
||||
import dev.inmo.micro_utils.mime_types.KnownMimeTypes
|
||||
import dev.inmo.postssystem.features.content.binary.common.BinaryContent
|
||||
import dev.inmo.postssystem.features.content.text.common.TextContent
|
||||
import dev.inmo.postssystem.features.publication.server.PublicationPost
|
||||
import dev.inmo.postssystem.features.publication.server.PublicationTarget
|
||||
import dev.inmo.tgbotapi.bot.TelegramBot
|
||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.executeUnsafe
|
||||
import dev.inmo.tgbotapi.requests.abstracts.MultipartFile
|
||||
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
|
||||
import dev.inmo.tgbotapi.requests.send.SendTextMessage
|
||||
import dev.inmo.tgbotapi.requests.send.media.*
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.utils.StorageFile
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
class PublicationTargetTelegram(
|
||||
private val bot: TelegramBot,
|
||||
private val targetChatId: ChatId
|
||||
) : PublicationTarget {
|
||||
override suspend fun publish(post: PublicationPost) {
|
||||
post.content.mapNotNull {
|
||||
val content = it.content
|
||||
when (content) {
|
||||
is BinaryContent -> {
|
||||
val storageFile by lazy {
|
||||
StorageFile(content.filename.name, content.bytesAllocator()).asMultipartFile()
|
||||
}
|
||||
when (content.mimeType) {
|
||||
is KnownMimeTypes.Image.Jpeg,
|
||||
is KnownMimeTypes.Image.Png -> {
|
||||
SendPhoto(targetChatId, storageFile)
|
||||
}
|
||||
is KnownMimeTypes.Video.Mp4 -> {
|
||||
SendVideo(targetChatId, storageFile)
|
||||
}
|
||||
is KnownMimeTypes.Audio.Mpeg -> {
|
||||
SendAudio(targetChatId, storageFile)
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
is TextContent -> {
|
||||
SendTextMessage(targetChatId, content.text)
|
||||
}
|
||||
else -> null
|
||||
}
|
||||
}.forEach { request ->
|
||||
bot.executeUnsafe(request, 3, 1000L)
|
||||
delay(100L)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user