Skip to content

Commit

Permalink
feat: 新增推送消息归类
Browse files Browse the repository at this point in the history
  • Loading branch information
ganzizi committed Nov 21, 2023
1 parent 11a103b commit 497890c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
13 changes: 13 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ var (
BotDialogId = ""
BotSilence = ""

LineBotUrl = ""
LineBotVersion = ""
LineBotToken = ""
LineBotDialogId = ""
LineBotSilence = ""

RedisHost = ""
RedisPort = ""
RedisPassword = ""
Expand Down Expand Up @@ -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)
Expand Down
21 changes: 20 additions & 1 deletion hi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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{}{
Expand Down
16 changes: 16 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 497890c

Please sign in to comment.