1
0
mirror of https://github.com/InsanusMokrassar/TelegramBotAPI.git synced 2024-11-26 03:58:44 +00:00

fill edit and stop live location methods

This commit is contained in:
InsanusMokrassar 2020-02-07 00:40:47 +06:00
parent e7bdc85279
commit c7d5fdd2e0
5 changed files with 120 additions and 26 deletions

View File

@ -1,12 +1,13 @@
package com.github.insanusmokrassar.TelegramBotAPI.requests package com.github.insanusmokrassar.TelegramBotAPI.requests
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.requests.edit.LiveLocation.EditChatMessageLiveLocation import com.github.insanusmokrassar.TelegramBotAPI.requests.edit.LiveLocation.*
import com.github.insanusmokrassar.TelegramBotAPI.requests.edit.LiveLocation.StopChatMessageLiveLocation
import com.github.insanusmokrassar.TelegramBotAPI.requests.send.SendLocation import com.github.insanusmokrassar.TelegramBotAPI.requests.send.SendLocation
import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.KeyboardMarkup
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.LocationContent
import com.soywiz.klock.DateTime import com.soywiz.klock.DateTime
import com.soywiz.klock.TimeSpan import com.soywiz.klock.TimeSpan
import io.ktor.utils.io.core.Closeable import io.ktor.utils.io.core.Closeable
@ -16,9 +17,7 @@ private val livePeriodDelayDouble = ((livePeriodLimit.last - 60L) * 1000L).toDou
class LiveLocation internal constructor( class LiveLocation internal constructor(
private val scope: CoroutineScope, private val scope: CoroutineScope,
private val requestsExecutor: RequestsExecutor, private val requestsExecutor: RequestsExecutor,
private val chatId: ChatIdentifier, initMessage: ContentMessage<LocationContent>
private val messageId: MessageIdentifier,
location: Location
) : Closeable { ) : Closeable {
var isClosed: Boolean = false var isClosed: Boolean = false
private set private set
@ -26,8 +25,8 @@ class LiveLocation internal constructor(
val leftUntilCloseMillis: TimeSpan val leftUntilCloseMillis: TimeSpan
get() = autoCloseTime - DateTime.now() get() = autoCloseTime - DateTime.now()
private var updateJob: Job? = null private var updateJob: Job? = null
var lastLocation: Location = location private var message: ContentMessage<LocationContent> = initMessage
private set(value) { set(value) {
field = value field = value
updateJob ?.cancel() updateJob ?.cancel()
updateJob = scope.launch { updateJob = scope.launch {
@ -37,9 +36,11 @@ class LiveLocation internal constructor(
close() close()
} }
} }
val lastLocation: Location
get() = message.content.location
init { init {
this.lastLocation = location // required to init updateJob message = initMessage // required to init updateJob
} }
suspend fun updateLocation( suspend fun updateLocation(
@ -47,15 +48,11 @@ class LiveLocation internal constructor(
replyMarkup: InlineKeyboardMarkup? = null replyMarkup: InlineKeyboardMarkup? = null
): Location { ): Location {
if (!isClosed) { if (!isClosed) {
lastLocation = requestsExecutor.execute( message = requestsExecutor.editLiveLocation(
EditChatMessageLiveLocation( message,
chatId, location,
messageId, replyMarkup
location.latitude, )
location.longitude,
replyMarkup
)
).content.location
return lastLocation return lastLocation
} else { } else {
error("LiveLocation is closed") error("LiveLocation is closed")
@ -69,12 +66,7 @@ class LiveLocation internal constructor(
isClosed = true isClosed = true
updateJob ?.cancel() updateJob ?.cancel()
scope.launch { scope.launch {
requestsExecutor.execute( requestsExecutor.stopLiveLocation(message)
StopChatMessageLiveLocation(
chatId,
messageId
)
)
} }
} }
} }
@ -103,8 +95,6 @@ suspend fun RequestsExecutor.startLiveLocation(
return LiveLocation( return LiveLocation(
scope, scope,
this, this,
chatId, locationMessage
locationMessage.messageId,
locationMessage.content.location
) )
} }

View File

@ -1,8 +1,10 @@
package com.github.insanusmokrassar.TelegramBotAPI.requests.edit.LiveLocation package com.github.insanusmokrassar.TelegramBotAPI.requests.edit.LiveLocation
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.requests.edit.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.requests.edit.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.LocationContent import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.LocationContent
@ -29,3 +31,54 @@ data class EditChatMessageLiveLocation(
override val requestSerializer: SerializationStrategy<*> override val requestSerializer: SerializationStrategy<*>
get() = serializer() get() = serializer()
} }
suspend fun RequestsExecutor.editLiveLocation(
chatId: ChatIdentifier,
messageId: MessageIdentifier,
latitude: Double,
longitude: Double,
replyMarkup: InlineKeyboardMarkup? = null
) = execute(
EditChatMessageLiveLocation(
chatId, messageId, latitude, longitude, replyMarkup
)
)
suspend fun RequestsExecutor.editLiveLocation(
chat: Chat,
messageId: MessageIdentifier,
latitude: Double,
longitude: Double,
replyMarkup: InlineKeyboardMarkup? = null
) = editLiveLocation(chat.id, messageId, latitude, longitude, replyMarkup)
suspend fun RequestsExecutor.editLiveLocation(
message: ContentMessage<LocationContent>,
latitude: Double,
longitude: Double,
replyMarkup: InlineKeyboardMarkup? = null
) = editLiveLocation(message.chat, message.messageId, latitude, longitude, replyMarkup)
suspend fun RequestsExecutor.editLiveLocation(
chatId: ChatIdentifier,
messageId: MessageIdentifier,
location: Location,
replyMarkup: InlineKeyboardMarkup? = null
) = execute(
EditChatMessageLiveLocation(
chatId, messageId, location.latitude, location.longitude, replyMarkup
)
)
suspend fun RequestsExecutor.editLiveLocation(
chat: Chat,
messageId: MessageIdentifier,
location: Location,
replyMarkup: InlineKeyboardMarkup? = null
) = editLiveLocation(chat.id, messageId, location.latitude, location.longitude, replyMarkup)
suspend fun RequestsExecutor.editLiveLocation(
message: ContentMessage<LocationContent>,
location: Location,
replyMarkup: InlineKeyboardMarkup? = null
) = editLiveLocation(message.chat, message.messageId, location.latitude, location.longitude, replyMarkup)

View File

@ -1,5 +1,6 @@
package com.github.insanusmokrassar.TelegramBotAPI.requests.edit.LiveLocation package com.github.insanusmokrassar.TelegramBotAPI.requests.edit.LiveLocation
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.requests.edit.abstracts.* import com.github.insanusmokrassar.TelegramBotAPI.requests.edit.abstracts.*
import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup
@ -20,3 +21,19 @@ data class EditInlineMessageLiveLocation(
override val requestSerializer: SerializationStrategy<*> override val requestSerializer: SerializationStrategy<*>
get() = serializer() get() = serializer()
} }
suspend fun RequestsExecutor.editLiveLocation(
inlineMessageId: InlineMessageIdentifier,
latitude: Double,
longitude: Double,
replyMarkup: InlineKeyboardMarkup? = null
) = execute(
EditInlineMessageLiveLocation(
inlineMessageId, latitude, longitude, replyMarkup
)
)
suspend fun RequestsExecutor.editLiveLocation(
inlineMessageId: InlineMessageIdentifier,
location: Location,
replyMarkup: InlineKeyboardMarkup? = null
) = editLiveLocation(inlineMessageId, location.latitude, location.longitude, replyMarkup)

View File

@ -1,9 +1,11 @@
package com.github.insanusmokrassar.TelegramBotAPI.requests.edit.LiveLocation package com.github.insanusmokrassar.TelegramBotAPI.requests.edit.LiveLocation
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.requests.edit.abstracts.EditChatMessage import com.github.insanusmokrassar.TelegramBotAPI.requests.edit.abstracts.EditChatMessage
import com.github.insanusmokrassar.TelegramBotAPI.requests.edit.abstracts.EditReplyMessage import com.github.insanusmokrassar.TelegramBotAPI.requests.edit.abstracts.EditReplyMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.*
import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup import com.github.insanusmokrassar.TelegramBotAPI.types.buttons.InlineKeyboardMarkup
import com.github.insanusmokrassar.TelegramBotAPI.types.chat.abstracts.Chat
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.ContentMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass import com.github.insanusmokrassar.TelegramBotAPI.types.message.abstracts.TelegramBotAPIMessageDeserializationStrategyClass
import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.LocationContent import com.github.insanusmokrassar.TelegramBotAPI.types.message.content.LocationContent
@ -26,3 +28,25 @@ data class StopChatMessageLiveLocation(
override val requestSerializer: SerializationStrategy<*> override val requestSerializer: SerializationStrategy<*>
get() = serializer() get() = serializer()
} }
suspend fun RequestsExecutor.stopLiveLocation(
chatId: ChatIdentifier,
messageId: MessageIdentifier,
replyMarkup: InlineKeyboardMarkup? = null
) = execute(
StopChatMessageLiveLocation(
chatId, messageId, replyMarkup
)
)
suspend fun RequestsExecutor.stopLiveLocation(
chat: Chat,
messageId: MessageIdentifier,
replyMarkup: InlineKeyboardMarkup? = null
) = stopLiveLocation(chat.id, messageId, replyMarkup)
suspend fun RequestsExecutor.stopLiveLocation(
message: ContentMessage<LocationContent>,
replyMarkup: InlineKeyboardMarkup? = null
) = stopLiveLocation(message.chat, message.messageId, replyMarkup)

View File

@ -1,5 +1,6 @@
package com.github.insanusmokrassar.TelegramBotAPI.requests.edit.LiveLocation package com.github.insanusmokrassar.TelegramBotAPI.requests.edit.LiveLocation
import com.github.insanusmokrassar.TelegramBotAPI.bot.RequestsExecutor
import com.github.insanusmokrassar.TelegramBotAPI.requests.edit.abstracts.EditInlineMessage import com.github.insanusmokrassar.TelegramBotAPI.requests.edit.abstracts.EditInlineMessage
import com.github.insanusmokrassar.TelegramBotAPI.requests.edit.abstracts.EditReplyMessage import com.github.insanusmokrassar.TelegramBotAPI.requests.edit.abstracts.EditReplyMessage
import com.github.insanusmokrassar.TelegramBotAPI.types.* import com.github.insanusmokrassar.TelegramBotAPI.types.*
@ -17,3 +18,12 @@ data class StopInlineMessageLiveLocation(
override val requestSerializer: SerializationStrategy<*> override val requestSerializer: SerializationStrategy<*>
get() = serializer() get() = serializer()
} }
suspend fun RequestsExecutor.stopLiveLocation(
inlineMessageId: InlineMessageIdentifier,
replyMarkup: InlineKeyboardMarkup? = null
) = execute(
StopInlineMessageLiveLocation(
inlineMessageId, replyMarkup
)
)