2021-01-05 18:01:41 +00:00
|
|
|
import dev.inmo.micro_utils.coroutines.safely
|
2020-10-04 11:32:50 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
2020-10-04 13:18:16 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
2020-10-04 11:32:50 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.api.send.sendTextMessage
|
2021-01-05 18:01:41 +00:00
|
|
|
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
|
|
|
import dev.inmo.tgbotapi.extensions.utils.asChannelChat
|
2020-10-04 11:32:50 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.utils.formatting.linkMarkdownV2
|
|
|
|
import dev.inmo.tgbotapi.extensions.utils.formatting.textMentionMarkdownV2
|
2021-03-12 09:48:31 +00:00
|
|
|
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling
|
2020-10-04 11:32:50 +00:00
|
|
|
import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2
|
|
|
|
import dev.inmo.tgbotapi.types.User
|
|
|
|
import dev.inmo.tgbotapi.types.chat.abstracts.*
|
|
|
|
import dev.inmo.tgbotapi.utils.extensions.escapeMarkdownV2Common
|
2020-06-02 15:22:09 +00:00
|
|
|
import kotlinx.coroutines.*
|
|
|
|
import kotlinx.coroutines.flow.launchIn
|
|
|
|
import kotlinx.coroutines.flow.onEach
|
|
|
|
|
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
|
|
|
|
*/
|
2020-06-02 15:22:09 +00:00
|
|
|
suspend fun main(vararg args: String) {
|
|
|
|
val botToken = args.first()
|
|
|
|
|
|
|
|
val bot = telegramBot(botToken)
|
|
|
|
|
|
|
|
val scope = CoroutineScope(Dispatchers.Default)
|
|
|
|
|
2021-03-12 09:48:31 +00:00
|
|
|
bot.longPolling(scope = scope) {
|
2020-06-02 15:22:09 +00:00
|
|
|
messageFlow.onEach {
|
|
|
|
safely {
|
2020-10-04 13:18:16 +00:00
|
|
|
val message = it.data
|
|
|
|
val chat = message.chat
|
|
|
|
val answerText = "Oh, hi, " + when (chat) {
|
2020-06-02 15:22:09 +00:00
|
|
|
is PrivateChat -> "${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 {
|
|
|
|
chat.title.linkMarkdownV2(it)
|
|
|
|
} ?: chat.title
|
|
|
|
is GroupChat -> bot.getChat(chat).inviteLink ?.let {
|
|
|
|
chat.title.linkMarkdownV2(it)
|
|
|
|
} ?: chat.title
|
|
|
|
else -> "Unknown :(".escapeMarkdownV2Common()
|
|
|
|
}
|
2020-10-04 13:18:16 +00:00
|
|
|
bot.reply(
|
|
|
|
message,
|
|
|
|
answerText,
|
|
|
|
MarkdownV2
|
|
|
|
)
|
2020-06-02 15:22:09 +00:00
|
|
|
}
|
|
|
|
}.launchIn(scope)
|
2020-07-02 10:37:12 +00:00
|
|
|
channelPostFlow.onEach {
|
|
|
|
safely {
|
|
|
|
val chat = it.data.chat
|
2021-01-05 18:01:41 +00:00
|
|
|
val message = "Hi everybody in this channel \"${(chat.asChannelChat()) ?.title}\""
|
2020-07-02 10:37:12 +00:00
|
|
|
bot.sendTextMessage(chat, message, MarkdownV2)
|
|
|
|
}
|
|
|
|
}.launchIn(scope)
|
2020-06-02 15:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
scope.coroutineContext[Job]!!.join()
|
|
|
|
}
|