mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-12-20 05:05:38 +00:00
Compare commits
76 Commits
0.38.12
...
0698915cdf
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0698915cdf | ||
| 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 | |||
| b740203116 | |||
| 149717301d | |||
| 60e72be844 | |||
| 831b558724 | |||
| df778b4e93 | |||
| bf0c6497fe | |||
|
|
71a047f867 | ||
| 2ae3e1165d | |||
| 9a5d02512b | |||
| dd2c528006 | |||
| 1c16a9f868 | |||
| 00c3aba12b | |||
| a547bbce65 | |||
| 0b7d8c087f | |||
| 8ec282e3d5 | |||
|
|
e8433cd8ac | ||
| 222134836a | |||
| c18e02dcb3 |
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@@ -8,9 +8,9 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
- name: Set up JDK 1.8
|
- name: Set up JDK 11
|
||||||
uses: actions/setup-java@v1
|
uses: actions/setup-java@v1
|
||||||
with:
|
with:
|
||||||
java-version: 1.8
|
java-version: 11
|
||||||
- name: Build with Gradle
|
- name: Build with Gradle
|
||||||
run: ./gradlew build
|
run: ./gradlew build
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
|
import dev.inmo.micro_utils.coroutines.AccumulatorFlow
|
||||||
import dev.inmo.micro_utils.fsm.common.State
|
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.api.send.sendMessage
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.expectations.*
|
||||||
@@ -9,6 +11,7 @@ import dev.inmo.tgbotapi.types.ChatId
|
|||||||
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
import dev.inmo.tgbotapi.types.message.abstracts.CommonMessage
|
||||||
import dev.inmo.tgbotapi.types.message.content.TextContent
|
import dev.inmo.tgbotapi.types.message.content.TextContent
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
import kotlinx.coroutines.flow.first
|
||||||
|
|
||||||
sealed interface BotState : State
|
sealed interface BotState : State
|
||||||
data class ExpectContentOrStopState(override val context: ChatId, val sourceMessage: CommonMessage<TextContent>) : BotState
|
data class ExpectContentOrStopState(override val context: ChatId, val sourceMessage: CommonMessage<TextContent>) : BotState
|
||||||
@@ -17,7 +20,22 @@ data class StopState(override val context: ChatId) : BotState
|
|||||||
suspend fun main(args: Array<String>) {
|
suspend fun main(args: Array<String>) {
|
||||||
val botToken = args.first()
|
val botToken = args.first()
|
||||||
|
|
||||||
telegramBotWithBehaviourAndFSMAndStartLongPolling<BotState>(botToken, CoroutineScope(Dispatchers.IO)) {
|
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> {
|
strictlyOn<ExpectContentOrStopState> {
|
||||||
sendMessage(
|
sendMessage(
|
||||||
it.context,
|
it.context,
|
||||||
@@ -26,7 +44,9 @@ suspend fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
val content = waitContentMessage().first()
|
val contentMessage = waitContentMessage().first()
|
||||||
|
val content = contentMessage.content
|
||||||
|
|
||||||
when {
|
when {
|
||||||
content is TextContent && content.parseCommandsWithParams().keys.contains("stop") -> StopState(it.context)
|
content is TextContent && content.parseCommandsWithParams().keys.contains("stop") -> StopState(it.context)
|
||||||
else -> {
|
else -> {
|
||||||
@@ -36,7 +56,7 @@ suspend fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
strictlyOn<StopState> {
|
strictlyOn<StopState> {
|
||||||
sendMessage(it.context, "You have stopped sending of content")
|
send(it.context, "You have stopped sending of content")
|
||||||
|
|
||||||
null
|
null
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
import dev.inmo.tgbotapi.extensions.api.files.downloadFile
|
||||||
import dev.inmo.tgbotapi.extensions.api.get.getFileAdditionalInfo
|
import dev.inmo.tgbotapi.extensions.api.get.getFileAdditionalInfo
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
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.telegramBotWithBehaviourAndLongPolling
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMedia
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onMedia
|
||||||
@@ -21,11 +20,9 @@ suspend fun main(args: Array<String>) {
|
|||||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||||
onMedia(initialFilter = null) {
|
onMedia(initialFilter = null) {
|
||||||
val pathedFile = bot.getFileAdditionalInfo(it.content.media)
|
val pathedFile = bot.getFileAdditionalInfo(it.content.media)
|
||||||
val file = File(directoryOrFile, pathedFile.filePath.filenameFromUrl).apply {
|
val outFile = File(directoryOrFile, pathedFile.filePath.filenameFromUrl)
|
||||||
createNewFile()
|
bot.downloadFile(it.content.media, outFile)
|
||||||
writeBytes(bot.downloadFile(pathedFile))
|
reply(it, "Saved to ${outFile.absolutePath}")
|
||||||
}
|
|
||||||
reply(it, "Saved to ${file.absolutePath}")
|
|
||||||
}
|
}
|
||||||
onContentMessage { println(it) }
|
onContentMessage { println(it) }
|
||||||
}.second.join()
|
}.second.join()
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -2,7 +2,9 @@ import dev.inmo.tgbotapi.extensions.api.send.reply
|
|||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
||||||
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
import dev.inmo.tgbotapi.extensions.utils.formatting.*
|
||||||
import dev.inmo.tgbotapi.types.*
|
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.types.message.*
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
@@ -24,7 +26,13 @@ suspend fun main(vararg args: String) {
|
|||||||
is UserForwardInfo -> {
|
is UserForwardInfo -> {
|
||||||
val user = forwardInfo.from
|
val user = forwardInfo.from
|
||||||
when (user) {
|
when (user) {
|
||||||
is CommonUser -> regular("User ")
|
is CommonUser -> {
|
||||||
|
if (user.isPremium) {
|
||||||
|
regular("Premium user ")
|
||||||
|
} else {
|
||||||
|
regular("User ")
|
||||||
|
}
|
||||||
|
}
|
||||||
is CommonBot,
|
is CommonBot,
|
||||||
is ExtendedBot -> regular("Bot ")
|
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"})"
|
||||||
@@ -34,8 +42,6 @@ suspend fun main(vararg args: String) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
reply(it, toAnswer)
|
reply(it, toAnswer)
|
||||||
coroutineContext.job.invokeOnCompletion { println("completance of onContentMessage") }
|
|
||||||
}
|
}
|
||||||
coroutineContext.job.invokeOnCompletion { println("Completed :)") }
|
|
||||||
}.second.join()
|
}.second.join()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
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.bot.getMe
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -10,4 +10,4 @@ suspend fun main(vararg args: String) {
|
|||||||
val bot = telegramBot(botToken)
|
val bot = telegramBot(botToken)
|
||||||
|
|
||||||
println(bot.getMe())
|
println(bot.getMe())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -1,20 +1,23 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
import dev.inmo.tgbotapi.extensions.api.chat.get.getChat
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
import dev.inmo.tgbotapi.extensions.api.send.*
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.sendTextMessage
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.telegramBotWithBehaviourAndLongPolling
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.onContentMessage
|
||||||
import dev.inmo.tgbotapi.extensions.utils.formatting.linkMarkdownV2
|
import dev.inmo.tgbotapi.extensions.utils.formatting.linkMarkdownV2
|
||||||
import dev.inmo.tgbotapi.extensions.utils.formatting.textMentionMarkdownV2
|
import dev.inmo.tgbotapi.extensions.utils.formatting.textMentionMarkdownV2
|
||||||
import dev.inmo.tgbotapi.types.ParseMode.MarkdownV2
|
import dev.inmo.tgbotapi.types.chat.*
|
||||||
import dev.inmo.tgbotapi.types.User
|
import dev.inmo.tgbotapi.types.chat.GroupChat
|
||||||
import dev.inmo.tgbotapi.types.chat.abstracts.*
|
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 dev.inmo.tgbotapi.utils.extensions.escapeMarkdownV2Common
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The main purpose of this bot is just to answer "Oh, hi, " and add user mention here
|
* 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) {
|
suspend fun main(vararg args: String) {
|
||||||
val botToken = args.first()
|
val botToken = args.first()
|
||||||
|
|
||||||
@@ -23,12 +26,12 @@ suspend fun main(vararg args: String) {
|
|||||||
val chat = message.chat
|
val chat = message.chat
|
||||||
if (chat is ChannelChat) {
|
if (chat is ChannelChat) {
|
||||||
val answer = "Hi everybody in this channel \"${chat.title}\""
|
val answer = "Hi everybody in this channel \"${chat.title}\""
|
||||||
sendTextMessage(chat, answer, MarkdownV2)
|
send(chat, answer, MarkdownV2)
|
||||||
return@onContentMessage
|
return@onContentMessage
|
||||||
}
|
}
|
||||||
val answerText = "Oh, hi, " + when (chat) {
|
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 User -> "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id)
|
||||||
|
is PrivateChat -> "${chat.firstName} ${chat.lastName}".textMentionMarkdownV2(chat.id)
|
||||||
is SupergroupChat -> (chat.username ?.username ?: getChat(chat).inviteLink) ?.let {
|
is SupergroupChat -> (chat.username ?.username ?: getChat(chat).inviteLink) ?.let {
|
||||||
chat.title.linkMarkdownV2(it)
|
chat.title.linkMarkdownV2(it)
|
||||||
} ?: chat.title
|
} ?: chat.title
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
@@ -9,7 +9,7 @@ buildscript {
|
|||||||
}
|
}
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
id "org.jetbrains.kotlin.multiplatform" version "$kotlin_version"
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,17 +1,17 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
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.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.*
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.media.*
|
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
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.behaviour_builder.triggers_handling.*
|
||||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
|
import dev.inmo.tgbotapi.extensions.utils.formatting.botCommand
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.formatting.buildEntities
|
||||||
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
||||||
import dev.inmo.tgbotapi.extensions.utils.withContent
|
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.types.message.content.TextContent
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
@@ -86,12 +86,14 @@ suspend fun activateKeyboardsBot(
|
|||||||
return@onMessageDataCallbackQuery
|
return@onMessageDataCallbackQuery
|
||||||
}
|
}
|
||||||
|
|
||||||
editMessageText(
|
val text = "This is $page of $count"
|
||||||
|
|
||||||
|
edit(
|
||||||
it.message.withContent<TextContent>() ?: it.let {
|
it.message.withContent<TextContent>() ?: it.let {
|
||||||
answer(it, "Unsupported message type :(")
|
answer(it, "Unsupported message type :(")
|
||||||
return@onMessageDataCallbackQuery
|
return@onMessageDataCallbackQuery
|
||||||
},
|
},
|
||||||
"This is $page of $count",
|
text,
|
||||||
replyMarkup = inlineKeyboard {
|
replyMarkup = inlineKeyboard {
|
||||||
row {
|
row {
|
||||||
includePageButtons(page, count)
|
includePageButtons(page, count)
|
||||||
@@ -100,6 +102,22 @@ suspend fun activateKeyboardsBot(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onUnhandledCommand {
|
||||||
|
reply(
|
||||||
|
it,
|
||||||
|
buildEntities {
|
||||||
|
+"Use " + botCommand("inline") + " to get pagination inline keyboard"
|
||||||
|
},
|
||||||
|
replyMarkup = replyKeyboard(resizeKeyboard = true, oneTimeKeyboard = true) {
|
||||||
|
row {
|
||||||
|
simpleButton("/inline")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
setMyCommands(BotCommand("inline", "Creates message with pagination inline keyboard"))
|
||||||
|
|
||||||
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||||
println(it)
|
println(it)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -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.bot.TelegramBot
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
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.sendDocument
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.media.sendDocumentsGroup
|
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.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.extensions.behaviour_builder.triggers_handling.onCommandWithArgs
|
||||||
import dev.inmo.tgbotapi.requests.abstracts.asMultipartFile
|
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.BotCommand
|
||||||
import dev.inmo.tgbotapi.types.InputMedia.DocumentMediaGroupMemberInputMedia
|
import dev.inmo.tgbotapi.types.chat.Chat
|
||||||
import dev.inmo.tgbotapi.types.InputMedia.InputMediaDocument
|
import dev.inmo.tgbotapi.types.files.DocumentFile
|
||||||
import dev.inmo.tgbotapi.types.chat.abstracts.Chat
|
import dev.inmo.tgbotapi.types.media.TelegramMediaDocument
|
||||||
import dev.inmo.tgbotapi.types.mediaCountInMediaGroup
|
import dev.inmo.tgbotapi.types.mediaCountInMediaGroup
|
||||||
import kotlinx.coroutines.*
|
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
private const val command = "send_file"
|
private const val command = "send_file"
|
||||||
@@ -50,7 +46,7 @@ suspend fun main(args: Array<String>) {
|
|||||||
)
|
)
|
||||||
else -> sendDocumentsGroup(
|
else -> sendDocumentsGroup(
|
||||||
chat,
|
chat,
|
||||||
files.map { InputMediaDocument(it.asMultipartFile()) },
|
files.map { TelegramMediaDocument(it.asMultipartFile()) },
|
||||||
protectContent = true
|
protectContent = true
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -69,9 +65,9 @@ suspend fun main(args: Array<String>) {
|
|||||||
val chosen = mutableListOf<File>()
|
val chosen = mutableListOf<File>()
|
||||||
|
|
||||||
while (left > 0) {
|
while (left > 0) {
|
||||||
left--
|
val picked = pickFile() ?.takeIf { it.filesize > 0 } ?: continue
|
||||||
val picked = pickFile() ?: continue
|
|
||||||
chosen.add(picked)
|
chosen.add(picked)
|
||||||
|
left--
|
||||||
if (chosen.size >= mediaCountInMediaGroup.last) {
|
if (chosen.size >= mediaCountInMediaGroup.last) {
|
||||||
sendFiles(message.chat, chosen)
|
sendFiles(message.chat, chosen)
|
||||||
chosen.clear()
|
chosen.clear()
|
||||||
@@ -85,7 +81,7 @@ suspend fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!sent) {
|
if (!sent) {
|
||||||
bot.reply(message, "Nothing selected :(")
|
reply(message, "Nothing selected :(")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
@@ -9,7 +9,7 @@ buildscript {
|
|||||||
}
|
}
|
||||||
|
|
||||||
plugins {
|
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.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
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.*
|
||||||
import dev.inmo.tgbotapi.extensions.api.send.media.*
|
import dev.inmo.tgbotapi.extensions.api.send.media.*
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||||
@@ -8,7 +8,6 @@ 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.filters.MessageFilterByChat
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||||
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
|
import dev.inmo.tgbotapi.extensions.utils.shortcuts.*
|
||||||
import dev.inmo.tgbotapi.types.message.abstracts.Message
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
suspend fun activateResenderBot(
|
suspend fun activateResenderBot(
|
||||||
@@ -32,19 +31,19 @@ suspend fun activateResenderBot(
|
|||||||
onVisualGallery {
|
onVisualGallery {
|
||||||
val chat = it.chat ?: return@onVisualGallery
|
val chat = it.chat ?: return@onVisualGallery
|
||||||
withUploadPhotoAction(chat) {
|
withUploadPhotoAction(chat) {
|
||||||
sendVisualMediaGroup(chat, it.map { it.content.toMediaGroupMemberInputMedia() })
|
send(chat, it.map { it.content.toMediaGroupMemberTelegramMedia() })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
onPlaylist {
|
onPlaylist {
|
||||||
val chat = it.chat ?: return@onPlaylist
|
val chat = it.chat ?: return@onPlaylist
|
||||||
withUploadDocumentAction(chat) {
|
withUploadDocumentAction(chat) {
|
||||||
sendPlaylist(chat, it.map { it.content.toMediaGroupMemberInputMedia() })
|
send(chat, it.map { it.content.toMediaGroupMemberTelegramMedia() })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
onDocumentsGroup {
|
onDocumentsGroup {
|
||||||
val chat = it.chat ?: return@onDocumentsGroup
|
val chat = it.chat ?: return@onDocumentsGroup
|
||||||
withUploadDocumentAction(chat) {
|
withUploadDocumentAction(chat) {
|
||||||
sendDocumentsGroup(chat, it.map { it.content.toMediaGroupMemberInputMedia() })
|
send(chat, it.map { it.content.toMediaGroupMemberTelegramMedia() })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
buildscript {
|
buildscript {
|
||||||
repositories {
|
repositories {
|
||||||
jcenter()
|
mavenCentral()
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
|
|||||||
@@ -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.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.*
|
||||||
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.dice.SlotMachineDiceAnimationType
|
||||||
import dev.inmo.tgbotapi.types.message.content.DiceContent
|
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.flow.launchIn
|
|
||||||
import kotlinx.coroutines.flow.onEach
|
|
||||||
|
|
||||||
suspend fun main(args: Array<String>) {
|
suspend fun main(args: Array<String>) {
|
||||||
val bot = telegramBot(args.first())
|
val bot = telegramBot(args.first())
|
||||||
|
|
||||||
val scope = CoroutineScope(Dispatchers.Default)
|
bot.buildBehaviourWithLongPolling(scope = CoroutineScope(Dispatchers.IO)) {
|
||||||
bot.longPolling(scope = scope) {
|
onDice {
|
||||||
filterContentMessages<DiceContent>(scope).onEach {
|
|
||||||
val content = it.content
|
val content = it.content
|
||||||
val dice = content.dice
|
val dice = content.dice
|
||||||
val diceType = dice.animationType
|
val diceType = dice.animationType
|
||||||
|
|
||||||
safely ({ it.printStackTrace() }) {
|
if (diceType == SlotMachineDiceAnimationType) {
|
||||||
if (diceType == SlotMachineDiceAnimationType) {
|
val result = dice.calculateSlotMachineResult() ?: return@onDice
|
||||||
val result = dice.calculateSlotMachineResult() ?: return@safely
|
reply(it, "${result.leftReel}|${result.centerReel}|${result.rightReel}")
|
||||||
bot.reply(it, "${result.leftReel}|${result.centerReel}|${result.rightReel}")
|
} else {
|
||||||
} else {
|
reply(it, "There is no slot machine dice in message")
|
||||||
bot.reply(it, "There is no slot machine dice in message")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}.launchIn(scope)
|
}
|
||||||
}
|
}.join()
|
||||||
|
}
|
||||||
scope.coroutineContext[Job]!!.join()
|
|
||||||
}
|
|
||||||
|
|||||||
17
WebApp/README.md
Normal file
17
WebApp/README.md
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# WebApp
|
||||||
|
|
||||||
|
Here you may find simple example of `WebApp`. For work of this example you will need one of two things:
|
||||||
|
|
||||||
|
* Your own domain with SSL (letsencrypt is okay)
|
||||||
|
* Test account in telegram
|
||||||
|
|
||||||
|
What is there in this module:
|
||||||
|
|
||||||
|
* JVM part of this example is a server with simple static webapp sharing and bot which just gives the webapp button to open webapp
|
||||||
|
* JS part is the WebApp with one button and reacting to chaged user theme and app viewport
|
||||||
|
|
||||||
|
## How to run
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
./gradlew run --args="TOKEN WEB_APP_ADDRESS"
|
||||||
|
```
|
||||||
57
WebApp/build.gradle
Normal file
57
WebApp/build.gradle
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
id "org.jetbrains.kotlin.multiplatform"
|
||||||
|
id "org.jetbrains.kotlin.plugin.serialization"
|
||||||
|
}
|
||||||
|
|
||||||
|
apply plugin: 'application'
|
||||||
|
|
||||||
|
kotlin {
|
||||||
|
jvm()
|
||||||
|
js(IR) {
|
||||||
|
browser()
|
||||||
|
binaries.executable()
|
||||||
|
}
|
||||||
|
|
||||||
|
sourceSets {
|
||||||
|
commonMain {
|
||||||
|
dependencies {
|
||||||
|
implementation kotlin('stdlib')
|
||||||
|
implementation "org.jetbrains.kotlinx:kotlinx-serialization-json:$serialization_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
jsMain {
|
||||||
|
dependencies {
|
||||||
|
implementation "dev.inmo:tgbotapi.webapps:$telegram_bot_api_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
jvmMain {
|
||||||
|
dependencies {
|
||||||
|
implementation "dev.inmo:tgbotapi:$telegram_bot_api_version"
|
||||||
|
implementation "dev.inmo:micro_utils.ktor.server:$micro_utils_version"
|
||||||
|
implementation "io.ktor:ktor-server-cio:$ktor_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
application {
|
||||||
|
mainClassName = "WebAppServerKt"
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.getByName("compileKotlinJvm")
|
||||||
|
.dependsOn(jsBrowserDistribution)
|
||||||
|
tasks.getByName("compileKotlinJvm").configure {
|
||||||
|
mustRunAfter jsBrowserDistribution
|
||||||
|
}
|
||||||
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
|
||||||
|
)
|
||||||
106
WebApp/src/jsMain/kotlin/main.kt
Normal file
106
WebApp/src/jsMain/kotlin/main.kt
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
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 io.ktor.client.HttpClient
|
||||||
|
import io.ktor.client.request.*
|
||||||
|
import io.ktor.client.statement.bodyAsText
|
||||||
|
import io.ktor.http.*
|
||||||
|
import io.ktor.http.content.TextContent
|
||||||
|
import kotlinx.browser.document
|
||||||
|
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) {
|
||||||
|
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" }) {
|
||||||
|
client.post("${window.location.origin.removeSuffix("/")}/inline") {
|
||||||
|
parameter(webAppQueryIdField, it)
|
||||||
|
setBody(TextContent("Clicked", ContentType.Text.Plain))
|
||||||
|
document.body ?.log(url.build().toString())
|
||||||
|
}.coroutineContext.job.join()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
appendText("Example button")
|
||||||
|
} ?: window.alert("Unable to load body")
|
||||||
|
webApp.apply {
|
||||||
|
onThemeChanged {
|
||||||
|
document.body ?.log("Theme changed: ${webApp.themeParams}")
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
window.alert(it.stackTraceToString())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
16
WebApp/src/jsMain/resources/index.html
Normal file
16
WebApp/src/jsMain/resources/index.html
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<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>
|
||||||
|
<script type="application/javascript" src="https://telegram.org/js/telegram-web-app.js"></script>
|
||||||
|
<script type="application/javascript" src="WebApp.js"></script>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
123
WebApp/src/jvmMain/kotlin/WebAppServer.kt
Normal file
123
WebApp/src/jvmMain/kotlin/WebAppServer.kt
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
||||||
|
import dev.inmo.micro_utils.crypto.hmacSha256
|
||||||
|
import dev.inmo.micro_utils.ktor.server.createKtorServer
|
||||||
|
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
||||||
|
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.telegramBot
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||||
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.formatting.botCommand
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.formatting.buildEntities
|
||||||
|
import dev.inmo.tgbotapi.extensions.utils.types.buttons.*
|
||||||
|
import dev.inmo.tgbotapi.types.BotCommand
|
||||||
|
import dev.inmo.tgbotapi.types.InlineQueries.InlineQueryResult.InlineQueryResultArticle
|
||||||
|
import dev.inmo.tgbotapi.types.InlineQueries.InputMessageContent.InputTextMessageContent
|
||||||
|
import dev.inmo.tgbotapi.types.webAppQueryIdField
|
||||||
|
import dev.inmo.tgbotapi.types.webapps.WebAppInfo
|
||||||
|
import dev.inmo.tgbotapi.utils.PreviewFeature
|
||||||
|
import dev.inmo.tgbotapi.utils.TelegramAPIUrlsKeeper
|
||||||
|
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:
|
||||||
|
*
|
||||||
|
* * Telegram Token
|
||||||
|
* * URL where will be placed
|
||||||
|
*
|
||||||
|
* 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 telegramBotAPIUrlsKeeper = TelegramAPIUrlsKeeper(
|
||||||
|
args.first(),
|
||||||
|
testServer = args.any { it == "testServer" }
|
||||||
|
)
|
||||||
|
val bot = telegramBot(telegramBotAPIUrlsKeeper)
|
||||||
|
createKtorServer(
|
||||||
|
"0.0.0.0",
|
||||||
|
8080,
|
||||||
|
additionalEngineEnvironmentConfigurator = {
|
||||||
|
parentCoroutineContext += Dispatchers.IO
|
||||||
|
}
|
||||||
|
) {
|
||||||
|
routing {
|
||||||
|
static {
|
||||||
|
files(File("WebApp/build/distributions"))
|
||||||
|
default("WebApp/build/distributions/index.html")
|
||||||
|
}
|
||||||
|
post("inline") {
|
||||||
|
val requestBody = call.receiveText()
|
||||||
|
val queryId = call.parameters[webAppQueryIdField] ?: error("$webAppQueryIdField should be presented")
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
bot.buildBehaviourWithLongPolling(
|
||||||
|
defaultExceptionsHandler = { it.printStackTrace() }
|
||||||
|
) {
|
||||||
|
onCommand("reply_markup") {
|
||||||
|
reply(
|
||||||
|
it,
|
||||||
|
"Button",
|
||||||
|
replyMarkup = replyKeyboard(resizeKeyboard = true, oneTimeKeyboard = true) {
|
||||||
|
row {
|
||||||
|
webAppButton("Open WebApp", WebAppInfo(args[1]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
)
|
||||||
|
}
|
||||||
|
onCommand("inline") {
|
||||||
|
reply(
|
||||||
|
it,
|
||||||
|
"Button",
|
||||||
|
replyMarkup = inlineKeyboard {
|
||||||
|
row {
|
||||||
|
webAppButton("Open WebApp", WebAppInfo(args[1]))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
)
|
||||||
|
}
|
||||||
|
onUnhandledCommand {
|
||||||
|
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"),
|
||||||
|
BotCommand("inline", "Use to get inline keyboard with web app trigger"),
|
||||||
|
)
|
||||||
|
allUpdatesFlow.subscribeSafelyWithoutExceptions(this) {
|
||||||
|
println(it)
|
||||||
|
}
|
||||||
|
println(getMe())
|
||||||
|
}.join()
|
||||||
|
}
|
||||||
15
build.gradle
15
build.gradle
@@ -1,7 +1,18 @@
|
|||||||
|
buildscript {
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
|
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlin_version"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
allprojects {
|
allprojects {
|
||||||
repositories {
|
repositories {
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
jcenter()
|
mavenCentral()
|
||||||
if (project.hasProperty("GITHUB_USER") && project.hasProperty("GITHUB_TOKEN")) {
|
if (project.hasProperty("GITHUB_USER") && project.hasProperty("GITHUB_TOKEN")) {
|
||||||
maven {
|
maven {
|
||||||
url "https://maven.pkg.github.com/InsanusMokrassar/TelegramBotAPI"
|
url "https://maven.pkg.github.com/InsanusMokrassar/TelegramBotAPI"
|
||||||
@@ -12,4 +23,4 @@ allprojects {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
kotlin.code.style=official
|
kotlin.code.style=official
|
||||||
org.gradle.parallel=true
|
org.gradle.parallel=true
|
||||||
|
# Due to parallel compilation project require next amount of memory on full build
|
||||||
|
org.gradle.jvmargs=-Xmx768m
|
||||||
|
|
||||||
|
|
||||||
kotlin_version=1.6.10
|
kotlin_version=1.7.10
|
||||||
telegram_bot_api_version=0.38.12
|
telegram_bot_api_version=2.2.0
|
||||||
micro_utils_version=0.9.19
|
micro_utils_version=0.11.12
|
||||||
|
serialization_version=1.3.3
|
||||||
|
ktor_version=2.0.3
|
||||||
|
|||||||
@@ -16,4 +16,6 @@ include ":KeyboardsBot:jvm_launcher"
|
|||||||
|
|
||||||
include ":SlotMachineDetectorBot"
|
include ":SlotMachineDetectorBot"
|
||||||
|
|
||||||
|
include ":WebApp"
|
||||||
|
|
||||||
include ":FSMBot"
|
include ":FSMBot"
|
||||||
|
|||||||
Reference in New Issue
Block a user