From c213745d203e4ebc25766b196108b9edf657f345 Mon Sep 17 00:00:00 2001 From: soshial Date: Tue, 20 Apr 2021 18:41:04 +0400 Subject: Support native location messages for Telegram --- .../traccar/notificators/NotificatorTelegram.java | 49 ++++++++++++++++++---- 1 file changed, 41 insertions(+), 8 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/notificators/NotificatorTelegram.java b/src/main/java/org/traccar/notificators/NotificatorTelegram.java index 6b8bc426d..f4e748881 100644 --- a/src/main/java/org/traccar/notificators/NotificatorTelegram.java +++ b/src/main/java/org/traccar/notificators/NotificatorTelegram.java @@ -16,6 +16,7 @@ package org.traccar.notificators; import com.fasterxml.jackson.annotation.JsonProperty; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.Context; @@ -31,7 +32,8 @@ public class NotificatorTelegram extends Notificator { private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorTelegram.class); - private final String url; + private final String urlSendText; + private final String urlSendLocation; private final String chatId; public static class Message { @@ -43,10 +45,28 @@ public class NotificatorTelegram extends Notificator { private String parseMode = "html"; } + public static class LocationMessage { + @JsonProperty("chat_id") + private String chatId; + @JsonProperty("latitude") + private double latitude; + @JsonProperty("longitude") + private double longitude; + @JsonProperty("horizontal_accuracy") + private double accuracy; + @JsonProperty("bearing") + private double bearing; + @JsonProperty("parse_mode") + private String parseMode = "html"; + } + public NotificatorTelegram() { - url = String.format( + urlSendText = String.format( "https://api.telegram.org/bot%s/sendMessage", Context.getConfig().getString(Keys.NOTIFICATOR_TELEGRAM_KEY)); + urlSendLocation = String.format( + "https://api.telegram.org/bot%s/sendLocation", + Context.getConfig().getString(Keys.NOTIFICATOR_TELEGRAM_KEY)); chatId = Context.getConfig().getString(Keys.NOTIFICATOR_TELEGRAM_CHAT_ID); } @@ -55,8 +75,26 @@ public class NotificatorTelegram extends Notificator { Message message = new Message(); message.chatId = chatId; - message.text = NotificationFormatter.formatShortMessage(userId, event, position); + message.text = NotificationFormatter.formatFullMessage(userId, event, position); + executeRequest(urlSendText, message); + + if (position != null) { + LocationMessage locationMessage = new LocationMessage(); + locationMessage.chatId = chatId; + locationMessage.latitude = position.getLatitude(); + locationMessage.longitude = position.getLongitude(); + locationMessage.bearing = position.getCourse(); + locationMessage.accuracy = position.getAccuracy(); + executeRequest(urlSendLocation, locationMessage); + } + } + @Override + public void sendAsync(long userId, Event event, Position position) { + sendSync(userId, event, position); + } + + private void executeRequest(String url, Object message) { Context.getClient().target(url).request() .async().post(Entity.json(message), new InvocationCallback() { @Override @@ -70,9 +108,4 @@ public class NotificatorTelegram extends Notificator { }); } - @Override - public void sendAsync(long userId, Event event, Position position) { - sendSync(userId, event, position); - } - } -- cgit v1.2.3 From 383d61a7ebb64ea142fce47b3c6c627e5cb90be4 Mon Sep 17 00:00:00 2001 From: soshial Date: Wed, 21 Apr 2021 14:26:56 +0400 Subject: prepare NotificatorTelegram to send coupled messages; fix data class field types --- .../traccar/notificators/NotificatorTelegram.java | 51 +++++++++++----------- 1 file changed, 26 insertions(+), 25 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/notificators/NotificatorTelegram.java b/src/main/java/org/traccar/notificators/NotificatorTelegram.java index f4e748881..607eeaf24 100644 --- a/src/main/java/org/traccar/notificators/NotificatorTelegram.java +++ b/src/main/java/org/traccar/notificators/NotificatorTelegram.java @@ -25,6 +25,7 @@ import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.notification.NotificationFormatter; +import javax.annotation.Nullable; import javax.ws.rs.client.Entity; import javax.ws.rs.client.InvocationCallback; @@ -36,11 +37,14 @@ public class NotificatorTelegram extends Notificator { private final String urlSendLocation; private final String chatId; - public static class Message { + public static class TextMessage { @JsonProperty("chat_id") private String chatId; @JsonProperty("text") private String text; + @Nullable + @JsonProperty("reply_to_message_id") + private Long replyToMessageId; @JsonProperty("parse_mode") private String parseMode = "html"; } @@ -55,9 +59,7 @@ public class NotificatorTelegram extends Notificator { @JsonProperty("horizontal_accuracy") private double accuracy; @JsonProperty("bearing") - private double bearing; - @JsonProperty("parse_mode") - private String parseMode = "html"; + private int bearing; } public NotificatorTelegram() { @@ -70,23 +72,36 @@ public class NotificatorTelegram extends Notificator { chatId = Context.getConfig().getString(Keys.NOTIFICATOR_TELEGRAM_CHAT_ID); } - @Override - public void sendSync(long userId, Event event, Position position) { + private void executeRequest(String url, Object message) { + Context.getClient().target(url).request() + .async().post(Entity.json(message), new InvocationCallback() { + @Override + public void completed(Object o) { + } - Message message = new Message(); - message.chatId = chatId; - message.text = NotificationFormatter.formatFullMessage(userId, event, position); - executeRequest(urlSendText, message); + @Override + public void failed(Throwable throwable) { + LOGGER.warn("Telegram API error", throwable); + } + }); + } + @Override + public void sendSync(long userId, Event event, Position position) { if (position != null) { LocationMessage locationMessage = new LocationMessage(); locationMessage.chatId = chatId; locationMessage.latitude = position.getLatitude(); locationMessage.longitude = position.getLongitude(); - locationMessage.bearing = position.getCourse(); + locationMessage.bearing = (int) Math.ceil(position.getCourse()); locationMessage.accuracy = position.getAccuracy(); executeRequest(urlSendLocation, locationMessage); } + TextMessage message = new TextMessage(); + message.chatId = chatId; + // message.replyToMessageId = Long.MIN_VALUE; TODO add message ID from the first request here + message.text = NotificationFormatter.formatFullMessage(userId, event, position).getBody(); + executeRequest(urlSendText, message); } @Override @@ -94,18 +109,4 @@ public class NotificatorTelegram extends Notificator { sendSync(userId, event, position); } - private void executeRequest(String url, Object message) { - Context.getClient().target(url).request() - .async().post(Entity.json(message), new InvocationCallback() { - @Override - public void completed(Object o) { - } - - @Override - public void failed(Throwable throwable) { - LOGGER.warn("Telegram API error", throwable); - } - }); - } - } -- cgit v1.2.3 From 516dce5aedf9b45557e6d1eb62ac797987290c1f Mon Sep 17 00:00:00 2001 From: soshial Date: Sun, 25 Apr 2021 10:28:11 +0400 Subject: cleanse NotificatorTelegram before merge --- .../traccar/notificators/NotificatorTelegram.java | 23 +++++++++++----------- 1 file changed, 11 insertions(+), 12 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/notificators/NotificatorTelegram.java b/src/main/java/org/traccar/notificators/NotificatorTelegram.java index 607eeaf24..bd23684d4 100644 --- a/src/main/java/org/traccar/notificators/NotificatorTelegram.java +++ b/src/main/java/org/traccar/notificators/NotificatorTelegram.java @@ -16,7 +16,6 @@ package org.traccar.notificators; import com.fasterxml.jackson.annotation.JsonProperty; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.traccar.Context; @@ -25,7 +24,6 @@ import org.traccar.model.Event; import org.traccar.model.Position; import org.traccar.notification.NotificationFormatter; -import javax.annotation.Nullable; import javax.ws.rs.client.Entity; import javax.ws.rs.client.InvocationCallback; @@ -42,9 +40,6 @@ public class NotificatorTelegram extends Notificator { private String chatId; @JsonProperty("text") private String text; - @Nullable - @JsonProperty("reply_to_message_id") - private Long replyToMessageId; @JsonProperty("parse_mode") private String parseMode = "html"; } @@ -86,20 +81,24 @@ public class NotificatorTelegram extends Notificator { }); } + private LocationMessage createMessage(Position position) { + LocationMessage locationMessage = new LocationMessage(); + locationMessage.chatId = chatId; + locationMessage.latitude = position.getLatitude(); + locationMessage.longitude = position.getLongitude(); + locationMessage.bearing = (int) Math.ceil(position.getCourse()); + locationMessage.accuracy = position.getAccuracy(); + return locationMessage; + } + @Override public void sendSync(long userId, Event event, Position position) { if (position != null) { - LocationMessage locationMessage = new LocationMessage(); - locationMessage.chatId = chatId; - locationMessage.latitude = position.getLatitude(); - locationMessage.longitude = position.getLongitude(); - locationMessage.bearing = (int) Math.ceil(position.getCourse()); - locationMessage.accuracy = position.getAccuracy(); + LocationMessage locationMessage = createMessage(position); executeRequest(urlSendLocation, locationMessage); } TextMessage message = new TextMessage(); message.chatId = chatId; - // message.replyToMessageId = Long.MIN_VALUE; TODO add message ID from the first request here message.text = NotificationFormatter.formatFullMessage(userId, event, position).getBody(); executeRequest(urlSendText, message); } -- cgit v1.2.3 From 114264a79272764314ab991829fcc32a2e5a3b66 Mon Sep 17 00:00:00 2001 From: soshial Date: Mon, 26 Apr 2021 18:23:21 +0400 Subject: cleanse NotificatorTelegram before merge-2 --- src/main/java/org/traccar/notificators/NotificatorTelegram.java | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) (limited to 'src/main/java/org') diff --git a/src/main/java/org/traccar/notificators/NotificatorTelegram.java b/src/main/java/org/traccar/notificators/NotificatorTelegram.java index bd23684d4..963bdb091 100644 --- a/src/main/java/org/traccar/notificators/NotificatorTelegram.java +++ b/src/main/java/org/traccar/notificators/NotificatorTelegram.java @@ -81,7 +81,7 @@ public class NotificatorTelegram extends Notificator { }); } - private LocationMessage createMessage(Position position) { + private LocationMessage createLocationMessage(Position position) { LocationMessage locationMessage = new LocationMessage(); locationMessage.chatId = chatId; locationMessage.latitude = position.getLatitude(); @@ -94,8 +94,7 @@ public class NotificatorTelegram extends Notificator { @Override public void sendSync(long userId, Event event, Position position) { if (position != null) { - LocationMessage locationMessage = createMessage(position); - executeRequest(urlSendLocation, locationMessage); + executeRequest(urlSendLocation, createLocationMessage(position)); } TextMessage message = new TextMessage(); message.chatId = chatId; -- cgit v1.2.3