Skip to content

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:

  • Create your own flow of locations. For example:
    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
      }
    }
    
  • In case you needed, create your collector to store the message with live location:
    val currentMessageState = MutableStateFlow<ContentMessage<LocationContent>?>(null)
    
  • Start handle live location. handleLiveLocation works synchronosly (in current coroutine) and will ends only when your flow will ends. Thats why there are two ways to call it:
    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