mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-11-14 04:13:53 +00:00
commit
590f9ec6d8
@ -7,7 +7,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAn
|
|||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMedia
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMedia
|
||||||
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
|
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
|
||||||
import dev.inmo.tgbotapi.types.actions.TypingAction
|
import dev.inmo.tgbotapi.types.actions.*
|
||||||
import dev.inmo.tgbotapi.types.media.TelegramMediaAudio
|
import dev.inmo.tgbotapi.types.media.TelegramMediaAudio
|
||||||
import dev.inmo.tgbotapi.types.media.TelegramMediaDocument
|
import dev.inmo.tgbotapi.types.media.TelegramMediaDocument
|
||||||
import dev.inmo.tgbotapi.types.media.TelegramMediaPhoto
|
import dev.inmo.tgbotapi.types.media.TelegramMediaPhoto
|
||||||
@ -34,13 +34,27 @@ suspend fun main(args: Array<String>) {
|
|||||||
val content = it.content
|
val content = it.content
|
||||||
val pathedFile = bot.getFileAdditionalInfo(content.media)
|
val pathedFile = bot.getFileAdditionalInfo(content.media)
|
||||||
val outFile = File(directoryOrFile, pathedFile.filePath.filenameFromUrl)
|
val outFile = File(directoryOrFile, pathedFile.filePath.filenameFromUrl)
|
||||||
|
withTypingAction(it.chat.id) {
|
||||||
runCatching {
|
runCatching {
|
||||||
bot.downloadFile(content.media, outFile)
|
bot.downloadFile(content.media, outFile)
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
it.printStackTrace()
|
it.printStackTrace()
|
||||||
}.onSuccess { _ ->
|
}.onSuccess { _ ->
|
||||||
reply(it, "Saved to ${outFile.absolutePath}")
|
reply(it, "Saved to ${outFile.absolutePath}")
|
||||||
withAction(it.chat.id, TypingAction) {
|
}
|
||||||
|
}.onSuccess { _ ->
|
||||||
|
val action = when (content) {
|
||||||
|
is PhotoContent -> UploadPhotoAction
|
||||||
|
is AnimationContent,
|
||||||
|
is VideoContent -> UploadVideoAction
|
||||||
|
is StickerContent -> ChooseStickerAction
|
||||||
|
is MediaGroupContent<*> -> UploadPhotoAction
|
||||||
|
is DocumentContent -> UploadDocumentAction
|
||||||
|
is VoiceContent,
|
||||||
|
is AudioContent -> RecordVoiceAction
|
||||||
|
is VideoNoteContent -> UploadVideoNoteAction
|
||||||
|
}
|
||||||
|
withAction(it.chat.id, action) {
|
||||||
when (content) {
|
when (content) {
|
||||||
is PhotoContent -> replyWithPhoto(
|
is PhotoContent -> replyWithPhoto(
|
||||||
it,
|
it,
|
||||||
|
@ -3,7 +3,10 @@ import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
|||||||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMentionWithAnyContent
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMentionWithAnyContent
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.extensions.raw.sender_chat
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.extensions.raw.text
|
||||||
import dev.inmo.tgbotapi.extensions.utils.formatting.linkMarkdownV2
|
import dev.inmo.tgbotapi.extensions.utils.formatting.linkMarkdownV2
|
||||||
import dev.inmo.tgbotapi.extensions.utils.formatting.textMentionMarkdownV2
|
import dev.inmo.tgbotapi.extensions.utils.formatting.textMentionMarkdownV2
|
||||||
import dev.inmo.tgbotapi.extensions.utils.ifFromChannelGroupContentMessage
|
import dev.inmo.tgbotapi.extensions.utils.ifFromChannelGroupContentMessage
|
||||||
@ -23,22 +26,35 @@ suspend fun main(vararg args: String) {
|
|||||||
|
|
||||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||||
val me = getMe()
|
val me = getMe()
|
||||||
onMentionWithAnyContent(me) { message ->
|
onContentMessage(
|
||||||
|
initialFilter = initialFilter@{ it.text ?.contains(me.username ?.full ?: return@initialFilter false) == true }
|
||||||
|
) { message ->
|
||||||
val answerText = when (val chat = message.chat) {
|
val answerText = when (val chat = message.chat) {
|
||||||
is PreviewChannelChat -> {
|
is PreviewChannelChat -> {
|
||||||
val answer = "Hi everybody in this channel \"${chat.title}\""
|
val sender = message.sender_chat
|
||||||
reply(message, answer, MarkdownV2)
|
val answer = "Hi everybody in this channel \"${chat.title}\"" + if (sender != null) {
|
||||||
return@onMentionWithAnyContent
|
" and you, " + when (sender) {
|
||||||
|
is BusinessChat -> "business chat (wat) ${sender.original}"
|
||||||
|
is PrivateChat -> "${sender.lastName} ${sender.firstName}"
|
||||||
|
is GroupChat -> "group ${sender.title}"
|
||||||
|
is ChannelChat -> "channel ${sender.title}"
|
||||||
|
is UnknownChatType -> "wat chat (${sender})"
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
reply(message, answer.escapeMarkdownV2Common(), MarkdownV2)
|
||||||
|
return@onContentMessage
|
||||||
}
|
}
|
||||||
is PreviewPrivateChat -> {
|
is PreviewPrivateChat -> {
|
||||||
reply(message, "Hi, " + "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id), MarkdownV2)
|
reply(message, "Hi, " + "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id), MarkdownV2)
|
||||||
return@onMentionWithAnyContent
|
return@onContentMessage
|
||||||
}
|
}
|
||||||
is PreviewGroupChat -> {
|
is PreviewGroupChat -> {
|
||||||
message.ifFromChannelGroupContentMessage<Unit> {
|
message.ifFromChannelGroupContentMessage<Unit> {
|
||||||
val answer = "Hi, ${it.senderChat.title}"
|
val answer = "Hi, ${it.senderChat.title}"
|
||||||
reply(message, answer, MarkdownV2)
|
reply(message, answer, MarkdownV2)
|
||||||
return@onMentionWithAnyContent
|
return@onContentMessage
|
||||||
}
|
}
|
||||||
"Oh, hi, " + when (chat) {
|
"Oh, hi, " + when (chat) {
|
||||||
is SupergroupChat -> (chat.username ?.username ?: getChat(chat).inviteLink) ?.let {
|
is SupergroupChat -> (chat.username ?.username ?: getChat(chat).inviteLink) ?.let {
|
||||||
@ -51,7 +67,7 @@ suspend fun main(vararg args: String) {
|
|||||||
}
|
}
|
||||||
is PreviewBusinessChat -> {
|
is PreviewBusinessChat -> {
|
||||||
reply(message, "Hi, " + "${chat.original.firstName} ${chat.original.lastName} (as business chat :) )".textMentionMarkdownV2(chat.original.id), MarkdownV2)
|
reply(message, "Hi, " + "${chat.original.firstName} ${chat.original.lastName} (as business chat :) )".textMentionMarkdownV2(chat.original.id), MarkdownV2)
|
||||||
return@onMentionWithAnyContent
|
return@onContentMessage
|
||||||
}
|
}
|
||||||
is UnknownChatType -> "Unknown :(".escapeMarkdownV2Common()
|
is UnknownChatType -> "Unknown :(".escapeMarkdownV2Common()
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,7 @@ suspend fun main(vararg args: String) {
|
|||||||
when (it) {
|
when (it) {
|
||||||
is Reaction.CustomEmoji -> regular("• ") + customEmoji(it.customEmojiId) + regular("(customEmojiId: ${it.customEmojiId})")
|
is Reaction.CustomEmoji -> regular("• ") + customEmoji(it.customEmojiId) + regular("(customEmojiId: ${it.customEmojiId})")
|
||||||
is Reaction.Emoji -> regular("• ${it.emoji}")
|
is Reaction.Emoji -> regular("• ${it.emoji}")
|
||||||
|
is Reaction.Paid -> regular("• Some paid reaction")
|
||||||
is Reaction.Unknown -> regular("• Unknown emoji ($it)")
|
is Reaction.Unknown -> regular("• Unknown emoji ($it)")
|
||||||
}
|
}
|
||||||
regular("\n")
|
regular("\n")
|
||||||
|
@ -6,7 +6,7 @@ kotlin.daemon.jvmargs=-Xmx3g -Xms500m
|
|||||||
|
|
||||||
|
|
||||||
kotlin_version=2.0.10
|
kotlin_version=2.0.10
|
||||||
telegram_bot_api_version=16.0.0
|
telegram_bot_api_version=17.0.0
|
||||||
micro_utils_version=0.22.0
|
micro_utils_version=0.22.0
|
||||||
serialization_version=1.7.1
|
serialization_version=1.7.1
|
||||||
ktor_version=2.3.11
|
ktor_version=2.3.11
|
||||||
|
Loading…
Reference in New Issue
Block a user