mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-12-05 13:55:38 +00:00
Compare commits
100 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f9050061d1 | |||
| a3e3f6c22c | |||
| 516cc7bfcb | |||
| c707a11b66 | |||
| c6c418c393 | |||
| e0795c3c14 | |||
| dec55990dc | |||
|
|
169f3f5bd5 | ||
| e175bb6143 | |||
| 290d722ae6 | |||
| 9ee85df1ad | |||
|
|
2953c40963 | ||
| 512067b056 | |||
|
|
0cb209dc6e | ||
| 7cf6814d89 | |||
| 09e8492439 | |||
|
|
71222f42e5 | ||
| 426d649877 | |||
|
|
93faf9654a | ||
| 288386e25a | |||
| 99e83589a4 | |||
|
|
fbea95fc0a | ||
| 0edaa28151 | |||
| df874b0783 | |||
| 5f5722398d | |||
| cc29ec75ca | |||
|
|
a42173acf6 | ||
| d936bc4643 | |||
| 1b340d8db5 | |||
|
|
4cd1980778 | ||
| d89202aebb | |||
| be2de6e372 | |||
|
|
08796f18e8 | ||
| 8bd7e116e3 | |||
|
|
39d7a95c95 | ||
| 526ecac89a | |||
|
|
1e4e246fab | ||
| 6aa7fe151e | |||
| 16c27fa493 | |||
|
|
f6a811acca | ||
| 17c08fc906 | |||
| aee0943a5e | |||
| c07f4f2ed1 | |||
| 817acb8faf | |||
| 8b294a0cc0 | |||
|
|
0139cc5dcc | ||
| 6d16508277 | |||
| ce2051d32d | |||
| 1abbcc8789 | |||
| c5e05dfce1 | |||
| c1d7129af8 | |||
| 496fc45d2d | |||
| cdb634e91a | |||
| 9d17311918 | |||
| 12ef88c757 | |||
| 4435fdc80c | |||
| cd9eba0393 | |||
| ef74b33d68 | |||
| 522d1b55ba | |||
| d062ab86ae | |||
| 48292ecf72 | |||
| 22c0fcb439 | |||
| c90952d3ca | |||
| b9e9f981c4 | |||
| 11836b1e17 | |||
| d30e673539 | |||
|
|
5337f1e4f8 | ||
| 26fc3c5924 | |||
| 9e0f43dcc0 | |||
| aa89a289ce | |||
| aeb75d48b9 | |||
| a67db1c214 | |||
| 3c06da09c2 | |||
| c15c6d2cf1 | |||
| febfacc3d5 | |||
| 3c71bf287f | |||
| 6f265640a7 | |||
|
|
b9e8705b2e | ||
| 746f3e8a14 | |||
| bd621047ee | |||
|
|
98f14faeb2 | ||
| ccb29d6977 | |||
| f5b526e0c5 | |||
| c1cfd05446 | |||
| fb7795414b | |||
|
|
5cb998b390 | ||
| 6f182c774c | |||
| 79cc1a7c9a | |||
| 4ce79657ac | |||
| 9a86360682 | |||
| 50f8e0df61 | |||
| bb846406da | |||
| 6766dc4a8f | |||
| b440757331 | |||
|
|
94bfa67eb3 | ||
| 6d066532fb | |||
| d49ce88a0b | |||
| 3820e29bf2 | |||
| 7b66c89632 | |||
| 8f79b9e380 |
16
.github/workflows/build.yml
vendored
Normal file
16
.github/workflows/build.yml
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
name: Build
|
||||
|
||||
on: [push]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up JDK 1.8
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 1.8
|
||||
- name: Build with Gradle
|
||||
run: ./gradlew build
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -8,3 +8,5 @@ settings.xml
|
||||
.gradle/
|
||||
build/
|
||||
out/
|
||||
|
||||
kotlin-js-store/
|
||||
|
||||
11
.travis.yml
11
.travis.yml
@@ -1,11 +0,0 @@
|
||||
language: java
|
||||
install: true
|
||||
|
||||
os: linux
|
||||
dist: trusty
|
||||
jdk: oraclejdk8
|
||||
|
||||
script:
|
||||
- ./gradlew build -s
|
||||
|
||||
|
||||
10
FSMBot/README.md
Normal file
10
FSMBot/README.md
Normal file
@@ -0,0 +1,10 @@
|
||||
# FSM
|
||||
|
||||
This bot contains an example of working with FSM included in project
|
||||
[MicroUtils](https://github.com/InsanusMokrassar/MicroUtils)
|
||||
|
||||
## Launch
|
||||
|
||||
```bash
|
||||
../gradlew run --args="BOT_TOKEN"
|
||||
```
|
||||
26
FSMBot/build.gradle
Normal file
26
FSMBot/build.gradle
Normal file
@@ -0,0 +1,26 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'application'
|
||||
|
||||
mainClassName="SimpleFSMBotKt"
|
||||
|
||||
repositories {
|
||||
mavenLocal()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
||||
48
FSMBot/src/main/kotlin/SimpleFSMBot.kt
Normal file
48
FSMBot/src/main/kotlin/SimpleFSMBot.kt
Normal file
@@ -0,0 +1,48 @@
|
||||
import dev.inmo.micro_utils.fsm.common.State
|
||||
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
sealed interface BotState : State
|
||||
data class ExpectContentOrStopState(override val context: ChatId, val sourceMessage: CommonMessage<TextContent>) : BotState
|
||||
data class StopState(override val context: ChatId) : BotState
|
||||
|
||||
suspend fun main(args: Array<String>) {
|
||||
val botToken = args.first()
|
||||
|
||||
telegramBotWithBehaviourAndFSMAndStartLongPolling<BotState>(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||
strictlyOn<ExpectContentOrStopState> {
|
||||
sendMessage(
|
||||
it.context,
|
||||
buildEntities {
|
||||
+"Send me some content or " + botCommand("stop") + " if you want to stop sending"
|
||||
}
|
||||
)
|
||||
|
||||
val content = waitContentMessage().first()
|
||||
when {
|
||||
content is TextContent && content.parseCommandsWithParams().keys.contains("stop") -> StopState(it.context)
|
||||
else -> {
|
||||
execute(content.createResend(it.context))
|
||||
it
|
||||
}
|
||||
}
|
||||
}
|
||||
strictlyOn<StopState> {
|
||||
sendMessage(it.context, "You have stopped sending of content")
|
||||
|
||||
null
|
||||
}
|
||||
|
||||
command("start") {
|
||||
startChain(ExpectContentOrStopState(it.chat.id, it))
|
||||
}
|
||||
}.second.join()
|
||||
}
|
||||
@@ -13,12 +13,8 @@ apply plugin: 'application'
|
||||
|
||||
mainClassName="FilesLoaderBotKt"
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
import dev.inmo.tgbotapi.extensions.api.downloadFile
|
||||
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
||||
import dev.inmo.tgbotapi.extensions.api.get.getFileAdditionalInfo
|
||||
import dev.inmo.tgbotapi.extensions.api.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.utils.flatMap
|
||||
import dev.inmo.tgbotapi.extensions.utils.safely
|
||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling
|
||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaContent
|
||||
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.triggers_handling.onContentMessage
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMedia
|
||||
import dev.inmo.tgbotapi.utils.filenameFromUrl
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import java.io.File
|
||||
|
||||
/**
|
||||
@@ -19,24 +18,15 @@ suspend fun main(args: Array<String>) {
|
||||
val directoryOrFile = args.getOrNull(1) ?.let { File(it) } ?: File("")
|
||||
directoryOrFile.mkdirs()
|
||||
|
||||
val bot = telegramBot(botToken)
|
||||
val scope = CoroutineScope(Dispatchers.Default)
|
||||
|
||||
bot.startGettingFlowsUpdatesByLongPolling(scope = scope) {
|
||||
val flow = merge (
|
||||
filterContentMessages<MediaContent>(),
|
||||
mediaGroupMessages().flatMap()
|
||||
)
|
||||
flow.onEach {
|
||||
safely({ it.printStackTrace() }) {
|
||||
val pathedFile = bot.getFileAdditionalInfo(it.content.media)
|
||||
File(directoryOrFile, pathedFile.filePath.filenameFromUrl).apply {
|
||||
createNewFile()
|
||||
writeBytes(bot.downloadFile(pathedFile))
|
||||
}
|
||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||
onMedia(initialFilter = null) {
|
||||
val pathedFile = bot.getFileAdditionalInfo(it.content.media)
|
||||
val file = File(directoryOrFile, pathedFile.filePath.filenameFromUrl).apply {
|
||||
createNewFile()
|
||||
writeBytes(bot.downloadFile(pathedFile))
|
||||
}
|
||||
}.launchIn(scope)
|
||||
}
|
||||
|
||||
scope.coroutineContext[Job]!!.join()
|
||||
reply(it, "Saved to ${file.absolutePath}")
|
||||
}
|
||||
onContentMessage { println(it) }
|
||||
}.second.join()
|
||||
}
|
||||
|
||||
@@ -11,14 +11,11 @@ buildscript {
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'application'
|
||||
|
||||
mainClassName="ForwarderBotKt"
|
||||
mainClassName="ForwardInfoSenderBotKt"
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
||||
41
ForwardInfoSenderBot/src/main/kotlin/ForwardInfoSenderBot.kt
Normal file
41
ForwardInfoSenderBot/src/main/kotlin/ForwardInfoSenderBot.kt
Normal file
@@ -0,0 +1,41 @@
|
||||
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.onContentMessage
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
||||
import dev.inmo.tgbotapi.types.*
|
||||
import dev.inmo.tgbotapi.types.message.*
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
/**
|
||||
* This bot will always return message about forwarder. In cases when sent message was not a forward message it will
|
||||
* send suitable message
|
||||
*/
|
||||
suspend fun main(vararg args: String) {
|
||||
val botToken = args.first()
|
||||
|
||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||
onContentMessage(subcontextUpdatesFilter = { _, _ -> true }) {
|
||||
val toAnswer = buildEntities {
|
||||
when (val forwardInfo = it.forwardInfo) {
|
||||
null -> +"There is no forward info"
|
||||
is AnonymousForwardInfo -> {
|
||||
regular("Anonymous user which signed as \"") + code(forwardInfo.senderName) + "\""
|
||||
}
|
||||
is UserForwardInfo -> {
|
||||
val user = forwardInfo.from
|
||||
when (user) {
|
||||
is CommonUser -> regular("User ")
|
||||
is CommonBot,
|
||||
is ExtendedBot -> regular("Bot ")
|
||||
} + code(user.id.chatId.toString()) + " (${user.firstName} ${user.lastName}: ${user.username ?.username ?: "Without username"})"
|
||||
}
|
||||
is ForwardFromChannelInfo -> regular("Channel (") + code((forwardInfo.channelChat).title) + ")"
|
||||
is ForwardFromSupergroupInfo -> regular("Supergroup (") + code((forwardInfo.group).title) + ")"
|
||||
}
|
||||
}
|
||||
reply(it, toAnswer)
|
||||
coroutineContext.job.invokeOnCompletion { println("completance of onContentMessage") }
|
||||
}
|
||||
coroutineContext.job.invokeOnCompletion { println("Completed :)") }
|
||||
}.second.join()
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
import dev.inmo.tgbotapi.extensions.api.send.sendTextMessage
|
||||
import dev.inmo.tgbotapi.extensions.api.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.codeMarkdownV2
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.regularMarkdownV2
|
||||
import dev.inmo.tgbotapi.extensions.utils.safely
|
||||
import dev.inmo.tgbotapi.extensions.utils.updates.asContentMessagesFlow
|
||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling
|
||||
import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2
|
||||
import dev.inmo.tgbotapi.types.message.*
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.PossiblyForwardedMessage
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.*
|
||||
|
||||
/**
|
||||
* This bot will always return message about forwarder. In cases when sent message was not a forward message it will
|
||||
* send suitable message
|
||||
*/
|
||||
suspend fun main(vararg args: String) {
|
||||
val botToken = args.first()
|
||||
|
||||
val bot = telegramBot(botToken)
|
||||
|
||||
val scope = CoroutineScope(Dispatchers.Default)
|
||||
|
||||
bot.startGettingFlowsUpdatesByLongPolling(scope = scope) {
|
||||
messageFlow.asContentMessagesFlow().mapNotNull { it as? PossiblyForwardedMessage }.onEach { message ->
|
||||
safely({ it.printStackTrace() }) {
|
||||
val toAnswer = when (val forwardInfo = message.forwardInfo) {
|
||||
null -> "There is no forward info"
|
||||
is AnonymousForwardInfo -> "Anonymous user which signed as \"${forwardInfo.senderName.codeMarkdownV2()}\""
|
||||
is UserForwardInfo -> forwardInfo.from.let { user ->
|
||||
"User ${user.id.chatId.toString().codeMarkdownV2()} " + "(${user.firstName} ${user.lastName}: ${user.username ?.username ?: "Without username"})".regularMarkdownV2()
|
||||
}
|
||||
is ForwardFromChannelInfo -> "Channel (".regularMarkdownV2() + (forwardInfo.channelChat).title.codeMarkdownV2() + ")".regularMarkdownV2()
|
||||
is ForwardFromSupergroupInfo -> "Supergroup (".regularMarkdownV2() + (forwardInfo.group).title.codeMarkdownV2() + ")".regularMarkdownV2()
|
||||
}
|
||||
bot.sendTextMessage(message.chat, toAnswer, MarkdownV2)
|
||||
}
|
||||
}.launchIn(scope)
|
||||
}
|
||||
|
||||
scope.coroutineContext[Job]!!.join()
|
||||
}
|
||||
@@ -13,12 +13,9 @@ apply plugin: 'application'
|
||||
|
||||
mainClassName="HelloBotKt"
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.telegramBot
|
||||
|
||||
/**
|
||||
* This is one of the most easiest bot - it will just print information about itself
|
||||
|
||||
@@ -13,12 +13,9 @@ apply plugin: 'application'
|
||||
|
||||
mainClassName="HelloBotKt"
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
||||
|
||||
@@ -1,20 +1,16 @@
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChatAdministrators
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.api.send.sendTextMessage
|
||||
import dev.inmo.tgbotapi.extensions.api.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.linkMarkdownV2
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.textMentionMarkdownV2
|
||||
import dev.inmo.tgbotapi.extensions.utils.safely
|
||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling
|
||||
import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2
|
||||
import dev.inmo.tgbotapi.types.User
|
||||
import dev.inmo.tgbotapi.types.chat.abstracts.*
|
||||
import dev.inmo.tgbotapi.types.toChatId
|
||||
import dev.inmo.tgbotapi.utils.extensions.escapeMarkdownV2Common
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
|
||||
/**
|
||||
* The main purpose of this bot is just to answer "Oh, hi, " and add user mention here
|
||||
@@ -22,45 +18,31 @@ import kotlinx.coroutines.flow.onEach
|
||||
suspend fun main(vararg args: String) {
|
||||
val botToken = args.first()
|
||||
|
||||
val bot = telegramBot(botToken)
|
||||
|
||||
val scope = CoroutineScope(Dispatchers.Default)
|
||||
|
||||
scope.launch {
|
||||
println(bot.getChatAdministrators((-1001433262056L).toChatId()))
|
||||
}
|
||||
|
||||
bot.startGettingFlowsUpdatesByLongPolling(scope = scope) {
|
||||
messageFlow.onEach {
|
||||
safely {
|
||||
val message = it.data
|
||||
val chat = message.chat
|
||||
val answerText = "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
|
||||
else -> "Unknown :(".escapeMarkdownV2Common()
|
||||
}
|
||||
bot.reply(
|
||||
message,
|
||||
answerText,
|
||||
MarkdownV2
|
||||
)
|
||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||
onContentMessage { message ->
|
||||
val chat = message.chat
|
||||
if (chat is ChannelChat) {
|
||||
val answer = "Hi everybody in this channel \"${chat.title}\""
|
||||
sendTextMessage(chat, answer, MarkdownV2)
|
||||
return@onContentMessage
|
||||
}
|
||||
}.launchIn(scope)
|
||||
channelPostFlow.onEach {
|
||||
safely {
|
||||
val chat = it.data.chat
|
||||
val message = "Hi everybody in this channel \"${(chat as ChannelChat).title}\""
|
||||
bot.sendTextMessage(chat, message, MarkdownV2)
|
||||
val answerText = "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 ?: 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()
|
||||
}
|
||||
}.launchIn(scope)
|
||||
}
|
||||
|
||||
scope.coroutineContext[Job]!!.join()
|
||||
}
|
||||
reply(
|
||||
message,
|
||||
answerText,
|
||||
MarkdownV2
|
||||
)
|
||||
}
|
||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) { println(it) }
|
||||
}.second.join()
|
||||
}
|
||||
|
||||
32
KeyboardsBot/KeyboardsBotLib/build.gradle
Normal file
32
KeyboardsBot/KeyboardsBotLib/build.gradle
Normal file
@@ -0,0 +1,32 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version"
|
||||
}
|
||||
|
||||
|
||||
kotlin {
|
||||
jvm()
|
||||
js(IR) {
|
||||
browser()
|
||||
binaries.executable()
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
implementation kotlin('stdlib')
|
||||
|
||||
api "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
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.answers.answer
|
||||
import dev.inmo.tgbotapi.extensions.api.edit.text.editMessageText
|
||||
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.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.utils.shortcuts.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.withContent
|
||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
private const val nextPageData = "next"
|
||||
private const val previousPageData = "previous"
|
||||
|
||||
fun String.parsePageAndCount(): Pair<Int, Int>? {
|
||||
val (pageString, countString) = split(" ").takeIf { it.count() > 1 } ?: return null
|
||||
return Pair(
|
||||
pageString.toIntOrNull() ?: return null,
|
||||
countString.toIntOrNull() ?: return null
|
||||
)
|
||||
}
|
||||
|
||||
fun InlineKeyboardBuilder.includePageButtons(page: Int, count: Int) {
|
||||
val numericButtons = listOfNotNull(
|
||||
page - 1,
|
||||
page,
|
||||
page + 1,
|
||||
)
|
||||
row {
|
||||
val numbersRange = 1 .. count
|
||||
numericButtons.forEach {
|
||||
if (it in numbersRange) {
|
||||
dataButton(it.toString(), "$it $count")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
row {
|
||||
if (page - 1 > 2) {
|
||||
dataButton("<<", "1 $count")
|
||||
}
|
||||
if (page - 1 > 1) {
|
||||
dataButton("<", "${page - 2} $count")
|
||||
}
|
||||
|
||||
if (page + 1 < count) {
|
||||
dataButton(">", "${page + 2} $count")
|
||||
}
|
||||
if (page + 2 < count) {
|
||||
dataButton(">>", "$count $count")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun activateKeyboardsBot(
|
||||
token: String,
|
||||
print: (Any) -> Unit
|
||||
) {
|
||||
val bot = telegramBot(token)
|
||||
|
||||
print(bot.getMe())
|
||||
|
||||
bot.buildBehaviourWithLongPolling(CoroutineScope(currentCoroutineContext() + SupervisorJob())) {
|
||||
onCommandWithArgs("inline") { message, args ->
|
||||
val numberOfPages = args.firstOrNull() ?.toIntOrNull() ?: 10
|
||||
reply(
|
||||
message,
|
||||
"Your inline keyboard with $numberOfPages pages",
|
||||
replyMarkup = inlineKeyboard {
|
||||
row {
|
||||
includePageButtons(1, numberOfPages)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
onMessageDataCallbackQuery {
|
||||
val (page, count) = it.data.parsePageAndCount() ?: it.let {
|
||||
answer(it, "Unsupported data :(")
|
||||
return@onMessageDataCallbackQuery
|
||||
}
|
||||
|
||||
editMessageText(
|
||||
it.message.withContent<TextContent>() ?: it.let {
|
||||
answer(it, "Unsupported message type :(")
|
||||
return@onMessageDataCallbackQuery
|
||||
},
|
||||
"This is $page of $count",
|
||||
replyMarkup = inlineKeyboard {
|
||||
row {
|
||||
includePageButtons(page, count)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||
println(it)
|
||||
}
|
||||
}.join()
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
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 {
|
||||
activateKeyboardsBot(token) {
|
||||
infoDiv.innerHTML = it.toString()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
16
KeyboardsBot/KeyboardsBotLib/src/jsMain/resources/index.html
Normal file
16
KeyboardsBot/KeyboardsBotLib/src/jsMain/resources/index.html
Normal file
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Keyboards 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="KeyboardsBotLib.js"></script>
|
||||
<div id="bots_container"></div>
|
||||
</body>
|
||||
</html>
|
||||
21
KeyboardsBot/jvm_launcher/build.gradle
Normal file
21
KeyboardsBot/jvm_launcher/build.gradle
Normal file
@@ -0,0 +1,21 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'application'
|
||||
|
||||
mainClassName="KeyboardsBotJvmKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation project(":KeyboardsBot:KeyboardsBotLib")
|
||||
}
|
||||
10
KeyboardsBot/jvm_launcher/src/main/kotlin/KeyboardsBotJvm.kt
Normal file
10
KeyboardsBot/jvm_launcher/src/main/kotlin/KeyboardsBotJvm.kt
Normal file
@@ -0,0 +1,10 @@
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.withContext
|
||||
|
||||
suspend fun main(args: Array<String>) {
|
||||
withContext(Dispatchers.IO) { // IO for inheriting of it in side of activateKeyboardsBot
|
||||
activateKeyboardsBot(args.first()) {
|
||||
println(it)
|
||||
}
|
||||
}
|
||||
}
|
||||
30
README.md
30
README.md
@@ -1,2 +1,32 @@
|
||||
# TelegramBotAPI-examples
|
||||
|
||||
This repository contains several examples of simple bots which are using TelegramBotAPI
|
||||
|
||||
## How to use this repository
|
||||
|
||||
This repository contains several important things:
|
||||
|
||||
* Example subprojects
|
||||
* Commits
|
||||
* Structure
|
||||
|
||||
### Example subproject
|
||||
|
||||
Each example subproject contains information about how to run this example and what is it
|
||||
doing. Usually, it is some simple thing like sending "hello" message to the user which
|
||||
wrote to the bot.
|
||||
|
||||
### Commits
|
||||
|
||||
Commits can contains some things like migration onto new version (especially it is actual
|
||||
for major version changes), updates according to the new features in versions and
|
||||
different other things which usually more important in context of history or changes
|
||||
between library version
|
||||
|
||||
### Structure
|
||||
|
||||
Structure of this repository fully representative (it is the reason why this repo
|
||||
contains multiplatform subprojects) and you can use it as some template (but I am strongly
|
||||
recommend you to use my
|
||||
[TelegramBot template](https://github.com/InsanusMokrassar/TelegramBotAPI-bot_template) or
|
||||
[Multiplatform Project template](https://github.com/InsanusMokrassar/KotlinMultiplatformProjectTemplate))
|
||||
|
||||
@@ -13,12 +13,9 @@ apply plugin: 'application'
|
||||
|
||||
mainClassName="RandomFileSenderBotKt"
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
||||
|
||||
@@ -1,22 +1,33 @@
|
||||
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.bot.TelegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||
import dev.inmo.tgbotapi.extensions.api.send.media.sendDocument
|
||||
import dev.inmo.tgbotapi.extensions.api.send.sendTextMessage
|
||||
import dev.inmo.tgbotapi.extensions.api.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.utils.safely
|
||||
import dev.inmo.tgbotapi.extensions.utils.updates.filterExactCommands
|
||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling
|
||||
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.triggers_handling.onCommand
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommandWithArgs
|
||||
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.InputMedia.DocumentMediaGroupMemberInputMedia
|
||||
import dev.inmo.tgbotapi.types.InputMedia.InputMediaDocument
|
||||
import dev.inmo.tgbotapi.types.chat.abstracts.Chat
|
||||
import dev.inmo.tgbotapi.types.mediaCountInMediaGroup
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
import java.io.File
|
||||
|
||||
private const val command = "send_file"
|
||||
|
||||
/**
|
||||
* This bot will send files inside of working directory OR from directory in the second argument
|
||||
* This bot will send files inside of working directory OR from directory in the second argument.
|
||||
* You may send /send_file command to this bot to get random file from the directory OR
|
||||
* `/send_file $number` when you want to receive required number of files. For example,
|
||||
* /send_file and `/send_file 1` will have the same effect - bot will send one random file.
|
||||
* But if you will send `/send_file 5` it will choose 5 random files and send them as group
|
||||
*/
|
||||
suspend fun main(args: Array<String>) {
|
||||
val botToken = args.first()
|
||||
@@ -26,32 +37,63 @@ suspend fun main(args: Array<String>) {
|
||||
if (currentRoot.isFile) {
|
||||
return currentRoot
|
||||
} else {
|
||||
return pickFile(currentRoot.listFiles() ?.random() ?: return null)
|
||||
return pickFile(currentRoot.listFiles() ?.takeIf { it.isNotEmpty() } ?.random() ?: return null)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun TelegramBot.sendFiles(chat: Chat, files: List<File>) {
|
||||
when (files.size) {
|
||||
1 -> sendDocument(
|
||||
chat.id,
|
||||
files.first().asMultipartFile(),
|
||||
protectContent = true
|
||||
)
|
||||
else -> sendDocumentsGroup(
|
||||
chat,
|
||||
files.map { InputMediaDocument(it.asMultipartFile()) },
|
||||
protectContent = true
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
val bot = telegramBot(botToken)
|
||||
val scope = CoroutineScope(Dispatchers.Default)
|
||||
|
||||
bot.startGettingFlowsUpdatesByLongPolling(scope = scope) {
|
||||
messageFlow.filterExactCommands(Regex(command)).onEach { message ->
|
||||
safely {
|
||||
pickFile() ?.let {
|
||||
bot.sendDocument(
|
||||
message.chat.id,
|
||||
it.toInputFile()
|
||||
)
|
||||
} ?: bot.sendTextMessage(message.chat.id, "Nothing selected :(")
|
||||
bot.buildBehaviourWithLongPolling (defaultExceptionsHandler = { it.printStackTrace() }) {
|
||||
onCommandWithArgs(command) { message, args ->
|
||||
|
||||
withUploadDocumentAction(message.chat) {
|
||||
val count = args.firstOrNull() ?.toIntOrNull() ?: 1
|
||||
var sent = false
|
||||
|
||||
var left = count
|
||||
val chosen = mutableListOf<File>()
|
||||
|
||||
while (left > 0) {
|
||||
left--
|
||||
val picked = pickFile() ?: continue
|
||||
chosen.add(picked)
|
||||
if (chosen.size >= mediaCountInMediaGroup.last) {
|
||||
sendFiles(message.chat, chosen)
|
||||
chosen.clear()
|
||||
sent = true
|
||||
}
|
||||
}
|
||||
|
||||
if (chosen.isNotEmpty()) {
|
||||
sendFiles(message.chat, chosen)
|
||||
sent = true
|
||||
}
|
||||
|
||||
if (!sent) {
|
||||
bot.reply(message, "Nothing selected :(")
|
||||
}
|
||||
}
|
||||
}.launchIn(scope)
|
||||
}
|
||||
}
|
||||
|
||||
safely {
|
||||
bot.setMyCommands(
|
||||
setMyCommands(
|
||||
BotCommand(command, "Send some random file in picker directory")
|
||||
)
|
||||
println(bot.getMe())
|
||||
}
|
||||
|
||||
scope.coroutineContext[Job]!!.join()
|
||||
println(getMe())
|
||||
}.join()
|
||||
}
|
||||
|
||||
@@ -12,12 +12,10 @@ plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version"
|
||||
}
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
kotlin {
|
||||
jvm()
|
||||
// js(LEGACY) {
|
||||
js(IR) {
|
||||
browser()
|
||||
binaries.executable()
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.send.media.sendMediaGroup
|
||||
import dev.inmo.tgbotapi.extensions.api.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.utils.safely
|
||||
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||
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.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.utils.shortcuts.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.startGettingFlowsUpdatesByLongPolling
|
||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MediaGroupContent
|
||||
import dev.inmo.tgbotapi.types.message.content.abstracts.MessageContent
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.Message
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
|
||||
suspend fun activateResenderBot(
|
||||
token: String,
|
||||
@@ -18,26 +19,37 @@ suspend fun activateResenderBot(
|
||||
|
||||
print(bot.getMe())
|
||||
|
||||
supervisorScope {
|
||||
val scope = this
|
||||
bot.startGettingFlowsUpdatesByLongPolling {
|
||||
filterContentMessages<MessageContent>(scope).onEach {
|
||||
it.content.createResends(it.chat.id, replyToMessageId = it.messageId).forEach {
|
||||
bot.executeUnsafe(it) {
|
||||
it.forEach(print)
|
||||
} ?.also {
|
||||
print(it)
|
||||
}
|
||||
}
|
||||
}.launchIn(scope)
|
||||
mediaGroupMessages(scope).onEach {
|
||||
safely({ print(it.stackTraceToString()) }) {
|
||||
println(it.chat)
|
||||
bot.execute(it.createResend(it.chat ?: return@safely, replyTo = it.first().messageId)).also {
|
||||
print(it)
|
||||
}
|
||||
}
|
||||
}.launchIn(scope)
|
||||
bot.buildBehaviourWithLongPolling(CoroutineScope(currentCoroutineContext() + SupervisorJob())) {
|
||||
onContentMessage(
|
||||
initialFilter = CommonMessageFilterExcludeMediaGroups,
|
||||
subcontextUpdatesFilter = MessageFilterByChat
|
||||
) {
|
||||
val chat = it.chat
|
||||
withTypingAction(chat) {
|
||||
executeUnsafe(it.content.createResend(chat.id, replyToMessageId = it.messageId))
|
||||
}
|
||||
}
|
||||
}
|
||||
onVisualGallery {
|
||||
val chat = it.chat ?: return@onVisualGallery
|
||||
withUploadPhotoAction(chat) {
|
||||
sendVisualMediaGroup(chat, it.map { it.content.toMediaGroupMemberInputMedia() })
|
||||
}
|
||||
}
|
||||
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() })
|
||||
}
|
||||
}
|
||||
|
||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||
println(it)
|
||||
}
|
||||
}.join()
|
||||
}
|
||||
|
||||
@@ -13,12 +13,9 @@ apply plugin: 'application'
|
||||
|
||||
mainClassName="ResenderBotJvmKt"
|
||||
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation project(":ResenderBot:ResenderBotLib")
|
||||
}
|
||||
|
||||
9
SlotMachineDetectorBot/README.md
Normal file
9
SlotMachineDetectorBot/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# SlotMachineDetectorBot
|
||||
|
||||
This bot must reply with information about slot machine answer
|
||||
|
||||
## Launch
|
||||
|
||||
```bash
|
||||
../gradlew run --args="BOT_TOKEN"
|
||||
```
|
||||
21
SlotMachineDetectorBot/build.gradle
Normal file
21
SlotMachineDetectorBot/build.gradle
Normal file
@@ -0,0 +1,21 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
jcenter()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'application'
|
||||
|
||||
mainClassName="SlotMachineDetectorBotKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
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.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.message.content.DiceContent
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
|
||||
suspend fun main(args: Array<String>) {
|
||||
val bot = telegramBot(args.first())
|
||||
|
||||
val scope = CoroutineScope(Dispatchers.Default)
|
||||
bot.longPolling(scope = scope) {
|
||||
filterContentMessages<DiceContent>(scope).onEach {
|
||||
val content = it.content
|
||||
val dice = content.dice
|
||||
val diceType = dice.animationType
|
||||
|
||||
safely ({ it.printStackTrace() }) {
|
||||
if (diceType == SlotMachineDiceAnimationType) {
|
||||
val result = dice.calculateSlotMachineResult() ?: return@safely
|
||||
bot.reply(it, "${result.leftReel}|${result.centerReel}|${result.rightReel}")
|
||||
} else {
|
||||
bot.reply(it, "There is no slot machine dice in message")
|
||||
}
|
||||
}
|
||||
}.launchIn(scope)
|
||||
}
|
||||
|
||||
scope.coroutineContext[Job]!!.join()
|
||||
}
|
||||
10
build.gradle
10
build.gradle
@@ -1,5 +1,15 @@
|
||||
allprojects {
|
||||
repositories {
|
||||
mavenLocal()
|
||||
jcenter()
|
||||
if (project.hasProperty("GITHUB_USER") && project.hasProperty("GITHUB_TOKEN")) {
|
||||
maven {
|
||||
url "https://maven.pkg.github.com/InsanusMokrassar/TelegramBotAPI"
|
||||
credentials {
|
||||
username = project.getProperty("GITHUB_USER")
|
||||
password = project.getProperty("GITHUB_TOKEN")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
kotlin.code.style=official
|
||||
org.gradle.parallel=true
|
||||
|
||||
kotlin_version=1.4.10
|
||||
telegram_bot_api_version=0.29.0
|
||||
|
||||
kotlin_version=1.6.10
|
||||
telegram_bot_api_version=0.38.12
|
||||
micro_utils_version=0.9.19
|
||||
|
||||
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
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
|
||||
|
||||
@@ -1,7 +1,19 @@
|
||||
include ":ForwarderBot"
|
||||
include ":ForwardInfoSenderBot"
|
||||
|
||||
include ":RandomFileSenderBot"
|
||||
|
||||
include ":HelloBot"
|
||||
|
||||
include ":GetMeBot"
|
||||
|
||||
include ":FilesLoaderBot"
|
||||
|
||||
include ":ResenderBot:ResenderBotLib"
|
||||
include ":ResenderBot:jvm_launcher"
|
||||
|
||||
include ":KeyboardsBot:KeyboardsBotLib"
|
||||
include ":KeyboardsBot:jvm_launcher"
|
||||
|
||||
include ":SlotMachineDetectorBot"
|
||||
|
||||
include ":FSMBot"
|
||||
|
||||
Reference in New Issue
Block a user