1
zj
2025-06-25 a0361e762fc672d844ef15e18db5971893cce2bf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package project.monitor.telegram.internal;
 
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
 
import kernel.util.StringUtils;
import kernel.util.ThreadUtils;
import project.hobi.http.HttpHelper;
import project.hobi.http.HttpMethodType;
import project.monitor.telegram.TelegramMessageService;
import project.syspara.SysparaService;
 
public class TelegramMessageServiceImpl implements TelegramMessageService {
 
    private static final Logger logger = LoggerFactory.getLogger(TelegramMessageServiceImpl.class);
    
    private SysparaService sysparaService;
    
    private String chat_id;
 
    private String url;
 
    private boolean initia = false;
 
    /**
     * 接口调用间隔(毫秒)
     */
    private int interval = 500;
 
    private volatile Date last_time = new Date();
 
    public void send(String text) {
        this.send(text, "HTML");
    }
 
    @Override
    public void send(String text, String parse_mode) {
 
        while (true) {
            if ((new Date().getTime() - last_time.getTime()) < interval) {
                ThreadUtils.sleep(interval);
            } else {
                break;
            }
        }
 
        if (!initia) {
            init();
        }
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("chat_id", this.chat_id);
        param.put("text", text);
        if (!StringUtils.isNullOrEmpty(parse_mode)) {
            param.put("parse_mode", parse_mode);
        } else {
            param.put("parse_mode", "");
        }
        try {
            String result = HttpHelper.getJSONFromHttp(this.url, param, HttpMethodType.GET);
            JSONObject resultJson = JSON.parseObject(result);
 
            String status = resultJson.getString("ok");
            if ("false".equals(status)) {
                logger.error("Telegram消息发送失败,失败原因:" + resultJson.getString("description"));
            }
 
        } catch (Exception e) {
            logger.error("error", e);
        } finally {
            last_time = new Date();
 
        }
 
    }
 
    private void init() {
        String token = sysparaService.find("telegram_message_token").getValue();
        chat_id = sysparaService.find("telegram_message_chat_id").getValue();
        this.url = "https://api.telegram.org/bot" + token + "/sendMessage";
        initia = true;
 
    }
 
    public void setSysparaService(SysparaService sysparaService) {
        this.sysparaService = sysparaService;
    }
 
}