aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/notificators/NotificatorTelegram.java
diff options
context:
space:
mode:
authorcasswarry0 <casswarry0@gmail.com>2023-01-17 17:14:53 -0700
committercasswarry0 <casswarry0@gmail.com>2023-01-17 17:14:53 -0700
commit7338b8730949ed027b3f8b31d7dca20687ebbb8b (patch)
treec2d171e6121818ab511460a786f69aab97a2a628 /src/main/java/org/traccar/notificators/NotificatorTelegram.java
parentcdecd3fa4427a382c0b09f8ad9d69ec14388960a (diff)
parent85501f9cf4918d5eee345f83aed7a31eecb26b8d (diff)
downloadtrackermap-server-7338b8730949ed027b3f8b31d7dca20687ebbb8b.tar.gz
trackermap-server-7338b8730949ed027b3f8b31d7dca20687ebbb8b.tar.bz2
trackermap-server-7338b8730949ed027b3f8b31d7dca20687ebbb8b.zip
Merge branch 'master' into develop
Diffstat (limited to 'src/main/java/org/traccar/notificators/NotificatorTelegram.java')
-rw-r--r--src/main/java/org/traccar/notificators/NotificatorTelegram.java63
1 files changed, 23 insertions, 40 deletions
diff --git a/src/main/java/org/traccar/notificators/NotificatorTelegram.java b/src/main/java/org/traccar/notificators/NotificatorTelegram.java
index 70148110c..38e87c222 100644
--- a/src/main/java/org/traccar/notificators/NotificatorTelegram.java
+++ b/src/main/java/org/traccar/notificators/NotificatorTelegram.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2019 - 2021 Anton Tananaev (anton@traccar.org)
+ * Copyright 2019 - 2022 Anton Tananaev (anton@traccar.org)
* Copyright 2021 Rafael Miquelino (rafaelmiquelino@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -17,22 +17,23 @@
package org.traccar.notificators;
import com.fasterxml.jackson.annotation.JsonProperty;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.traccar.Context;
-import org.traccar.model.User;
+import org.traccar.config.Config;
import org.traccar.config.Keys;
import org.traccar.model.Event;
import org.traccar.model.Position;
+import org.traccar.model.User;
import org.traccar.notification.NotificationFormatter;
-import org.traccar.notification.NotificationMessage;
+import javax.inject.Inject;
+import javax.inject.Singleton;
+import javax.ws.rs.client.Client;
import javax.ws.rs.client.Entity;
-import javax.ws.rs.client.InvocationCallback;
-public class NotificatorTelegram extends Notificator {
+@Singleton
+public class NotificatorTelegram implements Notificator {
- private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorTelegram.class);
+ private final NotificationFormatter notificationFormatter;
+ private final Client client;
private final String urlSendText;
private final String urlSendLocation;
@@ -61,29 +62,16 @@ public class NotificatorTelegram extends Notificator {
private int bearing;
}
- public NotificatorTelegram() {
+ @Inject
+ public NotificatorTelegram(Config config, NotificationFormatter notificationFormatter, Client client) {
+ this.notificationFormatter = notificationFormatter;
+ this.client = client;
urlSendText = String.format(
- "https://api.telegram.org/bot%s/sendMessage",
- Context.getConfig().getString(Keys.NOTIFICATOR_TELEGRAM_KEY));
+ "https://api.telegram.org/bot%s/sendMessage", config.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);
- sendLocation = Context.getConfig().getBoolean(Keys.NOTIFICATOR_TELEGRAM_SEND_LOCATION);
- }
-
- private void executeRequest(String url, Object message) {
- Context.getClient().target(url).request()
- .async().post(Entity.json(message), new InvocationCallback<Object>() {
- @Override
- public void completed(Object o) {
- }
-
- @Override
- public void failed(Throwable throwable) {
- LOGGER.warn("Telegram API error", throwable);
- }
- });
+ "https://api.telegram.org/bot%s/sendLocation", config.getString(Keys.NOTIFICATOR_TELEGRAM_KEY));
+ chatId = config.getString(Keys.NOTIFICATOR_TELEGRAM_CHAT_ID);
+ sendLocation = config.getBoolean(Keys.NOTIFICATOR_TELEGRAM_SEND_LOCATION);
}
private LocationMessage createLocationMessage(String messageChatId, Position position) {
@@ -97,9 +85,8 @@ public class NotificatorTelegram extends Notificator {
}
@Override
- public void sendSync(long userId, Event event, Position position) {
- User user = Context.getPermissionsManager().getUser(userId);
- NotificationMessage shortMessage = NotificationFormatter.formatMessage(userId, event, position, "short");
+ public void send(User user, Event event, Position position) {
+ var shortMessage = notificationFormatter.formatMessage(user, event, position, "short");
TextMessage message = new TextMessage();
message.chatId = user.getString("telegramChatId");
@@ -107,15 +94,11 @@ public class NotificatorTelegram extends Notificator {
message.chatId = chatId;
}
message.text = shortMessage.getBody();
- executeRequest(urlSendText, message);
+ client.target(urlSendText).request().post(Entity.json(message)).close();
if (sendLocation && position != null) {
- executeRequest(urlSendLocation, createLocationMessage(message.chatId, position));
+ client.target(urlSendLocation).request().post(
+ Entity.json(createLocationMessage(message.chatId, position))).close();
}
}
- @Override
- public void sendAsync(long userId, Event event, Position position) {
- sendSync(userId, event, position);
- }
-
}