Live Location

Bot API allows you to send live locations and update them during their lifetime. In this library there are several ways to use this API:

sendLiveLocation

In the Bot API there is no independent sendLiveLocation method, instead it is suggested to use sendLocation with setting up live_period. In this library in difference with original Bot API live location is special request. It was required because of in fact live locations and static locations are different types of location info and you as bot developer may interact with them differently.

Anyway, in common case the logic looks like:

startLiveLocation

In difference with sendLiveLocation, startLiveLocation using LiveLocationProvider. With this provider you need not to handle chat and message ids and keep some other data for location changes. Instead, you workflow with provider will be next:

Besides, LiveLocationProvider contains different useful parameters about live location

handleLiveLocation

This way of live locations handling is based on coroutines Flow and allow you to pass some external Flow with EditLiveLocationInfo. So, workflow:

flow {
  var i = 0
  while (isActive) {
    val newInfo = EditLiveLocationInfo(
      latitude = i.toDouble(),
      longitude = i.toDouble(),
      replyMarkup = flatInlineKeyboard {
        dataButton("Cancel", "cancel")
      }
    )
    emit(newInfo)
    i++
    delay(10000L) // 10 seconds
  }
}
val currentMessageState = MutableStateFlow<ContentMessage<LocationContent>?>(null)
handleLiveLocation(
  it.chat.id,
  locationsFlow,
  sentMessageFlow = FlowCollector { currentMessageState.emit(it) }
)
// this code will be called after `locationsFlow` will ends

OR

scope.launch {
  handleLiveLocation(
    it.chat.id,
    locationsFlow,
    sentMessageFlow = FlowCollector { currentMessageState.emit(it) }
  )
}
// this code will be called right after launch will be completed

See our example to get more detailed sample


Revision #6
Created 4 August 2022 19:27:12 by InsanusMokrassar
Updated 17 February 2023 10:06:56 by InsanusMokrassar