From 497890c42cbf404daecff12456061f69e2dfdc73 Mon Sep 17 00:00:00 2001 From: ganzizi Date: Tue, 21 Nov 2023 11:56:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E6=8E=A8=E9=80=81?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E5=BD=92=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.go | 13 +++++++++++++ hi.go | 21 ++++++++++++++++++++- main.go | 16 ++++++++++++++++ 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 18bc3ac..db97a6d 100644 --- a/config/config.go +++ b/config/config.go @@ -17,6 +17,12 @@ var ( BotDialogId = "" BotSilence = "" + LineBotUrl = "" + LineBotVersion = "" + LineBotToken = "" + LineBotDialogId = "" + LineBotSilence = "" + RedisHost = "" RedisPort = "" RedisPassword = "" @@ -104,6 +110,13 @@ func Parse(c *cli.Context) *Config { getConfigOpt(yamlCfg, "bot-dialog_id", &BotDialogId) getConfigOpt(yamlCfg, "bot-silence", &BotSilence) + // 上下线推送消息机器人 + getConfigOpt(yamlCfg, "line-bot-url", &LineBotUrl) + getConfigOpt(yamlCfg, "line-bot-version", &LineBotVersion) + getConfigOpt(yamlCfg, "line-bot-token", &LineBotToken) + getConfigOpt(yamlCfg, "line-bot-dialog_id", &LineBotDialogId) + getConfigOpt(yamlCfg, "line-bot-silence", &LineBotSilence) + getConfigOpt(yamlCfg, "redis-host", &RedisHost) getConfigOpt(yamlCfg, "redis-port", &RedisPort) getConfigOpt(yamlCfg, "redis-password", &RedisPassword) diff --git a/hi.go b/hi.go index 5d76dd0..c7b6f1b 100644 --- a/hi.go +++ b/hi.go @@ -729,6 +729,21 @@ func hiPushMsg(msg string) { }).Post(config.BotUrl) } +// hiPushLineMsg 上下线推送消息 +func hiPushLineMsg(msg string) { + if config.LineBotUrl == "" { + return + } + _, _ = gohttp.NewRequest().Headers(map[string]string{ + "version": config.LineBotVersion, + "token": config.LineBotToken, + }).FormData(map[string]string{ + "dialog_id": config.LineBotDialogId, + "text": msg, + "silence": config.LineBotSilence, + }).Post(config.LineBotUrl) +} + // hiSaveMessage 保存到消息表 func hiSaveMessage(dbCfg string, devid, action, token, errMsg string, timeout bool) { if _, ok := dictionary[action]; ok { @@ -799,10 +814,11 @@ func hiPushMessages(dbCfg string) { for _, groupMessage := range groupMessages { var msgs []string + var lineMsgs []string for _, pushingMsg := range groupMessage { timeString := time.Unix(int64(pushingMsg.CreatedAt), 0).Format("2006-01-02 15:04:05") if pushingMsg.Action == Connected || pushingMsg.Action == Disconnected { - msgs = append(msgs, fmt.Sprintf("设备[%s]%s(时间:%s,今日第%d次)", pushingMsg.Devid, dictionary[pushingMsg.Action], timeString, pushingMsg.NumberIndex)) + lineMsgs = append(lineMsgs, fmt.Sprintf("设备[%s]%s(时间:%s,今日第%d次)", pushingMsg.Devid, dictionary[pushingMsg.Action], timeString, pushingMsg.NumberIndex)) } else if pushingMsg.Status == StatusTimeout { msgs = append(msgs, fmt.Sprintf("设备[%s]%s,执行超时,token=%s(时间:%s)", pushingMsg.Devid, dictionary[pushingMsg.Action], pushingMsg.Token, timeString)) } else if pushingMsg.Status == StatusFail { @@ -812,6 +828,9 @@ func hiPushMessages(dbCfg string) { if len(msgs) > 0 { hiPushMsg(strings.Join(msgs, "\n")) } + if len(lineMsgs) > 0 { + hiPushLineMsg(strings.Join(lineMsgs, "\n")) + } } tx.Updates(map[string]interface{}{ diff --git a/main.go b/main.go index 53dacbe..ed00ca7 100644 --- a/main.go +++ b/main.go @@ -188,6 +188,22 @@ func main() { if len(os.Getenv("RTTYS_BOT_SILENCE")) > 0 { config.BotSilence = os.Getenv("RTTYS_BOT_SILENCE") } + // 推送上下线消息机器人 + if len(os.Getenv("RTTYS_LINE_BOT_URL")) > 0 { + config.LineBotUrl = os.Getenv("RTTYS_LINE_BOT_URL") + } + if len(os.Getenv("RTTYS_LINE_BOT_VERSION")) > 0 { + config.LineBotVersion = os.Getenv("RTTYS_LINE_BOT_VERSION") + } + if len(os.Getenv("RTTYS_LINE_BOT_TOKEN")) > 0 { + config.LineBotToken = os.Getenv("RTTYS_LINE_BOT_TOKEN") + } + if len(os.Getenv("RTTYS_LINE_BOT_DIALOG_ID")) > 0 { + config.LineBotDialogId = os.Getenv("RTTYS_LINE_BOT_DIALOG_ID") + } + if len(os.Getenv("RTTYS_LINE_BOT_SILENCE")) > 0 { + config.LineBotSilence = os.Getenv("RTTYS_LINE_BOT_SILENCE") + } // redis 配置 if len(os.Getenv("REDIS_HOST")) > 0 {