mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2026-10-12 10:25:09 +00:00
update examples for ktgbotapi 37.0.0
This commit is contained in:
@@ -8,17 +8,23 @@ The bot defines no commands. It prints every incoming update to standard output
|
||||
|
||||
| Trigger | Behavior |
|
||||
| --- | --- |
|
||||
| Standalone Live Photo | Logs its file identifiers, dimensions, duration, thumbnail, MIME type, size, and caption. It resends the Live Photo, sends the same media as paid content costing 1 Star, and then edits the resent message with `TelegramMediaLivePhoto`. |
|
||||
| Live Photo gallery | Logs every item and resends the gallery with `sendMediaGroup`. |
|
||||
| Standalone Live Photo | Logs its file identifiers, dimensions, duration, thumbnail, MIME type, size, and caption. It resends the Live Photo by file ID, downloads both components, edits the resent message with new multipart files, and uploads the files again as paid content costing 1 Star. |
|
||||
| Live Photo gallery | Logs every item, downloads each main and cover file, and re-uploads the gallery with `sendMediaGroup`. |
|
||||
| Paid-media message containing Live Photos | Logs each Live Photo and replies with the number found. Paid-media messages without a Live Photo get no reply from this handler. |
|
||||
| Edited Live Photo | Logs the file ID and updated caption. |
|
||||
| Media group containing at least one regular photo and one regular video | Uses the first photo as the cover and the first video as the motion part, then replies with a Live Photo. Albums missing either type are ignored by this handler. |
|
||||
|
||||
## Live Photo handling
|
||||
|
||||
The bot does not download or transform media. It reuses Telegram file IDs: the received Live Photo is passed directly to `sendLivePhoto`, while its main file ID and thumbnail file ID are used to construct `TelegramMediaLivePhoto` and `TelegramPaidMediaLivePhoto`. If Telegram supplies no thumbnail, the code falls back to the main file ID for the photo field.
|
||||
The initial `sendLivePhoto` call reuses Telegram file IDs. The regular-media edit,
|
||||
paid-media send, and gallery resend then demonstrate ktgbotapi 37.0.0 collecting
|
||||
the secondary Live Photo `photo` attachment as well as the main multipart file.
|
||||
If Telegram supplies no cover photo, the code falls back to the main file ID for
|
||||
the `photo` field. Downloaded files are held in memory and are not transformed.
|
||||
|
||||
The standalone handler performs its requests in order: resend, send paid media, then edit the resent message. Consequently, a failure while sending paid media prevents the edit for that update.
|
||||
The standalone handler performs its requests in order: resend, download, edit,
|
||||
then send paid media. The edit can therefore succeed in a private or group chat
|
||||
before the channel-only paid-media request fails.
|
||||
|
||||
## Telegram setup and permissions
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import dev.inmo.kslog.common.setDefaultKSLog
|
||||
import dev.inmo.micro_utils.coroutines.subscribeLoggingDropExceptions
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.tgbotapi.extensions.api.edit.media.editMessageMedia
|
||||
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
||||
import dev.inmo.tgbotapi.extensions.api.send.media.sendLivePhoto
|
||||
import dev.inmo.tgbotapi.extensions.api.send.media.sendMediaGroup
|
||||
import dev.inmo.tgbotapi.extensions.api.send.media.sendPaidMedia
|
||||
@@ -23,6 +24,7 @@ import dev.inmo.tgbotapi.extensions.utils.photoFileOrNull
|
||||
import dev.inmo.tgbotapi.extensions.utils.videoContentOrNull
|
||||
import dev.inmo.tgbotapi.extensions.utils.videoFileOrNull
|
||||
import dev.inmo.tgbotapi.extensions.utils.withContentOrNull
|
||||
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
|
||||
import dev.inmo.tgbotapi.types.message.content.LivePhotoContent
|
||||
import dev.inmo.tgbotapi.types.message.payments.PaidMedia
|
||||
import dev.inmo.tgbotapi.types.media.TelegramMediaLivePhoto
|
||||
@@ -90,34 +92,42 @@ suspend fun main(vararg args: String) {
|
||||
)
|
||||
println(" sent message id: ${sent.messageId}")
|
||||
|
||||
// InputPaidMediaLivePhoto (TelegramPaidMediaLivePhoto): send the live photo as paid media (1 star)
|
||||
sendPaidMedia(
|
||||
chatId = message.chat.id,
|
||||
starCount = 1,
|
||||
media = listOf(
|
||||
// TelegramPaidMediaLivePhoto is InputPaidMediaLivePhoto
|
||||
TelegramPaidMediaLivePhoto(
|
||||
file = livePhotoFile.fileId,
|
||||
photo = livePhotoFile.photo?.fileId ?: livePhotoFile.fileId
|
||||
)
|
||||
),
|
||||
text = "Paid live photo (1 star)"
|
||||
)
|
||||
// Download both Live Photo components once. ktgbotapi 37.0.0 collects the secondary `photo`
|
||||
// MultipartFile alongside the main file for edits, media groups, and paid-media requests.
|
||||
val livePhotoBytes = downloadFile(livePhotoFile)
|
||||
val coverPhotoBytes = livePhotoFile.photo?.let { downloadFile(it) }
|
||||
|
||||
// editMessageMedia with InputMediaLivePhoto (TelegramMediaLivePhoto):
|
||||
// edit the previously sent message to replace it with itself via TelegramMediaLivePhoto
|
||||
// edit the previously sent message using newly uploaded main and cover files.
|
||||
val sentAsMedia = sent.withContentOrNull<LivePhotoContent>()
|
||||
if (sentAsMedia != null) {
|
||||
editMessageMedia(
|
||||
message = sentAsMedia,
|
||||
// TelegramMediaLivePhoto is InputMediaLivePhoto
|
||||
media = TelegramMediaLivePhoto(
|
||||
file = livePhotoFile.fileId,
|
||||
photo = livePhotoFile.photo?.fileId ?: livePhotoFile.fileId,
|
||||
text = "Edited via editMessageMedia with TelegramMediaLivePhoto"
|
||||
file = livePhotoBytes.asMultipartFile("edited-live-photo.mp4"),
|
||||
photo = coverPhotoBytes?.asMultipartFile("edited-live-photo-cover.jpg")
|
||||
?: livePhotoFile.photo?.fileId
|
||||
?: livePhotoFile.fileId,
|
||||
text = "Edited with newly uploaded Live Photo files"
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
// InputPaidMediaLivePhoto (TelegramPaidMediaLivePhoto): upload both files as paid media (1 star).
|
||||
// Telegram currently restricts sendPaidMedia to channel chats.
|
||||
sendPaidMedia(
|
||||
chatId = message.chat.id,
|
||||
starCount = 1,
|
||||
media = listOf(
|
||||
TelegramPaidMediaLivePhoto(
|
||||
file = livePhotoBytes.asMultipartFile("paid-live-photo.mp4"),
|
||||
photo = coverPhotoBytes?.asMultipartFile("paid-live-photo-cover.jpg")
|
||||
?: livePhotoFile.photo?.fileId
|
||||
?: livePhotoFile.fileId
|
||||
)
|
||||
),
|
||||
text = "Paid live photo uploaded as new files (1 star)"
|
||||
)
|
||||
}
|
||||
|
||||
// Demonstrates: sendMediaGroup with live photos, InputMediaLivePhoto (TelegramMediaLivePhoto)
|
||||
@@ -128,15 +138,17 @@ suspend fun main(vararg args: String) {
|
||||
println(" - fileId: ${livePhotoFile.fileId}, ${livePhotoFile.width}x${livePhotoFile.height}")
|
||||
}
|
||||
|
||||
// sendMediaGroup with TelegramMediaLivePhoto (InputMediaLivePhoto)
|
||||
// sendMediaGroup with newly uploaded main and cover files for every TelegramMediaLivePhoto.
|
||||
sendMediaGroup(
|
||||
chatId = mediaGroupContent.group.first().sourceMessage.chat.id,
|
||||
media = mediaGroupContent.group.map { groupMember ->
|
||||
media = mediaGroupContent.group.mapIndexed { index, groupMember ->
|
||||
val livePhotoFile = groupMember.content.media
|
||||
// TelegramMediaLivePhoto is InputMediaLivePhoto — used here in sendMediaGroup
|
||||
val coverPhoto = livePhotoFile.photo
|
||||
TelegramMediaLivePhoto(
|
||||
file = livePhotoFile.fileId,
|
||||
photo = livePhotoFile.photo?.fileId ?: livePhotoFile.fileId
|
||||
file = downloadFile(livePhotoFile).asMultipartFile("gallery-live-photo-$index.mp4"),
|
||||
photo = coverPhoto?.let {
|
||||
downloadFile(it).asMultipartFile("gallery-live-photo-cover-$index.jpg")
|
||||
} ?: livePhotoFile.fileId
|
||||
)
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user