mirror of
https://github.com/InsanusMokrassar/TelegramBotAPI-examples.git
synced 2026-04-09 03:22:23 +00:00
Compare commits
16 Commits
5.1.0
...
fcedbf30da
| Author | SHA1 | Date | |
|---|---|---|---|
| fcedbf30da | |||
|
|
fd030a92e3 | ||
| d54abf0b32 | |||
|
|
877a20188f | ||
| d0151ff048 | |||
| f8f517cfbb | |||
|
|
b3cbbac917 | ||
| 3e85bb4b22 | |||
| 4e0fb1c137 | |||
| d8c90ef377 | |||
| cb84fd0884 | |||
| 4379862c78 | |||
|
|
ac1d812db0 | ||
| f52590868c | |||
| 51c300c734 | |||
| f6082cff30 |
@@ -48,7 +48,7 @@ suspend fun main(args: Array<String>) {
|
|||||||
+"Send me some content or " + botCommand("stop") + " if you want to stop sending"
|
+"Send me some content or " + botCommand("stop") + " if you want to stop sending"
|
||||||
}
|
}
|
||||||
|
|
||||||
val contentMessage = waitContentMessage().filter { message ->
|
val contentMessage = waitAnyContentMessage().filter { message ->
|
||||||
message.sameThread(it.sourceMessage)
|
message.sameThread(it.sourceMessage)
|
||||||
}.first()
|
}.first()
|
||||||
val content = contentMessage.content
|
val content = contentMessage.content
|
||||||
|
|||||||
@@ -1,12 +1,14 @@
|
|||||||
import dev.inmo.tgbotapi.extensions.api.send.reply
|
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.makeLink
|
||||||
import dev.inmo.tgbotapi.types.chat.CommonBot
|
import dev.inmo.tgbotapi.types.chat.CommonBot
|
||||||
import dev.inmo.tgbotapi.types.chat.CommonUser
|
import dev.inmo.tgbotapi.types.chat.CommonUser
|
||||||
import dev.inmo.tgbotapi.types.chat.ExtendedBot
|
import dev.inmo.tgbotapi.types.chat.ExtendedBot
|
||||||
import dev.inmo.tgbotapi.types.message.*
|
import dev.inmo.tgbotapi.types.message.*
|
||||||
|
import dev.inmo.tgbotapi.utils.buildEntities
|
||||||
import dev.inmo.tgbotapi.utils.code
|
import dev.inmo.tgbotapi.utils.code
|
||||||
|
import dev.inmo.tgbotapi.utils.link
|
||||||
import dev.inmo.tgbotapi.utils.regular
|
import dev.inmo.tgbotapi.utils.regular
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
|
|
||||||
@@ -40,7 +42,14 @@ suspend fun main(vararg args: String) {
|
|||||||
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 ForwardInfo.PublicChat.FromChannel -> regular("Channel (") + code(forwardInfo.channelChat.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.FromSupergroup -> regular("Supergroup (") + code(forwardInfo.group.title) + ")"
|
||||||
is ForwardInfo.PublicChat.SentByChannel -> regular("Sent by channel (") + code(forwardInfo.channelChat.title) + ")"
|
is ForwardInfo.PublicChat.SentByChannel -> regular("Sent by channel (") + code(forwardInfo.channelChat.title) + ")"
|
||||||
}
|
}
|
||||||
|
|||||||
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()
|
||||||
|
}
|
||||||
|
|
||||||
@@ -109,7 +109,11 @@ suspend fun main(args: Array<String>) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bot.buildBehaviourWithLongPolling {
|
bot.buildBehaviourWithLongPolling(
|
||||||
|
defaultExceptionsHandler = {
|
||||||
|
println(it)
|
||||||
|
}
|
||||||
|
) {
|
||||||
onCommand("simple", initialFilter = { it.chat is PublicChat && it.fromUserMessageOrNull() ?.user ?.id == allowedAdmin }) {
|
onCommand("simple", initialFilter = { it.chat is PublicChat && it.fromUserMessageOrNull() ?.user ?.id == allowedAdmin }) {
|
||||||
val replyMessage = it.replyTo
|
val replyMessage = it.replyTo
|
||||||
val userInReply = replyMessage ?.fromUserMessageOrNull() ?.user ?.id ?: return@onCommand
|
val userInReply = replyMessage ?.fromUserMessageOrNull() ?.user ?.id ?: return@onCommand
|
||||||
|
|||||||
@@ -1,11 +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
|
# Due to parallel compilation project require next amount of memory on full build
|
||||||
org.gradle.jvmargs=-Xmx768m
|
org.gradle.jvmargs=-Xmx1g
|
||||||
|
|
||||||
|
|
||||||
kotlin_version=1.7.22
|
kotlin_version=1.8.10
|
||||||
telegram_bot_api_version=5.1.0
|
telegram_bot_api_version=6.0.3
|
||||||
micro_utils_version=0.16.8
|
micro_utils_version=0.17.3
|
||||||
serialization_version=1.4.1
|
serialization_version=1.5.0
|
||||||
ktor_version=2.2.3
|
ktor_version=2.2.4
|
||||||
|
|||||||
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.6-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-bin.zip
|
||||||
|
|||||||
@@ -32,3 +32,5 @@ include ":TopicsHandling"
|
|||||||
include ":UserChatShared"
|
include ":UserChatShared"
|
||||||
|
|
||||||
include ":RightsChangerBot"
|
include ":RightsChangerBot"
|
||||||
|
|
||||||
|
include ":LiveLocationsBot"
|
||||||
|
|||||||
Reference in New Issue
Block a user