mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2024-11-25 19:48:49 +00:00
commit
115c76a9d4
9
DeepLinksBot/README.md
Normal file
9
DeepLinksBot/README.md
Normal file
@ -0,0 +1,9 @@
|
||||
# DeepLinksBot
|
||||
|
||||
This bot will send you deeplink to this bot when you send some text message and react on the `start` button
|
||||
|
||||
## Launch
|
||||
|
||||
```bash
|
||||
../gradlew run --args="BOT_TOKEN"
|
||||
```
|
21
DeepLinksBot/build.gradle
Normal file
21
DeepLinksBot/build.gradle
Normal file
@ -0,0 +1,21 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'application'
|
||||
|
||||
mainClassName="DeepLinksBotKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
39
DeepLinksBot/src/main/kotlin/DeepLinksBot.kt
Normal file
39
DeepLinksBot/src/main/kotlin/DeepLinksBot.kt
Normal file
@ -0,0 +1,39 @@
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelySkippingExceptions
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitDeepLinks
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.makeTelegramDeepLink
|
||||
import dev.inmo.tgbotapi.types.message.textsources.BotCommandTextSource
|
||||
|
||||
/**
|
||||
* This bot will send you deeplink to this bot when you send some text message and react on the `start` button
|
||||
*/
|
||||
suspend fun main(vararg args: String) {
|
||||
val botToken = args.first()
|
||||
|
||||
telegramBotWithBehaviourAndLongPolling(botToken) {
|
||||
val me = bot.getMe()
|
||||
println(me)
|
||||
|
||||
onText(
|
||||
initialFilter = { it.content.textSources.none { it is BotCommandTextSource } } // excluding messages with commands
|
||||
) {
|
||||
reply(it, makeTelegramDeepLink(me.username, it.content.text))
|
||||
}
|
||||
|
||||
onCommand("start", requireOnlyCommandInMessage = true) { // handling of `start` without args
|
||||
reply(it, "Hi :) Send me any text and I will try hard to create deeplink for you")
|
||||
}
|
||||
onDeepLink { (it, deepLink) ->
|
||||
reply(it, "Ok, I got deep link \"${deepLink}\" in trigger")
|
||||
}
|
||||
waitDeepLinks().subscribeSafelyWithoutExceptions(this) { (it, deepLink) ->
|
||||
reply(it, "Ok, I got deep link \"${deepLink}\" in waiter")
|
||||
println(triggersHolder.handleableCommandsHolder.handleable)
|
||||
}
|
||||
}.second.join()
|
||||
}
|
@ -11,7 +11,7 @@ buildscript {
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'application'
|
||||
|
||||
mainClassName="HelloBotKt"
|
||||
mainClassName="GetMeBotKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
|
@ -3,8 +3,11 @@ import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
||||
import dev.inmo.tgbotapi.extensions.utils.extensions.raw.sender_chat
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.linkMarkdownV2
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.textMentionMarkdownV2
|
||||
import dev.inmo.tgbotapi.extensions.utils.ifChannelChat
|
||||
import dev.inmo.tgbotapi.extensions.utils.ifFromChannelGroupContentMessage
|
||||
import dev.inmo.tgbotapi.types.chat.*
|
||||
import dev.inmo.tgbotapi.types.chat.GroupChat
|
||||
import dev.inmo.tgbotapi.types.chat.PrivateChat
|
||||
@ -24,21 +27,35 @@ suspend fun main(vararg args: String) {
|
||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||
onContentMessage { message ->
|
||||
val chat = message.chat
|
||||
if (chat is ChannelChat) {
|
||||
val answer = "Hi everybody in this channel \"${chat.title}\""
|
||||
send(chat, answer, MarkdownV2)
|
||||
return@onContentMessage
|
||||
}
|
||||
val answerText = "Oh, hi, " + when (chat) {
|
||||
is User -> "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id)
|
||||
is PrivateChat -> "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id)
|
||||
is SupergroupChat -> (chat.username ?.username ?: 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()
|
||||
|
||||
val answerText = when (val chat = message.chat) {
|
||||
is ChannelChat -> {
|
||||
val answer = "Hi everybody in this channel \"${chat.title}\""
|
||||
reply(message, answer, MarkdownV2)
|
||||
return@onContentMessage
|
||||
}
|
||||
is PrivateChat -> {
|
||||
reply(message, "Hi, " + "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id), MarkdownV2)
|
||||
return@onContentMessage
|
||||
}
|
||||
is GroupChat -> {
|
||||
message.ifFromChannelGroupContentMessage {
|
||||
val answer = "Hi, ${it.senderChat.title}"
|
||||
reply(message, answer, MarkdownV2)
|
||||
return@onContentMessage
|
||||
}
|
||||
"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")
|
||||
}
|
||||
reply(
|
||||
message,
|
||||
|
@ -5,7 +5,7 @@ org.gradle.jvmargs=-Xmx768m
|
||||
|
||||
|
||||
kotlin_version=1.7.10
|
||||
telegram_bot_api_version=3.1.1
|
||||
micro_utils_version=0.12.1
|
||||
serialization_version=1.4.0-RC
|
||||
telegram_bot_api_version=3.2.0
|
||||
micro_utils_version=0.12.4
|
||||
serialization_version=1.4.0
|
||||
ktor_version=2.1.0
|
||||
|
@ -6,6 +6,8 @@ include ":HelloBot"
|
||||
|
||||
include ":GetMeBot"
|
||||
|
||||
include ":DeepLinksBot"
|
||||
|
||||
include ":FilesLoaderBot"
|
||||
|
||||
include ":ResenderBot:ResenderBotLib"
|
||||
|
Loading…
Reference in New Issue
Block a user