aboutsummaryrefslogtreecommitdiff
path: root/src/main/java
diff options
context:
space:
mode:
authorsoshial <soshial@users.noreply.github.com>2021-04-20 18:41:04 +0400
committerGitHub <noreply@github.com>2021-04-20 18:41:04 +0400
commitc213745d203e4ebc25766b196108b9edf657f345 (patch)
treec10fcce8f98c4a69d7c918e5b5605406e3401fd1 /src/main/java
parentd1d3be228d270355c9d48ede69a6e3570d3710b7 (diff)
downloadtrackermap-server-c213745d203e4ebc25766b196108b9edf657f345.tar.gz
trackermap-server-c213745d203e4ebc25766b196108b9edf657f345.tar.bz2
trackermap-server-c213745d203e4ebc25766b196108b9edf657f345.zip
Support native location messages for Telegram
Diffstat (limited to 'src/main/java')
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorTelegram.java49
1 files changed, 41 insertions, 8 deletions
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<Object>() {
@Override
@@ -70,9 +108,4 @@ public class NotificatorTelegram extends Notificator {
});
}
- @Override
- public void sendAsync(long userId, Event event, Position position) {
- sendSync(userId, event, position);
- }
-
}