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') 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