From db1e8ee1f08d371e270146237e417eab4c0d20d7 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Wed, 4 Jul 2018 10:57:57 +0300 Subject: [PATCH] Add support for messages from osmand_bot --- .../osmand/telegram/helpers/TelegramHelper.kt | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index f598c02f3b..de9cd011a0 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -7,6 +7,7 @@ import org.drinkless.td.libcore.telegram.Client import org.drinkless.td.libcore.telegram.Client.ResultHandler import org.drinkless.td.libcore.telegram.TdApi import org.drinkless.td.libcore.telegram.TdApi.AuthorizationState +import org.json.JSONObject import java.io.File import java.util.* import java.util.concurrent.ConcurrentHashMap @@ -54,7 +55,7 @@ class TelegramHelper private constructor() { private val downloadChatFilesMap = ConcurrentHashMap() private val downloadUserFilesMap = ConcurrentHashMap() - // Can contain TdApi.MessageLocation or TdApi.MessageText from osmand_bot + // value.content can be TdApi.MessageLocation or MessageOsmAndBotLocation private val usersLocationMessages = ConcurrentHashMap() private val usersFullInfo = ConcurrentHashMap() @@ -358,6 +359,10 @@ class TelegramHelper private constructor() { private fun addNewMessage(message: TdApi.Message) { if (message.isAppropriate()) { + val oldContent = message.content + if (oldContent is TdApi.MessageText) { + message.content = parseOsmAndBotLocation(oldContent.text.text) + } usersLocationMessages[message.senderUserId] = message val chatTitle = chats[message.chatId]?.title if (chatTitle != null) { @@ -607,6 +612,40 @@ class TelegramHelper private constructor() { } } + private fun parseOsmAndBotLocation(json: String): MessageOsmAndBotLocation { + val obj = JSONObject(json) + return MessageOsmAndBotLocation( + obj.optString("name", ""), + obj.optDouble("lat", -1.0), + obj.optDouble("lon", -1.0), + obj.optDouble("alt", -1.0), + obj.optDouble("azi", -1.0), + obj.optDouble("spd", -1.0), + obj.optInt("updAgo", -1), + obj.optInt("locAgo", -1), + obj.optInt("updId", -1), + obj.optInt("updTime", -1) + ) + } + + class MessageOsmAndBotLocation internal constructor( + val name: String, + val lat: Double, + val lon: Double, + val alt: Double, + val azi: Double, + val spd: Double, + val updAgo: Int, + val locAgo: Int, + val updId: Int, + val updTime: Int + ) : TdApi.MessageContent() { + + override fun getConstructor() = -1 + + fun isValid() = name != "" && lat != -1.0 && lon != -1.0 + } + class OrderedChat internal constructor(internal val order: Long, internal val chatId: Long, internal val isChannel: Boolean) : Comparable { override fun compareTo(other: OrderedChat): Int { @@ -797,7 +836,12 @@ class TelegramHelper private constructor() { } } else { synchronized(message) { - message.content = updateMessageContent.newContent + val newContent = updateMessageContent.newContent + message.content = if (newContent is TdApi.MessageText) { + parseOsmAndBotLocation(newContent.text.text) + } else { + newContent + } } val chatTitle = chats[message.chatId]?.title if (chatTitle != null) {