mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-09-03 15:19:49 +00:00
complete sample with sticker set handler example
This commit is contained in:
9
StickerSetHandler/README.md
Normal file
9
StickerSetHandler/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# StickerSetHandler
|
||||
|
||||
Send sticker to this bot to form your own stickers set. Send /delete to delete this sticker set
|
||||
|
||||
## How to run
|
||||
|
||||
```bash
|
||||
./gradlew run --args="TOKEN"
|
||||
```
|
@@ -3,46 +3,99 @@ import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
||||
import dev.inmo.tgbotapi.extensions.api.files.downloadFileToTemp
|
||||
import dev.inmo.tgbotapi.extensions.api.get.getStickerSet
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.api.stickers.addStickerToSet
|
||||
import dev.inmo.tgbotapi.extensions.api.stickers.createNewStickerSet
|
||||
import dev.inmo.tgbotapi.extensions.api.stickers.deleteStickerSet
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onSticker
|
||||
import dev.inmo.tgbotapi.extensions.utils.extensions.raw.sticker
|
||||
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
|
||||
import dev.inmo.tgbotapi.requests.stickers.InputSticker
|
||||
import dev.inmo.tgbotapi.types.chat.Chat
|
||||
import dev.inmo.tgbotapi.types.files.*
|
||||
import dev.inmo.tgbotapi.types.toChatId
|
||||
import dev.inmo.tgbotapi.utils.botCommand
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
||||
/**
|
||||
* Send sticker to this bot to form your own stickers set. Send /delete to delete this sticker set
|
||||
*/
|
||||
suspend fun main(args: Array<String>) {
|
||||
telegramBotWithBehaviourAndLongPolling(args.first(), scope = CoroutineScope(Dispatchers.IO)) {
|
||||
telegramBotWithBehaviourAndLongPolling(
|
||||
args.first(),
|
||||
scope = CoroutineScope(Dispatchers.IO),
|
||||
defaultExceptionsHandler = {
|
||||
it.printStackTrace()
|
||||
}
|
||||
) {
|
||||
val me = getMe()
|
||||
fun Chat.stickerSetName() = "s${id.chatId}_by_${me.username.usernameWithoutAt}"
|
||||
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")
|
||||
}
|
||||
}
|
||||
onSticker {
|
||||
val stickerSetName = "${it.chat.id}_by_${me.username.username}"
|
||||
val stickerSetName = it.chat.stickerSetName()
|
||||
val sticker = it.content.media
|
||||
val newSticker = when (sticker) {
|
||||
is CustomEmojiSticker -> InputSticker.WithKeywords.CustomEmoji(
|
||||
downloadFileToTemp(sticker.fileId).asMultipartFile(),
|
||||
listOf(sticker.emoji ?: "\uD83D\uDE0A"),
|
||||
emptyList()
|
||||
)
|
||||
is MaskSticker -> InputSticker.Mask(
|
||||
downloadFileToTemp(sticker.fileId).asMultipartFile(),
|
||||
listOf(sticker.emoji ?: "\uD83D\uDE0A"),
|
||||
sticker.maskPosition
|
||||
)
|
||||
is RegularSticker -> InputSticker.WithKeywords.Regular(
|
||||
downloadFileToTemp(sticker.fileId).asMultipartFile(),
|
||||
listOf(sticker.emoji ?: "\uD83D\uDE0A"),
|
||||
emptyList()
|
||||
)
|
||||
is UnknownSticker -> return@onSticker
|
||||
}
|
||||
runCatchingSafely {
|
||||
getStickerSet(stickerSetName)
|
||||
}.getOrElse { _ ->
|
||||
}.onSuccess { stickerSet ->
|
||||
addStickerToSet(it.chat.id.toChatId(), stickerSet.name, newSticker).also { _ ->
|
||||
reply(
|
||||
it,
|
||||
getStickerSet(stickerSetName).stickers.last()
|
||||
)
|
||||
}
|
||||
}.onFailure { _ ->
|
||||
createNewStickerSet(
|
||||
it.chat.id.toChatId(),
|
||||
stickerSetName,
|
||||
"Sticker set by ${me.firstName}",
|
||||
it.content.media.stickerFormat,
|
||||
listOf(
|
||||
when (sticker) {
|
||||
is CustomEmojiSticker -> InputSticker.WithKeywords.CustomEmoji(
|
||||
downloadFileToTemp(sticker.fileId).asMultipartFile(),
|
||||
sticker.emoji ?.let(::listOf) ?: emptyList(),
|
||||
emptyList()
|
||||
)
|
||||
is MaskSticker -> TODO()
|
||||
is RegularSticker -> TODO()
|
||||
is UnknownSticker -> TODO()
|
||||
}
|
||||
newSticker
|
||||
),
|
||||
sticker
|
||||
)
|
||||
(sticker as? CustomEmojiSticker) ?.needsRepainting ?: false
|
||||
).also { _ ->
|
||||
reply(
|
||||
it,
|
||||
getStickerSet(stickerSetName).stickers.first()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}.second.join()
|
||||
}
|
||||
|
Reference in New Issue
Block a user