Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions ManagedBotsBot/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
buildscript {
repositories {
mavenCentral()
}

dependencies {
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

apply plugin: 'kotlin'
apply plugin: 'application'

mainClassName="CustomBotKt"


dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
}
143 changes: 143 additions & 0 deletions ManagedBotsBot/src/main/kotlin/ManagedBotsBot.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import dev.inmo.kslog.common.KSLog
import dev.inmo.kslog.common.LogLevel
import dev.inmo.kslog.common.defaultMessageFormatter
import dev.inmo.kslog.common.setDefaultKSLog
import dev.inmo.micro_utils.coroutines.subscribeLoggingDropExceptions
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.tgbotapi.extensions.api.bot.getMe
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
import dev.inmo.tgbotapi.extensions.api.managed_bots.getManagedBotToken
import dev.inmo.tgbotapi.extensions.api.managed_bots.replaceManagedBotToken
import dev.inmo.tgbotapi.extensions.api.send.reply
import dev.inmo.tgbotapi.extensions.api.send.send
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContextData
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildSubcontextInitialAction
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onManagedBotCreated
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onManagedBotUpdated
import dev.inmo.tgbotapi.extensions.utils.chatEventMessageOrNull
import dev.inmo.tgbotapi.extensions.utils.groupContentMessageOrNull
import dev.inmo.tgbotapi.extensions.utils.managedBotCreatedOrNull
import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatReplyKeyboard
import dev.inmo.tgbotapi.extensions.utils.types.buttons.replyKeyboard
import dev.inmo.tgbotapi.extensions.utils.types.buttons.requestManagedBotButton
import dev.inmo.tgbotapi.types.Username
import dev.inmo.tgbotapi.types.buttons.KeyboardButtonRequestManagedBot
import dev.inmo.tgbotapi.types.buttons.PreparedKeyboardButtonId
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
import dev.inmo.tgbotapi.types.request.RequestId
import dev.inmo.tgbotapi.types.toChatId
import dev.inmo.tgbotapi.types.update.abstracts.Update
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers

private var BehaviourContextData.update: Update?
get() = get("update") as? Update
set(value) = set("update", value)

private var BehaviourContextData.commonMessage: CommonMessage<*>?
get() = get("commonMessage") as? CommonMessage<*>
set(value) = set("commonMessage", value)

/**
* This place can be the playground for your code.
*/
suspend fun main(vararg args: String) {
val botToken = args.first()

val isDebug = args.any { it == "debug" }
val isTestServer = args.any { it == "testServer" }

if (isDebug) {
setDefaultKSLog(
KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
println(defaultMessageFormatter(level, tag, message, throwable))
}
)
}

telegramBotWithBehaviourAndLongPolling(
botToken,
CoroutineScope(Dispatchers.IO),
testServer = isTestServer,
builder = {
includeMiddlewares {
addMiddleware {
doOnRequestReturnResult { result, request, _ ->
println("Result of $request:\n\n$result")
null
}
}
}
},
subcontextInitialAction = buildSubcontextInitialAction {
add {
data.update = it
}
}
) {
// start here!!
val me = getMe()
println(me)

onCommand("start") {
println(data.update)
println(data.commonMessage)
println(getChat(it.chat))
}

onCommand("canManageBots") {
val me = getMe()
reply(it, if (me.canManageBots) "Yes" else "No")
}

val requestId = RequestId(0)
onCommand("keyboard") {
reply(
it,
"Keyboard",
replyMarkup = flatReplyKeyboard(
resizeKeyboard = true,
oneTimeKeyboard = true,
) {
requestManagedBotButton(
"Add managed bot",
KeyboardButtonRequestManagedBot(
requestId = requestId,
suggestedName = "SampleName",
suggestedUsername = Username("@some_sample_bot")
)
)
}
)
}

onManagedBotCreated {
reply(it, "Managed bot created successfully: ${it.chatEvent.bot}")
val token = getManagedBotToken(
it.chatEvent.bot.id.toChatId()
)
reply(it, "Token: $token")
}

onManagedBotUpdated {
send(it.user, "Managed bot has been updated: ${it.bot}")
val token = getManagedBotToken(
it.bot.id.toChatId()
)
send(it.user, "Token: $token")
}

onCommand("replaceToken") {
val reply = it.replyTo ?.chatEventMessageOrNull() ?: return@onCommand
val managedBotCreated = reply.chatEvent.managedBotCreatedOrNull() ?: return@onCommand

reply(it, "Token in replace update: ${replaceManagedBotToken(managedBotCreated.bot.id.toChatId())}")
}

allUpdatesFlow.subscribeLoggingDropExceptions(this) {
println(it)
}
}.second.join()
}
57 changes: 53 additions & 4 deletions PollsBot/src/main/kotlin/PollsBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
import dev.inmo.tgbotapi.extensions.api.send.polls.sendQuizPoll
import dev.inmo.tgbotapi.extensions.api.send.polls.sendRegularPoll
import dev.inmo.tgbotapi.extensions.api.send.reply
import dev.inmo.tgbotapi.extensions.api.send.send
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onPollAnswer
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onPollOptionAdded
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onPollOptionDeleted
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onPollUpdates
import dev.inmo.tgbotapi.extensions.utils.accessibleMessageOrNull
import dev.inmo.tgbotapi.extensions.utils.customEmojiTextSourceOrNull
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithArgsSources
import dev.inmo.tgbotapi.types.BotCommand
Expand Down Expand Up @@ -105,7 +110,9 @@ suspend fun main(vararg args: String) {
}
},
isAnonymous = false,
replyParameters = ReplyParameters(it)
replyParameters = ReplyParameters(it),
allowAddingOptions = true,
hideResultsUntilCloses = true,
)
pollToChatMutex.withLock {
pollToChat[sentPoll.content.poll.id] = sentPoll.chat.id
Expand All @@ -118,7 +125,12 @@ suspend fun main(vararg args: String) {
.firstOrNull { it.first.command == "quiz" }
?.second
?.firstNotNullOfOrNull { it.customEmojiTextSourceOrNull() }
val correctAnswer = Random.nextInt(10)
val correctAnswer = mutableListOf<Int>()
(1 until Random.nextInt(9)).forEach {
val option = Random.nextInt(10)
if (correctAnswer.contains(option)) return@forEach
correctAnswer.add(option)
}
val sentPoll = sendQuizPoll(
it.chat.id,
questionEntities = buildEntities {
Expand All @@ -127,7 +139,13 @@ suspend fun main(vararg args: String) {
customEmoji(customEmoji.customEmojiId, customEmoji.subsources)
}
},
(1 .. 10).map {
descriptionTextSources = buildEntities {
regular("Test quiz poll description:")
if (customEmoji != null) {
customEmoji(customEmoji.customEmojiId, customEmoji.subsources)
}
},
options = (1 .. 10).map {
InputPollOption {
regular(it.toString()) + " "
if (customEmoji != null) {
Expand All @@ -137,14 +155,19 @@ suspend fun main(vararg args: String) {
},
isAnonymous = false,
replyParameters = ReplyParameters(it),
correctOptionId = correctAnswer,
correctOptionIds = correctAnswer.sorted(),
allowMultipleAnswers = correctAnswer.size > 1,
allowsRevoting = true,
shuffleOptions = true,
hideResultsUntilCloses = true,
explanationTextSources = buildEntities {
regular("Random solved it to be ") + underline((correctAnswer + 1).toString()) + " "
if (customEmoji != null) {
customEmoji(customEmoji.customEmojiId, customEmoji.subsources)
}
}
)
println("Sent poll data: $sentPoll")
pollToChatMutex.withLock {
pollToChat[sentPoll.content.poll.id] = sentPoll.chat.id
}
Expand All @@ -168,6 +191,32 @@ suspend fun main(vararg args: String) {
}
}

onPollOptionAdded {
it.chatEvent.pollMessage ?.accessibleMessageOrNull() ?.let { pollMessage ->
reply(pollMessage) {
+"Poll option added: \n"
+it.chatEvent.optionTextSources
}
}
}
onPollOptionDeleted {
it.chatEvent.pollMessage ?.accessibleMessageOrNull() ?.let { pollMessage ->
reply(pollMessage) {
+"Poll option deleted: \n"
+it.chatEvent.optionTextSources
}
}
}

onContentMessage {
val replyPollOptionId = it.replyInfo ?.pollOptionId ?: return@onContentMessage
it.replyTo ?.accessibleMessageOrNull() ?.let { replied ->
reply(replied, pollOptionId = replyPollOptionId) {
+"Reply to poll option"
}
}
}

setMyCommands(
BotCommand("anonymous", "Create anonymous regular poll"),
BotCommand("public", "Create non anonymous regular poll"),
Expand Down
6 changes: 6 additions & 0 deletions WebApp/src/commonMain/kotlin/PreparedSampleData.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import dev.inmo.tgbotapi.types.buttons.PreparedKeyboardButtonId
import dev.inmo.tgbotapi.types.request.RequestId
import kotlin.random.Random
import kotlin.random.nextUInt

val preparedSampleKeyboardRequestId = RequestId(Random.nextUInt().toUShort())
Loading
Loading