Skip to content

Commit fb6ed8b

Browse files
rename GetMe to My bot
1 parent 9981e82 commit fb6ed8b

File tree

6 files changed

+97
-30
lines changed

6 files changed

+97
-30
lines changed

CustomBot/src/main/kotlin/CustomBot.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextData
1111
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildSubcontextInitialAction
1212
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
1313
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChannelDirectMessagesConfigurationChanged
14+
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatOwnerChanged
15+
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatOwnerLeft
1416
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
1517
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
1618
import dev.inmo.tgbotapi.types.update.abstracts.Update

GetMeBot/src/main/kotlin/GetMeBot.kt

Lines changed: 0 additions & 29 deletions
This file was deleted.
File renamed without changes.

MyBot/src/main/kotlin/MyBot.kt

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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.launchLoggingDropExceptions
6+
import dev.inmo.micro_utils.coroutines.runCatchingLogging
7+
import dev.inmo.tgbotapi.bot.ktor.telegramBot
8+
import dev.inmo.tgbotapi.extensions.api.bot.getMe
9+
import dev.inmo.tgbotapi.extensions.api.bot.removeMyProfilePhoto
10+
import dev.inmo.tgbotapi.extensions.api.bot.setMyProfilePhoto
11+
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
12+
import dev.inmo.tgbotapi.extensions.api.files.downloadFileToTemp
13+
import dev.inmo.tgbotapi.extensions.api.send.reply
14+
import dev.inmo.tgbotapi.extensions.api.send.sendMessageDraftFlowWithTexts
15+
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitPhotoMessage
16+
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
17+
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
18+
import dev.inmo.tgbotapi.extensions.utils.extensions.sameChat
19+
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
20+
import dev.inmo.tgbotapi.requests.business_connection.InputProfilePhoto
21+
import kotlinx.coroutines.CoroutineScope
22+
import kotlinx.coroutines.Dispatchers
23+
import kotlinx.coroutines.channels.Channel
24+
import kotlinx.coroutines.flow.consumeAsFlow
25+
import kotlinx.coroutines.flow.filter
26+
import kotlinx.coroutines.flow.first
27+
28+
/**
29+
* This is one of the easiest bots - it will just print information about itself
30+
*/
31+
suspend fun main(vararg args: String) {
32+
val botToken = args.first()
33+
val isDebug = args.any { it == "debug" }
34+
val isTestServer = args.any { it == "testServer" }
35+
36+
if (isDebug) {
37+
setDefaultKSLog(
38+
KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
39+
println(defaultMessageFormatter(level, tag, message, throwable))
40+
}
41+
)
42+
}
43+
44+
val bot = telegramBot(botToken)
45+
46+
telegramBotWithBehaviourAndLongPolling(
47+
botToken,
48+
CoroutineScope(Dispatchers.Default),
49+
testServer = isTestServer,
50+
) {
51+
val me = bot.getMe()
52+
println(me)
53+
println(bot.getChat(me))
54+
55+
onCommand("setMyProfilePhoto") { commandMessage ->
56+
reply(commandMessage, "ok, send me new photo")
57+
val newPhotoMessage = waitPhotoMessage().filter { potentialPhotoMessage ->
58+
potentialPhotoMessage.sameChat(commandMessage)
59+
}.first()
60+
val draftMessagesChannel = Channel<String>(capacity = 1)
61+
62+
launchLoggingDropExceptions {
63+
sendMessageDraftFlowWithTexts(commandMessage.chat.id, draftMessagesChannel.consumeAsFlow())
64+
}.invokeOnCompletion {
65+
draftMessagesChannel.close(it)
66+
}
67+
68+
draftMessagesChannel.send("Start downloading photo")
69+
val photoFile = downloadFileToTemp(newPhotoMessage.content)
70+
71+
draftMessagesChannel.send("Photo file have been downloaded. Start set my profile photo")
72+
73+
val setResult = setMyProfilePhoto(
74+
InputProfilePhoto.Static(
75+
photoFile.asMultipartFile()
76+
)
77+
)
78+
if (setResult) {
79+
reply(commandMessage, "New photo have been set")
80+
}
81+
}
82+
83+
onCommand("removeMyProfilePhoto") {
84+
runCatchingLogging {
85+
if (removeMyProfilePhoto()) {
86+
reply(it, "Photo have been removed")
87+
}
88+
}.onFailure { e ->
89+
e.printStackTrace()
90+
reply(it, "Something web wrong. See logs for details.")
91+
}
92+
}
93+
}.second.join()
94+
}

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ include ":HelloBot"
66

77
include ":PollsBot"
88

9-
include ":GetMeBot"
9+
include ":MyBot"
1010

1111
include ":DeepLinksBot"
1212

0 commit comments

Comments
 (0)