/*
 * Copyright 2019 - 2020 Anton Tananaev (anton@traccar.org)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
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.config.Keys;
import org.traccar.model.Event;
import org.traccar.model.Position;
import org.traccar.notification.NotificationFormatter;

import javax.ws.rs.client.Entity;
import javax.ws.rs.client.InvocationCallback;

public class NotificatorTelegram extends Notificator {

    private static final Logger LOGGER = LoggerFactory.getLogger(NotificatorTelegram.class);

    private final String url;
    private final String chatId;

    public static class Message {
        @JsonProperty("chat_id")
        private String chatId;
        @JsonProperty("text")
        private String text;
        @JsonProperty("parse_mode")
        private String parseMode = "html";
    }

    public NotificatorTelegram() {
        url = String.format(
                "https://api.telegram.org/bot%s/sendMessage",
                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);

        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);
            }
        });
    }

    @Override
    public void sendAsync(long userId, Event event, Position position) {
        sendSync(userId, event, position);
    }

}

@@ -703,24 +703,25 @@
 onProperty;%0A
+%0A
 import org.s
@@ -1207,16 +1207,66 @@
 ring url
+SendText;%0A    private final String urlSendLocation
 ;%0A    pr
@@ -1532,24 +1532,495 @@
 ml%22;%0A    %7D%0A%0A
+    public static class LocationMessage %7B%0A        @JsonProperty(%22chat_id%22)%0A        private String chatId;%0A        @JsonProperty(%22latitude%22)%0A        private double latitude;%0A        @JsonProperty(%22longitude%22)%0A        private double longitude;%0A        @JsonProperty(%22horizontal_accuracy%22)%0A        private double accuracy;%0A        @JsonProperty(%22bearing%22)%0A        private double bearing;%0A        @JsonProperty(%22parse_mode%22)%0A        private String parseMode = %22html%22;%0A    %7D%0A%0A
     public N
@@ -2053,16 +2053,24 @@
      url
+SendText
  = Strin
@@ -2220,16 +2220,199 @@
 _KEY));%0A
+        urlSendLocation = String.format(%0A                %22https://api.telegram.org/bot%25s/sendLocation%22,%0A                Context.getConfig().getString(Keys.NOTIFICATOR_TELEGRAM_KEY));%0A
         
@@ -2709,47 +2709,767 @@
 rmat
-ShortMessage(userId, event, position);%0A
+FullMessage(userId, event, position);%0A        executeRequest(urlSendText, message);%0A%0A        if (position != null) %7B%0A            LocationMessage locationMessage = new LocationMessage();%0A            locationMessage.chatId = chatId;%0A            locationMessage.latitude = position.getLatitude();%0A            locationMessage.longitude = position.getLongitude();%0A            locationMessage.bearing = position.getCourse();%0A            locationMessage.accuracy = position.getAccuracy();%0A            executeRequest(urlSendLocation, locationMessage);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public void sendAsync(long userId, Event event, Position position) %7B%0A        sendSync(userId, event, position);%0A    %7D%0A%0A    private void executeRequest(String url, Object message) %7B
 %0A   
@@ -3860,143 +3860,6 @@
  %7D%0A%0A
-    @Override%0A    public void sendAsync(long userId, Event event, Position position) %7B%0A        sendSync(userId, event, position);%0A    %7D%0A%0A
 %7D%0A

Support native location messages for Telegram