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

52 lines
2.4 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.*
2022-05-10 18:23:14 +00:00
import dev.inmo.tgbotapi.types.chat.CommonBot
import dev.inmo.tgbotapi.types.chat.CommonUser
import dev.inmo.tgbotapi.types.chat.ExtendedBot
2020-10-04 11:32:50 +00:00
import dev.inmo.tgbotapi.types.message.*
2022-09-19 08:37:01 +00:00
import dev.inmo.tgbotapi.utils.code
import dev.inmo.tgbotapi.utils.regular
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)) {
2022-08-04 16:32:36 +00:00
onContentMessage {
2021-06-27 19:14:16 +00:00
val toAnswer = buildEntities {
when (val forwardInfo = it.forwardInfo) {
null -> +"There is no forward info"
2022-08-05 13:45:52 +00:00
is ForwardInfo.ByAnonymous -> {
2021-06-27 19:14:16 +00:00
regular("Anonymous user which signed as \"") + code(forwardInfo.senderName) + "\""
2020-06-02 14:24:41 +00:00
}
2022-08-05 13:45:52 +00:00
is ForwardInfo.ByUser -> {
2021-06-27 19:14:16 +00:00
val user = forwardInfo.from
2021-08-08 14:32:26 +00:00
when (user) {
is CommonUser -> {
if (user.isPremium) {
regular("Premium user ")
} else {
regular("User ")
}
}
2022-09-19 08:37:01 +00:00
2021-08-08 14:32:26 +00:00
is CommonBot,
is ExtendedBot -> regular("Bot ")
2022-09-19 08:37:01 +00:00
} + code(user.id.chatId.toString()) + " (${user.firstName} ${user.lastName}: ${user.username?.username ?: "Without username"})"
2021-06-27 19:14:16 +00:00
}
2022-08-05 13:45:52 +00:00
is ForwardInfo.PublicChat.FromChannel -> regular("Channel (") + code(forwardInfo.channelChat.title) + ")"
is ForwardInfo.PublicChat.FromSupergroup -> regular("Supergroup (") + code(forwardInfo.group.title) + ")"
is ForwardInfo.PublicChat.SentByChannel -> regular("Sent by channel (") + code(forwardInfo.channelChat.title) + ")"
2020-06-02 14:24:41 +00:00
}
}
2021-06-27 19:14:16 +00:00
reply(it, toAnswer)
}
}.second.join()
2020-06-02 14:24:41 +00:00
}