From 0517f451f1ffd5a8a11ce19fa9254795278354c9 Mon Sep 17 00:00:00 2001 From: InsanusMokrassar Date: Tue, 2 Jun 2020 21:22:09 +0600 Subject: [PATCH] add HelloBot --- HelloBot/README.md | 9 ++++++ HelloBot/build.gradle | 24 ++++++++++++++ HelloBot/src/main/kotlin/HelloBot.kt | 47 ++++++++++++++++++++++++++++ settings.gradle | 1 + 4 files changed, 81 insertions(+) create mode 100644 HelloBot/README.md create mode 100644 HelloBot/build.gradle create mode 100644 HelloBot/src/main/kotlin/HelloBot.kt diff --git a/HelloBot/README.md b/HelloBot/README.md new file mode 100644 index 0000000..3644a6d --- /dev/null +++ b/HelloBot/README.md @@ -0,0 +1,9 @@ +# ForwarderBot + +The main purpose of this bot is just to answer "Oh, hi, " and add user mention here + +## Launch + +```bash +../gradlew run --args="BOT_TOKEN" +``` diff --git a/HelloBot/build.gradle b/HelloBot/build.gradle new file mode 100644 index 0000000..034ded0 --- /dev/null +++ b/HelloBot/build.gradle @@ -0,0 +1,24 @@ +buildscript { + repositories { + jcenter() + } + + dependencies { + classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" + } +} + +apply plugin: 'kotlin' +apply plugin: 'application' + +mainClassName="HelloBotKt" + +repositories { + jcenter() +} + +dependencies { + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" + + implementation "com.github.insanusmokrassar:TelegramBotAPI-all:$telegram_bot_api_version" +} diff --git a/HelloBot/src/main/kotlin/HelloBot.kt b/HelloBot/src/main/kotlin/HelloBot.kt new file mode 100644 index 0000000..d036ff3 --- /dev/null +++ b/HelloBot/src/main/kotlin/HelloBot.kt @@ -0,0 +1,47 @@ +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.chat.get.getChat +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.send.sendTextMessage +import com.github.insanusmokrassar.TelegramBotAPI.extensions.api.telegramBot +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.formatting.linkMarkdownV2 +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.formatting.textMentionMarkdownV2 +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.safely +import com.github.insanusmokrassar.TelegramBotAPI.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling +import com.github.insanusmokrassar.TelegramBotAPI.types.ParseMode.MarkdownV2 +import com.github.insanusmokrassar.TelegramBotAPI.types.User +import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.* +import com.github.insanusmokrassar.TelegramBotAPI.utils.extensions.escapeMarkdownV2Common +import kotlinx.coroutines.* +import kotlinx.coroutines.flow.launchIn +import kotlinx.coroutines.flow.onEach + +suspend fun main(vararg args: String) { + val botToken = args.first() + + val bot = telegramBot(botToken) + + val scope = CoroutineScope(Dispatchers.Default) + + bot.startGettingFlowsUpdatesByLongPolling(scope = scope) { + messageFlow.onEach { + safely { + val chat = it.data.chat + val message = "Oh, hi, " + when (chat) { + 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 + is ChannelChat -> (chat.username ?.username ?: bot.getChat(chat).inviteLink) ?.let { + chat.title.linkMarkdownV2(it) + } ?: chat.title + else -> "Unknown :(".escapeMarkdownV2Common() + } + bot.sendTextMessage(chat, message, MarkdownV2) + } + }.launchIn(scope) + } + + scope.coroutineContext[Job]!!.join() +} \ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 141a24b..ec7e4a5 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,3 @@ include ":ForwarderBot" include ":RandomFileSenderBot" +include ":HelloBot"