mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-11-24 19:18:49 +00:00
update up to 0.35.1
This commit is contained in:
parent
c90952d3ca
commit
d062ab86ae
@ -1,14 +1,12 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.safely
|
|
||||||
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.downloadFile
|
import dev.inmo.tgbotapi.extensions.api.downloadFile
|
||||||
import dev.inmo.tgbotapi.extensions.api.get.getFileAdditionalInfo
|
import dev.inmo.tgbotapi.extensions.api.get.getFileAdditionalInfo
|
||||||
import dev.inmo.tgbotapi.extensions.utils.flatMap
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviour
|
||||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
||||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMedia
|
||||||
import dev.inmo.tgbotapi.utils.filenameFromUrl
|
import dev.inmo.tgbotapi.utils.filenameFromUrl
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.flow.*
|
import kotlinx.coroutines.Dispatchers
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -19,24 +17,15 @@ suspend fun main(args: Array<String>) {
|
|||||||
val directoryOrFile = args.getOrNull(1) ?.let { File(it) } ?: File("")
|
val directoryOrFile = args.getOrNull(1) ?.let { File(it) } ?: File("")
|
||||||
directoryOrFile.mkdirs()
|
directoryOrFile.mkdirs()
|
||||||
|
|
||||||
val bot = telegramBot(botToken)
|
telegramBotWithBehaviour(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||||
val scope = CoroutineScope(Dispatchers.Default)
|
onMedia(includeMediaGroups = true) {
|
||||||
|
|
||||||
bot.longPolling(scope = scope) {
|
|
||||||
val flow = merge (
|
|
||||||
filterContentMessages<MediaContent>(),
|
|
||||||
mediaGroupMessages().flatMap()
|
|
||||||
)
|
|
||||||
flow.onEach {
|
|
||||||
safely({ it.printStackTrace() }) {
|
|
||||||
val pathedFile = bot.getFileAdditionalInfo(it.content.media)
|
val pathedFile = bot.getFileAdditionalInfo(it.content.media)
|
||||||
File(directoryOrFile, pathedFile.filePath.filenameFromUrl).apply {
|
val file = File(directoryOrFile, pathedFile.filePath.filenameFromUrl).apply {
|
||||||
createNewFile()
|
createNewFile()
|
||||||
writeBytes(bot.downloadFile(pathedFile))
|
writeBytes(bot.downloadFile(pathedFile))
|
||||||
}
|
}
|
||||||
|
reply(it, "Saved to ${file.absolutePath}")
|
||||||
}
|
}
|
||||||
}.launchIn(scope)
|
onContentMessage { println(it) }
|
||||||
}
|
}.second.join()
|
||||||
|
|
||||||
scope.coroutineContext[Job]!!.join()
|
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,12 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.safely
|
import dev.inmo.micro_utils.coroutines.defaultSafelyExceptionHandler
|
||||||
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.sendTextMessage
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
import dev.inmo.tgbotapi.extensions.api.telegramBot
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviour
|
||||||
import dev.inmo.tgbotapi.extensions.utils.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
||||||
import dev.inmo.tgbotapi.extensions.utils.formatting.codeMarkdownV2
|
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
||||||
import dev.inmo.tgbotapi.extensions.utils.formatting.regularMarkdownV2
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.mediaGroupMessages
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.updates.asContentMessagesFlow
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling
|
|
||||||
import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2
|
import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2
|
||||||
import dev.inmo.tgbotapi.types.message.*
|
import dev.inmo.tgbotapi.types.message.*
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.flow.*
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This bot will always return message about forwarder. In cases when sent message was not a forward message it will
|
* This bot will always return message about forwarder. In cases when sent message was not a forward message it will
|
||||||
@ -20,26 +15,25 @@ import kotlinx.coroutines.flow.*
|
|||||||
suspend fun main(vararg args: String) {
|
suspend fun main(vararg args: String) {
|
||||||
val botToken = args.first()
|
val botToken = args.first()
|
||||||
|
|
||||||
val bot = telegramBot(botToken)
|
telegramBotWithBehaviour(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||||
|
onContentMessage(includeMediaGroups = true) {
|
||||||
val scope = CoroutineScope(Dispatchers.Default)
|
val toAnswer = buildEntities {
|
||||||
|
when (val forwardInfo = it.forwardInfo) {
|
||||||
bot.longPolling(scope = scope) {
|
null -> +"There is no forward info"
|
||||||
(merge(messageFlow.asContentMessagesFlow(), mediaGroupMessages(scope).flatMap())).mapNotNull { it.asPossiblyForwardedMessage() }.onEach { message ->
|
is AnonymousForwardInfo -> {
|
||||||
safely({ it.printStackTrace() }) {
|
regular("Anonymous user which signed as \"") + code(forwardInfo.senderName) + "\""
|
||||||
val toAnswer = when (val forwardInfo = message.forwardInfo) {
|
|
||||||
null -> "There is no forward info"
|
|
||||||
is AnonymousForwardInfo -> "Anonymous user which signed as \"${forwardInfo.senderName.codeMarkdownV2()}\""
|
|
||||||
is UserForwardInfo -> forwardInfo.from.let { user ->
|
|
||||||
"User ${user.id.chatId.toString().codeMarkdownV2()} " + "(${user.firstName} ${user.lastName}: ${user.username ?.username ?: "Without username"})".regularMarkdownV2()
|
|
||||||
}
|
}
|
||||||
is ForwardFromChannelInfo -> "Channel (".regularMarkdownV2() + (forwardInfo.channelChat).title.codeMarkdownV2() + ")".regularMarkdownV2()
|
is UserForwardInfo -> {
|
||||||
is ForwardFromSupergroupInfo -> "Supergroup (".regularMarkdownV2() + (forwardInfo.group).title.codeMarkdownV2() + ")".regularMarkdownV2()
|
val user = forwardInfo.from
|
||||||
|
regular("User ") + code(user.id.chatId.toString()) + " (${user.firstName} ${user.lastName}: ${user.username ?.username ?: "Without username"})"
|
||||||
}
|
}
|
||||||
bot.sendTextMessage(message.chat, toAnswer, MarkdownV2)
|
is ForwardFromChannelInfo -> regular("Channel (") + code((forwardInfo.channelChat).title) + ")"
|
||||||
|
is ForwardFromSupergroupInfo -> regular("Supergroup (") + code((forwardInfo.group).title) + ")"
|
||||||
}
|
}
|
||||||
}.launchIn(scope)
|
|
||||||
}
|
}
|
||||||
|
reply(it, toAnswer)
|
||||||
scope.coroutineContext[Job]!!.join()
|
coroutineContext.job.invokeOnCompletion { println("completance of onContentMessage") }
|
||||||
|
}
|
||||||
|
coroutineContext.job.invokeOnCompletion { println("Completed :)") }
|
||||||
|
}.second.join()
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@ 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.api.send.sendTextMessage
|
import dev.inmo.tgbotapi.extensions.api.send.sendTextMessage
|
||||||
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviour
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
||||||
import dev.inmo.tgbotapi.extensions.utils.asChannelChat
|
import dev.inmo.tgbotapi.extensions.utils.asChannelChat
|
||||||
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
|
||||||
@ -21,19 +23,18 @@ import kotlinx.coroutines.flow.onEach
|
|||||||
suspend fun main(vararg args: String) {
|
suspend fun main(vararg args: String) {
|
||||||
val botToken = args.first()
|
val botToken = args.first()
|
||||||
|
|
||||||
val bot = telegramBot(botToken)
|
telegramBotWithBehaviour(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||||
|
onContentMessage { message ->
|
||||||
val scope = CoroutineScope(Dispatchers.Default)
|
|
||||||
|
|
||||||
bot.longPolling(scope = scope) {
|
|
||||||
messageFlow.onEach {
|
|
||||||
safely {
|
|
||||||
val message = it.data
|
|
||||||
val chat = message.chat
|
val chat = message.chat
|
||||||
|
if (chat is ChannelChat) {
|
||||||
|
val answer = "Hi everybody in this channel \"${chat.title}\""
|
||||||
|
sendTextMessage(chat, answer, MarkdownV2)
|
||||||
|
return@onContentMessage
|
||||||
|
}
|
||||||
val answerText = "Oh, hi, " + when (chat) {
|
val answerText = "Oh, hi, " + when (chat) {
|
||||||
is PrivateChat -> "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id)
|
is PrivateChat -> "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id)
|
||||||
is User -> "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id)
|
is User -> "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id)
|
||||||
is SupergroupChat -> (chat.username ?.username ?: bot.getChat(chat).inviteLink) ?.let {
|
is SupergroupChat -> (chat.username ?.username ?: getChat(chat).inviteLink) ?.let {
|
||||||
chat.title.linkMarkdownV2(it)
|
chat.title.linkMarkdownV2(it)
|
||||||
} ?: chat.title
|
} ?: chat.title
|
||||||
is GroupChat -> bot.getChat(chat).inviteLink ?.let {
|
is GroupChat -> bot.getChat(chat).inviteLink ?.let {
|
||||||
@ -41,21 +42,11 @@ suspend fun main(vararg args: String) {
|
|||||||
} ?: chat.title
|
} ?: chat.title
|
||||||
else -> "Unknown :(".escapeMarkdownV2Common()
|
else -> "Unknown :(".escapeMarkdownV2Common()
|
||||||
}
|
}
|
||||||
bot.reply(
|
reply(
|
||||||
message,
|
message,
|
||||||
answerText,
|
answerText,
|
||||||
MarkdownV2
|
MarkdownV2
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}.launchIn(scope)
|
}.second.join()
|
||||||
channelPostFlow.onEach {
|
|
||||||
safely {
|
|
||||||
val chat = it.data.chat
|
|
||||||
val message = "Hi everybody in this channel \"${(chat.asChannelChat()) ?.title}\""
|
|
||||||
bot.sendTextMessage(chat, message, MarkdownV2)
|
|
||||||
}
|
|
||||||
}.launchIn(scope)
|
|
||||||
}
|
|
||||||
|
|
||||||
scope.coroutineContext[Job]!!.join()
|
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
kotlin.code.style=official
|
kotlin.code.style=official
|
||||||
org.gradle.parallel=true
|
org.gradle.parallel=true
|
||||||
|
|
||||||
kotlin_version=1.4.32
|
kotlin_version=1.5.20
|
||||||
telegram_bot_api_version=0.34.1
|
telegram_bot_api_version=0.35.1
|
||||||
|
Loading…
Reference in New Issue
Block a user