Skip to content

Commit 9d4b7b5

Browse files
add gifts bot
1 parent e1f5e40 commit 9d4b7b5

File tree

4 files changed

+136
-1
lines changed

4 files changed

+136
-1
lines changed

GiftsBot/build.gradle

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
buildscript {
2+
repositories {
3+
mavenCentral()
4+
}
5+
6+
dependencies {
7+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
8+
}
9+
}
10+
11+
apply plugin: 'kotlin'
12+
apply plugin: 'application'
13+
14+
mainClassName="GiftsBotKt"
15+
16+
17+
dependencies {
18+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
19+
20+
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
21+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import dev.inmo.kslog.common.KSLog
2+
import dev.inmo.kslog.common.LogLevel
3+
import dev.inmo.kslog.common.defaultMessageFormatter
4+
import dev.inmo.kslog.common.setDefaultKSLog
5+
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
6+
import dev.inmo.tgbotapi.extensions.api.bot.getMe
7+
import dev.inmo.tgbotapi.extensions.api.business.getBusinessAccountGiftsFlow
8+
import dev.inmo.tgbotapi.extensions.api.gifts.getChatGiftsFlow
9+
import dev.inmo.tgbotapi.extensions.api.gifts.getUserGiftsFlow
10+
import dev.inmo.tgbotapi.extensions.api.send.reply
11+
import dev.inmo.tgbotapi.extensions.api.send.withTypingAction
12+
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
13+
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
14+
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayCompleted
15+
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayContent
16+
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayCreated
17+
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onGiveawayWinners
18+
import dev.inmo.tgbotapi.types.chat.BusinessChat
19+
import dev.inmo.tgbotapi.types.chat.PrivateChat
20+
import dev.inmo.tgbotapi.types.chat.PublicChat
21+
import dev.inmo.tgbotapi.types.chat.UnknownChatType
22+
import dev.inmo.tgbotapi.types.gifts.OwnedGift
23+
import dev.inmo.tgbotapi.types.message.textsources.splitForText
24+
import dev.inmo.tgbotapi.utils.bold
25+
import dev.inmo.tgbotapi.utils.buildEntities
26+
import kotlinx.coroutines.CoroutineScope
27+
import kotlinx.coroutines.Dispatchers
28+
29+
suspend fun main(vararg args: String) {
30+
val botToken = args.first()
31+
32+
val isDebug = args.any { it == "debug" }
33+
val isTestServer = args.any { it == "testServer" }
34+
35+
if (isDebug) {
36+
setDefaultKSLog(
37+
KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
38+
println(defaultMessageFormatter(level, tag, message, throwable))
39+
}
40+
)
41+
}
42+
43+
telegramBotWithBehaviourAndLongPolling(botToken, testServer = isTestServer) {
44+
// start here!!
45+
val me = getMe()
46+
println(me)
47+
48+
onCommand("start") {
49+
val giftsFlow = when (val chat = it.chat) {
50+
is BusinessChat -> {
51+
getBusinessAccountGiftsFlow(
52+
chat.id.businessConnectionId
53+
)
54+
}
55+
is PrivateChat -> {
56+
getUserGiftsFlow(it.chat.id)
57+
}
58+
is UnknownChatType,
59+
is PublicChat -> {
60+
getChatGiftsFlow(it.chat.id)
61+
}
62+
}
63+
64+
withTypingAction(it.chat) {
65+
val texts = buildEntities {
66+
giftsFlow.collect { ownedGifts ->
67+
ownedGifts.gifts.forEach {
68+
when (it) {
69+
is OwnedGift.Regular.Common -> {
70+
bold("Type") + ": Regular common\n"
71+
bold("Id") + ": ${it.gift.id.string}\n"
72+
bold("Text") + ": ${it.text ?: "(None)"}\n"
73+
bold("Stars cost") + ": ${it.gift.starCount}\n"
74+
}
75+
is OwnedGift.Unique.Common -> {
76+
bold("Type") + ": Unique common\n"
77+
bold("Id") + ": ${it.gift.id ?.string ?: "(None)"}\n"
78+
bold("Name") + ": ${it.gift.name.value}\n"
79+
bold("Model") + ": ${it.gift.model.name}\n"
80+
bold("Number") + ": ${it.gift.number}\n"
81+
}
82+
is OwnedGift.Regular.OwnedByBusinessAccount -> {
83+
bold("Type") + ": Regular owned by business\n"
84+
bold("Id") + ": ${it.gift.id.string}\n"
85+
bold("Text") + ": ${it.text ?: "(None)"}\n"
86+
bold("Stars cost") + ": ${it.gift.starCount}\n"
87+
}
88+
is OwnedGift.Unique.OwnedByBusinessAccount -> {
89+
bold("Type") + ": Unique owned by business\n"
90+
bold("Id") + ": ${it.gift.id ?.string ?: "(None)"}\n"
91+
bold("Name") + ": ${it.gift.name.value}\n"
92+
bold("Model") + ": ${it.gift.model.name}\n"
93+
bold("Number") + ": ${it.gift.number}\n"
94+
}
95+
}
96+
}
97+
}
98+
}
99+
val preparedTexts = texts.splitForText()
100+
if (preparedTexts.isEmpty()) {
101+
reply(it, "This chat have no any gifts")
102+
} else {
103+
preparedTexts.forEach { preparedText -> reply(it, preparedText) }
104+
}
105+
}
106+
}
107+
108+
// allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
109+
// println(it)
110+
// }
111+
}.second.join()
112+
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ kotlin.daemon.jvmargs=-Xmx3g -Xms500m
66

77

88
kotlin_version=2.2.21
9-
telegram_bot_api_version=31.0.0-branch_31.0.0-build3116
9+
telegram_bot_api_version=31.0.0-branch_31.0.0-build3119
1010
micro_utils_version=0.26.9
1111
serialization_version=1.9.0
1212
ktor_version=3.3.2

settings.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,5 @@ include ":SuggestedPosts"
6565
include ":ChecklistsBot"
6666

6767
include ":DraftsBot"
68+
69+
include ":GiftsBot"

0 commit comments

Comments
 (0)