mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2025-12-17 11:45:36 +00:00
Compare commits
32 Commits
5fb1239d63
...
3.0.0
| Author | SHA1 | Date | |
|---|---|---|---|
| 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 |
@@ -1,5 +1,6 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.AccumulatorFlow
|
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.*
|
||||||
@@ -55,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
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,17 +14,19 @@ import java.io.File
|
|||||||
*/
|
*/
|
||||||
suspend fun main(args: Array<String>) {
|
suspend fun main(args: Array<String>) {
|
||||||
val botToken = args.first()
|
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()
|
directoryOrFile.mkdirs()
|
||||||
|
|
||||||
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()
|
runCatching {
|
||||||
writeBytes(bot.downloadFile(pathedFile))
|
bot.downloadFile(it.content.media, outFile)
|
||||||
|
}.onFailure {
|
||||||
|
it.printStackTrace()
|
||||||
}
|
}
|
||||||
reply(it, "Saved to ${file.absolutePath}")
|
reply(it, "Saved to ${outFile.absolutePath}")
|
||||||
}
|
}
|
||||||
onContentMessage { println(it) }
|
onContentMessage { println(it) }
|
||||||
}.second.join()
|
}.second.join()
|
||||||
|
|||||||
@@ -16,23 +16,30 @@ suspend fun main(vararg args: String) {
|
|||||||
val botToken = args.first()
|
val botToken = args.first()
|
||||||
|
|
||||||
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
telegramBotWithBehaviourAndLongPolling(botToken, CoroutineScope(Dispatchers.IO)) {
|
||||||
onContentMessage(subcontextUpdatesFilter = { _, _ -> true }) {
|
onContentMessage {
|
||||||
val toAnswer = buildEntities {
|
val toAnswer = buildEntities {
|
||||||
when (val forwardInfo = it.forwardInfo) {
|
when (val forwardInfo = it.forwardInfo) {
|
||||||
null -> +"There is no forward info"
|
null -> +"There is no forward info"
|
||||||
is AnonymousForwardInfo -> {
|
is ForwardInfo.ByAnonymous -> {
|
||||||
regular("Anonymous user which signed as \"") + code(forwardInfo.senderName) + "\""
|
regular("Anonymous user which signed as \"") + code(forwardInfo.senderName) + "\""
|
||||||
}
|
}
|
||||||
is UserForwardInfo -> {
|
is ForwardInfo.ByUser -> {
|
||||||
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"})"
|
||||||
}
|
}
|
||||||
is ForwardFromChannelInfo -> regular("Channel (") + code((forwardInfo.channelChat).title) + ")"
|
is ForwardInfo.PublicChat.FromChannel -> regular("Channel (") + code(forwardInfo.channelChat.title) + ")"
|
||||||
is ForwardFromSupergroupInfo -> regular("Supergroup (") + code((forwardInfo.group).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)
|
reply(it, toAnswer)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
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
|
||||||
@@ -27,7 +26,7 @@ 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) {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ 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.bot.setMyCommands
|
import dev.inmo.tgbotapi.extensions.api.bot.setMyCommands
|
||||||
import dev.inmo.tgbotapi.extensions.api.edit.text.editMessageText
|
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.behaviour_builder.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.*
|
||||||
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.triggers_handling.*
|
||||||
@@ -88,7 +88,7 @@ suspend fun activateKeyboardsBot(
|
|||||||
|
|
||||||
val text = "This is $page of $count"
|
val text = "This is $page of $count"
|
||||||
|
|
||||||
editMessageText(
|
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
|
||||||
@@ -100,6 +100,7 @@ suspend fun activateKeyboardsBot(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
answer(it)
|
||||||
}
|
}
|
||||||
|
|
||||||
onUnhandledCommand {
|
onUnhandledCommand {
|
||||||
|
|||||||
@@ -3,15 +3,15 @@ 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.buildBehaviourWithLongPolling
|
import dev.inmo.tgbotapi.extensions.behaviour_builder.buildBehaviourWithLongPolling
|
||||||
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.types.BotCommand
|
import dev.inmo.tgbotapi.types.BotCommand
|
||||||
import dev.inmo.tgbotapi.types.chat.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.media.TelegramMediaDocument
|
||||||
import dev.inmo.tgbotapi.types.mediaCountInMediaGroup
|
import dev.inmo.tgbotapi.types.mediaCountInMediaGroup
|
||||||
import java.io.File
|
import java.io.File
|
||||||
@@ -81,7 +81,7 @@ suspend fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!sent) {
|
if (!sent) {
|
||||||
bot.reply(message, "Nothing selected :(")
|
reply(message, "Nothing selected :(")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,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.toMediaGroupMemberTelegramMedia() })
|
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.toMediaGroupMemberTelegramMedia() })
|
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.toMediaGroupMemberTelegramMedia() })
|
send(chat, it.map { it.content.toMediaGroupMemberTelegramMedia() })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -17,9 +17,9 @@ suspend fun main(args: Array<String>) {
|
|||||||
|
|
||||||
if (diceType == SlotMachineDiceAnimationType) {
|
if (diceType == SlotMachineDiceAnimationType) {
|
||||||
val result = dice.calculateSlotMachineResult() ?: return@onDice
|
val result = dice.calculateSlotMachineResult() ?: return@onDice
|
||||||
bot.reply(it, "${result.leftReel}|${result.centerReel}|${result.rightReel}")
|
reply(it, "${result.leftReel}|${result.centerReel}|${result.rightReel}")
|
||||||
} else {
|
} else {
|
||||||
bot.reply(it, "There is no slot machine dice in message")
|
reply(it, "There is no slot machine dice in message")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.join()
|
}.join()
|
||||||
|
|||||||
@@ -1,6 +1,8 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
|
import dev.inmo.micro_utils.coroutines.launchSafelyWithoutExceptions
|
||||||
import dev.inmo.tgbotapi.types.webAppQueryIdField
|
import dev.inmo.tgbotapi.types.webAppQueryIdField
|
||||||
import dev.inmo.tgbotapi.webapps.*
|
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.HttpClient
|
||||||
import io.ktor.client.request.*
|
import io.ktor.client.request.*
|
||||||
import io.ktor.client.statement.bodyAsText
|
import io.ktor.client.statement.bodyAsText
|
||||||
@@ -15,8 +17,8 @@ import kotlinx.serialization.json.Json
|
|||||||
import org.w3c.dom.HTMLElement
|
import org.w3c.dom.HTMLElement
|
||||||
|
|
||||||
fun HTMLElement.log(text: String) {
|
fun HTMLElement.log(text: String) {
|
||||||
appendElement("p", {})
|
|
||||||
appendText(text)
|
appendText(text)
|
||||||
|
appendElement("p", {})
|
||||||
}
|
}
|
||||||
|
|
||||||
fun main() {
|
fun main() {
|
||||||
@@ -39,13 +41,17 @@ fun main() {
|
|||||||
}
|
}
|
||||||
val dataIsSafe = response.bodyAsText().toBoolean()
|
val dataIsSafe = response.bodyAsText().toBoolean()
|
||||||
|
|
||||||
document.body ?.appendElement("div") {
|
document.body ?.log(
|
||||||
textContent = if (dataIsSafe) {
|
if (dataIsSafe) {
|
||||||
"Data is safe"
|
"Data is safe"
|
||||||
} else {
|
} else {
|
||||||
"Data is unsafe"
|
"Data is unsafe"
|
||||||
}
|
}
|
||||||
}
|
)
|
||||||
|
|
||||||
|
document.body ?.log(
|
||||||
|
webApp.initDataUnsafe.chat.toString()
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
document.body ?.appendElement("button") {
|
document.body ?.appendElement("button") {
|
||||||
@@ -69,6 +75,28 @@ fun main() {
|
|||||||
onViewportChanged {
|
onViewportChanged {
|
||||||
document.body ?.log("Viewport changed: ${it.isStateStable}")
|
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()
|
webApp.ready()
|
||||||
}.onFailure {
|
}.onFailure {
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import dev.inmo.micro_utils.coroutines.subscribeSafelyWithoutExceptions
|
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.micro_utils.ktor.server.createKtorServer
|
||||||
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
import dev.inmo.tgbotapi.extensions.api.answers.answer
|
||||||
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
import dev.inmo.tgbotapi.extensions.api.bot.getMe
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ org.gradle.parallel=true
|
|||||||
org.gradle.jvmargs=-Xmx768m
|
org.gradle.jvmargs=-Xmx768m
|
||||||
|
|
||||||
|
|
||||||
kotlin_version=1.6.21
|
kotlin_version=1.7.10
|
||||||
telegram_bot_api_version=2.0.3
|
telegram_bot_api_version=3.0.2
|
||||||
micro_utils_version=0.11.0
|
micro_utils_version=0.12.0
|
||||||
serialization_version=1.3.3
|
serialization_version=1.4.0-RC
|
||||||
ktor_version=2.0.2
|
ktor_version=2.0.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
|
distributionPath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
|
||||||
|
|||||||
Reference in New Issue
Block a user