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

72 lines
3.3 KiB
Kotlin
Raw Normal View History

2021-12-20 08:13:55 +00:00
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
2023-05-19 16:45:46 +00:00
import dev.inmo.tgbotapi.extensions.api.bot.getMe
2020-10-04 11:32:50 +00:00
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
2022-06-26 07:03:52 +00:00
import dev.inmo.tgbotapi.extensions.api.send.*
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
2023-05-19 16:45:46 +00:00
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMentionWithAnyContent
2022-08-26 10:35:36 +00:00
import dev.inmo.tgbotapi.extensions.utils.extensions.raw.sender_chat
2020-10-04 11:32:50 +00:00
import dev.inmo.tgbotapi.extensions.utils.formatting.linkMarkdownV2
import dev.inmo.tgbotapi.extensions.utils.formatting.textMentionMarkdownV2
2022-08-26 10:35:36 +00:00
import dev.inmo.tgbotapi.extensions.utils.ifChannelChat
import dev.inmo.tgbotapi.extensions.utils.ifFromChannelGroupContentMessage
2022-05-10 18:23:14 +00:00
import dev.inmo.tgbotapi.types.chat.*
import dev.inmo.tgbotapi.types.chat.GroupChat
import dev.inmo.tgbotapi.types.chat.PrivateChat
import dev.inmo.tgbotapi.types.chat.SupergroupChat
import dev.inmo.tgbotapi.types.message.MarkdownV2
import dev.inmo.tgbotapi.utils.PreviewFeature
2020-10-04 11:32:50 +00:00
import dev.inmo.tgbotapi.utils.extensions.escapeMarkdownV2Common
2020-06-02 15:22:09 +00:00
import kotlinx.coroutines.*
2020-06-02 15:48:07 +00:00
/**
* The main purpose of this bot is just to answer "Oh, hi, " and add user mention here
*/
2022-05-10 18:23:14 +00:00
@OptIn(PreviewFeature::class)
2020-06-02 15:22:09 +00:00
suspend fun main(vararg args: String) {
val botToken = args.first()
2021-11-11 06:34:52 +00:00
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
2023-05-19 16:45:46 +00:00
val me = getMe()
2023-06-30 11:49:19 +00:00
onMentionWithAnyContent(me) { message ->
2021-06-27 19:14:16 +00:00
val chat = message.chat
2022-08-26 10:35:36 +00:00
val answerText = when (val chat = message.chat) {
is ChannelChat -> {
val answer = "Hi everybody in this channel \"${chat.title}\""
reply(message, answer, MarkdownV2)
2023-05-19 16:45:46 +00:00
return@onMentionWithAnyContent
2022-08-26 10:35:36 +00:00
}
is PrivateChat -> {
reply(message, "Hi, " + "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id), MarkdownV2)
2023-05-19 16:45:46 +00:00
return@onMentionWithAnyContent
2022-08-26 10:35:36 +00:00
}
is GroupChat -> {
message.ifFromChannelGroupContentMessage {
val answer = "Hi, ${it.senderChat.title}"
reply(message, answer, MarkdownV2)
2023-05-19 16:45:46 +00:00
return@onMentionWithAnyContent
2022-08-26 10:35:36 +00:00
}
"Oh, hi, " + when (chat) {
is SupergroupChat -> (chat.username ?.username ?: getChat(chat).inviteLink) ?.let {
chat.title.linkMarkdownV2(it)
} ?: chat.title
else -> bot.getChat(chat).inviteLink ?.let {
chat.title.linkMarkdownV2(it)
} ?: chat.title
}
}
is UnknownExtendedChat,
is UnknownChatType -> "Unknown :(".escapeMarkdownV2Common()
else -> error("Something went wrong: unknown type of chat $chat")
}
2021-06-27 19:14:16 +00:00
reply(
message,
answerText,
MarkdownV2
)
}
2021-12-20 08:13:55 +00:00
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) { println(it) }
2021-06-27 19:14:16 +00:00
}.second.join()
}