Skip to content

Commit b0554ad

Browse files
add delete_story test
1 parent ad90180 commit b0554ad

1 file changed

Lines changed: 42 additions & 8 deletions

File tree

BusinessConnectionsBot/src/main/kotlin/BusinessConnectionsBot.kt

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import dev.inmo.tgbotapi.extensions.api.files.downloadFileToTemp
1919
import dev.inmo.tgbotapi.extensions.api.get.getBusinessConnection
2020
import dev.inmo.tgbotapi.extensions.api.send.reply
2121
import dev.inmo.tgbotapi.extensions.api.send.send
22+
import dev.inmo.tgbotapi.extensions.api.stories.deleteStory
2223
import dev.inmo.tgbotapi.extensions.api.stories.postStory
2324
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
2425
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
@@ -40,7 +41,10 @@ import dev.inmo.tgbotapi.types.business_connection.BusinessConnectionId
4041
import dev.inmo.tgbotapi.types.chat.PrivateChat
4142
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
4243
import dev.inmo.tgbotapi.types.message.content.PhotoContent
44+
import dev.inmo.tgbotapi.types.message.content.StoryContent
4345
import dev.inmo.tgbotapi.types.message.content.TextContent
46+
import dev.inmo.tgbotapi.types.message.content.VideoContent
47+
import dev.inmo.tgbotapi.types.message.content.VisualMediaGroupPartContent
4448
import dev.inmo.tgbotapi.types.stories.InputStoryContent
4549
import dev.inmo.tgbotapi.types.stories.StoryArea
4650
import dev.inmo.tgbotapi.types.stories.StoryAreaPosition
@@ -325,23 +329,28 @@ suspend fun main(args: Array<String>) {
325329
handleSetProfilePhoto(it, true)
326330
}
327331

328-
onCommand("postStory", initialFilter = { it.chat is PrivateChat }) {
332+
onCommand("post_story", initialFilter = { it.chat is PrivateChat }) {
329333
val businessConnectionId = chatsBusinessConnections[it.chat.id] ?: return@onCommand
330-
val replyTo = it.replyTo ?.commonMessageOrNull() ?.withContentOrNull<PhotoContent>()
334+
val replyTo = it.replyTo ?.commonMessageOrNull() ?.withContentOrNull<VisualMediaGroupPartContent>()
331335
if (replyTo == null) {
332336
reply(it) {
333-
+"Reply to photo for using of this command"
337+
+"Reply to photo or video for using of this command"
334338
}
335339
return@onCommand
336340
}
337341

338-
val set = runCatching {
342+
val posted = runCatching {
339343
val file = downloadFileToTemp(replyTo.content)
340344
postStory(
341345
businessConnectionId,
342-
InputStoryContent.Photo(
343-
file.multipartFile()
344-
),
346+
when (replyTo.content) {
347+
is PhotoContent -> InputStoryContent.Photo(
348+
file.multipartFile()
349+
)
350+
is VideoContent -> InputStoryContent.Video(
351+
file.multipartFile()
352+
)
353+
},
345354
activePeriod = PostStory.ACTIVE_PERIOD_6_HOURS,
346355
areas = listOf(
347356
StoryArea(
@@ -366,12 +375,37 @@ suspend fun main(args: Array<String>) {
366375
null
367376
}
368377
reply(it) {
369-
if (set != null) {
378+
if (posted != null) {
370379
+"Story has been posted. You may unpost it with " + botCommand("remove_story")
371380
} else {
372381
+"Story has not been posted"
373382
}
374383
}
375384
}
385+
386+
onCommand("delete_story", initialFilter = { it.chat is PrivateChat }) {
387+
val businessConnectionId = chatsBusinessConnections[it.chat.id] ?: return@onCommand
388+
val replyTo = it.replyTo ?.commonMessageOrNull() ?.withContentOrNull<StoryContent>()
389+
if (replyTo == null) {
390+
reply(it) {
391+
+"Reply to photo or video for using of this command"
392+
}
393+
return@onCommand
394+
}
395+
396+
val deleted = runCatching {
397+
deleteStory(businessConnectionId, replyTo.content.story.id)
398+
}.getOrElse {
399+
it.printStackTrace()
400+
false
401+
}
402+
reply(it) {
403+
if (deleted) {
404+
+"Story has been deleted"
405+
} else {
406+
+"Story has not been deleted"
407+
}
408+
}
409+
}
376410
}.second.join()
377411
}

0 commit comments

Comments
 (0)