mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-12-26 16:15:39 +00:00
Compare commits
6 Commits
6a04b3980c
...
10.1.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 578d00cac6 | |||
| 13ecb3f0df | |||
| a008d861da | |||
| 6f3766dff6 | |||
| fda366d820 | |||
| 578887ac63 |
9
BoostsInfoBot/README.md
Normal file
9
BoostsInfoBot/README.md
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# UserChatShared
|
||||||
|
|
||||||
|
Showing info about boosts
|
||||||
|
|
||||||
|
## Launch
|
||||||
|
|
||||||
|
```bash
|
||||||
|
../gradlew run --args="BOT_TOKEN"
|
||||||
|
```
|
||||||
21
BoostsInfoBot/build.gradle
Normal file
21
BoostsInfoBot/build.gradle
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
apply plugin: 'kotlin'
|
||||||
|
apply plugin: 'application'
|
||||||
|
|
||||||
|
mainClassName="BoostsInfoKt"
|
||||||
|
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||||
|
|
||||||
|
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||||
|
}
|
||||||
65
BoostsInfoBot/src/main/kotlin/BoostsInfo.kt
Normal file
65
BoostsInfoBot/src/main/kotlin/BoostsInfo.kt
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
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.tgbotapi.bot.ktor.telegramBot
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.get.getUserChatBoosts
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatBoostUpdated
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatShared
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
||||||
|
import dev.inmo.tgbotapi.types.chat.member.ChatCommonAdministratorRights
|
||||||
|
import dev.inmo.tgbotapi.types.request.RequestId
|
||||||
|
import dev.inmo.tgbotapi.utils.regular
|
||||||
|
import korlibs.time.DateFormat
|
||||||
|
import korlibs.time.format
|
||||||
|
|
||||||
|
suspend fun main(args: Array<String>) {
|
||||||
|
val isDebug = args.getOrNull(1) == "debug"
|
||||||
|
|
||||||
|
if (isDebug) {
|
||||||
|
setDefaultKSLog(
|
||||||
|
KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
|
||||||
|
println(defaultMessageFormatter(level, tag, message, throwable))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
val requestChatId = RequestId(1)
|
||||||
|
|
||||||
|
val bot = telegramBot(args.first())
|
||||||
|
|
||||||
|
bot.buildBehaviourWithLongPolling (defaultExceptionsHandler = { it.printStackTrace() }) {
|
||||||
|
onChatBoostUpdated {
|
||||||
|
println(it)
|
||||||
|
}
|
||||||
|
|
||||||
|
onCommand("start") {
|
||||||
|
reply(
|
||||||
|
it,
|
||||||
|
replyMarkup = flatReplyKeyboard {
|
||||||
|
requestChannelButton(
|
||||||
|
"Click me :)",
|
||||||
|
requestChatId,
|
||||||
|
botIsMember = true
|
||||||
|
)
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
regular("Select chat to get know about your boosts")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
onChatShared(initialFilter = { it.chatEvent.requestId == requestChatId }) {
|
||||||
|
val boosts = getUserChatBoosts(it.chatEvent.chatId, it.chat.id)
|
||||||
|
reply(
|
||||||
|
it
|
||||||
|
) {
|
||||||
|
boosts.boosts.forEach {
|
||||||
|
regular("Boost added: ${DateFormat.FORMAT1.format(it.addDate.asDate)}; Boost expire: ${DateFormat.FORMAT1.format(it.expirationDate.asDate)}; Unformatted: $it") + "\n"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.join()
|
||||||
|
}
|
||||||
@@ -42,7 +42,7 @@ suspend fun main(vararg args: String) {
|
|||||||
val replyResult = reply(
|
val replyResult = reply(
|
||||||
it.chat.id,
|
it.chat.id,
|
||||||
it.messageId,
|
it.messageId,
|
||||||
replyInChat = it.reactedUser.id
|
replyInChatId = it.reactedUser.id
|
||||||
) {
|
) {
|
||||||
regular("Current reactions for message in reply:\n")
|
regular("Current reactions for message in reply:\n")
|
||||||
it.new.forEach {
|
it.new.forEach {
|
||||||
|
|||||||
@@ -1,4 +1,9 @@
|
|||||||
|
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.firstOf
|
import dev.inmo.micro_utils.coroutines.firstOf
|
||||||
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
import dev.inmo.micro_utils.fsm.common.State
|
import dev.inmo.micro_utils.fsm.common.State
|
||||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||||
@@ -24,6 +29,7 @@ import dev.inmo.tgbotapi.types.chat.ChatPermissions
|
|||||||
import dev.inmo.tgbotapi.types.chat.PublicChat
|
import dev.inmo.tgbotapi.types.chat.PublicChat
|
||||||
import dev.inmo.tgbotapi.types.chat.member.*
|
import dev.inmo.tgbotapi.types.chat.member.*
|
||||||
import dev.inmo.tgbotapi.types.commands.BotCommandScope
|
import dev.inmo.tgbotapi.types.commands.BotCommandScope
|
||||||
|
import dev.inmo.tgbotapi.types.message.abstracts.AccessibleMessage
|
||||||
import dev.inmo.tgbotapi.types.request.RequestId
|
import dev.inmo.tgbotapi.types.request.RequestId
|
||||||
import dev.inmo.tgbotapi.utils.*
|
import dev.inmo.tgbotapi.utils.*
|
||||||
import dev.inmo.tgbotapi.utils.mention
|
import dev.inmo.tgbotapi.utils.mention
|
||||||
@@ -49,6 +55,16 @@ sealed interface UserRetrievingStep : State {
|
|||||||
suspend fun main(args: Array<String>) {
|
suspend fun main(args: Array<String>) {
|
||||||
val botToken = args.first()
|
val botToken = args.first()
|
||||||
|
|
||||||
|
val isDebug = args.getOrNull(2) == "debug"
|
||||||
|
|
||||||
|
if (isDebug) {
|
||||||
|
setDefaultKSLog(
|
||||||
|
KSLog { level: LogLevel, tag: String?, message: Any, throwable: Throwable? ->
|
||||||
|
println(defaultMessageFormatter(level, tag, message, throwable))
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
val bot = telegramBot(botToken)
|
val bot = telegramBot(botToken)
|
||||||
|
|
||||||
val allowedAdmin = ChatId(args[1].toLong())
|
val allowedAdmin = ChatId(args[1].toLong())
|
||||||
@@ -185,14 +201,21 @@ suspend fun main(args: Array<String>) {
|
|||||||
) {
|
) {
|
||||||
onCommand(
|
onCommand(
|
||||||
"simple",
|
"simple",
|
||||||
initialFilter = { it.chat is PublicChat && it.fromUserMessageOrNull()?.user?.id == allowedAdmin }) {
|
initialFilter = { it.chat is PublicChat && it.fromUserMessageOrNull()?.user?.id == allowedAdmin }
|
||||||
|
) {
|
||||||
val replyMessage = it.replyTo
|
val replyMessage = it.replyTo
|
||||||
val userInReply = replyMessage?.fromUserMessageOrNull()?.user?.id ?: return@onCommand
|
val userInReply = replyMessage?.fromUserMessageOrNull()?.user?.id ?: return@onCommand
|
||||||
reply(
|
if (replyMessage is AccessibleMessage) {
|
||||||
replyMessage,
|
reply(
|
||||||
"Manage keyboard:",
|
replyMessage,
|
||||||
replyMarkup = buildCommonKeyboard(it.chat.id.toChatId(), userInReply) ?: return@onCommand
|
"Manage keyboard:",
|
||||||
)
|
replyMarkup = buildCommonKeyboard(it.chat.id.toChatId(), userInReply) ?: return@onCommand
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
reply(it) {
|
||||||
|
regular("Reply to somebody's message to get hist/her rights keyboard")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
onCommand(
|
onCommand(
|
||||||
"granular",
|
"granular",
|
||||||
@@ -204,11 +227,17 @@ suspend fun main(args: Array<String>) {
|
|||||||
val usernameInText = it.content.textSources.firstNotNullOfOrNull { it.mentionTextSourceOrNull() } ?.username
|
val usernameInText = it.content.textSources.firstNotNullOfOrNull { it.mentionTextSourceOrNull() } ?.username
|
||||||
val userInReply = replyMessage?.fromUserMessageOrNull()?.user?.id ?: return@onCommand
|
val userInReply = replyMessage?.fromUserMessageOrNull()?.user?.id ?: return@onCommand
|
||||||
|
|
||||||
reply(
|
if (replyMessage is AccessibleMessage) {
|
||||||
replyMessage,
|
reply(
|
||||||
"Manage keyboard:",
|
replyMessage,
|
||||||
replyMarkup = buildGranularKeyboard(it.chat.id.toChatId(), userInReply) ?: return@onCommand
|
"Manage keyboard:",
|
||||||
)
|
replyMarkup = buildGranularKeyboard(it.chat.id.toChatId(), userInReply) ?: return@onCommand
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
reply(it) {
|
||||||
|
regular("Reply to somebody's message to get hist/her rights keyboard")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMessageDataCallbackQuery(
|
onMessageDataCallbackQuery(
|
||||||
@@ -503,5 +532,9 @@ suspend fun main(args: Array<String>) {
|
|||||||
BotCommand("rights_in_channel", "Trigger granular keyboard. Use with reply to user"),
|
BotCommand("rights_in_channel", "Trigger granular keyboard. Use with reply to user"),
|
||||||
scope = BotCommandScope.AllGroupChats
|
scope = BotCommandScope.AllGroupChats
|
||||||
)
|
)
|
||||||
|
|
||||||
|
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||||
|
println(it)
|
||||||
|
}
|
||||||
}.join()
|
}.join()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,54 +27,87 @@ suspend fun main(args: Array<String>) {
|
|||||||
val requestIdUserPremium = RequestId(3)
|
val requestIdUserPremium = RequestId(3)
|
||||||
val requestIdBot = RequestId(4)
|
val requestIdBot = RequestId(4)
|
||||||
|
|
||||||
val requestIdAnyChat = RequestId(5)
|
val requestIdUsersOrBots = RequestId(5)
|
||||||
val requestIdChannel = RequestId(6)
|
val requestIdUsersNonPremium = RequestId(6)
|
||||||
val requestIdPublicChannel = RequestId(7)
|
val requestIdUsersAny = RequestId(7)
|
||||||
val requestIdPrivateChannel = RequestId(8)
|
val requestIdUsersPremium = RequestId(8)
|
||||||
val requestIdChannelUserOwner = RequestId(9)
|
val requestIdBots = RequestId(9)
|
||||||
|
|
||||||
val requestIdGroup = RequestId(10)
|
val requestIdAnyChat = RequestId(10)
|
||||||
val requestIdPublicGroup = RequestId(11)
|
val requestIdChannel = RequestId(11)
|
||||||
val requestIdPrivateGroup = RequestId(12)
|
val requestIdPublicChannel = RequestId(12)
|
||||||
val requestIdGroupUserOwner = RequestId(13)
|
val requestIdPrivateChannel = RequestId(13)
|
||||||
|
val requestIdChannelUserOwner = RequestId(14)
|
||||||
|
|
||||||
val requestIdForum = RequestId(14)
|
val requestIdGroup = RequestId(15)
|
||||||
val requestIdPublicForum = RequestId(15)
|
val requestIdPublicGroup = RequestId(16)
|
||||||
val requestIdPrivateForum = RequestId(16)
|
val requestIdPrivateGroup = RequestId(17)
|
||||||
val requestIdForumUserOwner = RequestId(17)
|
val requestIdGroupUserOwner = RequestId(18)
|
||||||
|
|
||||||
|
val requestIdForum = RequestId(19)
|
||||||
|
val requestIdPublicForum = RequestId(20)
|
||||||
|
val requestIdPrivateForum = RequestId(21)
|
||||||
|
val requestIdForumUserOwner = RequestId(22)
|
||||||
|
|
||||||
val keyboard = replyKeyboard(
|
val keyboard = replyKeyboard(
|
||||||
resizeKeyboard = true,
|
resizeKeyboard = true,
|
||||||
) {
|
) {
|
||||||
|
row {
|
||||||
|
requestUserOrBotButton(
|
||||||
|
"\uD83D\uDC64/\uD83E\uDD16 (1)",
|
||||||
|
requestIdUserOrBot
|
||||||
|
)
|
||||||
|
}
|
||||||
|
row {
|
||||||
|
requestUserButton(
|
||||||
|
"\uD83D\uDC64☆ (1)",
|
||||||
|
requestIdUserNonPremium,
|
||||||
|
premiumUser = false
|
||||||
|
)
|
||||||
|
requestUserButton(
|
||||||
|
"\uD83D\uDC64 (1)",
|
||||||
|
requestIdUserAny,
|
||||||
|
premiumUser = null
|
||||||
|
)
|
||||||
|
requestUserButton(
|
||||||
|
"\uD83D\uDC64★ (1)",
|
||||||
|
requestIdUserPremium,
|
||||||
|
premiumUser = true
|
||||||
|
)
|
||||||
|
requestBotButton(
|
||||||
|
"\uD83E\uDD16 (1)",
|
||||||
|
requestIdBot
|
||||||
|
)
|
||||||
|
}
|
||||||
row {
|
row {
|
||||||
requestUsersOrBotsButton(
|
requestUsersOrBotsButton(
|
||||||
"\uD83D\uDC64/\uD83E\uDD16",
|
"\uD83D\uDC64/\uD83E\uDD16",
|
||||||
requestIdUserOrBot,
|
requestIdUsersOrBots,
|
||||||
maxCount = keyboardButtonRequestUserLimit.last
|
maxCount = keyboardButtonRequestUserLimit.last
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
row {
|
row {
|
||||||
requestUsersButton(
|
requestUsersButton(
|
||||||
"\uD83D\uDC64☆",
|
"\uD83D\uDC64☆",
|
||||||
requestIdUserNonPremium,
|
requestIdUsersNonPremium,
|
||||||
premiumUser = false,
|
premiumUser = false,
|
||||||
maxCount = keyboardButtonRequestUserLimit.last
|
maxCount = keyboardButtonRequestUserLimit.last
|
||||||
)
|
)
|
||||||
requestUsersButton(
|
requestUsersButton(
|
||||||
"\uD83D\uDC64",
|
"\uD83D\uDC64",
|
||||||
requestIdUserAny,
|
requestIdUsersAny,
|
||||||
premiumUser = null,
|
premiumUser = null,
|
||||||
maxCount = keyboardButtonRequestUserLimit.last
|
maxCount = keyboardButtonRequestUserLimit.last
|
||||||
)
|
)
|
||||||
requestUsersButton(
|
requestUsersButton(
|
||||||
"\uD83D\uDC64★",
|
"\uD83D\uDC64★",
|
||||||
requestIdUserPremium,
|
requestIdUsersPremium,
|
||||||
premiumUser = true,
|
premiumUser = true,
|
||||||
maxCount = keyboardButtonRequestUserLimit.last
|
maxCount = keyboardButtonRequestUserLimit.last
|
||||||
)
|
)
|
||||||
requestBotsButton(
|
requestBotsButton(
|
||||||
"\uD83E\uDD16",
|
"\uD83E\uDD16",
|
||||||
requestIdBot,
|
requestIdBots,
|
||||||
maxCount = keyboardButtonRequestUserLimit.last
|
maxCount = keyboardButtonRequestUserLimit.last
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ org.gradle.jvmargs=-Xmx2344m
|
|||||||
|
|
||||||
|
|
||||||
kotlin_version=1.9.22
|
kotlin_version=1.9.22
|
||||||
telegram_bot_api_version=10.0.0-branch_10.0.0-build2050
|
telegram_bot_api_version=10.1.0
|
||||||
micro_utils_version=0.20.25
|
micro_utils_version=0.20.34
|
||||||
serialization_version=1.6.2
|
serialization_version=1.6.2
|
||||||
ktor_version=2.3.7
|
ktor_version=2.3.8
|
||||||
|
|||||||
@@ -45,3 +45,5 @@ include ":InlineQueriesBot"
|
|||||||
include ":ReactionsInfoBot"
|
include ":ReactionsInfoBot"
|
||||||
|
|
||||||
include ":LinkPreviewsBot"
|
include ":LinkPreviewsBot"
|
||||||
|
|
||||||
|
include ":BoostsInfoBot"
|
||||||
|
|||||||
Reference in New Issue
Block a user