mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-12-05 13:55:38 +00:00
Compare commits
160 Commits
658bb6356e
...
6.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 4e0fb1c137 | |||
| d8c90ef377 | |||
| cb84fd0884 | |||
| 4379862c78 | |||
|
|
ac1d812db0 | ||
| f52590868c | |||
| 51c300c734 | |||
| f6082cff30 | |||
| a40c16fe05 | |||
| a7fe62f4af | |||
| b9c745a21e | |||
| 1c2b068a94 | |||
| 51c2cb1b0e | |||
| cfd4e2fcd5 | |||
|
|
76ceeac757 | ||
| 340de11b0a | |||
| 68a59ca5c8 | |||
|
|
cdb8581318 | ||
|
|
8b3a2ac1ed | ||
| a5b925fc59 | |||
| 9ec9f7a68c | |||
| 0f2829945f | |||
| 4eb80ea53c | |||
| 17cff21847 | |||
| 431069d190 | |||
|
|
fdbac78603 | ||
| da73acd379 | |||
|
|
6e3880f152 | ||
| 1ede6e58e6 | |||
| 0e46f176fb | |||
|
|
2bd449b8b8 | ||
| 82f9da0529 | |||
| 78b7d468f2 | |||
|
|
08059f8174 | ||
|
|
16766046d7 | ||
| 91ea20a269 | |||
| 11e280d177 | |||
| a8d4a307ef | |||
| 2bd2328a38 | |||
| 139de35db9 | |||
|
|
5dd22e1da2 | ||
| 4186ab8270 | |||
| 5aa69d7990 | |||
|
|
df952c69b2 | ||
| 9a03a02bac | |||
| 0e7c050e9e | |||
|
|
bdec902b58 | ||
|
|
cc3c87590d | ||
| 910f892b89 | |||
| 8232cb4d62 | |||
| 3b26971152 | |||
| c0019bcbf8 | |||
| c3dcb4d738 | |||
| 6a9921d4bd | |||
| 33b14f320c | |||
| 50ad281132 | |||
| 6abfc3d369 | |||
| f73620afec | |||
| a3d5112c83 | |||
|
|
23abae07b7 | ||
| c5a9f657cf | |||
|
|
51f7715915 | ||
|
|
bdfb900ce3 | ||
| 27790a2576 | |||
| e722055871 | |||
| 29e1552618 | |||
| 7ef942a2b4 | |||
| fdd4dfdbcf | |||
|
|
ba88205249 | ||
| 9a38fe51f9 | |||
|
|
c172bd1fa7 | ||
| 7392e85f1b | |||
|
|
f2c7fd79d9 | ||
| 9d77b61ea3 | |||
|
|
d567b1382b | ||
| 557d4ad3ca | |||
|
|
f070fb804b | ||
| 53de520c3a | |||
| f164ba901d | |||
|
|
2a04b4979d | ||
| 4a0279c89e | |||
|
|
fcef9f1f64 | ||
|
|
33e191816a | ||
| e0b707ceba | |||
| 115c76a9d4 | |||
| 0ebfb3c44a | |||
| 9fa3690db6 | |||
| a8c5d403c6 | |||
| 0c5186a37d | |||
| 7e9968ced9 | |||
| f1e8ed88a8 | |||
| 068dc79ac8 | |||
| 36273adfd1 | |||
| bca2ae905b | |||
| 19a713a3e3 | |||
| e039f90961 | |||
| 21692d16ca | |||
| d547dce2ab | |||
| 9bfe88a79c | |||
|
|
bfa327acf3 | ||
| 88a031b05a | |||
| 20e942b2ac | |||
|
|
3e20cbd22c | ||
| bcd35de038 | |||
|
|
2aec45d453 | ||
| ee55378e7a | |||
| b402c7b6e7 | |||
|
|
32b7c7b9a4 | ||
| 0b2d6b20de | |||
|
|
182ee7a865 | ||
| 0652a95d11 | |||
|
|
f6066c60c0 | ||
| 2fa340292c | |||
| 7d94007905 | |||
|
|
9638174d48 | ||
|
|
18bacaea2e | ||
| 0f2b3760dd | |||
| 730923f55c | |||
| 9cf8bd9f28 | |||
| 370fa45dba | |||
|
|
72e7a73e40 | ||
| 10dd9bd851 | |||
| 0217f97014 | |||
| 4ae700b58a | |||
|
|
804fb1e5ee | ||
| d443c39813 | |||
|
|
cd96547d15 | ||
| f107b4144d | |||
| 39f4196b76 | |||
|
|
22b9776c26 | ||
| 2f8f6f3169 | |||
| f2b6a50cb5 | |||
| acd602d6fa | |||
| fdb80bf471 | |||
|
|
78c58db4b7 | ||
| edd6399bcc | |||
|
|
9513e77ba3 | ||
|
|
ac75f6487e | ||
| 07750a71c0 | |||
| 0bf7e33df3 | |||
| 7fb308ea4b | |||
|
|
75b403ac98 | ||
| 5976e37046 | |||
|
|
c8a94496c7 | ||
| f6550bd401 | |||
| 389d96f323 | |||
| 803c5fd664 | |||
| b6eb4fe134 | |||
| 9a4bd55ef6 | |||
| 4dac411693 | |||
| d386d50f1c | |||
|
|
1b846a6383 | ||
| 568b845890 | |||
|
|
3ebca4a058 | ||
| 2b9af2f667 | |||
| 16410debff | |||
| a18f22fe8f | |||
| 0489f217b3 | |||
| e772cfda3b | |||
| 9d42385662 |
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@@ -8,9 +8,9 @@ jobs:
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- name: Set up JDK 1.8
|
||||
- name: Set up JDK 11
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 1.8
|
||||
java-version: 11
|
||||
- name: Build with Gradle
|
||||
run: ./gradlew build
|
||||
|
||||
9
ChatAvatarSetter/README.md
Normal file
9
ChatAvatarSetter/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# ChatAvatarSetter
|
||||
|
||||
This bot will set the chat avatar based on the image sent to bot
|
||||
|
||||
## Launch
|
||||
|
||||
```bash
|
||||
../gradlew run --args="BOT_TOKEN"
|
||||
```
|
||||
21
ChatAvatarSetter/build.gradle
Normal file
21
ChatAvatarSetter/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="ChatAvatarSetterKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
||||
36
ChatAvatarSetter/src/main/kotlin/ChatAvatarSetter.kt
Normal file
36
ChatAvatarSetter/src/main/kotlin/ChatAvatarSetter.kt
Normal file
@@ -0,0 +1,36 @@
|
||||
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.modify.setChatPhoto
|
||||
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onPhoto
|
||||
import dev.inmo.tgbotapi.extensions.utils.*
|
||||
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
suspend fun main(args: Array<String>) {
|
||||
val bot = telegramBot(args.first())
|
||||
|
||||
bot.buildBehaviourWithLongPolling(scope = CoroutineScope(Dispatchers.IO)) {
|
||||
onPhoto {
|
||||
val bytes = downloadFile(it.content)
|
||||
runCatchingSafely {
|
||||
setChatPhoto(
|
||||
it.chat.id,
|
||||
bytes.asMultipartFile("sample.jpg")
|
||||
)
|
||||
}.onSuccess { b ->
|
||||
if (b) {
|
||||
reply(it, "Done")
|
||||
} else {
|
||||
reply(it, "Something went wrong")
|
||||
}
|
||||
}.onFailure { e ->
|
||||
e.printStackTrace()
|
||||
|
||||
reply(it, "Something went wrong (see logs)")
|
||||
}
|
||||
}
|
||||
}.join()
|
||||
}
|
||||
9
DeepLinksBot/README.md
Normal file
9
DeepLinksBot/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# DeepLinksBot
|
||||
|
||||
This bot will send you deeplink to this bot when you send some text message and react on the `start` button
|
||||
|
||||
## Launch
|
||||
|
||||
```bash
|
||||
../gradlew run --args="BOT_TOKEN"
|
||||
```
|
||||
21
DeepLinksBot/build.gradle
Normal file
21
DeepLinksBot/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="DeepLinksBotKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
||||
39
DeepLinksBot/src/main/kotlin/DeepLinksBot.kt
Normal file
39
DeepLinksBot/src/main/kotlin/DeepLinksBot.kt
Normal file
@@ -0,0 +1,39 @@
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelySkippingExceptions
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitDeepLinks
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.makeTelegramDeepLink
|
||||
import dev.inmo.tgbotapi.types.message.textsources.BotCommandTextSource
|
||||
|
||||
/**
|
||||
* This bot will send you deeplink to this bot when you send some text message and react on the `start` button
|
||||
*/
|
||||
suspend fun main(vararg args: String) {
|
||||
val botToken = args.first()
|
||||
|
||||
telegramBotWithBehaviourAndLongPolling(botToken) {
|
||||
val me = bot.getMe()
|
||||
println(me)
|
||||
|
||||
onText(
|
||||
initialFilter = { it.content.textSources.none { it is BotCommandTextSource } } // excluding messages with commands
|
||||
) {
|
||||
reply(it, makeTelegramDeepLink(me.username, it.content.text))
|
||||
}
|
||||
|
||||
onCommand("start", requireOnlyCommandInMessage = true) { // handling of `start` without args
|
||||
reply(it, "Hi :) Send me any text and I will try hard to create deeplink for you")
|
||||
}
|
||||
onDeepLink { (it, deepLink) ->
|
||||
reply(it, "Ok, I got deep link \"${deepLink}\" in trigger")
|
||||
}
|
||||
waitDeepLinks().subscribeSafelyWithoutExceptions(this) { (it, deepLink) ->
|
||||
reply(it, "Ok, I got deep link \"${deepLink}\" in waiter")
|
||||
println(triggersHolder.handleableCommandsHolder.handleable)
|
||||
}
|
||||
}.second.join()
|
||||
}
|
||||
@@ -1,32 +1,56 @@
|
||||
import dev.inmo.micro_utils.coroutines.AccumulatorFlow
|
||||
import dev.inmo.micro_utils.fsm.common.State
|
||||
import dev.inmo.tgbotapi.extensions.api.send.send
|
||||
import dev.inmo.tgbotapi.extensions.api.send.sendMessage
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.extensions.parseCommandsWithParams
|
||||
import dev.inmo.tgbotapi.extensions.utils.extensions.sameThread
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.IdChatIdentifier
|
||||
import dev.inmo.tgbotapi.types.MessageThreadId
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||
import dev.inmo.tgbotapi.utils.botCommand
|
||||
import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.filter
|
||||
import kotlinx.coroutines.flow.first
|
||||
|
||||
sealed interface BotState : State
|
||||
data class ExpectContentOrStopState(override val context: ChatId, val sourceMessage: CommonMessage<TextContent>) : BotState
|
||||
data class StopState(override val context: ChatId) : BotState
|
||||
data class ExpectContentOrStopState(override val context: IdChatIdentifier, val sourceMessage: CommonMessage<TextContent>) : BotState
|
||||
data class StopState(override val context: IdChatIdentifier) : BotState
|
||||
|
||||
suspend fun main(args: Array<String>) {
|
||||
val botToken = args.first()
|
||||
|
||||
telegramBotWithBehaviourAndFSMAndStartLongPolling<BotState>(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||
strictlyOn<ExpectContentOrStopState> {
|
||||
sendMessage(
|
||||
it.context,
|
||||
buildEntities {
|
||||
+"Send me some content or " + botCommand("stop") + " if you want to stop sending"
|
||||
telegramBotWithBehaviourAndFSMAndStartLongPolling<BotState>(
|
||||
botToken,
|
||||
CoroutineScope(Dispatchers.IO),
|
||||
onStateHandlingErrorHandler = { state, e ->
|
||||
when (state) {
|
||||
is ExpectContentOrStopState -> {
|
||||
println("Thrown error on ExpectContentOrStopState")
|
||||
}
|
||||
)
|
||||
is StopState -> {
|
||||
println("Thrown error on StopState")
|
||||
}
|
||||
}
|
||||
e.printStackTrace()
|
||||
state
|
||||
}
|
||||
) {
|
||||
strictlyOn<ExpectContentOrStopState> {
|
||||
send(
|
||||
it.context,
|
||||
) {
|
||||
+"Send me some content or " + botCommand("stop") + " if you want to stop sending"
|
||||
}
|
||||
|
||||
val contentMessage = waitContentMessage().first()
|
||||
val contentMessage = waitAnyContentMessage().filter { message ->
|
||||
message.sameThread(it.sourceMessage)
|
||||
}.first()
|
||||
val content = contentMessage.content
|
||||
|
||||
when {
|
||||
@@ -38,12 +62,14 @@ suspend fun main(args: Array<String>) {
|
||||
}
|
||||
}
|
||||
strictlyOn<StopState> {
|
||||
sendMessage(it.context, "You have stopped sending of content")
|
||||
send(it.context) { +"You have stopped sending of content" }
|
||||
|
||||
null
|
||||
}
|
||||
|
||||
command("start") {
|
||||
command(
|
||||
"start"
|
||||
) {
|
||||
startChain(ExpectContentOrStopState(it.chat.id, it))
|
||||
}
|
||||
}.second.join()
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
||||
import dev.inmo.tgbotapi.extensions.api.get.getFileAdditionalInfo
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviour
|
||||
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.onMedia
|
||||
import dev.inmo.tgbotapi.utils.filenameFromUrl
|
||||
@@ -15,17 +15,22 @@ import java.io.File
|
||||
*/
|
||||
suspend fun main(args: Array<String>) {
|
||||
val botToken = args.first()
|
||||
val directoryOrFile = args.getOrNull(1) ?.let { File(it) } ?: File("")
|
||||
val directoryOrFile = args.getOrNull(1) ?.let { File(it) } ?: File("/tmp/")
|
||||
directoryOrFile.mkdirs()
|
||||
|
||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||
onCommand("start") {
|
||||
reply(it, "Send me any media (like photo or video) to download it")
|
||||
}
|
||||
onMedia(initialFilter = null) {
|
||||
val pathedFile = bot.getFileAdditionalInfo(it.content.media)
|
||||
val file = File(directoryOrFile, pathedFile.filePath.filenameFromUrl).apply {
|
||||
createNewFile()
|
||||
writeBytes(bot.downloadFile(pathedFile))
|
||||
val outFile = File(directoryOrFile, pathedFile.filePath.filenameFromUrl)
|
||||
runCatching {
|
||||
bot.downloadFile(it.content.media, outFile)
|
||||
}.onFailure {
|
||||
it.printStackTrace()
|
||||
}
|
||||
reply(it, "Saved to ${file.absolutePath}")
|
||||
reply(it, "Saved to ${outFile.absolutePath}")
|
||||
}
|
||||
onContentMessage { println(it) }
|
||||
}.second.join()
|
||||
|
||||
@@ -1,9 +1,15 @@
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
||||
import dev.inmo.tgbotapi.types.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.makeLink
|
||||
import dev.inmo.tgbotapi.types.chat.CommonBot
|
||||
import dev.inmo.tgbotapi.types.chat.CommonUser
|
||||
import dev.inmo.tgbotapi.types.chat.ExtendedBot
|
||||
import dev.inmo.tgbotapi.types.message.*
|
||||
import dev.inmo.tgbotapi.utils.buildEntities
|
||||
import dev.inmo.tgbotapi.utils.code
|
||||
import dev.inmo.tgbotapi.utils.link
|
||||
import dev.inmo.tgbotapi.utils.regular
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
/**
|
||||
@@ -14,28 +20,41 @@ suspend fun main(vararg args: String) {
|
||||
val botToken = args.first()
|
||||
|
||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||
onContentMessage(subcontextUpdatesFilter = { _, _ -> true }) {
|
||||
onContentMessage {
|
||||
val toAnswer = buildEntities {
|
||||
when (val forwardInfo = it.forwardInfo) {
|
||||
null -> +"There is no forward info"
|
||||
is AnonymousForwardInfo -> {
|
||||
is ForwardInfo.ByAnonymous -> {
|
||||
regular("Anonymous user which signed as \"") + code(forwardInfo.senderName) + "\""
|
||||
}
|
||||
is UserForwardInfo -> {
|
||||
is ForwardInfo.ByUser -> {
|
||||
val user = forwardInfo.from
|
||||
when (user) {
|
||||
is CommonUser -> regular("User ")
|
||||
is CommonUser -> {
|
||||
if (user.isPremium) {
|
||||
regular("Premium user ")
|
||||
} else {
|
||||
regular("User ")
|
||||
}
|
||||
}
|
||||
|
||||
is CommonBot,
|
||||
is ExtendedBot -> regular("Bot ")
|
||||
} + code(user.id.chatId.toString()) + " (${user.firstName} ${user.lastName}: ${user.username ?.username ?: "Without username"})"
|
||||
} + code(user.id.chatId.toString()) + " (${user.firstName} ${user.lastName}: ${user.username?.username ?: "Without username"})"
|
||||
}
|
||||
is ForwardFromChannelInfo -> regular("Channel (") + code((forwardInfo.channelChat).title) + ")"
|
||||
is ForwardFromSupergroupInfo -> regular("Supergroup (") + code((forwardInfo.group).title) + ")"
|
||||
is ForwardInfo.PublicChat.FromChannel -> {
|
||||
regular("Channel (") + (forwardInfo.channelChat.username ?.let {
|
||||
link(
|
||||
forwardInfo.channelChat.title,
|
||||
makeLink(it)
|
||||
)
|
||||
} ?: code(forwardInfo.channelChat.title)) + ")"
|
||||
}
|
||||
is ForwardInfo.PublicChat.FromSupergroup -> regular("Supergroup (") + code(forwardInfo.group.title) + ")"
|
||||
is ForwardInfo.PublicChat.SentByChannel -> regular("Sent by channel (") + code(forwardInfo.channelChat.title) + ")"
|
||||
}
|
||||
}
|
||||
reply(it, toAnswer)
|
||||
coroutineContext.job.invokeOnCompletion { println("completance of onContentMessage") }
|
||||
}
|
||||
coroutineContext.job.invokeOnCompletion { println("Completed :)") }
|
||||
}.second.join()
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ buildscript {
|
||||
apply plugin: 'kotlin'
|
||||
apply plugin: 'application'
|
||||
|
||||
mainClassName="HelloBotKt"
|
||||
mainClassName="GetMeBotKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
|
||||
@@ -1,5 +1,16 @@
|
||||
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.closeForumTopic
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.createForumTopic
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.deleteForumTopic
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.reopenForumTopic
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onForumTopicClosed
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.CustomEmojiId
|
||||
import dev.inmo.tgbotapi.types.ForumTopic
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
/**
|
||||
* This is one of the most easiest bot - it will just print information about itself
|
||||
@@ -10,4 +21,4 @@ suspend fun main(vararg args: String) {
|
||||
val bot = telegramBot(botToken)
|
||||
|
||||
println(bot.getMe())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,41 +1,61 @@
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.api.send.sendTextMessage
|
||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
||||
import dev.inmo.tgbotapi.extensions.utils.extensions.raw.sender_chat
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.linkMarkdownV2
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.textMentionMarkdownV2
|
||||
import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2
|
||||
import dev.inmo.tgbotapi.types.User
|
||||
import dev.inmo.tgbotapi.types.chat.abstracts.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.ifChannelChat
|
||||
import dev.inmo.tgbotapi.extensions.utils.ifFromChannelGroupContentMessage
|
||||
import dev.inmo.tgbotapi.types.chat.*
|
||||
import dev.inmo.tgbotapi.types.chat.GroupChat
|
||||
import dev.inmo.tgbotapi.types.chat.PrivateChat
|
||||
import dev.inmo.tgbotapi.types.chat.SupergroupChat
|
||||
import dev.inmo.tgbotapi.types.message.MarkdownV2
|
||||
import dev.inmo.tgbotapi.utils.PreviewFeature
|
||||
import dev.inmo.tgbotapi.utils.extensions.escapeMarkdownV2Common
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
/**
|
||||
* The main purpose of this bot is just to answer "Oh, hi, " and add user mention here
|
||||
*/
|
||||
@OptIn(PreviewFeature::class)
|
||||
suspend fun main(vararg args: String) {
|
||||
val botToken = args.first()
|
||||
|
||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||
onContentMessage { message ->
|
||||
val chat = message.chat
|
||||
if (chat is ChannelChat) {
|
||||
val answer = "Hi everybody in this channel \"${chat.title}\""
|
||||
sendTextMessage(chat, answer, MarkdownV2)
|
||||
return@onContentMessage
|
||||
}
|
||||
val answerText = "Oh, hi, " + when (chat) {
|
||||
is PrivateChat -> "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id)
|
||||
is User -> "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id)
|
||||
is SupergroupChat -> (chat.username ?.username ?: getChat(chat).inviteLink) ?.let {
|
||||
chat.title.linkMarkdownV2(it)
|
||||
} ?: chat.title
|
||||
is GroupChat -> bot.getChat(chat).inviteLink ?.let {
|
||||
chat.title.linkMarkdownV2(it)
|
||||
} ?: chat.title
|
||||
else -> "Unknown :(".escapeMarkdownV2Common()
|
||||
|
||||
val answerText = when (val chat = message.chat) {
|
||||
is ChannelChat -> {
|
||||
val answer = "Hi everybody in this channel \"${chat.title}\""
|
||||
reply(message, answer, MarkdownV2)
|
||||
return@onContentMessage
|
||||
}
|
||||
is PrivateChat -> {
|
||||
reply(message, "Hi, " + "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id), MarkdownV2)
|
||||
return@onContentMessage
|
||||
}
|
||||
is GroupChat -> {
|
||||
message.ifFromChannelGroupContentMessage {
|
||||
val answer = "Hi, ${it.senderChat.title}"
|
||||
reply(message, answer, MarkdownV2)
|
||||
return@onContentMessage
|
||||
}
|
||||
"Oh, hi, " + when (chat) {
|
||||
is SupergroupChat -> (chat.username ?.username ?: getChat(chat).inviteLink) ?.let {
|
||||
chat.title.linkMarkdownV2(it)
|
||||
} ?: chat.title
|
||||
else -> bot.getChat(chat).inviteLink ?.let {
|
||||
chat.title.linkMarkdownV2(it)
|
||||
} ?: chat.title
|
||||
}
|
||||
}
|
||||
is UnknownExtendedChat,
|
||||
is UnknownChatType -> "Unknown :(".escapeMarkdownV2Common()
|
||||
else -> error("Something went wrong: unknown type of chat $chat")
|
||||
}
|
||||
reply(
|
||||
message,
|
||||
|
||||
@@ -9,7 +9,7 @@ buildscript {
|
||||
}
|
||||
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version"
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,18 +1,17 @@
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
||||
import dev.inmo.tgbotapi.extensions.api.edit.text.editMessageText
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||
import dev.inmo.tgbotapi.extensions.api.edit.edit
|
||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||
import dev.inmo.tgbotapi.extensions.api.send.media.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilterExcludeMediaGroups
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.withContent
|
||||
import dev.inmo.tgbotapi.types.BotCommand
|
||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||
import dev.inmo.tgbotapi.utils.*
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
private const val nextPageData = "next"
|
||||
@@ -71,13 +70,14 @@ suspend fun activateKeyboardsBot(
|
||||
val numberOfPages = args.firstOrNull() ?.toIntOrNull() ?: 10
|
||||
reply(
|
||||
message,
|
||||
"Your inline keyboard with $numberOfPages pages",
|
||||
replyMarkup = inlineKeyboard {
|
||||
row {
|
||||
includePageButtons(1, numberOfPages)
|
||||
}
|
||||
}
|
||||
)
|
||||
) {
|
||||
regular("Your inline keyboard with $numberOfPages pages")
|
||||
}
|
||||
}
|
||||
|
||||
onMessageDataCallbackQuery {
|
||||
@@ -86,20 +86,37 @@ suspend fun activateKeyboardsBot(
|
||||
return@onMessageDataCallbackQuery
|
||||
}
|
||||
|
||||
editMessageText(
|
||||
edit(
|
||||
it.message.withContent<TextContent>() ?: it.let {
|
||||
answer(it, "Unsupported message type :(")
|
||||
return@onMessageDataCallbackQuery
|
||||
},
|
||||
"This is $page of $count",
|
||||
replyMarkup = inlineKeyboard {
|
||||
row {
|
||||
includePageButtons(page, count)
|
||||
}
|
||||
}
|
||||
)
|
||||
) {
|
||||
regular("This is $page of $count")
|
||||
}
|
||||
answer(it)
|
||||
}
|
||||
|
||||
onUnhandledCommand {
|
||||
reply(
|
||||
it,
|
||||
replyMarkup = replyKeyboard(resizeKeyboard = true, oneTimeKeyboard = true) {
|
||||
row {
|
||||
simpleButton("/inline")
|
||||
}
|
||||
}
|
||||
) {
|
||||
+"Use " + botCommand("inline") + " to get pagination inline keyboard"
|
||||
}
|
||||
}
|
||||
|
||||
setMyCommands(BotCommand("inline", "Creates message with pagination inline keyboard"))
|
||||
|
||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||
println(it)
|
||||
}
|
||||
|
||||
9
LiveLocationsBot/README.md
Normal file
9
LiveLocationsBot/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# LiveLocationsBot
|
||||
|
||||
This bot will send you live location and update it from time to time
|
||||
|
||||
## Launch
|
||||
|
||||
```bash
|
||||
../gradlew run --args="BOT_TOKEN"
|
||||
```
|
||||
21
LiveLocationsBot/build.gradle
Normal file
21
LiveLocationsBot/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="LiveLocationsBotKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
||||
84
LiveLocationsBot/src/main/kotlin/LiveLocationsBot.kt
Normal file
84
LiveLocationsBot/src/main/kotlin/LiveLocationsBot.kt
Normal file
@@ -0,0 +1,84 @@
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.tgbotapi.extensions.api.EditLiveLocationInfo
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||
import dev.inmo.tgbotapi.extensions.api.edit.edit
|
||||
import dev.inmo.tgbotapi.extensions.api.handleLiveLocation
|
||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.waitMessageDataCallbackQuery
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.oneOf
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.parallel
|
||||
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.utils.extensions.sameMessage
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.linkMarkdownV2
|
||||
import dev.inmo.tgbotapi.extensions.utils.formatting.textMentionMarkdownV2
|
||||
import dev.inmo.tgbotapi.extensions.utils.ifFromChannelGroupContentMessage
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.dataButton
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.flatInlineKeyboard
|
||||
import dev.inmo.tgbotapi.types.chat.*
|
||||
import dev.inmo.tgbotapi.types.chat.GroupChat
|
||||
import dev.inmo.tgbotapi.types.chat.PrivateChat
|
||||
import dev.inmo.tgbotapi.types.chat.SupergroupChat
|
||||
import dev.inmo.tgbotapi.types.location.LiveLocation
|
||||
import dev.inmo.tgbotapi.types.message.MarkdownV2
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.ContentMessage
|
||||
import dev.inmo.tgbotapi.types.message.content.LiveLocationContent
|
||||
import dev.inmo.tgbotapi.types.message.content.LocationContent
|
||||
import dev.inmo.tgbotapi.utils.PreviewFeature
|
||||
import dev.inmo.tgbotapi.utils.extensions.escapeMarkdownV2Common
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.FlowCollector
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.filter
|
||||
import kotlinx.coroutines.flow.first
|
||||
import kotlinx.coroutines.flow.flow
|
||||
|
||||
/**
|
||||
* This bot will send you live location and update it from time to time
|
||||
*/
|
||||
suspend fun main(vararg args: String) {
|
||||
val botToken = args.first()
|
||||
|
||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||
val locationsFlow = flow {
|
||||
var i = 0
|
||||
while (isActive) {
|
||||
val newInfo = EditLiveLocationInfo(
|
||||
latitude = i.toDouble(),
|
||||
longitude = i.toDouble(),
|
||||
replyMarkup = flatInlineKeyboard {
|
||||
dataButton("Cancel", "cancel")
|
||||
}
|
||||
)
|
||||
emit(newInfo)
|
||||
i++
|
||||
delay(3000L) // 3 seconds
|
||||
}
|
||||
}
|
||||
onCommand("start") {
|
||||
// in this flow will be actual message with live location
|
||||
val currentMessageState = MutableStateFlow<ContentMessage<LocationContent>?>(null)
|
||||
val sendingJob = launch {
|
||||
handleLiveLocation(
|
||||
it.chat.id,
|
||||
locationsFlow,
|
||||
sentMessageFlow = FlowCollector { currentMessageState.emit(it) }
|
||||
)
|
||||
}
|
||||
|
||||
waitMessageDataCallbackQuery().filter {
|
||||
it.message.sameMessage(
|
||||
currentMessageState.value ?: return@filter false
|
||||
) && it.data == "cancel"
|
||||
}.first()
|
||||
|
||||
sendingJob.cancel() // ends live location
|
||||
currentMessageState.value ?.let {
|
||||
edit(it, replyMarkup = null) // removing reply keyboard
|
||||
}
|
||||
}
|
||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) { println(it) }
|
||||
}.second.join()
|
||||
}
|
||||
|
||||
@@ -1,23 +1,19 @@
|
||||
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||
import dev.inmo.micro_utils.common.filesize
|
||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.bot.TelegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||
import dev.inmo.tgbotapi.extensions.api.send.media.sendDocument
|
||||
import dev.inmo.tgbotapi.extensions.api.send.media.sendDocumentsGroup
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.api.send.withUploadDocumentAction
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviour
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommandWithArgs
|
||||
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
|
||||
import dev.inmo.tgbotapi.requests.abstracts.toInputFile
|
||||
import dev.inmo.tgbotapi.types.BotCommand
|
||||
import dev.inmo.tgbotapi.types.InputMedia.DocumentMediaGroupMemberInputMedia
|
||||
import dev.inmo.tgbotapi.types.InputMedia.InputMediaDocument
|
||||
import dev.inmo.tgbotapi.types.chat.abstracts.Chat
|
||||
import dev.inmo.tgbotapi.types.chat.Chat
|
||||
import dev.inmo.tgbotapi.types.files.DocumentFile
|
||||
import dev.inmo.tgbotapi.types.media.TelegramMediaDocument
|
||||
import dev.inmo.tgbotapi.types.mediaCountInMediaGroup
|
||||
import kotlinx.coroutines.*
|
||||
import java.io.File
|
||||
|
||||
private const val command = "send_file"
|
||||
@@ -50,7 +46,7 @@ suspend fun main(args: Array<String>) {
|
||||
)
|
||||
else -> sendDocumentsGroup(
|
||||
chat,
|
||||
files.map { InputMediaDocument(it.asMultipartFile()) },
|
||||
files.map { TelegramMediaDocument(it.asMultipartFile()) },
|
||||
protectContent = true
|
||||
)
|
||||
}
|
||||
@@ -69,9 +65,9 @@ suspend fun main(args: Array<String>) {
|
||||
val chosen = mutableListOf<File>()
|
||||
|
||||
while (left > 0) {
|
||||
left--
|
||||
val picked = pickFile() ?: continue
|
||||
val picked = pickFile() ?.takeIf { it.filesize > 0 } ?: continue
|
||||
chosen.add(picked)
|
||||
left--
|
||||
if (chosen.size >= mediaCountInMediaGroup.last) {
|
||||
sendFiles(message.chat, chosen)
|
||||
chosen.clear()
|
||||
@@ -85,7 +81,7 @@ suspend fun main(args: Array<String>) {
|
||||
}
|
||||
|
||||
if (!sent) {
|
||||
bot.reply(message, "Nothing selected :(")
|
||||
reply(message, "Nothing selected :(")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ buildscript {
|
||||
}
|
||||
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version"
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||
import dev.inmo.tgbotapi.extensions.api.send.media.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||
@@ -8,7 +8,7 @@ import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.CommonMessageFilte
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.filters.MessageFilterByChat
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
|
||||
import dev.inmo.tgbotapi.types.message.abstracts.Message
|
||||
import dev.inmo.tgbotapi.utils.extensions.threadIdOrNull
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
suspend fun activateResenderBot(
|
||||
@@ -21,31 +21,23 @@ suspend fun activateResenderBot(
|
||||
|
||||
bot.buildBehaviourWithLongPolling(CoroutineScope(currentCoroutineContext() + SupervisorJob())) {
|
||||
onContentMessage(
|
||||
initialFilter = CommonMessageFilterExcludeMediaGroups,
|
||||
subcontextUpdatesFilter = MessageFilterByChat
|
||||
subcontextUpdatesFilter = MessageFilterByChat,
|
||||
) {
|
||||
val chat = it.chat
|
||||
withTypingAction(chat) {
|
||||
executeUnsafe(it.content.createResend(chat.id, replyToMessageId = it.messageId))
|
||||
}
|
||||
}
|
||||
onVisualGallery {
|
||||
val chat = it.chat ?: return@onVisualGallery
|
||||
withUploadPhotoAction(chat) {
|
||||
sendVisualMediaGroup(chat, it.map { it.content.toMediaGroupMemberInputMedia() })
|
||||
}
|
||||
}
|
||||
onPlaylist {
|
||||
val chat = it.chat ?: return@onPlaylist
|
||||
withUploadDocumentAction(chat) {
|
||||
sendPlaylist(chat, it.map { it.content.toMediaGroupMemberInputMedia() })
|
||||
}
|
||||
}
|
||||
onDocumentsGroup {
|
||||
val chat = it.chat ?: return@onDocumentsGroup
|
||||
withUploadDocumentAction(chat) {
|
||||
sendDocumentsGroup(chat, it.map { it.content.toMediaGroupMemberInputMedia() })
|
||||
|
||||
val answer = withTypingAction(chat) {
|
||||
executeUnsafe(
|
||||
it.content.createResend(
|
||||
chat.id,
|
||||
messageThreadId = it.threadIdOrNull,
|
||||
replyToMessageId = it.messageId
|
||||
)
|
||||
) {
|
||||
it.forEach(print)
|
||||
}
|
||||
}
|
||||
|
||||
println("Answer info: $answer")
|
||||
}
|
||||
|
||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||
|
||||
12
RightsChangerBot/README.md
Normal file
12
RightsChangerBot/README.md
Normal file
@@ -0,0 +1,12 @@
|
||||
# RightsChanger
|
||||
|
||||
All the commands should be called with reply to some common user.
|
||||
|
||||
* Use `/simple` with bot to get request buttons for non-independent permissions change
|
||||
* Use `/granular` with bot to get request buttons for independent permissions change
|
||||
|
||||
## Launch
|
||||
|
||||
```bash
|
||||
../gradlew run --args="BOT_TOKEN allowed_user_id_long"
|
||||
```
|
||||
21
RightsChangerBot/build.gradle
Normal file
21
RightsChangerBot/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="RightsChangerKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
||||
255
RightsChangerBot/src/main/kotlin/RightsChanger.kt
Normal file
255
RightsChangerBot/src/main/kotlin/RightsChanger.kt
Normal file
@@ -0,0 +1,255 @@
|
||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.members.getChatMember
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.members.restrictChatMember
|
||||
import dev.inmo.tgbotapi.extensions.api.edit.edit
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.BehaviourContext
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMessageDataCallbackQuery
|
||||
import dev.inmo.tgbotapi.extensions.utils.asContentMessage
|
||||
import dev.inmo.tgbotapi.extensions.utils.asPossiblyReplyMessage
|
||||
import dev.inmo.tgbotapi.extensions.utils.commonMessageOrNull
|
||||
import dev.inmo.tgbotapi.extensions.utils.contentMessageOrNull
|
||||
import dev.inmo.tgbotapi.extensions.utils.extendedGroupChatOrNull
|
||||
import dev.inmo.tgbotapi.extensions.utils.fromUserMessageOrNull
|
||||
import dev.inmo.tgbotapi.extensions.utils.restrictedChatMemberOrNull
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.dataButton
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.inlineKeyboard
|
||||
import dev.inmo.tgbotapi.extensions.utils.whenMemberChatMember
|
||||
import dev.inmo.tgbotapi.types.BotCommand
|
||||
import dev.inmo.tgbotapi.types.ChatId
|
||||
import dev.inmo.tgbotapi.types.UserId
|
||||
import dev.inmo.tgbotapi.types.buttons.InlineKeyboardMarkup
|
||||
import dev.inmo.tgbotapi.types.chat.ChatPermissions
|
||||
import dev.inmo.tgbotapi.types.chat.PublicChat
|
||||
import dev.inmo.tgbotapi.types.commands.BotCommandScope
|
||||
import dev.inmo.tgbotapi.types.toChatId
|
||||
import dev.inmo.tgbotapi.utils.row
|
||||
|
||||
suspend fun main(args: Array<String>) {
|
||||
val botToken = args.first()
|
||||
|
||||
val bot = telegramBot(botToken)
|
||||
|
||||
val allowedAdmin = ChatId(args[1].toLong())
|
||||
|
||||
fun Boolean?.allowedSymbol() = when (this) {
|
||||
true -> "✅"
|
||||
false -> "❌"
|
||||
null -> ""
|
||||
}
|
||||
|
||||
val granularDataPrefix = "granular"
|
||||
val messagesToggleGranularData = "$granularDataPrefix messages"
|
||||
val otherMessagesToggleGranularData = "$granularDataPrefix other messages"
|
||||
val audiosToggleGranularData = "$granularDataPrefix audios"
|
||||
val voicesToggleGranularData = "$granularDataPrefix voices"
|
||||
val videosToggleGranularData = "$granularDataPrefix videos"
|
||||
val videoNotesToggleGranularData = "$granularDataPrefix video notes"
|
||||
val photosToggleGranularData = "$granularDataPrefix photos"
|
||||
val webPagePreviewToggleGranularData = "$granularDataPrefix web page preview"
|
||||
val pollsToggleGranularData = "$granularDataPrefix polls"
|
||||
val documentsToggleGranularData = "$granularDataPrefix documents"
|
||||
|
||||
val commonDataPrefix = "common"
|
||||
val pollsToggleCommonData = "$commonDataPrefix polls"
|
||||
val otherMessagesToggleCommonData = "$commonDataPrefix other messages"
|
||||
val webPagePreviewToggleCommonData = "$commonDataPrefix web page preview"
|
||||
|
||||
suspend fun BehaviourContext.getUserChatPermissions(chatId: ChatId, userId: UserId): ChatPermissions? {
|
||||
val chatMember = getChatMember(chatId, userId)
|
||||
return chatMember.restrictedChatMemberOrNull() ?: chatMember.whenMemberChatMember {
|
||||
getChat(chatId).extendedGroupChatOrNull() ?.permissions
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun BehaviourContext.buildGranularKeyboard(chatId: ChatId, userId: UserId): InlineKeyboardMarkup? {
|
||||
val permissions = getUserChatPermissions(chatId, userId) ?: return null
|
||||
|
||||
return inlineKeyboard {
|
||||
row {
|
||||
dataButton("Send messages${permissions.canSendMessages.allowedSymbol()}", messagesToggleGranularData)
|
||||
dataButton("Send other messages${permissions.canSendOtherMessages.allowedSymbol()}", otherMessagesToggleGranularData)
|
||||
}
|
||||
row {
|
||||
dataButton("Send audios${permissions.canSendAudios.allowedSymbol()}", audiosToggleGranularData)
|
||||
dataButton("Send voices${permissions.canSendVoiceNotes.allowedSymbol()}", voicesToggleGranularData)
|
||||
}
|
||||
row {
|
||||
dataButton("Send videos${permissions.canSendVideos.allowedSymbol()}", videosToggleGranularData)
|
||||
dataButton("Send video notes${permissions.canSendVideoNotes.allowedSymbol()}", videoNotesToggleGranularData)
|
||||
}
|
||||
row {
|
||||
dataButton("Send photos${permissions.canSendPhotos.allowedSymbol()}", photosToggleGranularData)
|
||||
dataButton("Add web preview${permissions.canAddWebPagePreviews.allowedSymbol()}", webPagePreviewToggleGranularData)
|
||||
}
|
||||
row {
|
||||
dataButton("Send polls${permissions.canSendPolls.allowedSymbol()}", pollsToggleGranularData)
|
||||
dataButton("Send documents${permissions.canSendDocuments.allowedSymbol()}", documentsToggleGranularData)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun BehaviourContext.buildCommonKeyboard(chatId: ChatId, userId: UserId): InlineKeyboardMarkup? {
|
||||
val permissions = getUserChatPermissions(chatId, userId) ?: return null
|
||||
|
||||
return inlineKeyboard {
|
||||
row {
|
||||
dataButton("Send polls${permissions.canSendPolls.allowedSymbol()}", pollsToggleCommonData)
|
||||
}
|
||||
row {
|
||||
dataButton("Send other messages${permissions.canSendOtherMessages.allowedSymbol()}", otherMessagesToggleCommonData)
|
||||
}
|
||||
row {
|
||||
dataButton("Add web preview${permissions.canAddWebPagePreviews.allowedSymbol()}", webPagePreviewToggleCommonData)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bot.buildBehaviourWithLongPolling(
|
||||
defaultExceptionsHandler = {
|
||||
println(it)
|
||||
}
|
||||
) {
|
||||
onCommand("simple", initialFilter = { it.chat is PublicChat && it.fromUserMessageOrNull() ?.user ?.id == allowedAdmin }) {
|
||||
val replyMessage = it.replyTo
|
||||
val userInReply = replyMessage ?.fromUserMessageOrNull() ?.user ?.id ?: return@onCommand
|
||||
reply(
|
||||
replyMessage,
|
||||
"Manage keyboard:",
|
||||
replyMarkup = buildCommonKeyboard(it.chat.id.toChatId(), userInReply) ?: return@onCommand
|
||||
)
|
||||
}
|
||||
onCommand("granular", initialFilter = { it.chat is PublicChat && it.fromUserMessageOrNull() ?.user ?.id == allowedAdmin }) {
|
||||
val replyMessage = it.replyTo
|
||||
val userInReply = replyMessage ?.fromUserMessageOrNull() ?.user ?.id ?: return@onCommand
|
||||
reply(
|
||||
replyMessage,
|
||||
"Manage keyboard:",
|
||||
replyMarkup = buildGranularKeyboard(it.chat.id.toChatId(), userInReply) ?: return@onCommand
|
||||
)
|
||||
}
|
||||
|
||||
onMessageDataCallbackQuery(
|
||||
Regex("^${granularDataPrefix}.*"),
|
||||
initialFilter = { it.user.id == allowedAdmin }
|
||||
) {
|
||||
val messageReply = it.message.commonMessageOrNull() ?.replyTo ?.fromUserMessageOrNull() ?: return@onMessageDataCallbackQuery
|
||||
val userId = messageReply.user.id
|
||||
val permissions = getUserChatPermissions(it.message.chat.id.toChatId(), userId) ?: return@onMessageDataCallbackQuery
|
||||
val newPermission = when (it.data) {
|
||||
messagesToggleGranularData -> {
|
||||
permissions.copyGranular(
|
||||
canSendMessages = permissions.canSendMessages ?.let { !it } ?: false
|
||||
)
|
||||
}
|
||||
otherMessagesToggleGranularData -> {
|
||||
permissions.copyGranular(
|
||||
canSendOtherMessages = permissions.canSendOtherMessages ?.let { !it } ?: false
|
||||
)
|
||||
}
|
||||
audiosToggleGranularData -> {
|
||||
permissions.copyGranular(
|
||||
canSendAudios = permissions.canSendAudios ?.let { !it } ?: false
|
||||
)
|
||||
}
|
||||
voicesToggleGranularData -> {
|
||||
permissions.copyGranular(
|
||||
canSendVoiceNotes = permissions.canSendVoiceNotes ?.let { !it } ?: false
|
||||
)
|
||||
}
|
||||
videosToggleGranularData -> {
|
||||
permissions.copyGranular(
|
||||
canSendVideos = permissions.canSendVideos ?.let { !it } ?: false
|
||||
)
|
||||
}
|
||||
videoNotesToggleGranularData -> {
|
||||
permissions.copyGranular(
|
||||
canSendVideoNotes = permissions.canSendVideoNotes ?.let { !it } ?: false
|
||||
)
|
||||
}
|
||||
photosToggleGranularData -> {
|
||||
permissions.copyGranular(
|
||||
canSendPhotos = permissions.canSendPhotos ?.let { !it } ?: false
|
||||
)
|
||||
}
|
||||
webPagePreviewToggleGranularData -> {
|
||||
permissions.copyGranular(
|
||||
canAddWebPagePreviews = permissions.canAddWebPagePreviews ?.let { !it } ?: false
|
||||
)
|
||||
}
|
||||
pollsToggleGranularData -> {
|
||||
permissions.copyGranular(
|
||||
canSendPolls = permissions.canSendPolls ?.let { !it } ?: false
|
||||
)
|
||||
}
|
||||
documentsToggleGranularData -> {
|
||||
permissions.copyGranular(
|
||||
canSendDocuments = permissions.canSendDocuments ?.let { !it } ?: false
|
||||
)
|
||||
}
|
||||
else -> permissions.copyGranular()
|
||||
}
|
||||
|
||||
restrictChatMember(
|
||||
it.message.chat.id,
|
||||
userId,
|
||||
permissions = newPermission,
|
||||
useIndependentChatPermissions = true
|
||||
)
|
||||
|
||||
edit(
|
||||
it.message,
|
||||
replyMarkup = buildGranularKeyboard(it.message.chat.id.toChatId(), userId) ?: return@onMessageDataCallbackQuery
|
||||
)
|
||||
}
|
||||
|
||||
onMessageDataCallbackQuery(
|
||||
Regex("^${commonDataPrefix}.*"),
|
||||
initialFilter = { it.user.id == allowedAdmin }
|
||||
) {
|
||||
val messageReply = it.message.commonMessageOrNull() ?.replyTo ?.fromUserMessageOrNull() ?: return@onMessageDataCallbackQuery
|
||||
val userId = messageReply.user.id
|
||||
val permissions = getUserChatPermissions(it.message.chat.id.toChatId(), userId) ?: return@onMessageDataCallbackQuery
|
||||
val newPermission = when (it.data) {
|
||||
pollsToggleCommonData -> {
|
||||
permissions.copyCommon(
|
||||
canSendPolls = permissions.canSendPolls ?.let { !it } ?: false
|
||||
)
|
||||
}
|
||||
otherMessagesToggleCommonData -> {
|
||||
permissions.copyCommon(
|
||||
canSendOtherMessages = permissions.canSendOtherMessages ?.let { !it } ?: false
|
||||
)
|
||||
}
|
||||
webPagePreviewToggleCommonData -> {
|
||||
permissions.copyCommon(
|
||||
canAddWebPagePreviews = permissions.canAddWebPagePreviews ?.let { !it } ?: false
|
||||
)
|
||||
}
|
||||
else -> permissions.copyCommon()
|
||||
}
|
||||
|
||||
restrictChatMember(
|
||||
it.message.chat.id,
|
||||
userId,
|
||||
permissions = newPermission,
|
||||
useIndependentChatPermissions = false
|
||||
)
|
||||
|
||||
edit(
|
||||
it.message,
|
||||
replyMarkup = buildCommonKeyboard(it.message.chat.id.toChatId(), userId) ?: return@onMessageDataCallbackQuery
|
||||
)
|
||||
}
|
||||
|
||||
setMyCommands(
|
||||
BotCommand("simple", "Trigger simple keyboard. Use with reply to user"),
|
||||
BotCommand("granular", "Trigger granular keyboard. Use with reply to user"),
|
||||
scope = BotCommandScope.AllGroupChats
|
||||
)
|
||||
}.join()
|
||||
}
|
||||
@@ -1,35 +1,26 @@
|
||||
import dev.inmo.micro_utils.coroutines.safely
|
||||
import dev.inmo.tgbotapi.bot.Ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onDice
|
||||
import dev.inmo.tgbotapi.extensions.utils.*
|
||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.filterContentMessages
|
||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.longPolling
|
||||
import dev.inmo.tgbotapi.types.dice.SlotMachineDiceAnimationType
|
||||
import dev.inmo.tgbotapi.types.message.content.DiceContent
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.coroutines.flow.launchIn
|
||||
import kotlinx.coroutines.flow.onEach
|
||||
|
||||
suspend fun main(args: Array<String>) {
|
||||
val bot = telegramBot(args.first())
|
||||
|
||||
val scope = CoroutineScope(Dispatchers.Default)
|
||||
bot.longPolling(scope = scope) {
|
||||
filterContentMessages<DiceContent>(scope).onEach {
|
||||
bot.buildBehaviourWithLongPolling(scope = CoroutineScope(Dispatchers.IO)) {
|
||||
onDice {
|
||||
val content = it.content
|
||||
val dice = content.dice
|
||||
val diceType = dice.animationType
|
||||
|
||||
safely ({ it.printStackTrace() }) {
|
||||
if (diceType == SlotMachineDiceAnimationType) {
|
||||
val result = dice.calculateSlotMachineResult() ?: return@safely
|
||||
bot.reply(it, "${result.leftReel}|${result.centerReel}|${result.rightReel}")
|
||||
} else {
|
||||
bot.reply(it, "There is no slot machine dice in message")
|
||||
}
|
||||
if (diceType == SlotMachineDiceAnimationType) {
|
||||
val result = dice.calculateSlotMachineResult() ?: return@onDice
|
||||
reply(it, "${result.leftReel}|${result.centerReel}|${result.rightReel}")
|
||||
} else {
|
||||
reply(it, "There is no slot machine dice in message")
|
||||
}
|
||||
}.launchIn(scope)
|
||||
}
|
||||
|
||||
scope.coroutineContext[Job]!!.join()
|
||||
}
|
||||
}
|
||||
}.join()
|
||||
}
|
||||
|
||||
33
StickerInfoBot/StickerInfoBotLib/build.gradle
Normal file
33
StickerInfoBot/StickerInfoBotLib/build.gradle
Normal file
@@ -0,0 +1,33 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
}
|
||||
|
||||
|
||||
kotlin {
|
||||
jvm()
|
||||
// js(LEGACY) {
|
||||
js(IR) {
|
||||
browser()
|
||||
binaries.executable()
|
||||
}
|
||||
|
||||
sourceSets {
|
||||
commonMain {
|
||||
dependencies {
|
||||
implementation kotlin('stdlib')
|
||||
|
||||
api "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import dev.inmo.micro_utils.coroutines.defaultSafelyWithoutExceptionHandler
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.get.*
|
||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||
import dev.inmo.tgbotapi.types.StickerType
|
||||
import dev.inmo.tgbotapi.types.message.textsources.*
|
||||
import dev.inmo.tgbotapi.types.stickers.StickerSet
|
||||
import dev.inmo.tgbotapi.utils.bold
|
||||
import dev.inmo.tgbotapi.utils.buildEntities
|
||||
import kotlinx.coroutines.*
|
||||
|
||||
fun StickerSet?.buildInfo() = buildEntities {
|
||||
if (this@buildInfo == null) {
|
||||
bold("Looks like this stickerset has been removed")
|
||||
} else {
|
||||
bold("StickerSet name: ") + "${name}\n"
|
||||
bold("StickerSet title: ") + "${title}\n"
|
||||
bold(
|
||||
when (stickerType) {
|
||||
StickerType.CustomEmoji -> "Custom emoji"
|
||||
StickerType.Mask -> "Mask"
|
||||
StickerType.Regular -> "Regular"
|
||||
is StickerType.Unknown -> "Unknown type \"${stickerType.type}\""
|
||||
}
|
||||
) + " sticker set with title " + bold(title) + " and name " + bold(name)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun activateStickerInfoBot(
|
||||
token: String,
|
||||
print: (Any) -> Unit
|
||||
) {
|
||||
val bot = telegramBot(token)
|
||||
|
||||
print(bot.getMe())
|
||||
|
||||
defaultSafelyWithoutExceptionHandler = {
|
||||
it.printStackTrace()
|
||||
}
|
||||
|
||||
bot.buildBehaviourWithLongPolling(CoroutineScope(currentCoroutineContext() + SupervisorJob())) {
|
||||
onText {
|
||||
withTypingAction(it.chat) {
|
||||
it.content.textSources.mapNotNull {
|
||||
if (it is CustomEmojiTextSource) {
|
||||
getCustomEmojiStickerOrNull(it.customEmojiId) ?.stickerSetName
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}.distinct().map {
|
||||
getStickerSet(it)
|
||||
}.distinct().flatMap {
|
||||
it.buildInfo() + regular("\n")
|
||||
}.separateForText().map { entities ->
|
||||
reply(it, entities)
|
||||
}
|
||||
}
|
||||
}
|
||||
onSticker {
|
||||
val stickerSetInfo = getStickerSetOrNull(it.content.media)
|
||||
reply(
|
||||
it,
|
||||
stickerSetInfo.buildInfo()
|
||||
)
|
||||
}
|
||||
|
||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||
println(it)
|
||||
}
|
||||
}.join()
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import kotlinx.browser.document
|
||||
import kotlinx.coroutines.*
|
||||
import org.w3c.dom.*
|
||||
|
||||
private val scope = CoroutineScope(Dispatchers.Default)
|
||||
|
||||
fun main() {
|
||||
document.addEventListener(
|
||||
"DOMContentLoaded",
|
||||
{
|
||||
val botsContainer = document.getElementById("bots_container") ?: return@addEventListener
|
||||
|
||||
(document.getElementById("bot_token_form") as? HTMLFormElement) ?.onsubmit = {
|
||||
(document.getElementById("bot_token") as? HTMLInputElement) ?.value ?.let { token ->
|
||||
val botContainer = document.createElement("div") as HTMLDivElement
|
||||
botsContainer.append(botContainer)
|
||||
|
||||
val infoDiv = document.createElement("div") as HTMLDivElement
|
||||
botContainer.append(infoDiv)
|
||||
|
||||
scope.launch {
|
||||
activateStickerInfoBot(token) {
|
||||
infoDiv.innerHTML = it.toString()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Resender bot</title>
|
||||
</head>
|
||||
<body>
|
||||
<form id="bot_token_form">
|
||||
<input type="text" id="bot_token">
|
||||
<input type="submit" value="Start bot">
|
||||
</form>
|
||||
<div id="start_offer">Type your bot token to the input above to start its work</div>
|
||||
<script type="text/javascript" src="StickerInfoBotLib.js"></script>
|
||||
<div id="bots_container"></div>
|
||||
</body>
|
||||
</html>
|
||||
21
StickerInfoBot/jvm_launcher/build.gradle
Normal file
21
StickerInfoBot/jvm_launcher/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="StickerInfoBotJvmKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation project(":StickerInfoBot:StickerInfoBotLib")
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
suspend fun main(args: Array<String>) {
|
||||
activateStickerInfoBot(args.first()) {
|
||||
println(it)
|
||||
}
|
||||
}
|
||||
9
TopicsHandling/README.md
Normal file
9
TopicsHandling/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# HelloBot
|
||||
|
||||
The main purpose of this bot is just to answer "Oh, hi, " and add user mention here
|
||||
|
||||
## Launch
|
||||
|
||||
```bash
|
||||
../gradlew run --args="BOT_TOKEN"
|
||||
```
|
||||
21
TopicsHandling/build.gradle
Normal file
21
TopicsHandling/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="TopicsHandlingKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
||||
145
TopicsHandling/src/main/kotlin/TopicsHandling.kt
Normal file
145
TopicsHandling/src/main/kotlin/TopicsHandling.kt
Normal file
@@ -0,0 +1,145 @@
|
||||
import com.benasher44.uuid.uuid4
|
||||
import dev.inmo.micro_utils.common.repeatOnFailure
|
||||
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.closeForumTopic
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.closeGeneralForumTopic
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.createForumTopic
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.deleteForumTopic
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.editForumTopic
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.editGeneralForumTopic
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.hideGeneralForumTopic
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.reopenForumTopic
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.reopenGeneralForumTopic
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.forum.unhideGeneralForumTopic
|
||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onCommand
|
||||
import dev.inmo.tgbotapi.extensions.utils.updates.retrieving.flushAccumulatedUpdates
|
||||
import dev.inmo.tgbotapi.types.BotCommand
|
||||
import dev.inmo.tgbotapi.types.ForumTopic
|
||||
import dev.inmo.tgbotapi.types.commands.BotCommandScope
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.delay
|
||||
|
||||
suspend fun main(vararg args: String) {
|
||||
telegramBotWithBehaviourAndLongPolling(
|
||||
args.first(),
|
||||
CoroutineScope(Dispatchers.Default),
|
||||
defaultExceptionsHandler = {
|
||||
it.printStackTrace()
|
||||
}
|
||||
) {
|
||||
flushAccumulatedUpdates()
|
||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||
println(it)
|
||||
}
|
||||
onCommand("start_test_topics") {
|
||||
val forumTopic = createForumTopic(
|
||||
it.chat,
|
||||
"Test",
|
||||
ForumTopic.GREEN
|
||||
)
|
||||
|
||||
reply(it, "Test topic has been created")
|
||||
|
||||
delay(1000L)
|
||||
editForumTopic(
|
||||
it.chat.id,
|
||||
forumTopic.messageThreadId,
|
||||
"Test 01"
|
||||
)
|
||||
|
||||
reply(it, "Test topic has changed its name to Test 01")
|
||||
|
||||
delay(1000L)
|
||||
closeForumTopic(
|
||||
it.chat.id,
|
||||
forumTopic.messageThreadId,
|
||||
)
|
||||
|
||||
reply(it, "Test topic has been closed")
|
||||
|
||||
delay(1000L)
|
||||
reopenForumTopic(
|
||||
it.chat.id,
|
||||
forumTopic.messageThreadId,
|
||||
)
|
||||
|
||||
reply(it, "Test topic has been reopened")
|
||||
|
||||
delay(1000L)
|
||||
deleteForumTopic(
|
||||
it.chat.id,
|
||||
forumTopic.messageThreadId,
|
||||
)
|
||||
|
||||
reply(it, "Test topic has been deleted")
|
||||
|
||||
delay(1000L)
|
||||
hideGeneralForumTopic(
|
||||
it.chat.id,
|
||||
)
|
||||
|
||||
reply(it, "General topic has been hidden")
|
||||
|
||||
delay(1000L)
|
||||
unhideGeneralForumTopic(
|
||||
it.chat.id
|
||||
)
|
||||
|
||||
reply(it, "General topic has been shown")
|
||||
|
||||
delay(1000L)
|
||||
runCatchingSafely(
|
||||
{ _ ->
|
||||
reopenGeneralForumTopic(
|
||||
it.chat.id
|
||||
)
|
||||
|
||||
closeGeneralForumTopic(
|
||||
it.chat.id
|
||||
)
|
||||
}
|
||||
) {
|
||||
closeGeneralForumTopic(
|
||||
it.chat.id
|
||||
)
|
||||
}
|
||||
|
||||
reply(it, "General topic has been closed")
|
||||
|
||||
delay(1000L)
|
||||
reopenGeneralForumTopic(
|
||||
it.chat.id
|
||||
)
|
||||
|
||||
reply(it, "General topic has been opened")
|
||||
|
||||
delay(1000L)
|
||||
editGeneralForumTopic(
|
||||
it.chat.id,
|
||||
uuid4().toString().take(10)
|
||||
)
|
||||
|
||||
reply(it, "General topic has been renamed")
|
||||
|
||||
delay(1000L)
|
||||
editGeneralForumTopic(
|
||||
it.chat.id,
|
||||
"Main topic"
|
||||
)
|
||||
|
||||
reply(it, "General topic has been renamed")
|
||||
|
||||
delay(1000L)
|
||||
}
|
||||
|
||||
setMyCommands(
|
||||
BotCommand("start_test_topics", "start test topics"),
|
||||
scope = BotCommandScope.AllGroupChats
|
||||
)
|
||||
}.second.join()
|
||||
}
|
||||
9
UserChatShared/README.md
Normal file
9
UserChatShared/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# UserChatShared
|
||||
|
||||
Use `/start` with bot to get request buttons. Bot will ask you to choose user/chat from your list and send it to him.
|
||||
|
||||
## Launch
|
||||
|
||||
```bash
|
||||
../gradlew run --args="BOT_TOKEN"
|
||||
```
|
||||
21
UserChatShared/build.gradle
Normal file
21
UserChatShared/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="UserChatSharedKt"
|
||||
|
||||
|
||||
dependencies {
|
||||
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
||||
|
||||
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||
}
|
||||
218
UserChatShared/src/main/kotlin/UserChatShared.kt
Normal file
218
UserChatShared/src/main/kotlin/UserChatShared.kt
Normal file
@@ -0,0 +1,218 @@
|
||||
import dev.inmo.micro_utils.coroutines.runCatchingSafely
|
||||
import dev.inmo.tgbotapi.bot.ktor.telegramBot
|
||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
||||
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.onUserShared
|
||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.replyKeyboard
|
||||
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.chat.PrivateChat
|
||||
import dev.inmo.tgbotapi.types.message.textsources.mention
|
||||
import dev.inmo.tgbotapi.types.request.RequestId
|
||||
import dev.inmo.tgbotapi.utils.row
|
||||
|
||||
suspend fun main(args: Array<String>) {
|
||||
val botToken = args.first()
|
||||
|
||||
val bot = telegramBot(botToken)
|
||||
|
||||
val requestIdUserOrBot = RequestId(0)
|
||||
val requestIdUserNonPremium = RequestId(1)
|
||||
val requestIdUserAny = RequestId(2)
|
||||
val requestIdUserPremium = RequestId(3)
|
||||
val requestIdBot = RequestId(4)
|
||||
|
||||
val requestIdAnyChat = RequestId(5)
|
||||
val requestIdChannel = RequestId(6)
|
||||
val requestIdPublicChannel = RequestId(7)
|
||||
val requestIdPrivateChannel = RequestId(8)
|
||||
val requestIdChannelUserOwner = RequestId(9)
|
||||
|
||||
val requestIdGroup = RequestId(10)
|
||||
val requestIdPublicGroup = RequestId(11)
|
||||
val requestIdPrivateGroup = RequestId(12)
|
||||
val requestIdGroupUserOwner = RequestId(13)
|
||||
|
||||
val requestIdForum = RequestId(14)
|
||||
val requestIdPublicForum = RequestId(15)
|
||||
val requestIdPrivateForum = RequestId(16)
|
||||
val requestIdForumUserOwner = RequestId(17)
|
||||
|
||||
val keyboard = replyKeyboard(
|
||||
resizeKeyboard = true,
|
||||
) {
|
||||
row {
|
||||
requestUserOrBotButton(
|
||||
"\uD83D\uDC64/\uD83E\uDD16",
|
||||
requestIdUserOrBot
|
||||
)
|
||||
}
|
||||
row {
|
||||
requestUserButton(
|
||||
"\uD83D\uDC64☆",
|
||||
requestIdUserNonPremium,
|
||||
premiumUser = false
|
||||
)
|
||||
requestUserButton(
|
||||
"\uD83D\uDC64",
|
||||
requestIdUserAny,
|
||||
premiumUser = null
|
||||
)
|
||||
requestUserButton(
|
||||
"\uD83D\uDC64★",
|
||||
requestIdUserPremium,
|
||||
premiumUser = true
|
||||
)
|
||||
requestBotButton(
|
||||
"\uD83E\uDD16",
|
||||
requestIdBot
|
||||
)
|
||||
}
|
||||
row {
|
||||
requestChatButton(
|
||||
"\uD83D\uDDE3/\uD83D\uDC65",
|
||||
requestIdAnyChat
|
||||
)
|
||||
}
|
||||
row {
|
||||
requestChatButton(
|
||||
"\uD83D\uDDE3",
|
||||
requestIdChannel,
|
||||
isChannel = true
|
||||
)
|
||||
requestChatButton(
|
||||
"\uD83D\uDDE3\uD83D\uDD17",
|
||||
requestIdPublicChannel,
|
||||
isChannel = true,
|
||||
isPublic = true
|
||||
)
|
||||
requestChatButton(
|
||||
"\uD83D\uDDE3❌\uD83D\uDD17",
|
||||
requestIdPrivateChannel,
|
||||
isChannel = true,
|
||||
isPublic = false
|
||||
)
|
||||
requestChatButton(
|
||||
"\uD83D\uDDE3\uD83D\uDC6E",
|
||||
requestIdChannelUserOwner,
|
||||
isChannel = true,
|
||||
isOwnedBy = true
|
||||
)
|
||||
}
|
||||
row {
|
||||
requestGroupButton(
|
||||
"👥",
|
||||
requestIdGroup
|
||||
)
|
||||
requestGroupButton(
|
||||
"👥\uD83D\uDD17",
|
||||
requestIdPublicGroup,
|
||||
isPublic = true
|
||||
)
|
||||
requestGroupButton(
|
||||
"👥❌\uD83D\uDD17",
|
||||
requestIdPrivateGroup,
|
||||
isPublic = false
|
||||
)
|
||||
requestGroupButton(
|
||||
"👥\uD83D\uDC6E",
|
||||
requestIdGroupUserOwner,
|
||||
isOwnedBy = true
|
||||
)
|
||||
}
|
||||
row {
|
||||
requestGroupButton(
|
||||
"🏛",
|
||||
requestIdForum,
|
||||
isForum = true
|
||||
)
|
||||
requestGroupButton(
|
||||
"🏛\uD83D\uDD17",
|
||||
requestIdPublicForum,
|
||||
isPublic = true,
|
||||
isForum = true
|
||||
)
|
||||
requestGroupButton(
|
||||
"🏛❌\uD83D\uDD17",
|
||||
requestIdPrivateForum,
|
||||
isPublic = false,
|
||||
isForum = true
|
||||
)
|
||||
requestGroupButton(
|
||||
"🏛\uD83D\uDC6E",
|
||||
requestIdForumUserOwner,
|
||||
isOwnedBy = true,
|
||||
isForum = true
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
bot.buildBehaviourWithLongPolling (defaultExceptionsHandler = { it.printStackTrace() }) {
|
||||
onCommand("start", initialFilter = { it.chat is PrivateChat }) {
|
||||
reply(
|
||||
it,
|
||||
"Here possible requests buttons:",
|
||||
replyMarkup = keyboard
|
||||
)
|
||||
}
|
||||
|
||||
onUserShared {
|
||||
val userId = it.chatEvent.userId
|
||||
val userInfo = runCatchingSafely { getChat(userId) }.getOrNull()
|
||||
reply(
|
||||
it,
|
||||
) {
|
||||
+"You have shared "
|
||||
+mention(
|
||||
when (it.chatEvent.requestId) {
|
||||
requestIdUserOrBot -> "user or bot"
|
||||
requestIdUserNonPremium -> "non premium user"
|
||||
requestIdUserAny -> "any user"
|
||||
requestIdUserPremium -> "premium user"
|
||||
requestIdBot -> "bot"
|
||||
else -> "somebody O.o"
|
||||
},
|
||||
userId
|
||||
)
|
||||
+" (user info: $userInfo; user id: $userId)"
|
||||
}
|
||||
}
|
||||
|
||||
onChatShared {
|
||||
val chatId = it.chatEvent.chatId
|
||||
val chatInfo = runCatchingSafely { getChat(chatId) }.getOrNull()
|
||||
reply(
|
||||
it,
|
||||
) {
|
||||
+"You have shared "
|
||||
+when (it.chatEvent.requestId) {
|
||||
requestIdAnyChat -> "some chat"
|
||||
requestIdChannel -> "any channel"
|
||||
requestIdPublicChannel -> "public channel"
|
||||
requestIdPrivateChannel -> "private channel"
|
||||
requestIdChannelUserOwner -> "channel owned by you"
|
||||
requestIdGroup -> "any group"
|
||||
requestIdPublicGroup -> "public group"
|
||||
requestIdPrivateGroup -> "private group"
|
||||
requestIdGroupUserOwner -> "group owned by you"
|
||||
requestIdForum -> "any forum"
|
||||
requestIdPublicForum -> "public forum"
|
||||
requestIdPrivateForum -> "private forum"
|
||||
requestIdForumUserOwner -> "forum owned by you"
|
||||
else -> "some chat O.o"
|
||||
}
|
||||
+" (chat info: $chatInfo; chat id: $chatId)"
|
||||
}
|
||||
}
|
||||
|
||||
setMyCommands(BotCommand("start", "Trigger buttons"))
|
||||
}.join()
|
||||
}
|
||||
@@ -9,7 +9,8 @@ buildscript {
|
||||
}
|
||||
|
||||
plugins {
|
||||
id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version"
|
||||
id "org.jetbrains.kotlin.multiplatform"
|
||||
id "org.jetbrains.kotlin.plugin.serialization"
|
||||
}
|
||||
|
||||
apply plugin: 'application'
|
||||
@@ -25,6 +26,7 @@ kotlin {
|
||||
commonMain {
|
||||
dependencies {
|
||||
implementation kotlin('stdlib')
|
||||
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$serialization_version"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
7
WebApp/src/commonMain/kotlin/WebAppDataWrapper.kt
Normal file
7
WebApp/src/commonMain/kotlin/WebAppDataWrapper.kt
Normal file
@@ -0,0 +1,7 @@
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class WebAppDataWrapper(
|
||||
val data: String,
|
||||
val hash: String
|
||||
)
|
||||
@@ -1,11 +1,12 @@
|
||||
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
|
||||
import dev.inmo.tgbotapi.types.webAppQueryIdField
|
||||
import dev.inmo.tgbotapi.webapps.*
|
||||
import dev.inmo.tgbotapi.webapps.haptic.HapticFeedbackStyle
|
||||
import dev.inmo.tgbotapi.webapps.haptic.HapticFeedbackType
|
||||
import dev.inmo.tgbotapi.webapps.popup.*
|
||||
import io.ktor.client.HttpClient
|
||||
import io.ktor.client.call.receive
|
||||
import io.ktor.client.request.*
|
||||
import io.ktor.client.statement.HttpResponse
|
||||
import io.ktor.client.statement.readText
|
||||
import io.ktor.client.statement.bodyAsText
|
||||
import io.ktor.http.*
|
||||
import io.ktor.http.content.TextContent
|
||||
import kotlinx.browser.document
|
||||
@@ -13,25 +14,54 @@ import kotlinx.browser.window
|
||||
import kotlinx.coroutines.*
|
||||
import kotlinx.dom.appendElement
|
||||
import kotlinx.dom.appendText
|
||||
import kotlinx.serialization.json.Json
|
||||
import org.w3c.dom.HTMLElement
|
||||
|
||||
fun HTMLElement.log(text: String) {
|
||||
appendElement("p", {})
|
||||
appendText(text)
|
||||
appendElement("p", {})
|
||||
}
|
||||
|
||||
fun main() {
|
||||
console.log("Web app started")
|
||||
val client = HttpClient()
|
||||
val baseUrl = window.location.origin.removeSuffix("/")
|
||||
|
||||
window.onload = {
|
||||
val scope = CoroutineScope(Dispatchers.Default)
|
||||
runCatching {
|
||||
|
||||
scope.launchSafelyWithoutExceptions {
|
||||
val response = client.post("$baseUrl/check") {
|
||||
setBody(
|
||||
Json { }.encodeToString(
|
||||
WebAppDataWrapper.serializer(),
|
||||
WebAppDataWrapper(webApp.initData, webApp.initDataUnsafe.hash)
|
||||
)
|
||||
)
|
||||
}
|
||||
val dataIsSafe = response.bodyAsText().toBoolean()
|
||||
|
||||
document.body ?.log(
|
||||
if (dataIsSafe) {
|
||||
"Data is safe"
|
||||
} else {
|
||||
"Data is unsafe"
|
||||
}
|
||||
)
|
||||
|
||||
document.body ?.log(
|
||||
webApp.initDataUnsafe.chat.toString()
|
||||
)
|
||||
}
|
||||
|
||||
document.body ?.appendElement("button") {
|
||||
addEventListener("click", {
|
||||
scope.launchSafelyWithoutExceptions {
|
||||
handleResult({ "Clicked" }) {
|
||||
HttpClient().post<HttpResponse>("${window.location.origin.removeSuffix("/")}/inline") {
|
||||
client.post("${window.location.origin.removeSuffix("/")}/inline") {
|
||||
parameter(webAppQueryIdField, it)
|
||||
body = TextContent("Clicked", ContentType.Text.Plain)
|
||||
setBody(TextContent("Clicked", ContentType.Text.Plain))
|
||||
document.body ?.log(url.build().toString())
|
||||
}.coroutineContext.job.join()
|
||||
}
|
||||
@@ -39,6 +69,79 @@ fun main() {
|
||||
})
|
||||
appendText("Example button")
|
||||
} ?: window.alert("Unable to load body")
|
||||
|
||||
document.body ?.appendElement("p", {})
|
||||
document.body ?.appendText("Alerts:")
|
||||
|
||||
document.body ?.appendElement("button") {
|
||||
addEventListener("click", {
|
||||
webApp.showPopup(
|
||||
PopupParams(
|
||||
"It is sample title of default button",
|
||||
"It is sample message of default button",
|
||||
DefaultPopupButton("default", "Default button"),
|
||||
OkPopupButton("ok"),
|
||||
DestructivePopupButton("destructive", "Destructive button")
|
||||
)
|
||||
) {
|
||||
document.body ?.log(
|
||||
when (it) {
|
||||
"default" -> "You have clicked default button in popup"
|
||||
"ok" -> "You have clicked ok button in popup"
|
||||
"destructive" -> "You have clicked destructive button in popup"
|
||||
else -> "I can't imagine where you take button with id $it"
|
||||
}
|
||||
)
|
||||
}
|
||||
})
|
||||
appendText("Popup")
|
||||
} ?: window.alert("Unable to load body")
|
||||
|
||||
document.body ?.appendElement("button") {
|
||||
addEventListener("click", {
|
||||
webApp.showAlert(
|
||||
"This is alert message"
|
||||
) {
|
||||
document.body ?.log(
|
||||
"You have closed alert"
|
||||
)
|
||||
}
|
||||
})
|
||||
appendText("Alert")
|
||||
} ?: window.alert("Unable to load body")
|
||||
|
||||
document.body ?.appendElement("button") {
|
||||
addEventListener("click", {
|
||||
webApp.showConfirm(
|
||||
"This is confirm message"
|
||||
) {
|
||||
document.body ?.log(
|
||||
"You have pressed \"${if (it) "Ok" else "Cancel"}\" in confirm"
|
||||
)
|
||||
}
|
||||
})
|
||||
appendText("Confirm")
|
||||
} ?: window.alert("Unable to load body")
|
||||
|
||||
document.body ?.appendElement("p", {})
|
||||
|
||||
document.body ?.appendElement("button") {
|
||||
fun updateText() {
|
||||
textContent = if (webApp.isClosingConfirmationEnabled) {
|
||||
"Disable closing confirmation"
|
||||
} else {
|
||||
"Enable closing confirmation"
|
||||
}
|
||||
}
|
||||
addEventListener("click", {
|
||||
webApp.toggleClosingConfirmation()
|
||||
updateText()
|
||||
})
|
||||
updateText()
|
||||
} ?: window.alert("Unable to load body")
|
||||
|
||||
document.body ?.appendElement("p", {})
|
||||
|
||||
webApp.apply {
|
||||
onThemeChanged {
|
||||
document.body ?.log("Theme changed: ${webApp.themeParams}")
|
||||
@@ -46,6 +149,28 @@ fun main() {
|
||||
onViewportChanged {
|
||||
document.body ?.log("Viewport changed: ${it.isStateStable}")
|
||||
}
|
||||
backButton.apply {
|
||||
onClick {
|
||||
document.body ?.log("Back button clicked")
|
||||
hapticFeedback.impactOccurred(
|
||||
HapticFeedbackStyle.Heavy
|
||||
)
|
||||
}
|
||||
show()
|
||||
}
|
||||
mainButton.apply {
|
||||
setText("Main button")
|
||||
onClick {
|
||||
document.body ?.log("Main button clicked")
|
||||
hapticFeedback.notificationOccurred(
|
||||
HapticFeedbackType.Success
|
||||
)
|
||||
}
|
||||
show()
|
||||
}
|
||||
onSettingsButtonClicked {
|
||||
document.body ?.log("Settings button clicked")
|
||||
}
|
||||
}
|
||||
webApp.ready()
|
||||
}.onFailure {
|
||||
|
||||
@@ -2,6 +2,11 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="format-detection" content="telephone=no"/>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
|
||||
<meta name="HandheldFriendly" content="True"/>
|
||||
<meta name="robots" content="noindex,nofollow"/>
|
||||
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
|
||||
<title>Web App Example</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
@@ -13,14 +13,18 @@ import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResult
|
||||
import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputTextMessageContent
|
||||
import dev.inmo.tgbotapi.types.webAppQueryIdField
|
||||
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
|
||||
import io.ktor.application.call
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.http.content.*
|
||||
import io.ktor.request.receiveText
|
||||
import io.ktor.response.respond
|
||||
import io.ktor.routing.*
|
||||
import dev.inmo.tgbotapi.utils.*
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.call
|
||||
import io.ktor.server.http.content.*
|
||||
import io.ktor.server.request.receiveText
|
||||
import io.ktor.server.response.respond
|
||||
import io.ktor.server.routing.post
|
||||
import io.ktor.server.routing.routing
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.io.File
|
||||
import java.nio.charset.Charset
|
||||
|
||||
/**
|
||||
* Accepts two parameters:
|
||||
@@ -30,8 +34,13 @@ import java.io.File
|
||||
*
|
||||
* Will start the server to share the static (index.html and WebApp.js) on 0.0.0.0:8080
|
||||
*/
|
||||
@OptIn(PreviewFeature::class)
|
||||
suspend fun main(vararg args: String) {
|
||||
val bot = telegramBot(args.first(), testServer = args.any { it == "testServer" })
|
||||
val telegramBotAPIUrlsKeeper = TelegramAPIUrlsKeeper(
|
||||
args.first(),
|
||||
testServer = args.any { it == "testServer" }
|
||||
)
|
||||
val bot = telegramBot(telegramBotAPIUrlsKeeper)
|
||||
createKtorServer(
|
||||
"0.0.0.0",
|
||||
8080,
|
||||
@@ -51,6 +60,14 @@ suspend fun main(vararg args: String) {
|
||||
bot.answer(queryId, InlineQueryResultArticle(queryId, "Result", InputTextMessageContent(requestBody)))
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
post("check") {
|
||||
val requestBody = call.receiveText()
|
||||
val webAppCheckData = Json { }.decodeFromString(WebAppDataWrapper.serializer(), requestBody)
|
||||
|
||||
val isSafe = telegramBotAPIUrlsKeeper.checkWebAppData(webAppCheckData.data, webAppCheckData.hash)
|
||||
|
||||
call.respond(HttpStatusCode.OK, isSafe.toString())
|
||||
}
|
||||
}
|
||||
}.start(false)
|
||||
|
||||
@@ -82,7 +99,13 @@ suspend fun main(vararg args: String) {
|
||||
)
|
||||
}
|
||||
onUnhandledCommand {
|
||||
println("Unhandled command: ${it.content}")
|
||||
reply(
|
||||
it,
|
||||
buildEntities {
|
||||
+"Use " + botCommand("inline") + " to get inline web app button\n"
|
||||
+"Use " + botCommand("reply_markup") + " to get reply markup web app button\n"
|
||||
}
|
||||
)
|
||||
}
|
||||
setMyCommands(
|
||||
BotCommand("reply_markup", "Use to get reply markup keyboard with web app trigger"),
|
||||
|
||||
13
build.gradle
13
build.gradle
@@ -1,3 +1,14 @@
|
||||
buildscript {
|
||||
repositories {
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
|
||||
}
|
||||
}
|
||||
|
||||
allprojects {
|
||||
repositories {
|
||||
mavenLocal()
|
||||
@@ -11,5 +22,7 @@ allprojects {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
maven { url "https://git.inmo.dev/api/packages/InsanusMokrassar/maven" }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
kotlin.code.style=official
|
||||
org.gradle.parallel=true
|
||||
# Due to parallel compilation project require next amount of memory on full build
|
||||
org.gradle.jvmargs=-Xmx1g
|
||||
|
||||
|
||||
kotlin_version=1.6.10
|
||||
telegram_bot_api_version=0.38.23
|
||||
micro_utils_version=0.9.24
|
||||
ktor_version=1.6.8
|
||||
kotlin_version=1.8.10
|
||||
telegram_bot_api_version=6.0.0
|
||||
micro_utils_version=0.17.0
|
||||
serialization_version=1.5.0
|
||||
ktor_version=2.2.3
|
||||
|
||||
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -2,4 +2,4 @@ distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-bin.zip
|
||||
|
||||
@@ -6,6 +6,8 @@ include ":HelloBot"
|
||||
|
||||
include ":GetMeBot"
|
||||
|
||||
include ":DeepLinksBot"
|
||||
|
||||
include ":FilesLoaderBot"
|
||||
|
||||
include ":ResenderBot:ResenderBotLib"
|
||||
@@ -14,8 +16,21 @@ include ":ResenderBot:jvm_launcher"
|
||||
include ":KeyboardsBot:KeyboardsBotLib"
|
||||
include ":KeyboardsBot:jvm_launcher"
|
||||
|
||||
include ":StickerInfoBot:StickerInfoBotLib"
|
||||
include ":StickerInfoBot:jvm_launcher"
|
||||
|
||||
include ":SlotMachineDetectorBot"
|
||||
|
||||
include ":ChatAvatarSetter"
|
||||
|
||||
include ":WebApp"
|
||||
|
||||
include ":FSMBot"
|
||||
|
||||
include ":TopicsHandling"
|
||||
|
||||
include ":UserChatShared"
|
||||
|
||||
include ":RightsChangerBot"
|
||||
|
||||
include ":LiveLocationsBot"
|
||||
|
||||
Reference in New Issue
Block a user