2023-03-11 09:55:09 +00:00
|
|
|
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
2024-06-01 07:19:00 +00:00
|
|
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
2023-03-11 09:55:09 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.files.downloadFileToTemp
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.get.getStickerSet
|
2023-03-11 15:39:08 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
|
|
|
import dev.inmo.tgbotapi.extensions.api.stickers.addStickerToSet
|
2023-03-11 09:55:09 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.stickers.createNewStickerSet
|
2023-03-11 15:39:08 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.stickers.deleteStickerSet
|
2023-03-11 09:55:09 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
2023-03-11 15:39:08 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
2023-03-11 09:55:09 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSticker
|
|
|
|
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
|
|
|
|
import dev.inmo.tgbotapi.requests.stickers.InputSticker
|
2024-03-18 07:46:15 +00:00
|
|
|
import dev.inmo.tgbotapi.types.StickerSetName
|
2023-03-11 15:39:08 +00:00
|
|
|
import dev.inmo.tgbotapi.types.chat.Chat
|
2024-07-10 16:23:36 +00:00
|
|
|
import dev.inmo.tgbotapi.types.files.CustomEmojiSticker
|
|
|
|
import dev.inmo.tgbotapi.types.files.MaskSticker
|
|
|
|
import dev.inmo.tgbotapi.types.files.RegularSticker
|
|
|
|
import dev.inmo.tgbotapi.types.files.UnknownSticker
|
2023-03-11 09:55:09 +00:00
|
|
|
import dev.inmo.tgbotapi.types.toChatId
|
2023-03-11 15:39:08 +00:00
|
|
|
import dev.inmo.tgbotapi.utils.botCommand
|
2023-03-11 09:55:09 +00:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
2023-03-11 15:39:08 +00:00
|
|
|
/**
|
|
|
|
* Send sticker to this bot to form your own stickers set. Send /delete to delete this sticker set
|
|
|
|
*/
|
2023-03-11 09:24:18 +00:00
|
|
|
suspend fun main(args: Array<String>) {
|
2023-03-11 15:39:08 +00:00
|
|
|
telegramBotWithBehaviourAndLongPolling(
|
|
|
|
args.first(),
|
|
|
|
scope = CoroutineScope(Dispatchers.IO),
|
|
|
|
defaultExceptionsHandler = {
|
|
|
|
it.printStackTrace()
|
|
|
|
}
|
|
|
|
) {
|
2023-03-11 09:55:09 +00:00
|
|
|
val me = getMe()
|
2024-03-18 07:46:15 +00:00
|
|
|
fun Chat.stickerSetName() = StickerSetName("s${id.chatId}_by_${me.username ?.withoutAt}")
|
2023-03-11 15:39:08 +00:00
|
|
|
onCommand("start") {
|
|
|
|
reply(it) {
|
|
|
|
botCommand("delete") + " - to clear stickers"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
onCommand("delete") {
|
|
|
|
val deleted = runCatchingSafely {
|
|
|
|
deleteStickerSet(it.chat.stickerSetName())
|
|
|
|
}.getOrElse { false }
|
|
|
|
|
|
|
|
if (deleted) {
|
|
|
|
reply(it, "Deleted")
|
|
|
|
} else {
|
|
|
|
reply(it, "Can't delete for some of reason")
|
|
|
|
}
|
|
|
|
}
|
2023-03-11 09:55:09 +00:00
|
|
|
onSticker {
|
2023-03-11 15:39:08 +00:00
|
|
|
val stickerSetName = it.chat.stickerSetName()
|
2023-03-11 09:55:09 +00:00
|
|
|
val sticker = it.content.media
|
2023-03-11 15:39:08 +00:00
|
|
|
val newSticker = when (sticker) {
|
|
|
|
is CustomEmojiSticker -> InputSticker.WithKeywords.CustomEmoji(
|
|
|
|
downloadFileToTemp(sticker.fileId).asMultipartFile(),
|
2024-04-18 11:03:53 +00:00
|
|
|
sticker.stickerFormat,
|
2023-03-11 15:39:08 +00:00
|
|
|
listOf(sticker.emoji ?: "\uD83D\uDE0A"),
|
|
|
|
emptyList()
|
|
|
|
)
|
|
|
|
is MaskSticker -> InputSticker.Mask(
|
|
|
|
downloadFileToTemp(sticker.fileId).asMultipartFile(),
|
2024-04-18 11:03:53 +00:00
|
|
|
sticker.stickerFormat,
|
2023-03-11 15:39:08 +00:00
|
|
|
listOf(sticker.emoji ?: "\uD83D\uDE0A"),
|
|
|
|
sticker.maskPosition
|
|
|
|
)
|
|
|
|
is RegularSticker -> InputSticker.WithKeywords.Regular(
|
|
|
|
downloadFileToTemp(sticker.fileId).asMultipartFile(),
|
2024-04-18 11:03:53 +00:00
|
|
|
sticker.stickerFormat,
|
2023-03-11 15:39:08 +00:00
|
|
|
listOf(sticker.emoji ?: "\uD83D\uDE0A"),
|
|
|
|
emptyList()
|
|
|
|
)
|
|
|
|
is UnknownSticker -> return@onSticker
|
|
|
|
}
|
2023-03-11 09:55:09 +00:00
|
|
|
runCatchingSafely {
|
|
|
|
getStickerSet(stickerSetName)
|
2023-03-11 15:39:08 +00:00
|
|
|
}.onSuccess { stickerSet ->
|
2024-06-01 07:19:00 +00:00
|
|
|
runCatching {
|
|
|
|
addStickerToSet(it.chat.id.toChatId(), stickerSet.name, newSticker).also { _ ->
|
|
|
|
reply(
|
|
|
|
it,
|
|
|
|
getStickerSet(stickerSetName).stickers.last()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}.onFailure { exception ->
|
|
|
|
exception.printStackTrace()
|
2023-03-11 15:39:08 +00:00
|
|
|
reply(
|
|
|
|
it,
|
2024-06-01 07:19:00 +00:00
|
|
|
"Unable to add sticker in stickerset"
|
2023-03-11 15:39:08 +00:00
|
|
|
)
|
|
|
|
}
|
2024-04-18 14:06:59 +00:00
|
|
|
}.onFailure { exception ->
|
2023-03-11 09:55:09 +00:00
|
|
|
createNewStickerSet(
|
|
|
|
it.chat.id.toChatId(),
|
2024-03-18 07:46:15 +00:00
|
|
|
stickerSetName.string,
|
2023-03-11 09:55:09 +00:00
|
|
|
"Sticker set by ${me.firstName}",
|
|
|
|
listOf(
|
2023-03-11 15:39:08 +00:00
|
|
|
newSticker
|
2023-03-11 09:55:09 +00:00
|
|
|
),
|
2023-03-11 15:39:08 +00:00
|
|
|
(sticker as? CustomEmojiSticker) ?.needsRepainting ?: false
|
|
|
|
).also { _ ->
|
|
|
|
reply(
|
|
|
|
it,
|
|
|
|
getStickerSet(stickerSetName).stickers.first()
|
|
|
|
)
|
|
|
|
}
|
2023-03-11 09:55:09 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-01 07:19:00 +00:00
|
|
|
|
|
|
|
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
|
|
|
println(it)
|
|
|
|
}
|
2023-03-11 15:39:08 +00:00
|
|
|
}.second.join()
|
|
|
|
}
|