mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-12-15 02:35:38 +00:00
Compare commits
1 Commits
0.5.0
...
4aefa0608f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4aefa0608f |
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@@ -8,9 +8,9 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
- name: Set up JDK 11
|
- name: Set up JDK 1.8
|
||||||
uses: actions/setup-java@v1
|
uses: actions/setup-java@v1
|
||||||
with:
|
with:
|
||||||
java-version: 11
|
java-version: 1.8
|
||||||
- name: Build with Gradle
|
- name: Build with Gradle
|
||||||
run: ./gradlew build
|
run: ./gradlew build
|
||||||
|
|||||||
@@ -1,9 +0,0 @@
|
|||||||
# ChatAvatarSetter
|
|
||||||
|
|
||||||
This bot will set the chat avatar based on the image sent to bot
|
|
||||||
|
|
||||||
## Launch
|
|
||||||
|
|
||||||
```bash
|
|
||||||
../gradlew run --args="BOT_TOKEN"
|
|
||||||
```
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
buildscript {
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
apply plugin: 'kotlin'
|
|
||||||
apply plugin: 'application'
|
|
||||||
|
|
||||||
mainClassName="ChatAvatarSetterKt"
|
|
||||||
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
||||||
|
|
||||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
|
||||||
}
|
|
||||||
@@ -1,36 +0,0 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
|
||||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.modify.setChatPhoto
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onPhoto
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.*
|
|
||||||
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
|
|
||||||
import kotlinx.coroutines.*
|
|
||||||
|
|
||||||
suspend fun main(args: Array<String>) {
|
|
||||||
val bot = telegramBot(args.first())
|
|
||||||
|
|
||||||
bot.buildBehaviourWithLongPolling(scope = CoroutineScope(Dispatchers.IO)) {
|
|
||||||
onPhoto {
|
|
||||||
val bytes = downloadFile(it.content)
|
|
||||||
runCatchingSafely {
|
|
||||||
setChatPhoto(
|
|
||||||
it.chat.id,
|
|
||||||
bytes.asMultipartFile("sample.jpg")
|
|
||||||
)
|
|
||||||
}.onSuccess { b ->
|
|
||||||
if (b) {
|
|
||||||
reply(it, "Done")
|
|
||||||
} else {
|
|
||||||
reply(it, "Something went wrong")
|
|
||||||
}
|
|
||||||
}.onFailure { e ->
|
|
||||||
e.printStackTrace()
|
|
||||||
|
|
||||||
reply(it, "Something went wrong (see logs)")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}.join()
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
# 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"
|
|
||||||
```
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
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"
|
|
||||||
}
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
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()
|
|
||||||
}
|
|
||||||
@@ -1,56 +1,32 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.AccumulatorFlow
|
|
||||||
import dev.inmo.micro_utils.fsm.common.State
|
import dev.inmo.micro_utils.fsm.common.State
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.send
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
|
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.*
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||||
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams
|
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams
|
||||||
import dev.inmo.tgbotapi.extensions.utils.extensions.sameThread
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
||||||
import dev.inmo.tgbotapi.types.IdChatIdentifier
|
import dev.inmo.tgbotapi.types.ChatId
|
||||||
import dev.inmo.tgbotapi.types.MessageThreadId
|
|
||||||
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
||||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||||
import dev.inmo.tgbotapi.utils.botCommand
|
|
||||||
import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.flow.filter
|
|
||||||
import kotlinx.coroutines.flow.first
|
|
||||||
|
|
||||||
sealed interface BotState : State
|
sealed interface BotState : State
|
||||||
data class ExpectContentOrStopState(override val context: IdChatIdentifier, val sourceMessage: CommonMessage<TextContent>) : BotState
|
data class ExpectContentOrStopState(override val context: ChatId, val sourceMessage: CommonMessage<TextContent>) : BotState
|
||||||
data class StopState(override val context: IdChatIdentifier) : BotState
|
data class StopState(override val context: ChatId) : BotState
|
||||||
|
|
||||||
suspend fun main(args: Array<String>) {
|
suspend fun main(args: Array<String>) {
|
||||||
val botToken = args.first()
|
val botToken = args.first()
|
||||||
|
|
||||||
telegramBotWithBehaviourAndFSMAndStartLongPolling<BotState>(
|
telegramBotWithBehaviourAndFSMAndStartLongPolling<BotState>(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||||
botToken,
|
|
||||||
CoroutineScope(Dispatchers.IO),
|
|
||||||
onStateHandlingErrorHandler = { state, e ->
|
|
||||||
when (state) {
|
|
||||||
is ExpectContentOrStopState -> {
|
|
||||||
println("Thrown error on ExpectContentOrStopState")
|
|
||||||
}
|
|
||||||
is StopState -> {
|
|
||||||
println("Thrown error on StopState")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
e.printStackTrace()
|
|
||||||
state
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
strictlyOn<ExpectContentOrStopState> {
|
strictlyOn<ExpectContentOrStopState> {
|
||||||
send(
|
sendMessage(
|
||||||
it.context,
|
it.context,
|
||||||
) {
|
buildEntities {
|
||||||
+"Send me some content or " + botCommand("stop") + " if you want to stop sending"
|
+"Send me some content or " + botCommand("stop") + " if you want to stop sending"
|
||||||
}
|
}
|
||||||
|
)
|
||||||
|
|
||||||
val contentMessage = waitContentMessage().filter { message ->
|
val contentMessage = waitContentMessage().first()
|
||||||
message.sameThread(it.sourceMessage)
|
|
||||||
}.first()
|
|
||||||
val content = contentMessage.content
|
val content = contentMessage.content
|
||||||
|
|
||||||
when {
|
when {
|
||||||
@@ -62,14 +38,12 @@ suspend fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
strictlyOn<StopState> {
|
strictlyOn<StopState> {
|
||||||
send(it.context) { +"You have stopped sending of content" }
|
sendMessage(it.context, "You have stopped sending of content")
|
||||||
|
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|
||||||
command(
|
command("start") {
|
||||||
"start"
|
|
||||||
) {
|
|
||||||
startChain(ExpectContentOrStopState(it.chat.id, it))
|
startChain(ExpectContentOrStopState(it.chat.id, it))
|
||||||
}
|
}
|
||||||
}.second.join()
|
}.second.join()
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
||||||
import dev.inmo.tgbotapi.extensions.api.get.getFileAdditionalInfo
|
import dev.inmo.tgbotapi.extensions.api.get.getFileAdditionalInfo
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviour
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMedia
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMedia
|
||||||
@@ -14,19 +15,17 @@ import java.io.File
|
|||||||
*/
|
*/
|
||||||
suspend fun main(args: Array<String>) {
|
suspend fun main(args: Array<String>) {
|
||||||
val botToken = args.first()
|
val botToken = args.first()
|
||||||
val directoryOrFile = args.getOrNull(1) ?.let { File(it) } ?: File("/tmp/")
|
val directoryOrFile = args.getOrNull(1) ?.let { File(it) } ?: File("")
|
||||||
directoryOrFile.mkdirs()
|
directoryOrFile.mkdirs()
|
||||||
|
|
||||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||||
onMedia(initialFilter = null) {
|
onMedia(initialFilter = null) {
|
||||||
val pathedFile = bot.getFileAdditionalInfo(it.content.media)
|
val pathedFile = bot.getFileAdditionalInfo(it.content.media)
|
||||||
val outFile = File(directoryOrFile, pathedFile.filePath.filenameFromUrl)
|
val file = File(directoryOrFile, pathedFile.filePath.filenameFromUrl).apply {
|
||||||
runCatching {
|
createNewFile()
|
||||||
bot.downloadFile(it.content.media, outFile)
|
writeBytes(bot.downloadFile(pathedFile))
|
||||||
}.onFailure {
|
|
||||||
it.printStackTrace()
|
|
||||||
}
|
}
|
||||||
reply(it, "Saved to ${outFile.absolutePath}")
|
reply(it, "Saved to ${file.absolutePath}")
|
||||||
}
|
}
|
||||||
onContentMessage { println(it) }
|
onContentMessage { println(it) }
|
||||||
}.second.join()
|
}.second.join()
|
||||||
|
|||||||
@@ -2,12 +2,8 @@ import dev.inmo.tgbotapi.extensions.api.send.reply
|
|||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
||||||
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
||||||
import dev.inmo.tgbotapi.types.chat.CommonBot
|
import dev.inmo.tgbotapi.types.*
|
||||||
import dev.inmo.tgbotapi.types.chat.CommonUser
|
|
||||||
import dev.inmo.tgbotapi.types.chat.ExtendedBot
|
|
||||||
import dev.inmo.tgbotapi.types.message.*
|
import dev.inmo.tgbotapi.types.message.*
|
||||||
import dev.inmo.tgbotapi.utils.code
|
|
||||||
import dev.inmo.tgbotapi.utils.regular
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -18,34 +14,28 @@ suspend fun main(vararg args: String) {
|
|||||||
val botToken = args.first()
|
val botToken = args.first()
|
||||||
|
|
||||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||||
onContentMessage {
|
onContentMessage(subcontextUpdatesFilter = { _, _ -> true }) {
|
||||||
val toAnswer = buildEntities {
|
val toAnswer = buildEntities {
|
||||||
when (val forwardInfo = it.forwardInfo) {
|
when (val forwardInfo = it.forwardInfo) {
|
||||||
null -> +"There is no forward info"
|
null -> +"There is no forward info"
|
||||||
is ForwardInfo.ByAnonymous -> {
|
is AnonymousForwardInfo -> {
|
||||||
regular("Anonymous user which signed as \"") + code(forwardInfo.senderName) + "\""
|
regular("Anonymous user which signed as \"") + code(forwardInfo.senderName) + "\""
|
||||||
}
|
}
|
||||||
is ForwardInfo.ByUser -> {
|
is UserForwardInfo -> {
|
||||||
val user = forwardInfo.from
|
val user = forwardInfo.from
|
||||||
when (user) {
|
when (user) {
|
||||||
is CommonUser -> {
|
is CommonUser -> regular("User ")
|
||||||
if (user.isPremium) {
|
|
||||||
regular("Premium user ")
|
|
||||||
} else {
|
|
||||||
regular("User ")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
is CommonBot,
|
is CommonBot,
|
||||||
is ExtendedBot -> regular("Bot ")
|
is ExtendedBot -> regular("Bot ")
|
||||||
} + code(user.id.chatId.toString()) + " (${user.firstName} ${user.lastName}: ${user.username?.username ?: "Without username"})"
|
} + code(user.id.chatId.toString()) + " (${user.firstName} ${user.lastName}: ${user.username ?.username ?: "Without username"})"
|
||||||
}
|
}
|
||||||
is ForwardInfo.PublicChat.FromChannel -> regular("Channel (") + code(forwardInfo.channelChat.title) + ")"
|
is ForwardFromChannelInfo -> regular("Channel (") + code((forwardInfo.channelChat).title) + ")"
|
||||||
is ForwardInfo.PublicChat.FromSupergroup -> regular("Supergroup (") + code(forwardInfo.group.title) + ")"
|
is ForwardFromSupergroupInfo -> regular("Supergroup (") + code((forwardInfo.group).title) + ")"
|
||||||
is ForwardInfo.PublicChat.SentByChannel -> regular("Sent by channel (") + code(forwardInfo.channelChat.title) + ")"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
reply(it, toAnswer)
|
reply(it, toAnswer)
|
||||||
|
coroutineContext.job.invokeOnCompletion { println("completance of onContentMessage") }
|
||||||
}
|
}
|
||||||
|
coroutineContext.job.invokeOnCompletion { println("Completed :)") }
|
||||||
}.second.join()
|
}.second.join()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ buildscript {
|
|||||||
apply plugin: 'kotlin'
|
apply plugin: 'kotlin'
|
||||||
apply plugin: 'application'
|
apply plugin: 'application'
|
||||||
|
|
||||||
mainClassName="GetMeBotKt"
|
mainClassName="HelloBotKt"
|
||||||
|
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -1,16 +1,5 @@
|
|||||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.closeForumTopic
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.createForumTopic
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.deleteForumTopic
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.reopenForumTopic
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onForumTopicClosed
|
|
||||||
import dev.inmo.tgbotapi.types.ChatId
|
|
||||||
import dev.inmo.tgbotapi.types.CustomEmojiId
|
|
||||||
import dev.inmo.tgbotapi.types.ForumTopic
|
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is one of the most easiest bot - it will just print information about itself
|
* This is one of the most easiest bot - it will just print information about itself
|
||||||
@@ -21,4 +10,4 @@ suspend fun main(vararg args: String) {
|
|||||||
val bot = telegramBot(botToken)
|
val bot = telegramBot(botToken)
|
||||||
|
|
||||||
println(bot.getMe())
|
println(bot.getMe())
|
||||||
}
|
}
|
||||||
@@ -1,61 +1,41 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.sendTextMessage
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
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.linkMarkdownV2
|
||||||
import dev.inmo.tgbotapi.extensions.utils.formatting.textMentionMarkdownV2
|
import dev.inmo.tgbotapi.extensions.utils.formatting.textMentionMarkdownV2
|
||||||
import dev.inmo.tgbotapi.extensions.utils.ifChannelChat
|
import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2
|
||||||
import dev.inmo.tgbotapi.extensions.utils.ifFromChannelGroupContentMessage
|
import dev.inmo.tgbotapi.types.User
|
||||||
import dev.inmo.tgbotapi.types.chat.*
|
import dev.inmo.tgbotapi.types.chat.abstracts.*
|
||||||
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
|
|
||||||
import dev.inmo.tgbotapi.utils.extensions.escapeMarkdownV2Common
|
import dev.inmo.tgbotapi.utils.extensions.escapeMarkdownV2Common
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main purpose of this bot is just to answer "Oh, hi, " and add user mention here
|
* The main purpose of this bot is just to answer "Oh, hi, " and add user mention here
|
||||||
*/
|
*/
|
||||||
@OptIn(PreviewFeature::class)
|
|
||||||
suspend fun main(vararg args: String) {
|
suspend fun main(vararg args: String) {
|
||||||
val botToken = args.first()
|
val botToken = args.first()
|
||||||
|
|
||||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||||
onContentMessage { message ->
|
onContentMessage { message ->
|
||||||
val chat = message.chat
|
val chat = message.chat
|
||||||
|
if (chat is ChannelChat) {
|
||||||
val answerText = when (val chat = message.chat) {
|
val answer = "Hi everybody in this channel \"${chat.title}\""
|
||||||
is ChannelChat -> {
|
sendTextMessage(chat, answer, MarkdownV2)
|
||||||
val answer = "Hi everybody in this channel \"${chat.title}\""
|
return@onContentMessage
|
||||||
reply(message, answer, MarkdownV2)
|
}
|
||||||
return@onContentMessage
|
val answerText = "Oh, hi, " + when (chat) {
|
||||||
}
|
is PrivateChat -> "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id)
|
||||||
is PrivateChat -> {
|
is User -> "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id)
|
||||||
reply(message, "Hi, " + "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id), MarkdownV2)
|
is SupergroupChat -> (chat.username ?.username ?: getChat(chat).inviteLink) ?.let {
|
||||||
return@onContentMessage
|
chat.title.linkMarkdownV2(it)
|
||||||
}
|
} ?: chat.title
|
||||||
is GroupChat -> {
|
is GroupChat -> bot.getChat(chat).inviteLink ?.let {
|
||||||
message.ifFromChannelGroupContentMessage {
|
chat.title.linkMarkdownV2(it)
|
||||||
val answer = "Hi, ${it.senderChat.title}"
|
} ?: chat.title
|
||||||
reply(message, answer, MarkdownV2)
|
else -> "Unknown :(".escapeMarkdownV2Common()
|
||||||
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(
|
reply(
|
||||||
message,
|
message,
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ buildscript {
|
|||||||
}
|
}
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id "org.jetbrains.kotlin.multiplatform"
|
id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,18 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||||
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
import dev.inmo.tgbotapi.extensions.api.edit.text.editMessageText
|
||||||
import dev.inmo.tgbotapi.extensions.api.edit.edit
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.media.*
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilterExcludeMediaGroups
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
|
||||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
||||||
import dev.inmo.tgbotapi.extensions.utils.withContent
|
import dev.inmo.tgbotapi.extensions.utils.withContent
|
||||||
import dev.inmo.tgbotapi.types.BotCommand
|
|
||||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||||
import dev.inmo.tgbotapi.utils.*
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
private const val nextPageData = "next"
|
private const val nextPageData = "next"
|
||||||
@@ -70,14 +71,13 @@ suspend fun activateKeyboardsBot(
|
|||||||
val numberOfPages = args.firstOrNull() ?.toIntOrNull() ?: 10
|
val numberOfPages = args.firstOrNull() ?.toIntOrNull() ?: 10
|
||||||
reply(
|
reply(
|
||||||
message,
|
message,
|
||||||
|
"Your inline keyboard with $numberOfPages pages",
|
||||||
replyMarkup = inlineKeyboard {
|
replyMarkup = inlineKeyboard {
|
||||||
row {
|
row {
|
||||||
includePageButtons(1, numberOfPages)
|
includePageButtons(1, numberOfPages)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
) {
|
)
|
||||||
regular("Your inline keyboard with $numberOfPages pages")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onMessageDataCallbackQuery {
|
onMessageDataCallbackQuery {
|
||||||
@@ -86,37 +86,20 @@ suspend fun activateKeyboardsBot(
|
|||||||
return@onMessageDataCallbackQuery
|
return@onMessageDataCallbackQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
edit(
|
editMessageText(
|
||||||
it.message.withContent<TextContent>() ?: it.let {
|
it.message.withContent<TextContent>() ?: it.let {
|
||||||
answer(it, "Unsupported message type :(")
|
answer(it, "Unsupported message type :(")
|
||||||
return@onMessageDataCallbackQuery
|
return@onMessageDataCallbackQuery
|
||||||
},
|
},
|
||||||
|
"This is $page of $count",
|
||||||
replyMarkup = inlineKeyboard {
|
replyMarkup = inlineKeyboard {
|
||||||
row {
|
row {
|
||||||
includePageButtons(page, count)
|
includePageButtons(page, count)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
) {
|
)
|
||||||
regular("This is $page of $count")
|
|
||||||
}
|
|
||||||
answer(it)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
onUnhandledCommand {
|
|
||||||
reply(
|
|
||||||
it,
|
|
||||||
replyMarkup = replyKeyboard(resizeKeyboard = true, oneTimeKeyboard = true) {
|
|
||||||
row {
|
|
||||||
simpleButton("/inline")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
+"Use " + botCommand("inline") + " to get pagination inline keyboard"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
setMyCommands(BotCommand("inline", "Creates message with pagination inline keyboard"))
|
|
||||||
|
|
||||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||||
println(it)
|
println(it)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,23 @@
|
|||||||
import dev.inmo.micro_utils.common.filesize
|
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
|
||||||
import dev.inmo.tgbotapi.bot.TelegramBot
|
import dev.inmo.tgbotapi.bot.TelegramBot
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.media.sendDocument
|
import dev.inmo.tgbotapi.extensions.api.send.media.sendDocument
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.media.sendDocumentsGroup
|
import dev.inmo.tgbotapi.extensions.api.send.media.sendDocumentsGroup
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.withUploadDocumentAction
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommandWithArgs
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommandWithArgs
|
||||||
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
|
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
|
||||||
|
import dev.inmo.tgbotapi.requests.abstracts.toInputFile
|
||||||
import dev.inmo.tgbotapi.types.BotCommand
|
import dev.inmo.tgbotapi.types.BotCommand
|
||||||
import dev.inmo.tgbotapi.types.chat.Chat
|
import dev.inmo.tgbotapi.types.InputMedia.DocumentMediaGroupMemberInputMedia
|
||||||
import dev.inmo.tgbotapi.types.files.DocumentFile
|
import dev.inmo.tgbotapi.types.InputMedia.InputMediaDocument
|
||||||
import dev.inmo.tgbotapi.types.media.TelegramMediaDocument
|
import dev.inmo.tgbotapi.types.chat.abstracts.Chat
|
||||||
import dev.inmo.tgbotapi.types.mediaCountInMediaGroup
|
import dev.inmo.tgbotapi.types.mediaCountInMediaGroup
|
||||||
|
import kotlinx.coroutines.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
private const val command = "send_file"
|
private const val command = "send_file"
|
||||||
@@ -46,7 +50,7 @@ suspend fun main(args: Array<String>) {
|
|||||||
)
|
)
|
||||||
else -> sendDocumentsGroup(
|
else -> sendDocumentsGroup(
|
||||||
chat,
|
chat,
|
||||||
files.map { TelegramMediaDocument(it.asMultipartFile()) },
|
files.map { InputMediaDocument(it.asMultipartFile()) },
|
||||||
protectContent = true
|
protectContent = true
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -65,9 +69,9 @@ suspend fun main(args: Array<String>) {
|
|||||||
val chosen = mutableListOf<File>()
|
val chosen = mutableListOf<File>()
|
||||||
|
|
||||||
while (left > 0) {
|
while (left > 0) {
|
||||||
val picked = pickFile() ?.takeIf { it.filesize > 0 } ?: continue
|
|
||||||
chosen.add(picked)
|
|
||||||
left--
|
left--
|
||||||
|
val picked = pickFile() ?: continue
|
||||||
|
chosen.add(picked)
|
||||||
if (chosen.size >= mediaCountInMediaGroup.last) {
|
if (chosen.size >= mediaCountInMediaGroup.last) {
|
||||||
sendFiles(message.chat, chosen)
|
sendFiles(message.chat, chosen)
|
||||||
chosen.clear()
|
chosen.clear()
|
||||||
@@ -81,7 +85,7 @@ suspend fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!sent) {
|
if (!sent) {
|
||||||
reply(message, "Nothing selected :(")
|
bot.reply(message, "Nothing selected :(")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ buildscript {
|
|||||||
}
|
}
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id "org.jetbrains.kotlin.multiplatform"
|
id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.media.*
|
import dev.inmo.tgbotapi.extensions.api.send.media.*
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||||
@@ -8,7 +8,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilte
|
|||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
|
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
|
||||||
import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull
|
import dev.inmo.tgbotapi.types.message.abstracts.Message
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
suspend fun activateResenderBot(
|
suspend fun activateResenderBot(
|
||||||
@@ -21,23 +21,31 @@ suspend fun activateResenderBot(
|
|||||||
|
|
||||||
bot.buildBehaviourWithLongPolling(CoroutineScope(currentCoroutineContext() + SupervisorJob())) {
|
bot.buildBehaviourWithLongPolling(CoroutineScope(currentCoroutineContext() + SupervisorJob())) {
|
||||||
onContentMessage(
|
onContentMessage(
|
||||||
subcontextUpdatesFilter = MessageFilterByChat,
|
initialFilter = CommonMessageFilterExcludeMediaGroups,
|
||||||
|
subcontextUpdatesFilter = MessageFilterByChat
|
||||||
) {
|
) {
|
||||||
val chat = it.chat
|
val chat = it.chat
|
||||||
|
withTypingAction(chat) {
|
||||||
val answer = withTypingAction(chat) {
|
executeUnsafe(it.content.createResend(chat.id, replyToMessageId = it.messageId))
|
||||||
executeUnsafe(
|
}
|
||||||
it.content.createResend(
|
}
|
||||||
chat.id,
|
onVisualGallery {
|
||||||
messageThreadId = it.threadIdOrNull,
|
val chat = it.chat ?: return@onVisualGallery
|
||||||
replyToMessageId = it.messageId
|
withUploadPhotoAction(chat) {
|
||||||
)
|
sendVisualMediaGroup(chat, it.map { it.content.toMediaGroupMemberInputMedia() })
|
||||||
) {
|
}
|
||||||
it.forEach(print)
|
}
|
||||||
}
|
onPlaylist {
|
||||||
|
val chat = it.chat ?: return@onPlaylist
|
||||||
|
withUploadDocumentAction(chat) {
|
||||||
|
sendPlaylist(chat, it.map { it.content.toMediaGroupMemberInputMedia() })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
onDocumentsGroup {
|
||||||
|
val chat = it.chat ?: return@onDocumentsGroup
|
||||||
|
withUploadDocumentAction(chat) {
|
||||||
|
sendDocumentsGroup(chat, it.map { it.content.toMediaGroupMemberInputMedia() })
|
||||||
}
|
}
|
||||||
|
|
||||||
println("Answer info: $answer")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||||
|
|||||||
@@ -1,26 +1,35 @@
|
|||||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
import dev.inmo.micro_utils.coroutines.safely
|
||||||
|
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onDice
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.*
|
import dev.inmo.tgbotapi.extensions.utils.*
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.shortcuts.filterContentMessages
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling
|
||||||
import dev.inmo.tgbotapi.types.dice.SlotMachineDiceAnimationType
|
import dev.inmo.tgbotapi.types.dice.SlotMachineDiceAnimationType
|
||||||
|
import dev.inmo.tgbotapi.types.message.content.DiceContent
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
import kotlinx.coroutines.flow.launchIn
|
||||||
|
import kotlinx.coroutines.flow.onEach
|
||||||
|
|
||||||
suspend fun main(args: Array<String>) {
|
suspend fun main(args: Array<String>) {
|
||||||
val bot = telegramBot(args.first())
|
val bot = telegramBot(args.first())
|
||||||
|
|
||||||
bot.buildBehaviourWithLongPolling(scope = CoroutineScope(Dispatchers.IO)) {
|
val scope = CoroutineScope(Dispatchers.Default)
|
||||||
onDice {
|
bot.longPolling(scope = scope) {
|
||||||
|
filterContentMessages<DiceContent>(scope).onEach {
|
||||||
val content = it.content
|
val content = it.content
|
||||||
val dice = content.dice
|
val dice = content.dice
|
||||||
val diceType = dice.animationType
|
val diceType = dice.animationType
|
||||||
|
|
||||||
if (diceType == SlotMachineDiceAnimationType) {
|
safely ({ it.printStackTrace() }) {
|
||||||
val result = dice.calculateSlotMachineResult() ?: return@onDice
|
if (diceType == SlotMachineDiceAnimationType) {
|
||||||
reply(it, "${result.leftReel}|${result.centerReel}|${result.rightReel}")
|
val result = dice.calculateSlotMachineResult() ?: return@safely
|
||||||
} else {
|
bot.reply(it, "${result.leftReel}|${result.centerReel}|${result.rightReel}")
|
||||||
reply(it, "There is no slot machine dice in message")
|
} else {
|
||||||
|
bot.reply(it, "There is no slot machine dice in message")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}.launchIn(scope)
|
||||||
}.join()
|
}
|
||||||
}
|
|
||||||
|
scope.coroutineContext[Job]!!.join()
|
||||||
|
}
|
||||||
@@ -1,33 +0,0 @@
|
|||||||
buildscript {
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
plugins {
|
|
||||||
id "org.jetbrains.kotlin.multiplatform"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
kotlin {
|
|
||||||
jvm()
|
|
||||||
// js(LEGACY) {
|
|
||||||
js(IR) {
|
|
||||||
browser()
|
|
||||||
binaries.executable()
|
|
||||||
}
|
|
||||||
|
|
||||||
sourceSets {
|
|
||||||
commonMain {
|
|
||||||
dependencies {
|
|
||||||
implementation kotlin('stdlib')
|
|
||||||
|
|
||||||
api "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,75 +0,0 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.defaultSafelyWithoutExceptionHandler
|
|
||||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
|
||||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.get.*
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
|
||||||
import dev.inmo.tgbotapi.types.StickerType
|
|
||||||
import dev.inmo.tgbotapi.types.message.textsources.*
|
|
||||||
import dev.inmo.tgbotapi.types.stickers.StickerSet
|
|
||||||
import dev.inmo.tgbotapi.utils.bold
|
|
||||||
import dev.inmo.tgbotapi.utils.buildEntities
|
|
||||||
import kotlinx.coroutines.*
|
|
||||||
|
|
||||||
fun StickerSet?.buildInfo() = buildEntities {
|
|
||||||
if (this@buildInfo == null) {
|
|
||||||
bold("Looks like this stickerset has been removed")
|
|
||||||
} else {
|
|
||||||
bold("StickerSet name: ") + "${name}\n"
|
|
||||||
bold("StickerSet title: ") + "${title}\n"
|
|
||||||
bold(
|
|
||||||
when (stickerType) {
|
|
||||||
StickerType.CustomEmoji -> "Custom emoji"
|
|
||||||
StickerType.Mask -> "Mask"
|
|
||||||
StickerType.Regular -> "Regular"
|
|
||||||
is StickerType.Unknown -> "Unknown type \"${stickerType.type}\""
|
|
||||||
}
|
|
||||||
) + " sticker set with title " + bold(title) + " and name " + bold(name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun activateStickerInfoBot(
|
|
||||||
token: String,
|
|
||||||
print: (Any) -> Unit
|
|
||||||
) {
|
|
||||||
val bot = telegramBot(token)
|
|
||||||
|
|
||||||
print(bot.getMe())
|
|
||||||
|
|
||||||
defaultSafelyWithoutExceptionHandler = {
|
|
||||||
it.printStackTrace()
|
|
||||||
}
|
|
||||||
|
|
||||||
bot.buildBehaviourWithLongPolling(CoroutineScope(currentCoroutineContext() + SupervisorJob())) {
|
|
||||||
onText {
|
|
||||||
withTypingAction(it.chat) {
|
|
||||||
it.content.textSources.mapNotNull {
|
|
||||||
if (it is CustomEmojiTextSource) {
|
|
||||||
getCustomEmojiStickerOrNull(it.customEmojiId) ?.stickerSetName
|
|
||||||
} else {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
}.distinct().map {
|
|
||||||
getStickerSet(it)
|
|
||||||
}.distinct().flatMap {
|
|
||||||
it.buildInfo() + regular("\n")
|
|
||||||
}.separateForText().map { entities ->
|
|
||||||
reply(it, entities)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
onSticker {
|
|
||||||
val stickerSetInfo = getStickerSetOrNull(it.content.media)
|
|
||||||
reply(
|
|
||||||
it,
|
|
||||||
stickerSetInfo.buildInfo()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
|
||||||
println(it)
|
|
||||||
}
|
|
||||||
}.join()
|
|
||||||
}
|
|
||||||
@@ -1,32 +0,0 @@
|
|||||||
import kotlinx.browser.document
|
|
||||||
import kotlinx.coroutines.*
|
|
||||||
import org.w3c.dom.*
|
|
||||||
|
|
||||||
private val scope = CoroutineScope(Dispatchers.Default)
|
|
||||||
|
|
||||||
fun main() {
|
|
||||||
document.addEventListener(
|
|
||||||
"DOMContentLoaded",
|
|
||||||
{
|
|
||||||
val botsContainer = document.getElementById("bots_container") ?: return@addEventListener
|
|
||||||
|
|
||||||
(document.getElementById("bot_token_form") as? HTMLFormElement) ?.onsubmit = {
|
|
||||||
(document.getElementById("bot_token") as? HTMLInputElement) ?.value ?.let { token ->
|
|
||||||
val botContainer = document.createElement("div") as HTMLDivElement
|
|
||||||
botsContainer.append(botContainer)
|
|
||||||
|
|
||||||
val infoDiv = document.createElement("div") as HTMLDivElement
|
|
||||||
botContainer.append(infoDiv)
|
|
||||||
|
|
||||||
scope.launch {
|
|
||||||
activateStickerInfoBot(token) {
|
|
||||||
infoDiv.innerHTML = it.toString()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
@@ -1,16 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Resender bot</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<form id="bot_token_form">
|
|
||||||
<input type="text" id="bot_token">
|
|
||||||
<input type="submit" value="Start bot">
|
|
||||||
</form>
|
|
||||||
<div id="start_offer">Type your bot token to the input above to start its work</div>
|
|
||||||
<script type="text/javascript" src="StickerInfoBotLib.js"></script>
|
|
||||||
<div id="bots_container"></div>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
buildscript {
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
apply plugin: 'kotlin'
|
|
||||||
apply plugin: 'application'
|
|
||||||
|
|
||||||
mainClassName="StickerInfoBotJvmKt"
|
|
||||||
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
||||||
|
|
||||||
implementation project(":StickerInfoBot:StickerInfoBotLib")
|
|
||||||
}
|
|
||||||
@@ -1,5 +0,0 @@
|
|||||||
suspend fun main(args: Array<String>) {
|
|
||||||
activateStickerInfoBot(args.first()) {
|
|
||||||
println(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
# HelloBot
|
|
||||||
|
|
||||||
The main purpose of this bot is just to answer "Oh, hi, " and add user mention here
|
|
||||||
|
|
||||||
## Launch
|
|
||||||
|
|
||||||
```bash
|
|
||||||
../gradlew run --args="BOT_TOKEN"
|
|
||||||
```
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
buildscript {
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
apply plugin: 'kotlin'
|
|
||||||
apply plugin: 'application'
|
|
||||||
|
|
||||||
mainClassName="TopicsHandlingKt"
|
|
||||||
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
||||||
|
|
||||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
|
||||||
}
|
|
||||||
@@ -1,145 +0,0 @@
|
|||||||
import com.benasher44.uuid.uuid4
|
|
||||||
import dev.inmo.micro_utils.common.repeatOnFailure
|
|
||||||
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
|
||||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.closeForumTopic
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.closeGeneralForumTopic
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.createForumTopic
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.deleteForumTopic
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.editForumTopic
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.editGeneralForumTopic
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.hideGeneralForumTopic
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.reopenForumTopic
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.reopenGeneralForumTopic
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.unhideGeneralForumTopic
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
|
||||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.flushAccumulatedUpdates
|
|
||||||
import dev.inmo.tgbotapi.types.BotCommand
|
|
||||||
import dev.inmo.tgbotapi.types.ForumTopic
|
|
||||||
import dev.inmo.tgbotapi.types.commands.BotCommandScope
|
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
|
|
||||||
suspend fun main(vararg args: String) {
|
|
||||||
telegramBotWithBehaviourAndLongPolling(
|
|
||||||
args.first(),
|
|
||||||
CoroutineScope(Dispatchers.Default),
|
|
||||||
defaultExceptionsHandler = {
|
|
||||||
it.printStackTrace()
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
flushAccumulatedUpdates()
|
|
||||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
|
||||||
println(it)
|
|
||||||
}
|
|
||||||
onCommand("start_test_topics") {
|
|
||||||
val forumTopic = createForumTopic(
|
|
||||||
it.chat,
|
|
||||||
"Test",
|
|
||||||
ForumTopic.GREEN
|
|
||||||
)
|
|
||||||
|
|
||||||
reply(it, "Test topic has been created")
|
|
||||||
|
|
||||||
delay(1000L)
|
|
||||||
editForumTopic(
|
|
||||||
it.chat.id,
|
|
||||||
forumTopic.messageThreadId,
|
|
||||||
"Test 01"
|
|
||||||
)
|
|
||||||
|
|
||||||
reply(it, "Test topic has changed its name to Test 01")
|
|
||||||
|
|
||||||
delay(1000L)
|
|
||||||
closeForumTopic(
|
|
||||||
it.chat.id,
|
|
||||||
forumTopic.messageThreadId,
|
|
||||||
)
|
|
||||||
|
|
||||||
reply(it, "Test topic has been closed")
|
|
||||||
|
|
||||||
delay(1000L)
|
|
||||||
reopenForumTopic(
|
|
||||||
it.chat.id,
|
|
||||||
forumTopic.messageThreadId,
|
|
||||||
)
|
|
||||||
|
|
||||||
reply(it, "Test topic has been reopened")
|
|
||||||
|
|
||||||
delay(1000L)
|
|
||||||
deleteForumTopic(
|
|
||||||
it.chat.id,
|
|
||||||
forumTopic.messageThreadId,
|
|
||||||
)
|
|
||||||
|
|
||||||
reply(it, "Test topic has been deleted")
|
|
||||||
|
|
||||||
delay(1000L)
|
|
||||||
hideGeneralForumTopic(
|
|
||||||
it.chat.id,
|
|
||||||
)
|
|
||||||
|
|
||||||
reply(it, "General topic has been hidden")
|
|
||||||
|
|
||||||
delay(1000L)
|
|
||||||
unhideGeneralForumTopic(
|
|
||||||
it.chat.id
|
|
||||||
)
|
|
||||||
|
|
||||||
reply(it, "General topic has been shown")
|
|
||||||
|
|
||||||
delay(1000L)
|
|
||||||
runCatchingSafely(
|
|
||||||
{ _ ->
|
|
||||||
reopenGeneralForumTopic(
|
|
||||||
it.chat.id
|
|
||||||
)
|
|
||||||
|
|
||||||
closeGeneralForumTopic(
|
|
||||||
it.chat.id
|
|
||||||
)
|
|
||||||
}
|
|
||||||
) {
|
|
||||||
closeGeneralForumTopic(
|
|
||||||
it.chat.id
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
reply(it, "General topic has been closed")
|
|
||||||
|
|
||||||
delay(1000L)
|
|
||||||
reopenGeneralForumTopic(
|
|
||||||
it.chat.id
|
|
||||||
)
|
|
||||||
|
|
||||||
reply(it, "General topic has been opened")
|
|
||||||
|
|
||||||
delay(1000L)
|
|
||||||
editGeneralForumTopic(
|
|
||||||
it.chat.id,
|
|
||||||
uuid4().toString().take(10)
|
|
||||||
)
|
|
||||||
|
|
||||||
reply(it, "General topic has been renamed")
|
|
||||||
|
|
||||||
delay(1000L)
|
|
||||||
editGeneralForumTopic(
|
|
||||||
it.chat.id,
|
|
||||||
"Main topic"
|
|
||||||
)
|
|
||||||
|
|
||||||
reply(it, "General topic has been renamed")
|
|
||||||
|
|
||||||
delay(1000L)
|
|
||||||
}
|
|
||||||
|
|
||||||
setMyCommands(
|
|
||||||
BotCommand("start_test_topics", "start test topics"),
|
|
||||||
scope = BotCommandScope.AllGroupChats
|
|
||||||
)
|
|
||||||
}.second.join()
|
|
||||||
}
|
|
||||||
@@ -9,8 +9,7 @@ buildscript {
|
|||||||
}
|
}
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id "org.jetbrains.kotlin.multiplatform"
|
id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version"
|
||||||
id "org.jetbrains.kotlin.plugin.serialization"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
apply plugin: 'application'
|
apply plugin: 'application'
|
||||||
@@ -26,7 +25,6 @@ kotlin {
|
|||||||
commonMain {
|
commonMain {
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation kotlin('stdlib')
|
implementation kotlin('stdlib')
|
||||||
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$serialization_version"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +38,7 @@ kotlin {
|
|||||||
dependencies {
|
dependencies {
|
||||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||||
implementation "dev.inmo:micro_utils.ktor.server:$micro_utils_version"
|
implementation "dev.inmo:micro_utils.ktor.server:$micro_utils_version"
|
||||||
implementation "io.ktor:ktor-server-cio:$ktor_version"
|
implementation "io.ktor:ktor-server-tomcat:$ktor_version"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +0,0 @@
|
|||||||
import kotlinx.serialization.Serializable
|
|
||||||
|
|
||||||
@Serializable
|
|
||||||
data class WebAppDataWrapper(
|
|
||||||
val data: String,
|
|
||||||
val hash: String
|
|
||||||
)
|
|
||||||
@@ -1,175 +1,37 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
|
|
||||||
import dev.inmo.tgbotapi.types.webAppQueryIdField
|
|
||||||
import dev.inmo.tgbotapi.webapps.*
|
import dev.inmo.tgbotapi.webapps.*
|
||||||
import dev.inmo.tgbotapi.webapps.haptic.HapticFeedbackStyle
|
|
||||||
import dev.inmo.tgbotapi.webapps.haptic.HapticFeedbackType
|
|
||||||
import dev.inmo.tgbotapi.webapps.popup.*
|
|
||||||
import io.ktor.client.HttpClient
|
import io.ktor.client.HttpClient
|
||||||
import io.ktor.client.request.*
|
import io.ktor.client.request.get
|
||||||
import io.ktor.client.statement.bodyAsText
|
import io.ktor.client.statement.HttpResponse
|
||||||
import io.ktor.http.*
|
import io.ktor.client.statement.readText
|
||||||
import io.ktor.http.content.TextContent
|
import io.ktor.http.encodeURLPath
|
||||||
import kotlinx.browser.document
|
import kotlinx.browser.document
|
||||||
import kotlinx.browser.window
|
import kotlinx.browser.window
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.dom.appendElement
|
import kotlinx.dom.appendElement
|
||||||
import kotlinx.dom.appendText
|
import kotlinx.dom.appendText
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
import org.w3c.dom.HTMLElement
|
|
||||||
|
|
||||||
fun HTMLElement.log(text: String) {
|
|
||||||
appendText(text)
|
|
||||||
appendElement("p", {})
|
|
||||||
}
|
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
console.log("Web app started")
|
console.log("Web app started")
|
||||||
val client = HttpClient()
|
|
||||||
val baseUrl = window.location.origin.removeSuffix("/")
|
|
||||||
|
|
||||||
window.onload = {
|
window.onload = {
|
||||||
val scope = CoroutineScope(Dispatchers.Default)
|
val scope = CoroutineScope(Dispatchers.Default)
|
||||||
runCatching {
|
runCatching {
|
||||||
|
|
||||||
scope.launchSafelyWithoutExceptions {
|
|
||||||
val response = client.post("$baseUrl/check") {
|
|
||||||
setBody(
|
|
||||||
Json { }.encodeToString(
|
|
||||||
WebAppDataWrapper.serializer(),
|
|
||||||
WebAppDataWrapper(webApp.initData, webApp.initDataUnsafe.hash)
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
val dataIsSafe = response.bodyAsText().toBoolean()
|
|
||||||
|
|
||||||
document.body ?.log(
|
|
||||||
if (dataIsSafe) {
|
|
||||||
"Data is safe"
|
|
||||||
} else {
|
|
||||||
"Data is unsafe"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
|
|
||||||
document.body ?.log(
|
|
||||||
webApp.initDataUnsafe.chat.toString()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
document.body ?.appendElement("button") {
|
document.body ?.appendElement("button") {
|
||||||
addEventListener("click", {
|
addEventListener(
|
||||||
scope.launchSafelyWithoutExceptions {
|
"click",
|
||||||
handleResult({ "Clicked" }) {
|
{
|
||||||
client.post("${window.location.origin.removeSuffix("/")}/inline") {
|
webApp.sendData("Clicked")
|
||||||
parameter(webAppQueryIdField, it)
|
|
||||||
setBody(TextContent("Clicked", ContentType.Text.Plain))
|
|
||||||
document.body ?.log(url.build().toString())
|
|
||||||
}.coroutineContext.job.join()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
appendText("Example button")
|
appendText("Example button")
|
||||||
} ?: window.alert("Unable to load body")
|
} ?: window.alert("Unable to load body")
|
||||||
|
|
||||||
document.body ?.appendElement("p", {})
|
|
||||||
document.body ?.appendText("Alerts:")
|
|
||||||
|
|
||||||
document.body ?.appendElement("button") {
|
|
||||||
addEventListener("click", {
|
|
||||||
webApp.showPopup(
|
|
||||||
PopupParams(
|
|
||||||
"It is sample title of default button",
|
|
||||||
"It is sample message of default button",
|
|
||||||
DefaultPopupButton("default", "Default button"),
|
|
||||||
OkPopupButton("ok"),
|
|
||||||
DestructivePopupButton("destructive", "Destructive button")
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
document.body ?.log(
|
|
||||||
when (it) {
|
|
||||||
"default" -> "You have clicked default button in popup"
|
|
||||||
"ok" -> "You have clicked ok button in popup"
|
|
||||||
"destructive" -> "You have clicked destructive button in popup"
|
|
||||||
else -> "I can't imagine where you take button with id $it"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
appendText("Popup")
|
|
||||||
} ?: window.alert("Unable to load body")
|
|
||||||
|
|
||||||
document.body ?.appendElement("button") {
|
|
||||||
addEventListener("click", {
|
|
||||||
webApp.showAlert(
|
|
||||||
"This is alert message"
|
|
||||||
) {
|
|
||||||
document.body ?.log(
|
|
||||||
"You have closed alert"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
appendText("Alert")
|
|
||||||
} ?: window.alert("Unable to load body")
|
|
||||||
|
|
||||||
document.body ?.appendElement("button") {
|
|
||||||
addEventListener("click", {
|
|
||||||
webApp.showConfirm(
|
|
||||||
"This is confirm message"
|
|
||||||
) {
|
|
||||||
document.body ?.log(
|
|
||||||
"You have pressed \"${if (it) "Ok" else "Cancel"}\" in confirm"
|
|
||||||
)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
appendText("Confirm")
|
|
||||||
} ?: window.alert("Unable to load body")
|
|
||||||
|
|
||||||
document.body ?.appendElement("p", {})
|
|
||||||
|
|
||||||
document.body ?.appendElement("button") {
|
|
||||||
fun updateText() {
|
|
||||||
textContent = if (webApp.isClosingConfirmationEnabled) {
|
|
||||||
"Disable closing confirmation"
|
|
||||||
} else {
|
|
||||||
"Enable closing confirmation"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
addEventListener("click", {
|
|
||||||
webApp.toggleClosingConfirmation()
|
|
||||||
updateText()
|
|
||||||
})
|
|
||||||
updateText()
|
|
||||||
} ?: window.alert("Unable to load body")
|
|
||||||
|
|
||||||
document.body ?.appendElement("p", {})
|
|
||||||
|
|
||||||
webApp.apply {
|
webApp.apply {
|
||||||
onThemeChanged {
|
onThemeChanged {
|
||||||
document.body ?.log("Theme changed: ${webApp.themeParams}")
|
document.body ?.appendText("Theme changed: ${webApp.themeParams}")
|
||||||
|
document.body ?.appendElement("p", {})
|
||||||
}
|
}
|
||||||
onViewportChanged {
|
onViewportChanged {
|
||||||
document.body ?.log("Viewport changed: ${it.isStateStable}")
|
document.body ?.appendText("Viewport changed: ${it.isStateStable}")
|
||||||
}
|
document.body ?.appendElement("p", {})
|
||||||
backButton.apply {
|
|
||||||
onClick {
|
|
||||||
document.body ?.log("Back button clicked")
|
|
||||||
hapticFeedback.impactOccurred(
|
|
||||||
HapticFeedbackStyle.Heavy
|
|
||||||
)
|
|
||||||
}
|
|
||||||
show()
|
|
||||||
}
|
|
||||||
mainButton.apply {
|
|
||||||
setText("Main button")
|
|
||||||
onClick {
|
|
||||||
document.body ?.log("Main button clicked")
|
|
||||||
hapticFeedback.notificationOccurred(
|
|
||||||
HapticFeedbackType.Success
|
|
||||||
)
|
|
||||||
}
|
|
||||||
show()
|
|
||||||
}
|
|
||||||
onSettingsButtonClicked {
|
|
||||||
document.body ?.log("Settings button clicked")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
webApp.ready()
|
webApp.ready()
|
||||||
|
|||||||
@@ -2,11 +2,6 @@
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="format-detection" content="telephone=no"/>
|
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
|
|
||||||
<meta name="HandheldFriendly" content="True"/>
|
|
||||||
<meta name="robots" content="noindex,nofollow"/>
|
|
||||||
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
|
|
||||||
<title>Web App Example</title>
|
<title>Web App Example</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
|||||||
@@ -1,30 +1,18 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
import dev.inmo.micro_utils.ktor.server.createKtorServer
|
import dev.inmo.micro_utils.ktor.server.createKtorServer
|
||||||
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||||
import dev.inmo.tgbotapi.extensions.api.telegramBot
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
||||||
import dev.inmo.tgbotapi.types.BotCommand
|
import io.ktor.application.call
|
||||||
import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResultArticle
|
import io.ktor.http.content.files
|
||||||
import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputTextMessageContent
|
import io.ktor.http.content.static
|
||||||
import dev.inmo.tgbotapi.types.webAppQueryIdField
|
import io.ktor.routing.get
|
||||||
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
|
import io.ktor.routing.routing
|
||||||
import dev.inmo.tgbotapi.utils.*
|
import io.ktor.server.tomcat.Tomcat
|
||||||
import io.ktor.http.*
|
|
||||||
import io.ktor.server.application.call
|
|
||||||
import io.ktor.server.http.content.*
|
|
||||||
import io.ktor.server.request.receiveText
|
|
||||||
import io.ktor.server.response.respond
|
|
||||||
import io.ktor.server.routing.post
|
|
||||||
import io.ktor.server.routing.routing
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.nio.charset.Charset
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Accepts two parameters:
|
* Accepts two parameters:
|
||||||
@@ -34,14 +22,9 @@ import java.nio.charset.Charset
|
|||||||
*
|
*
|
||||||
* Will start the server to share the static (index.html and WebApp.js) on 0.0.0.0:8080
|
* Will start the server to share the static (index.html and WebApp.js) on 0.0.0.0:8080
|
||||||
*/
|
*/
|
||||||
@OptIn(PreviewFeature::class)
|
|
||||||
suspend fun main(vararg args: String) {
|
suspend fun main(vararg args: String) {
|
||||||
val telegramBotAPIUrlsKeeper = TelegramAPIUrlsKeeper(
|
|
||||||
args.first(),
|
|
||||||
testServer = args.any { it == "testServer" }
|
|
||||||
)
|
|
||||||
val bot = telegramBot(telegramBotAPIUrlsKeeper)
|
|
||||||
createKtorServer(
|
createKtorServer(
|
||||||
|
Tomcat,
|
||||||
"0.0.0.0",
|
"0.0.0.0",
|
||||||
8080,
|
8080,
|
||||||
additionalEngineEnvironmentConfigurator = {
|
additionalEngineEnvironmentConfigurator = {
|
||||||
@@ -51,69 +34,28 @@ suspend fun main(vararg args: String) {
|
|||||||
routing {
|
routing {
|
||||||
static {
|
static {
|
||||||
files(File("WebApp/build/distributions"))
|
files(File("WebApp/build/distributions"))
|
||||||
default("WebApp/build/distributions/index.html")
|
|
||||||
}
|
|
||||||
post("inline") {
|
|
||||||
val requestBody = call.receiveText()
|
|
||||||
val queryId = call.parameters[webAppQueryIdField] ?: error("$webAppQueryIdField should be presented")
|
|
||||||
|
|
||||||
bot.answer(queryId, InlineQueryResultArticle(queryId, "Result", InputTextMessageContent(requestBody)))
|
|
||||||
call.respond(HttpStatusCode.OK)
|
|
||||||
}
|
|
||||||
post("check") {
|
|
||||||
val requestBody = call.receiveText()
|
|
||||||
val webAppCheckData = Json { }.decodeFromString(WebAppDataWrapper.serializer(), requestBody)
|
|
||||||
|
|
||||||
val isSafe = telegramBotAPIUrlsKeeper.checkWebAppData(webAppCheckData.data, webAppCheckData.hash)
|
|
||||||
|
|
||||||
call.respond(HttpStatusCode.OK, isSafe.toString())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.start(false)
|
}.start(false)
|
||||||
|
|
||||||
bot.buildBehaviourWithLongPolling(
|
telegramBotWithBehaviourAndLongPolling(
|
||||||
|
args.first(),
|
||||||
defaultExceptionsHandler = { it.printStackTrace() }
|
defaultExceptionsHandler = { it.printStackTrace() }
|
||||||
) {
|
) {
|
||||||
onCommand("reply_markup") {
|
onCommand("start") {
|
||||||
reply(
|
reply(
|
||||||
it,
|
it,
|
||||||
"Button",
|
"Button:",
|
||||||
replyMarkup = replyKeyboard(resizeKeyboard = true, oneTimeKeyboard = true) {
|
|
||||||
row {
|
|
||||||
webAppButton("Open WebApp", WebAppInfo(args[1]))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
)
|
|
||||||
}
|
|
||||||
onCommand("inline") {
|
|
||||||
reply(
|
|
||||||
it,
|
|
||||||
"Button",
|
|
||||||
replyMarkup = inlineKeyboard {
|
replyMarkup = inlineKeyboard {
|
||||||
row {
|
row {
|
||||||
webAppButton("Open WebApp", WebAppInfo(args[1]))
|
webAppButton("Open", args[1])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
onUnhandledCommand {
|
|
||||||
reply(
|
|
||||||
it,
|
|
||||||
buildEntities {
|
|
||||||
+"Use " + botCommand("inline") + " to get inline web app button\n"
|
|
||||||
+"Use " + botCommand("reply_markup") + " to get reply markup web app button\n"
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
setMyCommands(
|
|
||||||
BotCommand("reply_markup", "Use to get reply markup keyboard with web app trigger"),
|
|
||||||
BotCommand("inline", "Use to get inline keyboard with web app trigger"),
|
|
||||||
)
|
|
||||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||||
println(it)
|
println(it)
|
||||||
}
|
}
|
||||||
println(getMe())
|
println(getMe())
|
||||||
}.join()
|
}.second.join()
|
||||||
}
|
}
|
||||||
|
|||||||
13
build.gradle
13
build.gradle
@@ -1,14 +1,3 @@
|
|||||||
buildscript {
|
|
||||||
repositories {
|
|
||||||
mavenCentral()
|
|
||||||
}
|
|
||||||
|
|
||||||
dependencies {
|
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
|
||||||
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
allprojects {
|
allprojects {
|
||||||
repositories {
|
repositories {
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
@@ -22,7 +11,5 @@ allprojects {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
maven { url "https://git.inmo.dev/api/packages/InsanusMokrassar/maven" }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,11 +1,8 @@
|
|||||||
kotlin.code.style=official
|
kotlin.code.style=official
|
||||||
org.gradle.parallel=true
|
org.gradle.parallel=true
|
||||||
# Due to parallel compilation project require next amount of memory on full build
|
|
||||||
org.gradle.jvmargs=-Xmx768m
|
|
||||||
|
|
||||||
|
|
||||||
kotlin_version=1.7.22
|
kotlin_version=1.6.21
|
||||||
telegram_bot_api_version=5.0.0
|
telegram_bot_api_version=0.38.17
|
||||||
micro_utils_version=0.16.4
|
micro_utils_version=0.9.24
|
||||||
serialization_version=1.4.1
|
ktor_version=1.6.8
|
||||||
ktor_version=2.2.1
|
|
||||||
|
|||||||
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
|
|||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
|
||||||
|
|||||||
@@ -6,8 +6,6 @@ include ":HelloBot"
|
|||||||
|
|
||||||
include ":GetMeBot"
|
include ":GetMeBot"
|
||||||
|
|
||||||
include ":DeepLinksBot"
|
|
||||||
|
|
||||||
include ":FilesLoaderBot"
|
include ":FilesLoaderBot"
|
||||||
|
|
||||||
include ":ResenderBot:ResenderBotLib"
|
include ":ResenderBot:ResenderBotLib"
|
||||||
@@ -16,15 +14,8 @@ include ":ResenderBot:jvm_launcher"
|
|||||||
include ":KeyboardsBot:KeyboardsBotLib"
|
include ":KeyboardsBot:KeyboardsBotLib"
|
||||||
include ":KeyboardsBot:jvm_launcher"
|
include ":KeyboardsBot:jvm_launcher"
|
||||||
|
|
||||||
include ":StickerInfoBot:StickerInfoBotLib"
|
|
||||||
include ":StickerInfoBot:jvm_launcher"
|
|
||||||
|
|
||||||
include ":SlotMachineDetectorBot"
|
include ":SlotMachineDetectorBot"
|
||||||
|
|
||||||
include ":ChatAvatarSetter"
|
|
||||||
|
|
||||||
include ":WebApp"
|
include ":WebApp"
|
||||||
|
|
||||||
include ":FSMBot"
|
include ":FSMBot"
|
||||||
|
|
||||||
include ":TopicsHandling"
|
|
||||||
|
|||||||
Reference in New Issue
Block a user