aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2021-04-26 09:39:43 -0700
committerGitHub <noreply@github.com>2021-04-26 09:39:43 -0700
commit802a0e395d48ac9dbab54a4c01227aec5801c6f3 (patch)
treec3647d8c4da4d00c39470080818d0b9259ec59f1 /src
parentf6f17c6d766a4bc02c5060c8df5eda190e307db3 (diff)
parent114264a79272764314ab991829fcc32a2e5a3b66 (diff)
downloadtraccar-server-802a0e395d48ac9dbab54a4c01227aec5801c6f3.tar.gz
traccar-server-802a0e395d48ac9dbab54a4c01227aec5801c6f3.tar.bz2
traccar-server-802a0e395d48ac9dbab54a4c01227aec5801c6f3.zip
Merge pull request #4664 from soshial/patch-1
Telegram native location messages
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorTelegram.java52
1 files changed, 42 insertions, 10 deletions
diff --git a/src/main/java/org/traccar/notificators/NotificatorTelegram.java b/src/main/java/org/traccar/notificators/NotificatorTelegram.java
index 6b8bc426d..963bdb091 100644
--- a/src/main/java/org/traccar/notificators/NotificatorTelegram.java
+++ b/src/main/java/org/traccar/notificators/NotificatorTelegram.java
@@ -31,10 +31,11 @@ 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 {
+ public static class TextMessage {
@JsonProperty("chat_id")
private String chatId;
@JsonProperty("text")
@@ -43,20 +44,30 @@ 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 int bearing;
+ }
+
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);
}
- @Override
- public void sendSync(long userId, Event event, Position position) {
-
- Message message = new Message();
- message.chatId = chatId;
- message.text = NotificationFormatter.formatShortMessage(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,6 +81,27 @@ public class NotificatorTelegram extends Notificator {
});
}
+ private LocationMessage createLocationMessage(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) {
+ executeRequest(urlSendLocation, createLocationMessage(position));
+ }
+ TextMessage message = new TextMessage();
+ message.chatId = chatId;
+ message.text = NotificationFormatter.formatFullMessage(userId, event, position).getBody();
+ executeRequest(urlSendText, message);
+ }
+
@Override
public void sendAsync(long userId, Event event, Position position) {
sendSync(userId, event, position);