mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-12-26 16:15:39 +00:00
Compare commits
1 Commits
10.1.0
...
cde935eef9
| Author | SHA1 | Date | |
|---|---|---|---|
| cde935eef9 |
@@ -1,9 +0,0 @@
|
|||||||
# UserChatShared
|
|
||||||
|
|
||||||
Showing info about boosts
|
|
||||||
|
|
||||||
## Launch
|
|
||||||
|
|
||||||
```bash
|
|
||||||
../gradlew run --args="BOT_TOKEN"
|
|
||||||
```
|
|
||||||
@@ -1,21 +0,0 @@
|
|||||||
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"
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
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,
|
||||||
replyInChatId = it.reactedUser.id
|
replyInChat = 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,9 +1,4 @@
|
|||||||
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
|
||||||
@@ -29,7 +24,6 @@ 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
|
||||||
@@ -55,16 +49,6 @@ 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())
|
||||||
@@ -201,21 +185,14 @@ 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
|
||||||
if (replyMessage is AccessibleMessage) {
|
reply(
|
||||||
reply(
|
replyMessage,
|
||||||
replyMessage,
|
"Manage keyboard:",
|
||||||
"Manage keyboard:",
|
replyMarkup = buildCommonKeyboard(it.chat.id.toChatId(), userInReply) ?: return@onCommand
|
||||||
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",
|
||||||
@@ -227,17 +204,11 @@ 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
|
||||||
|
|
||||||
if (replyMessage is AccessibleMessage) {
|
reply(
|
||||||
reply(
|
replyMessage,
|
||||||
replyMessage,
|
"Manage keyboard:",
|
||||||
"Manage keyboard:",
|
replyMarkup = buildGranularKeyboard(it.chat.id.toChatId(), userInReply) ?: return@onCommand
|
||||||
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(
|
||||||
@@ -532,9 +503,5 @@ 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()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,11 +7,14 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPoll
|
|||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onChatShared
|
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.behaviour_builder.triggers_handling.onCommand
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onUserShared
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onUserShared
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onUsersShared
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.replyKeyboard
|
||||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.requestBotButton
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.requestChatButton
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.requestGroupButton
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.requestUserButton
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.requestUserOrBotButton
|
||||||
import dev.inmo.tgbotapi.types.BotCommand
|
import dev.inmo.tgbotapi.types.BotCommand
|
||||||
import dev.inmo.tgbotapi.types.chat.PrivateChat
|
import dev.inmo.tgbotapi.types.chat.PrivateChat
|
||||||
import dev.inmo.tgbotapi.types.keyboardButtonRequestUserLimit
|
|
||||||
import dev.inmo.tgbotapi.types.message.textsources.mention
|
import dev.inmo.tgbotapi.types.message.textsources.mention
|
||||||
import dev.inmo.tgbotapi.types.request.RequestId
|
import dev.inmo.tgbotapi.types.request.RequestId
|
||||||
import dev.inmo.tgbotapi.utils.row
|
import dev.inmo.tgbotapi.utils.row
|
||||||
@@ -27,88 +30,50 @@ suspend fun main(args: Array<String>) {
|
|||||||
val requestIdUserPremium = RequestId(3)
|
val requestIdUserPremium = RequestId(3)
|
||||||
val requestIdBot = RequestId(4)
|
val requestIdBot = RequestId(4)
|
||||||
|
|
||||||
val requestIdUsersOrBots = RequestId(5)
|
val requestIdAnyChat = RequestId(5)
|
||||||
val requestIdUsersNonPremium = RequestId(6)
|
val requestIdChannel = RequestId(6)
|
||||||
val requestIdUsersAny = RequestId(7)
|
val requestIdPublicChannel = RequestId(7)
|
||||||
val requestIdUsersPremium = RequestId(8)
|
val requestIdPrivateChannel = RequestId(8)
|
||||||
val requestIdBots = RequestId(9)
|
val requestIdChannelUserOwner = RequestId(9)
|
||||||
|
|
||||||
val requestIdAnyChat = RequestId(10)
|
val requestIdGroup = RequestId(10)
|
||||||
val requestIdChannel = RequestId(11)
|
val requestIdPublicGroup = RequestId(11)
|
||||||
val requestIdPublicChannel = RequestId(12)
|
val requestIdPrivateGroup = RequestId(12)
|
||||||
val requestIdPrivateChannel = RequestId(13)
|
val requestIdGroupUserOwner = RequestId(13)
|
||||||
val requestIdChannelUserOwner = RequestId(14)
|
|
||||||
|
|
||||||
val requestIdGroup = RequestId(15)
|
val requestIdForum = RequestId(14)
|
||||||
val requestIdPublicGroup = RequestId(16)
|
val requestIdPublicForum = RequestId(15)
|
||||||
val requestIdPrivateGroup = RequestId(17)
|
val requestIdPrivateForum = RequestId(16)
|
||||||
val requestIdGroupUserOwner = RequestId(18)
|
val requestIdForumUserOwner = RequestId(17)
|
||||||
|
|
||||||
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 {
|
row {
|
||||||
requestUserOrBotButton(
|
requestUserOrBotButton(
|
||||||
"\uD83D\uDC64/\uD83E\uDD16 (1)",
|
"\uD83D\uDC64/\uD83E\uDD16",
|
||||||
requestIdUserOrBot
|
requestIdUserOrBot
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
row {
|
row {
|
||||||
requestUserButton(
|
requestUserButton(
|
||||||
"\uD83D\uDC64☆ (1)",
|
"\uD83D\uDC64☆",
|
||||||
requestIdUserNonPremium,
|
requestIdUserNonPremium,
|
||||||
premiumUser = false
|
premiumUser = false
|
||||||
)
|
)
|
||||||
requestUserButton(
|
requestUserButton(
|
||||||
"\uD83D\uDC64 (1)",
|
"\uD83D\uDC64",
|
||||||
requestIdUserAny,
|
requestIdUserAny,
|
||||||
premiumUser = null
|
premiumUser = null
|
||||||
)
|
)
|
||||||
requestUserButton(
|
requestUserButton(
|
||||||
"\uD83D\uDC64★ (1)",
|
"\uD83D\uDC64★",
|
||||||
requestIdUserPremium,
|
requestIdUserPremium,
|
||||||
premiumUser = true
|
premiumUser = true
|
||||||
)
|
)
|
||||||
requestBotButton(
|
requestBotButton(
|
||||||
"\uD83E\uDD16 (1)",
|
|
||||||
requestIdBot
|
|
||||||
)
|
|
||||||
}
|
|
||||||
row {
|
|
||||||
requestUsersOrBotsButton(
|
|
||||||
"\uD83D\uDC64/\uD83E\uDD16",
|
|
||||||
requestIdUsersOrBots,
|
|
||||||
maxCount = keyboardButtonRequestUserLimit.last
|
|
||||||
)
|
|
||||||
}
|
|
||||||
row {
|
|
||||||
requestUsersButton(
|
|
||||||
"\uD83D\uDC64☆",
|
|
||||||
requestIdUsersNonPremium,
|
|
||||||
premiumUser = false,
|
|
||||||
maxCount = keyboardButtonRequestUserLimit.last
|
|
||||||
)
|
|
||||||
requestUsersButton(
|
|
||||||
"\uD83D\uDC64",
|
|
||||||
requestIdUsersAny,
|
|
||||||
premiumUser = null,
|
|
||||||
maxCount = keyboardButtonRequestUserLimit.last
|
|
||||||
)
|
|
||||||
requestUsersButton(
|
|
||||||
"\uD83D\uDC64★",
|
|
||||||
requestIdUsersPremium,
|
|
||||||
premiumUser = true,
|
|
||||||
maxCount = keyboardButtonRequestUserLimit.last
|
|
||||||
)
|
|
||||||
requestBotsButton(
|
|
||||||
"\uD83E\uDD16",
|
"\uD83E\uDD16",
|
||||||
requestIdBots,
|
requestIdBot
|
||||||
maxCount = keyboardButtonRequestUserLimit.last
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
row {
|
row {
|
||||||
@@ -199,26 +164,25 @@ suspend fun main(args: Array<String>) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
onUsersShared {
|
onUserShared {
|
||||||
it.chatEvent.userIds.forEach { userId ->
|
val userId = it.chatEvent.userId
|
||||||
val userInfo = runCatchingSafely { getChat(userId) }.getOrNull()
|
val userInfo = runCatchingSafely { getChat(userId) }.getOrNull()
|
||||||
reply(
|
reply(
|
||||||
it,
|
it,
|
||||||
) {
|
) {
|
||||||
+"You have shared "
|
+"You have shared "
|
||||||
+mention(
|
+mention(
|
||||||
when (it.chatEvent.requestId) {
|
when (it.chatEvent.requestId) {
|
||||||
requestIdUserOrBot -> "user or bot"
|
requestIdUserOrBot -> "user or bot"
|
||||||
requestIdUserNonPremium -> "non premium user"
|
requestIdUserNonPremium -> "non premium user"
|
||||||
requestIdUserAny -> "any user"
|
requestIdUserAny -> "any user"
|
||||||
requestIdUserPremium -> "premium user"
|
requestIdUserPremium -> "premium user"
|
||||||
requestIdBot -> "bot"
|
requestIdBot -> "bot"
|
||||||
else -> "somebody O.o"
|
else -> "somebody O.o"
|
||||||
},
|
},
|
||||||
userId
|
userId
|
||||||
)
|
)
|
||||||
+" (user info: $userInfo; user id: $userId)"
|
+" (user info: $userInfo; user id: $userId)"
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ org.gradle.jvmargs=-Xmx2344m
|
|||||||
|
|
||||||
|
|
||||||
kotlin_version=1.9.22
|
kotlin_version=1.9.22
|
||||||
telegram_bot_api_version=10.1.0
|
telegram_bot_api_version=10.0.0-branch_10.0.0-build2050
|
||||||
micro_utils_version=0.20.34
|
micro_utils_version=0.20.25
|
||||||
serialization_version=1.6.2
|
serialization_version=1.6.2
|
||||||
ktor_version=2.3.8
|
ktor_version=2.3.7
|
||||||
|
|||||||
@@ -45,5 +45,3 @@ include ":InlineQueriesBot"
|
|||||||
include ":ReactionsInfoBot"
|
include ":ReactionsInfoBot"
|
||||||
|
|
||||||
include ":LinkPreviewsBot"
|
include ":LinkPreviewsBot"
|
||||||
|
|
||||||
include ":BoostsInfoBot"
|
|
||||||
|
|||||||
Reference in New Issue
Block a user