TelegramBotAPI-examples/ForwardInfoSenderBot/src/main/kotlin/ForwardInfoSenderBot.kt

42 lines
2.0 KiB
Kotlin
Raw Normal View History

2021-06-27 19:14:16 +00:00
import dev.inmo.tgbotapi.extensions.api.send.reply
2021-11-11 06:34:52 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
2021-06-27 19:14:16 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
import dev.inmo.tgbotapi.extensions.utils.formatting.*
2021-08-08 14:32:26 +00:00
import dev.inmo.tgbotapi.types.*
2020-10-04 11:32:50 +00:00
import dev.inmo.tgbotapi.types.message.*
2020-06-02 14:24:41 +00:00
import kotlinx.coroutines.*
/**
* This bot will always return message about forwarder. In cases when sent message was not a forward message it will
* send suitable message
*/
suspend fun main(vararg args: String) {
val botToken = args.first()
2021-11-11 06:34:52 +00:00
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
2021-09-21 17:22:13 +00:00
onContentMessage(subcontextUpdatesFilter = { _, _ -> true }) {
2021-06-27 19:14:16 +00:00
val toAnswer = buildEntities {
when (val forwardInfo = it.forwardInfo) {
null -> +"There is no forward info"
is AnonymousForwardInfo -> {
regular("Anonymous user which signed as \"") + code(forwardInfo.senderName) + "\""
2020-06-02 14:24:41 +00:00
}
2021-06-27 19:14:16 +00:00
is UserForwardInfo -> {
val user = forwardInfo.from
2021-08-08 14:32:26 +00:00
when (user) {
is CommonUser -> regular("User ")
is CommonBot,
is ExtendedBot -> regular("Bot ")
} + code(user.id.chatId.toString()) + " (${user.firstName} ${user.lastName}: ${user.username ?.username ?: "Without username"})"
2021-06-27 19:14:16 +00:00
}
is ForwardFromChannelInfo -> regular("Channel (") + code((forwardInfo.channelChat).title) + ")"
is ForwardFromSupergroupInfo -> regular("Supergroup (") + code((forwardInfo.group).title) + ")"
2020-06-02 14:24:41 +00:00
}
}
2021-06-27 19:14:16 +00:00
reply(it, toAnswer)
coroutineContext.job.invokeOnCompletion { println("completance of onContentMessage") }
}
coroutineContext.job.invokeOnCompletion { println("Completed :)") }
}.second.join()
2020-06-02 14:24:41 +00:00
}