Files
tele-recv/utils/telegram.go
T

46 lines
881 B
Go
Raw Normal View History

2020-12-21 16:18:08 +08:00
package utils
import (
"regexp"
"strings"
2020-12-28 16:36:59 +08:00
rotatelogs "github.com/lestrrat-go/file-rotatelogs"
2020-12-21 16:18:08 +08:00
)
const (
EndTag = "NNNN"
2020-12-28 16:52:56 +08:00
Expression = "(?s)ZCZC.*NNNN[\r\n]+"
2020-12-21 16:18:08 +08:00
)
var buffer []byte
2020-12-28 16:36:59 +08:00
var RawLog *rotatelogs.RotateLogs
2020-12-21 16:18:08 +08:00
func Append(data []byte) {
buffer = append(buffer, data...)
if buffer[len(buffer)-1] == '\r' {
buffer = append(buffer, '\n')
}
check()
}
func check() {
teleString := string(buffer)
if strings.Index(teleString, EndTag) > 0 {
exp, _ := regexp.Compile(Expression)
telegram := exp.FindString(string(buffer))
if len(telegram) > 0 {
2020-12-28 16:13:57 +08:00
// Log.Info("telegram ", zap.String("text", telegram))
2020-12-28 16:46:25 +08:00
telegram = removeEmpty(telegram) + "\n"
2020-12-28 16:36:59 +08:00
RawLog.Write([]byte(telegram))
2020-12-21 16:18:08 +08:00
buffer = nil
2020-12-21 17:59:37 +08:00
InsertTelegram(telegram)
2020-12-21 16:18:08 +08:00
}
}
}
2020-12-28 16:46:25 +08:00
func removeEmpty(s string) string {
2020-12-28 16:49:36 +08:00
return regexp.MustCompile(`[\t\r\n]+`).ReplaceAllString(strings.TrimSpace(s), "\n")
2020-12-28 16:46:25 +08:00
}